int main(int argc, char * argv[]) { size_t bytes = (argc>1) ? atol(argv[1]) : 1024*1024; int avail = hbw_check_available(); printf("%s returned %s\n", "hbw_check_available", avail==0 ? "SUCCESS" : "FAILURE"); assert(avail == 0); void * a = hbw_malloc(bytes); assert(a != NULL); hbw_free(a); void * b = hbw_calloc(bytes, 1); assert(b != NULL); hbw_free(b); void * c = hbw_realloc(NULL, bytes); printf("hbw_realloc c=%p\n", c); assert(c != NULL); void * d = hbw_realloc(c, 0); printf("hbw_realloc c=%p d=%p\n", c, d); void * e = NULL; int rc = hbw_posix_memalign(&e, 4096, bytes); printf("hbw_posix_memalign rc=%d e=%p\n", rc, e); assert(rc == 0 && e != NULL); return 0; }
/* PetscHBWMalloc - HBW malloc. Input Parameters: + a - number of bytes to allocate . lineno - line number where used . function - function calling routine - filename - file name where used Returns: double aligned pointer to requested storage, or null if not available. */ static PetscErrorCode PetscHBWMalloc(size_t a,int lineno,const char function[],const char filename[],void **result) { #if !defined(PETSC_HAVE_MEMKIND) return PetscMallocAlign(a,lineno,function,filename,result); #else if (!a) { *result = NULL; return 0; } /* The default policy is if insufficient memory is available from the high bandwidth memory fall back to standard memory. If we use the HBW_POLICY_BIND policy, errno is set to ENOMEM and the allocated pointer is set to NULL if there is not enough HWB memory available. */ { int ierr = hbw_posix_memalign(result,PETSC_MEMALIGN,a); if (ierr || !*result) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_MEM,"HBW Memory requested %.0f",(PetscLogDouble)a); } return 0; #endif }
void* seissol::memory::allocate(size_t i_size, size_t i_alignment, enum Memkind i_memkind) { void* l_ptrBuffer; bool error = false; /* handle zero allocation */ if ( i_size == 0 ) { logWarning() << "allocation of size 0 requested, returning NULL; (alignment: " << i_alignment << ", memkind: " << i_memkind << ")."; l_ptrBuffer = NULL; return l_ptrBuffer; } #ifdef USE_MEMKIND if( i_memkind == 0 ) { #endif if (i_alignment % (sizeof(void*)) != 0) { l_ptrBuffer = malloc( i_size ); error = (l_ptrBuffer == NULL); } else { error = (posix_memalign( &l_ptrBuffer, i_alignment, i_size ) != 0); } #ifdef USE_MEMKIND } else { if (i_alignment % (sizeof(void*)) != 0) { l_ptrBuffer = hbw_malloc( i_size ); error = (l_ptrBuffer == NULL); } else { error = (hbw_posix_memalign( &l_ptrBuffer, i_alignment, i_size ) != 0); } } #endif if (error) { logError() << "The malloc failed (bytes: " << i_size << ", alignment: " << i_alignment << ", memkind: " << i_memkind << ")."; } return l_ptrBuffer; }