xref: /petsc/src/sys/memory/mal.c (revision f0ba7cfc4d252c71d2b24195b126d66368c102fd)
17d0a6c19SBarry Smith 
2e5c89e4eSSatish Balay /*
3e5c89e4eSSatish Balay     Code that allows a user to dictate what malloc() PETSc uses.
4e5c89e4eSSatish Balay */
5c6db04a5SJed Brown #include <petscsys.h>             /*I   "petscsys.h"   I*/
6e5c89e4eSSatish Balay #if defined(PETSC_HAVE_MALLOC_H)
7e5c89e4eSSatish Balay #include <malloc.h>
8e5c89e4eSSatish Balay #endif
9e5c89e4eSSatish Balay 
10e5c89e4eSSatish Balay /*
11e5c89e4eSSatish Balay         We want to make sure that all mallocs of double or complex numbers are complex aligned.
12e5c89e4eSSatish Balay     1) on systems with memalign() we call that routine to get an aligned memory location
13e5c89e4eSSatish Balay     2) on systems without memalign() we
14e5c89e4eSSatish Balay        - allocate one sizeof(PetscScalar) extra space
15e5c89e4eSSatish Balay        - we shift the pointer up slightly if needed to get PetscScalar aligned
160700a824SBarry Smith        - if shifted we store at ptr[-1] the amount of shift (plus a classid)
17e5c89e4eSSatish Balay */
180700a824SBarry Smith #define SHIFT_CLASSID 456123
19e5c89e4eSSatish Balay 
20e5c89e4eSSatish Balay #undef __FUNCT__
21e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocAlign"
22efca3c55SSatish Balay PetscErrorCode  PetscMallocAlign(size_t mem,int line,const char func[],const char file[],void **result)
23e5c89e4eSSatish Balay {
24*f0ba7cfcSLisandro Dalcin   if (!mem) { *result = NULL; return 0; }
25e5c89e4eSSatish Balay #if defined(PETSC_HAVE_DOUBLE_ALIGN_MALLOC) && (PETSC_MEMALIGN == 8)
26e5c89e4eSSatish Balay   *result = malloc(mem);
27e5c89e4eSSatish Balay #elif defined(PETSC_HAVE_MEMALIGN)
28e5c89e4eSSatish Balay   *result = memalign(PETSC_MEMALIGN,mem);
29e5c89e4eSSatish Balay #else
30e5c89e4eSSatish Balay   {
31e5c89e4eSSatish Balay     /*
32e5c89e4eSSatish Balay       malloc space for two extra chunks and shift ptr 1 + enough to get it PetscScalar aligned
33e5c89e4eSSatish Balay     */
34*f0ba7cfcSLisandro Dalcin     int *ptr = (int*)malloc(mem + 2*PETSC_MEMALIGN);
35e5c89e4eSSatish Balay     if (ptr) {
36*f0ba7cfcSLisandro Dalcin       int shift    = (int)(((PETSC_UINTPTR_T) ptr) % PETSC_MEMALIGN);
37e5c89e4eSSatish Balay       shift        = (2*PETSC_MEMALIGN - shift)/sizeof(int);
380700a824SBarry Smith       ptr[shift-1] = shift + SHIFT_CLASSID;
39e5c89e4eSSatish Balay       ptr         += shift;
40e5c89e4eSSatish Balay       *result      = (void*)ptr;
41e5c89e4eSSatish Balay     } else {
42*f0ba7cfcSLisandro Dalcin       *result      = NULL;
43e5c89e4eSSatish Balay     }
44e5c89e4eSSatish Balay   }
45e5c89e4eSSatish Balay #endif
46*f0ba7cfcSLisandro Dalcin   if (!*result) return PetscError(PETSC_COMM_SELF,line,func,file,PETSC_ERR_MEM,PETSC_ERROR_INITIAL,"Memory requested %.0f",(PetscLogDouble)mem);
47e5c89e4eSSatish Balay   return 0;
48e5c89e4eSSatish Balay }
49e5c89e4eSSatish Balay 
50e5c89e4eSSatish Balay #undef __FUNCT__
51e5c89e4eSSatish Balay #define __FUNCT__ "PetscFreeAlign"
52efca3c55SSatish Balay PetscErrorCode  PetscFreeAlign(void *ptr,int line,const char func[],const char file[])
53e5c89e4eSSatish Balay {
54*f0ba7cfcSLisandro Dalcin   if (!ptr) return 0;
55e5c89e4eSSatish Balay #if (!(defined(PETSC_HAVE_DOUBLE_ALIGN_MALLOC) && (PETSC_MEMALIGN == 8)) && !defined(PETSC_HAVE_MEMALIGN))
56*f0ba7cfcSLisandro Dalcin   {
57e5c89e4eSSatish Balay     /*
58e5c89e4eSSatish Balay       Previous int tells us how many ints the pointer has been shifted from
59e5c89e4eSSatish Balay       the original address provided by the system malloc().
60e5c89e4eSSatish Balay     */
61*f0ba7cfcSLisandro Dalcin     int shift = *(((int*)ptr)-1) - SHIFT_CLASSID;
62efca3c55SSatish Balay     if (shift > PETSC_MEMALIGN-1) return PetscError(PETSC_COMM_SELF,line,func,file,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL,"Likely memory corruption in heap");
63efca3c55SSatish Balay     if (shift < 0) return PetscError(PETSC_COMM_SELF,line,func,file,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL,"Likely memory corruption in heap");
64e5c89e4eSSatish Balay     ptr = (void*)(((int*)ptr) - shift);
65e5c89e4eSSatish Balay   }
66*f0ba7cfcSLisandro Dalcin #endif
67e5c89e4eSSatish Balay 
68e5c89e4eSSatish Balay #if defined(PETSC_HAVE_FREE_RETURN_INT)
69e5c89e4eSSatish Balay   int err = free(ptr);
70efca3c55SSatish Balay   if (err) return PetscError(PETSC_COMM_SELF,line,func,file,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL,"System free returned error %d\n",err);
71e5c89e4eSSatish Balay #else
72e5c89e4eSSatish Balay   free(ptr);
73e5c89e4eSSatish Balay #endif
74e5c89e4eSSatish Balay   return 0;
75e5c89e4eSSatish Balay }
76e5c89e4eSSatish Balay 
77efca3c55SSatish Balay PetscErrorCode (*PetscTrMalloc)(size_t,int,const char[],const char[],void**) = PetscMallocAlign;
78efca3c55SSatish Balay PetscErrorCode (*PetscTrFree)(void*,int,const char[],const char[])           = PetscFreeAlign;
79e5c89e4eSSatish Balay 
80ace3abfcSBarry Smith PetscBool petscsetmallocvisited = PETSC_FALSE;
81e5c89e4eSSatish Balay 
82e5c89e4eSSatish Balay #undef __FUNCT__
831d1a0024SBarry Smith #define __FUNCT__ "PetscMallocSet"
84e5c89e4eSSatish Balay /*@C
851d1a0024SBarry Smith    PetscMallocSet - Sets the routines used to do mallocs and frees.
86e5c89e4eSSatish Balay    This routine MUST be called before PetscInitialize() and may be
87e5c89e4eSSatish Balay    called only once.
88e5c89e4eSSatish Balay 
89e5c89e4eSSatish Balay    Not Collective
90e5c89e4eSSatish Balay 
91e5c89e4eSSatish Balay    Input Parameters:
92e5c89e4eSSatish Balay +  malloc - the malloc routine
93e5c89e4eSSatish Balay -  free - the free routine
94e5c89e4eSSatish Balay 
95e5c89e4eSSatish Balay    Level: developer
96e5c89e4eSSatish Balay 
97e5c89e4eSSatish Balay    Concepts: malloc
98e5c89e4eSSatish Balay    Concepts: memory^allocation
99e5c89e4eSSatish Balay 
100e5c89e4eSSatish Balay @*/
101efca3c55SSatish Balay PetscErrorCode  PetscMallocSet(PetscErrorCode (*imalloc)(size_t,int,const char[],const char[],void**),
102efca3c55SSatish Balay                                               PetscErrorCode (*ifree)(void*,int,const char[],const char[]))
103e5c89e4eSSatish Balay {
104e5c89e4eSSatish Balay   PetscFunctionBegin;
105e32f2f54SBarry Smith   if (petscsetmallocvisited && (imalloc != PetscTrMalloc || ifree != PetscTrFree)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"cannot call multiple times");
106e5c89e4eSSatish Balay   PetscTrMalloc         = imalloc;
107e5c89e4eSSatish Balay   PetscTrFree           = ifree;
108e5c89e4eSSatish Balay   petscsetmallocvisited = PETSC_TRUE;
109e5c89e4eSSatish Balay   PetscFunctionReturn(0);
110e5c89e4eSSatish Balay }
111e5c89e4eSSatish Balay 
112e5c89e4eSSatish Balay #undef __FUNCT__
1131d1a0024SBarry Smith #define __FUNCT__ "PetscMallocClear"
114e5c89e4eSSatish Balay /*@C
1151d1a0024SBarry Smith    PetscMallocClear - Resets the routines used to do mallocs and frees to the
116e5c89e4eSSatish Balay         defaults.
117e5c89e4eSSatish Balay 
118e5c89e4eSSatish Balay    Not Collective
119e5c89e4eSSatish Balay 
120e5c89e4eSSatish Balay    Level: developer
121e5c89e4eSSatish Balay 
122e5c89e4eSSatish Balay    Notes:
123e5c89e4eSSatish Balay     In general one should never run a PETSc program with different malloc() and
124e5c89e4eSSatish Balay     free() settings for different parts; this is because one NEVER wants to
125e5c89e4eSSatish Balay     free() an address that was malloced by a different memory management system
126e5c89e4eSSatish Balay 
127e5c89e4eSSatish Balay @*/
1287087cfbeSBarry Smith PetscErrorCode  PetscMallocClear(void)
129e5c89e4eSSatish Balay {
130e5c89e4eSSatish Balay   PetscFunctionBegin;
131e5c89e4eSSatish Balay   PetscTrMalloc         = PetscMallocAlign;
132e5c89e4eSSatish Balay   PetscTrFree           = PetscFreeAlign;
133e5c89e4eSSatish Balay   petscsetmallocvisited = PETSC_FALSE;
134e5c89e4eSSatish Balay   PetscFunctionReturn(0);
135e5c89e4eSSatish Balay }
136b44d5720SBarry Smith 
137b44d5720SBarry Smith #undef __FUNCT__
138b44d5720SBarry Smith #define __FUNCT__ "PetscMemoryTrace"
139b44d5720SBarry Smith PetscErrorCode PetscMemoryTrace(const char label[])
140b44d5720SBarry Smith {
141b44d5720SBarry Smith   PetscErrorCode        ierr;
142b44d5720SBarry Smith   PetscLogDouble        mem,mal;
143b44d5720SBarry Smith   static PetscLogDouble oldmem = 0,oldmal = 0;
144b44d5720SBarry Smith 
145b44d5720SBarry Smith   PetscFunctionBegin;
146b44d5720SBarry Smith   ierr = PetscMemoryGetCurrentUsage(&mem);CHKERRQ(ierr);
147b44d5720SBarry Smith   ierr = PetscMallocGetCurrentUsage(&mal);CHKERRQ(ierr);
148b44d5720SBarry Smith 
149b44d5720SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"%s High water  %8.3f MB increase %8.3f MB Current %8.3f MB increase %8.3f MB\n",label,mem*1e-6,(mem - oldmem)*1e-6,mal*1e-6,(mal - oldmal)*1e-6);CHKERRQ(ierr);
150b44d5720SBarry Smith   oldmem = mem;
151b44d5720SBarry Smith   oldmal = mal;
152b44d5720SBarry Smith   PetscFunctionReturn(0);
153b44d5720SBarry Smith }
154