Actual source code: mhbw.c

      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) {
     33:     *result = NULL;
     34:     return 0;
     35:   }
     36:   /*
     37:     The default policy is if insufficient memory is available from the high bandwidth memory
     38:     fall back to standard memory. If we use the HBW_POLICY_BIND policy, errno is set to ENOMEM
     39:     and the allocated pointer is set to NULL if there is not enough HWB memory available.
     40:   */
     41:   {
     42:     int err = hbw_posix_memalign(result, PETSC_MEMALIGN, a);
     43:     PetscCheck(!err && *result, PETSC_COMM_SELF, PETSC_ERR_MEM, "HBW Memory requested %.0f", (PetscLogDouble)a);
     44:   }
     45:   return 0;
     46: #endif
     47: }

     49: static PetscErrorCode PetscHBWFree(void *aa, int lineno, const char function[], const char filename[])
     50: {
     51: #if !defined(PETSC_HAVE_MEMKIND)
     52:   return PetscFreeAlign(aa, lineno, function, filename);
     53: #else
     54:   hbw_free(aa);
     55:   return 0;
     56: #endif
     57: }

     59: static PetscErrorCode PetscHBWRealloc(size_t a, int lineno, const char function[], const char filename[], void **result)
     60: {
     61: #if !defined(PETSC_HAVE_MEMKIND)
     62:   return PetscReallocAlign(a, lineno, function, filename, result);
     63: #else
     64:   if (!a) {
     65:     int err = PetscFreeAlign(*result, lineno, function, filename);
     66:     if (err) return err;
     67:     *result = NULL;
     68:     return 0;
     69:   }
     70:   *result = hbw_realloc(*result, a);
     71:   PetscCheck(*result, PETSC_COMM_SELF, PETSC_ERR_MEM, "Memory requested %.0f", (PetscLogDouble)a);
     72:   return 0;
     73: #endif
     74: }

     76: PETSC_INTERN PetscErrorCode PetscSetUseHBWMalloc_Private(void)
     77: {
     78:   PetscFunctionBegin;
     79:   PetscCall(PetscMallocSet(PetscHBWMalloc, PetscHBWFree, PetscHBWRealloc));
     80:   PetscFunctionReturn(PETSC_SUCCESS);
     81: }