xref: /petsc/src/sys/error/err.h (revision b0b385f45f76f1e108f857efe1d02ffe3b58ed6c)
1 #pragma once
2 
3 #include <petscsys.h>
4 
5 static inline PetscErrorCode PetscErrorMemoryMessage(PetscErrorCode n)
6 {
7   PetscLogDouble mem, rss;
8   PetscErrorCode ierr;
9   PetscBool      flg1 = PETSC_FALSE, flg2 = PETSC_FALSE, flg3 = PETSC_FALSE;
10 
11   if (n == PETSC_ERR_MEM) {
12     ierr = (*PetscErrorPrintf)("Out of memory. This could be due to allocating\n");
13     ierr = (*PetscErrorPrintf)("too large an object or bleeding by not properly\n");
14     ierr = (*PetscErrorPrintf)("destroying unneeded objects.\n");
15   } else {
16     ierr = (*PetscErrorPrintf)("Memory leaked due to not properly destroying\n");
17     ierr = (*PetscErrorPrintf)("unneeded objects.\n");
18   }
19   ierr = PetscMallocGetCurrentUsage(&mem);
20   ierr = PetscMemoryGetCurrentUsage(&rss);
21   ierr = PetscOptionsGetBool(NULL, NULL, "-on_error_malloc_dump", &flg1, NULL);
22   ierr = PetscOptionsGetBool(NULL, NULL, "-malloc_view", &flg2, NULL);
23   ierr = PetscOptionsHasName(NULL, NULL, "-malloc_view_threshold", &flg3);
24   if (flg2 || flg3) ierr = PetscMallocView(stdout);
25   else {
26     ierr = (*PetscErrorPrintf)("Memory allocated %.0f Memory used by process %.0f\n", mem, rss);
27     if (flg1) ierr = PetscMallocDump(stdout);
28     else ierr = (*PetscErrorPrintf)("Try running with -on_error_malloc_dump or -malloc_view for info.\n");
29   }
30   return ierr;
31 }
32