Actual source code: mhbw.c
petsc-3.13.6 2020-09-29
1: #include <petscsys.h>
3: #if defined(PETSC_HAVE_MEMKIND)
4: #include <hbwmalloc.h>
5: #endif
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**);
14: /*
15: PetscHBWMalloc - HBW malloc.
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
23: Returns:
24: double aligned pointer to requested storage, or null if not
25: available.
26: */
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 hbw_posix_memalign(result,PETSC_MEMALIGN,a);
40: if (ierr || !*result) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_MEM,"HBW Memory requested %.0f",(PetscLogDouble)a);
41: }
42: return 0;
43: #endif
44: }
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: }
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 PetscFreeAlign(*result,lineno,function,filename);
63: if (ierr) return ierr;
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: }
73: PETSC_INTERN PetscErrorCode PetscSetUseHBWMalloc_Private(void)
74: {
78: PetscMallocSet(PetscHBWMalloc,PetscHBWFree,NULL);
79: PetscTrRealloc = PetscHBWRealloc;
80: return(0);
81: }