Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
balos1 committed Dec 13, 2023
1 parent f478e59 commit 089b6df
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 68 deletions.
16 changes: 8 additions & 8 deletions doc/shared/sunadaptcontroller/SUNAdaptController_Description.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ function pointers to the various controller operations, and is defined as
struct _generic_SUNAdaptController_Ops {
SUNAdaptController_Type (*getid)(SUNAdaptController C);
int (*destroy)(SUNAdaptController C);
int (*estimatestep)(SUNAdaptController C, sunrealtype h, int p, sunrealtype dsm, sunrealtype* hnew);
int (*reset)(SUNAdaptController C);
int (*setdefaults)(SUNAdaptController C);
int (*write)(SUNAdaptController C, FILE* fptr);
int (*seterrorbias)(SUNAdaptController C, sunrealtype bias);
int (*updateh)(SUNAdaptController C, sunrealtype h, sunrealtype dsm);
int (*space)(SUNAdaptController C, long int *lenrw, long int *leniw);
SUNErrCode (*destroy)(SUNAdaptController C);
SUNErrCode (*estimatestep)(SUNAdaptController C, sunrealtype h, int p, sunrealtype dsm, sunrealtype* hnew);
SUNErrCode (*reset)(SUNAdaptController C);
SUNErrCode (*setdefaults)(SUNAdaptController C);
SUNErrCode (*write)(SUNAdaptController C, FILE* fptr);
SUNErrCode (*seterrorbias)(SUNAdaptController C, sunrealtype bias);
SUNErrCode (*updateh)(SUNAdaptController C, sunrealtype h, sunrealtype dsm);
SUNErrCode (*space)(SUNAdaptController C, long int *lenrw, long int *leniw);
};
Expand Down
2 changes: 1 addition & 1 deletion doc/shared/sunmemory/SUNMemory_Description.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ must define:
**Returns:**
* A :c:type:`SUNErrCode` indicating success or failure.
* A new :c:type:`SUNMemory` object
.. c:function:: SUNErrCode SUNMemoryHelper_Dealloc(SUNMemoryHelper helper, \
Expand Down
2 changes: 1 addition & 1 deletion doc/shared/sunmemory/SUNMemory_SYCL.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ The implementation provides the following operations defined by the
**Returns:**
* A :c:type:`SUNErrCode` indicating success or failure.
* A new :c:type:`SUNMemory` object
.. c:function:: SUNErrCode SUNMemoryHelper_Dealloc_Sycl(SUNMemoryHelper helper, \
Expand Down
1 change: 1 addition & 0 deletions include/sundials/sundials_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
ENTRY(SUN_ERR_ARG_DIMSMISMATCH, "argument dimensions do not agree") \
\
ENTRY(SUN_ERR_CORRUPT, "Object is NULL or corrupt") \
ENTRY(SUN_ERR_OUTOFRANGE, "Value is out of the expected range") \
ENTRY(SUN_ERR_FILE_OPEN, "Unable to open file") \
ENTRY(SUN_ERR_MEM_FAIL, "a memory operation failed") \
ENTRY(SUN_ERR_MALLOC_FAIL, "malloc returned NULL") \
Expand Down
40 changes: 17 additions & 23 deletions src/sunmemory/cuda/sundials_cuda_memory.cu
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ SUNErrCode SUNMemoryHelper_Alloc_Cuda(SUNMemoryHelper helper, SUNMemory* memptr,
size_t mem_size, SUNMemoryType mem_type,
void* queue)
{
SUNFunctionBegin(helper->sunctx);

SUNMemory mem = SUNMemoryNewEmpty(helper->sunctx);
SUNCheckLastErr();

mem->ptr = NULL;
mem->own = SUNTRUE;
Expand All @@ -113,21 +116,12 @@ SUNErrCode SUNMemoryHelper_Alloc_Cuda(SUNMemoryHelper helper, SUNMemory* memptr,
if (mem_type == SUNMEMTYPE_HOST)
{
mem->ptr = malloc(mem_size);
if (mem->ptr == NULL)
{
SUNDIALS_DEBUG_PRINT(
"ERROR in SUNMemoryHelper_Alloc_Cuda: malloc returned NULL\n");
free(mem);
return (-1);
}
else
{
SUNHELPER_CONTENT(helper)->bytes_allocated_host += mem_size;
SUNHELPER_CONTENT(helper)->num_allocations_host++;
SUNHELPER_CONTENT(helper)->bytes_high_watermark_host =
SUNMAX(SUNHELPER_CONTENT(helper)->bytes_allocated_host,
SUNHELPER_CONTENT(helper)->bytes_high_watermark_host);
}
SUNAssert(mem->ptr, SUN_ERR_MALLOC_FAIL);
SUNHELPER_CONTENT(helper)->bytes_allocated_host += mem_size;
SUNHELPER_CONTENT(helper)->num_allocations_host++;
SUNHELPER_CONTENT(helper)->bytes_high_watermark_host =
SUNMAX(SUNHELPER_CONTENT(helper)->bytes_allocated_host,
SUNHELPER_CONTENT(helper)->bytes_high_watermark_host);
}
else if (mem_type == SUNMEMTYPE_PINNED)
{
Expand All @@ -136,7 +130,7 @@ SUNErrCode SUNMemoryHelper_Alloc_Cuda(SUNMemoryHelper helper, SUNMemory* memptr,
SUNDIALS_DEBUG_PRINT(
"ERROR in SUNMemoryHelper_Alloc_Cuda: cudaMallocHost failed\n");
free(mem);
return (-1);
return SUN_ERR_EXT_FAIL;
}
else
{
Expand Down Expand Up @@ -188,7 +182,7 @@ SUNErrCode SUNMemoryHelper_Alloc_Cuda(SUNMemoryHelper helper, SUNMemory* memptr,
SUNDIALS_DEBUG_PRINT(
"ERROR in SUNMemoryHelper_Alloc_Cuda: unknown memory type\n");
free(mem);
return SUN_ERR_CORRUPT;
return SUN_ERR_ARG_OUTOFRANGE;
}

*memptr = mem;
Expand Down Expand Up @@ -276,7 +270,7 @@ SUNErrCode SUNMemoryHelper_Copy_Cuda(SUNMemoryHelper helper, SUNMemory dst,
{
cuerr = cudaMemcpy(dst->ptr, src->ptr, memory_size, cudaMemcpyHostToDevice);
}
if (!SUNDIALS_CUDA_VERIFY(cuerr)) { retval = -1; }
if (!SUNDIALS_CUDA_VERIFY(cuerr)) { retval = SUN_ERR_EXT_FAIL; }
break;
case SUNMEMTYPE_UVM:
case SUNMEMTYPE_DEVICE:
Expand All @@ -289,12 +283,12 @@ SUNErrCode SUNMemoryHelper_Copy_Cuda(SUNMemoryHelper helper, SUNMemory dst,
cuerr = cudaMemcpy(dst->ptr, src->ptr, memory_size,
cudaMemcpyDeviceToDevice);
}
if (!SUNDIALS_CUDA_VERIFY(cuerr)) { retval = -1; }
if (!SUNDIALS_CUDA_VERIFY(cuerr)) { retval = SUN_ERR_EXT_FAIL; }
break;
default:
SUNDIALS_DEBUG_PRINT(
"ERROR in SUNMemoryHelper_CopyAsync_Cuda: unknown memory type\n");
retval = SUN_ERR_CORRUPT;
retval = SUN_ERR_OUTOFRANGE;
}

return (retval);
Expand Down Expand Up @@ -323,7 +317,7 @@ SUNErrCode SUNMemoryHelper_CopyAsync_Cuda(SUNMemoryHelper helper, SUNMemory dst,
cuerr = cudaMemcpyAsync(dst->ptr, src->ptr, memory_size,
cudaMemcpyHostToDevice, stream);
}
if (!SUNDIALS_CUDA_VERIFY(cuerr)) { retval = -1; }
if (!SUNDIALS_CUDA_VERIFY(cuerr)) { retval = SUN_ERR_EXT_FAIL; }
break;
case SUNMEMTYPE_UVM:
case SUNMEMTYPE_DEVICE:
Expand All @@ -337,12 +331,12 @@ SUNErrCode SUNMemoryHelper_CopyAsync_Cuda(SUNMemoryHelper helper, SUNMemory dst,
cuerr = cudaMemcpyAsync(dst->ptr, src->ptr, memory_size,
cudaMemcpyDeviceToDevice, stream);
}
if (!SUNDIALS_CUDA_VERIFY(cuerr)) { retval = -1; }
if (!SUNDIALS_CUDA_VERIFY(cuerr)) { retval = SUN_ERR_EXT_FAIL; }
break;
default:
SUNDIALS_DEBUG_PRINT(
"ERROR in SUNMemoryHelper_CopyAsync_Cuda: unknown memory type\n");
retval = SUN_ERR_CORRUPT;
retval = SUN_ERR_OUTOFRANGE;
}

return (retval);
Expand Down
45 changes: 19 additions & 26 deletions src/sunmemory/hip/sundials_hip_memory.hip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ SUNErrCode SUNMemoryHelper_Alloc_Hip(SUNMemoryHelper helper, SUNMemory* memptr,
size_t mem_size, SUNMemoryType mem_type,
void* queue)
{
SUNFunctionBegin(helper->sunctx);

SUNMemory mem = SUNMemoryNewEmpty(helper->sunctx);
SUNCheckLastErrNull();

Expand All @@ -114,21 +116,12 @@ SUNErrCode SUNMemoryHelper_Alloc_Hip(SUNMemoryHelper helper, SUNMemory* memptr,
if (mem_type == SUNMEMTYPE_HOST)
{
mem->ptr = malloc(mem_size);
if (mem->ptr == NULL)
{
SUNDIALS_DEBUG_PRINT(
"ERROR in SUNMemoryHelper_Alloc_Hip: malloc returned NULL\n");
free(mem);
return (-1);
}
else
{
SUNHELPER_CONTENT(helper)->bytes_allocated_host += mem_size;
SUNHELPER_CONTENT(helper)->num_allocations_host++;
SUNHELPER_CONTENT(helper)->bytes_high_watermark_host =
SUNMAX(SUNHELPER_CONTENT(helper)->bytes_allocated_host,
SUNHELPER_CONTENT(helper)->bytes_high_watermark_host);
}
SUNAssert(mem->ptr, SUN_ERR_MALLOC_FAIL);
SUNHELPER_CONTENT(helper)->bytes_allocated_host += mem_size;
SUNHELPER_CONTENT(helper)->num_allocations_host++;
SUNHELPER_CONTENT(helper)->bytes_high_watermark_host =
SUNMAX(SUNHELPER_CONTENT(helper)->bytes_allocated_host,
SUNHELPER_CONTENT(helper)->bytes_high_watermark_host);
}
else if (mem_type == SUNMEMTYPE_PINNED)
{
Expand All @@ -137,7 +130,7 @@ SUNErrCode SUNMemoryHelper_Alloc_Hip(SUNMemoryHelper helper, SUNMemory* memptr,
SUNDIALS_DEBUG_PRINT(
"ERROR in SUNMemoryHelper_Alloc_Hip: hipMallocHost failed\n");
free(mem);
return (-1);
return SUN_ERR_EXT_FAIL;
}
else
{
Expand All @@ -155,7 +148,7 @@ SUNErrCode SUNMemoryHelper_Alloc_Hip(SUNMemoryHelper helper, SUNMemory* memptr,
SUNDIALS_DEBUG_PRINT(
"ERROR in SUNMemoryHelper_Alloc_Hip: hipMalloc failed\n");
free(mem);
return (-1);
return SUN_ERR_EXT_FAIL;
}
else
{
Expand All @@ -173,7 +166,7 @@ SUNErrCode SUNMemoryHelper_Alloc_Hip(SUNMemoryHelper helper, SUNMemory* memptr,
SUNDIALS_DEBUG_PRINT(
"ERROR in SUNMemoryHelper_Alloc_Hip: hipMallocManaged failed\n");
free(mem);
return (-1);
return SUN_ERR_EXT_FAIL;
}
else
{
Expand All @@ -189,7 +182,7 @@ SUNErrCode SUNMemoryHelper_Alloc_Hip(SUNMemoryHelper helper, SUNMemory* memptr,
SUNDIALS_DEBUG_PRINT(
"ERROR in SUNMemoryHelper_Alloc_Hip: unknown memory type\n");
free(mem);
return (-1);
return SUN_ERR_OUTOFRANGE;
}

*memptr = mem;
Expand Down Expand Up @@ -218,7 +211,7 @@ SUNErrCode SUNMemoryHelper_Dealloc_Hip(SUNMemoryHelper helper, SUNMemory mem,
{
SUNDIALS_DEBUG_PRINT(
"ERROR in SUNMemoryHelper_Dealloc_Hip: hipFreeHost failed\n");
return (-1);
return SUN_ERR_EXT_FAIL;
}
mem->ptr = NULL;
}
Expand All @@ -230,7 +223,7 @@ SUNErrCode SUNMemoryHelper_Dealloc_Hip(SUNMemoryHelper helper, SUNMemory mem,
{
SUNDIALS_DEBUG_PRINT(
"ERROR in SUNMemoryHelper_Dealloc_Hip: hipFree failed\n");
return (-1);
return SUN_ERR_EXT_FAIL;
}
mem->ptr = NULL;
}
Expand All @@ -242,15 +235,15 @@ SUNErrCode SUNMemoryHelper_Dealloc_Hip(SUNMemoryHelper helper, SUNMemory mem,
{
SUNDIALS_DEBUG_PRINT(
"ERROR in SUNMemoryHelper_Dealloc_Hip: hipFree failed\n");
return (-1);
return SUN_ERR_EXT_FAIL;
}
mem->ptr = NULL;
}
else
{
SUNDIALS_DEBUG_PRINT(
"ERROR in SUNMemoryHelper_Dealloc_Hip: unknown memory type\n");
return (-1);
return SUN_ERR_OUTOFRANGE;
}
}

Expand Down Expand Up @@ -295,7 +288,7 @@ SUNErrCode SUNMemoryHelper_Copy_Hip(SUNMemoryHelper helper, SUNMemory dst,
default:
SUNDIALS_DEBUG_PRINT(
"ERROR in SUNMemoryHelper_CopyAsync_Hip: unknown memory type\n");
retval = SUN_ERR_CORRUPT;
retval = SUN_ERR_OUTOFRANGE;
}

return (retval);
Expand Down Expand Up @@ -343,7 +336,7 @@ SUNErrCode SUNMemoryHelper_CopyAsync_Hip(SUNMemoryHelper helper, SUNMemory dst,
default:
SUNDIALS_DEBUG_PRINT(
"ERROR in SUNMemoryHelper_CopyAsync_Hip: unknown memory type\n");
retval = SUN_ERR_CORRUPT;
retval = SUN_ERR_OUTOFRANGE;
}

return (retval);
Expand Down Expand Up @@ -395,6 +388,6 @@ SUNErrCode SUNMemoryHelper_GetAllocStats_Hip(SUNMemoryHelper helper,
*bytes_allocated = SUNHELPER_CONTENT(helper)->bytes_allocated_uvm;
*bytes_high_watermark = SUNHELPER_CONTENT(helper)->bytes_high_watermark_uvm;
}
else { return SUN_ERR_CORRUPT; }
else { return SUN_ERR_ARG_OUTOFRANGE; }
return SUN_SUCCESS;
}
17 changes: 8 additions & 9 deletions src/sunmemory/sycl/sundials_sycl_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,11 @@ typedef struct SUNMemoryHelper_Content_Sycl_ SUNMemoryHelper_Content_Sycl;

SUNMemoryHelper SUNMemoryHelper_Sycl(SUNContext sunctx)
{
SUNFunctionBegin(sunctx);

// Allocate the helper
SUNMemoryHelper helper = SUNMemoryHelper_NewEmpty(sunctx);
if (!helper)
{
SUNDIALS_DEBUG_PRINT("ERROR in SUNMemoryHelper_Sycl: "
"SUNMemoryHelper_NewEmpty returned NULL\n");
return NULL;
}
SUNCheckLastErrNull();

// Set the ops
helper->ops->alloc = SUNMemoryHelper_Alloc_Sycl;
Expand All @@ -69,6 +66,8 @@ SUNMemoryHelper SUNMemoryHelper_Sycl(SUNContext sunctx)
// Attach content
helper->content =
(SUNMemoryHelper_Content_Sycl*)malloc(sizeof(SUNMemoryHelper_Content_Sycl));
SUNAssertNull(helper->content, SUN_ERR_MALLOC_FAIL);

SUNHELPER_CONTENT(helper)->num_allocations_host = 0;
SUNHELPER_CONTENT(helper)->num_deallocations_host = 0;
SUNHELPER_CONTENT(helper)->bytes_allocated_host = 0;
Expand Down Expand Up @@ -199,7 +198,7 @@ SUNErrCode SUNMemoryHelper_Alloc_Sycl(SUNMemoryHelper helper, SUNMemory* memptr,
SUNDIALS_DEBUG_PRINT(
"ERROR in SUNMemoryHelper_Alloc_Sycl: unknown memory type\n");
free(mem);
return SUN_ERR_CORRUPT;
return SUN_ERR_ARG_OUTOFRANGE;
}

*memptr = mem;
Expand Down Expand Up @@ -250,7 +249,7 @@ SUNErrCode SUNMemoryHelper_Dealloc_Sycl(SUNMemoryHelper helper, SUNMemory mem,
{
SUNDIALS_DEBUG_PRINT(
"ERROR in SUNMemoryHelper_Dealloc_Sycl: unknown memory type\n");
return SUN_ERR_CORRUPT;
return SUN_ERR_OUTOFRANGE;
}
}

Expand Down Expand Up @@ -336,6 +335,6 @@ SUNErrCode SUNMemoryHelper_GetAllocStats_Sycl(SUNMemoryHelper helper,
*bytes_allocated = SUNHELPER_CONTENT(helper)->bytes_allocated_uvm;
*bytes_high_watermark = SUNHELPER_CONTENT(helper)->bytes_high_watermark_uvm;
}
else { return SUN_ERR_EXT_FAIL; }
else { return SUN_ERR_ARG_OUTOFRANGE; }
return SUN_SUCCESS;
}

0 comments on commit 089b6df

Please sign in to comment.