xref: /petsc/src/sys/memory/mhbw.c (revision 13850c04a7377a91d5b1bff18e84c75ea74ba331)
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 extern PetscErrorCode PetscMallocAlign(size_t,int,const char[],const char[],void**);
11 extern PetscErrorCode PetscFreeAlign(void*,int,const char[],const char[]);
12 
13 /*
14    PetscHBWMalloc - HBW malloc.
15 
16    Input Parameters:
17    +   a   - number of bytes to allocate
18    .   lineno - line number where used
19    .   function - function calling routine
20    -   filename  - file name where used
21 
22    Returns:
23    double aligned pointer to requested storage, or null if not
24    available.
25 */
26 PetscErrorCode PetscHBWMalloc(size_t a,int lineno,const char function[],const char filename[],void **result)
27 {
28 #if !defined(PETSC_HAVE_MEMKIND)
29   return PetscMallocAlign(a,lineno,function,filename,result);
30 #else
31   if (!a) { *result = NULL; return 0; }
32   /*
33     The default policy is if insufficient memory is available from the high bandwidth memory
34     fall back to standard memory. If we use the HBW_POLICY_BIND policy, errno is set to ENOMEM
35     and the allocated pointer is set to NULL if there is not enough HWB memory available.
36   */
37   {
38     int ierr = hbw_posix_memalign(result,PETSC_MEMALIGN,a);
39     if (ierr || !*result) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_MEM,"HBW Memory requested %.0f",(PetscLogDouble)a);
40   }
41   return 0;
42 #endif
43 }
44 
45 PetscErrorCode PetscHBWFree(void *aa,int line,const char function[],const char file[])
46 {
47 #if !defined(PETSC_HAVE_MEMKIND)
48   return PetscFreeAlign(aa,line,function,file);
49 #else
50   hbw_free(aa);
51   return 0;
52 #endif
53 }
54 
55 PetscErrorCode PetscSetUseHBWMalloc_Private(void)
56 {
57   PetscErrorCode ierr;
58 
59   PetscFunctionBegin;
60   ierr = PetscMallocSet(PetscHBWMalloc,PetscHBWFree);CHKERRQ(ierr);
61   PetscTrRealloc = NULL;
62   PetscFunctionReturn(0);
63 }
64