1 #include <petscsys.h> /*I "petscsys.h" I*/
2
3 #if defined(PETSC_HAVE_MEMKIND)
4 #include <hbwmalloc.h>
5 #endif
6
7 /*
8 These are defined in mal.c and ensure that malloced space is PetscScalar aligned
9 */
10 PETSC_EXTERN PetscErrorCode PetscMallocAlign(size_t,PetscBool,int,const char[],const char[],void**);
11 PETSC_EXTERN PetscErrorCode PetscFreeAlign(void*,int,const char[],const char[]);
12 PETSC_EXTERN PetscErrorCode PetscReallocAlign(size_t,int,const char[],const char[],void**);
13
14 /*
15 PetscHBWMalloc - HBW malloc.
16
17 Input Parameters:
18 + a - number of bytes to allocate
19 . lineno - line number where used
20 . function - function calling routine
21 - filename - file name where used
22
23 Returns:
24 double aligned pointer to requested storage, or null if not
25 available.
26 */
PetscHBWMalloc(size_t a,PetscBool clear,int lineno,const char function[],const char filename[],void ** result)27 static PetscErrorCode PetscHBWMalloc(size_t a,PetscBool clear,int lineno,const char function[],const char filename[],void **result)
28 {
29 #if !defined(PETSC_HAVE_MEMKIND)
30 return PetscMallocAlign(a,clear,lineno,function,filename,result);
31 #else
32 if (!a) { *result = NULL; return 0; }
33 /*
34 The default policy is if insufficient memory is available from the high bandwidth memory
35 fall back to standard memory. If we use the HBW_POLICY_BIND policy, errno is set to ENOMEM
36 and the allocated pointer is set to NULL if there is not enough HWB memory available.
37 */
38 {
39 int err = hbw_posix_memalign(result,PETSC_MEMALIGN,a);
40 if (err || !*result) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_MEM,"HBW Memory requested %.0f",(PetscLogDouble)a);
41 }
42 return 0;
43 #endif
44 }
45
PetscHBWFree(void * aa,int lineno,const char function[],const char filename[])46 static PetscErrorCode PetscHBWFree(void *aa,int lineno,const char function[],const char filename[])
47 {
48 #if !defined(PETSC_HAVE_MEMKIND)
49 return PetscFreeAlign(aa,lineno,function,filename);
50 #else
51 hbw_free(aa);
52 return 0;
53 #endif
54 }
55
PetscHBWRealloc(size_t a,int lineno,const char function[],const char filename[],void ** result)56 static PetscErrorCode PetscHBWRealloc(size_t a,int lineno,const char function[],const char filename[],void **result)
57 {
58 #if !defined(PETSC_HAVE_MEMKIND)
59 return PetscReallocAlign(a,lineno,function,filename,result);
60 #else
61 if (!a) {
62 int err = PetscFreeAlign(*result,lineno,function,filename);
63 if (err) return err;
64 *result = NULL;
65 return 0;
66 }
67 *result = hbw_realloc(*result,a);
68 if (!*result) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_MEM,"Memory requested %.0f",(PetscLogDouble)a);
69 return 0;
70 #endif
71 }
72
PetscSetUseHBWMalloc_Private(void)73 PETSC_INTERN PetscErrorCode PetscSetUseHBWMalloc_Private(void)
74 {
75 PetscErrorCode ierr;
76
77 PetscFunctionBegin;
78 ierr = PetscMallocSet(PetscHBWMalloc,PetscHBWFree,NULL);CHKERRQ(ierr);
79 PetscTrRealloc = PetscHBWRealloc;
80 PetscFunctionReturn(0);
81 }
82