xref: /petsc/src/sys/memory/mhbw.c (revision 58d68138c660dfb4e9f5b03334792cd4f2ffd7cc)
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 */
27 static PetscErrorCode PetscHBWMalloc(size_t a, PetscBool clear, int lineno, const char function[], const char filename[], void **result) {
28 #if !defined(PETSC_HAVE_MEMKIND)
29   return PetscMallocAlign(a, clear, lineno, function, filename, result);
30 #else
31   if (!a) {
32     *result = NULL;
33     return 0;
34   }
35   /*
36     The default policy is if insufficient memory is available from the high bandwidth memory
37     fall back to standard memory. If we use the HBW_POLICY_BIND policy, errno is set to ENOMEM
38     and the allocated pointer is set to NULL if there is not enough HWB memory available.
39   */
40   {
41     int err = hbw_posix_memalign(result, PETSC_MEMALIGN, a);
42     PetscCheck(!err && *result, PETSC_COMM_SELF, PETSC_ERR_MEM, "HBW Memory requested %.0f", (PetscLogDouble)a);
43   }
44   return 0;
45 #endif
46 }
47 
48 static PetscErrorCode PetscHBWFree(void *aa, int lineno, const char function[], const char filename[]) {
49 #if !defined(PETSC_HAVE_MEMKIND)
50   return PetscFreeAlign(aa, lineno, function, filename);
51 #else
52   hbw_free(aa);
53   return 0;
54 #endif
55 }
56 
57 static PetscErrorCode PetscHBWRealloc(size_t a, int lineno, const char function[], const char filename[], void **result) {
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   PetscCheck(*result, PETSC_COMM_SELF, PETSC_ERR_MEM, "Memory requested %.0f", (PetscLogDouble)a);
69   return 0;
70 #endif
71 }
72 
73 PETSC_INTERN PetscErrorCode PetscSetUseHBWMalloc_Private(void) {
74   PetscFunctionBegin;
75   PetscCall(PetscMallocSet(PetscHBWMalloc, PetscHBWFree, NULL));
76   PetscTrRealloc = PetscHBWRealloc;
77   PetscFunctionReturn(0);
78 }
79