提交 383edfbc authored 作者: Frederic's avatar Frederic

Precheck the error BEFORE the check for memory available.

上级 654c8804
...@@ -64,6 +64,14 @@ void * device_malloc(size_t size) ...@@ -64,6 +64,14 @@ void * device_malloc(size_t size)
void * device_malloc(size_t size, int verbose) void * device_malloc(size_t size, int verbose)
{ {
#if PRECHECK_ERROR
cudaThreadSynchronize();
cudaError_t prevError = cudaGetLastError();
if (cudaSuccess != prevError)
{
fprintf(stderr, "Error existed before calling device_malloc.\n");
}
#endif
void * rval=NULL; void * rval=NULL;
cudaError_t err = cudaMalloc(&rval, size); cudaError_t err = cudaMalloc(&rval, size);
if (cudaSuccess != err) if (cudaSuccess != err)
...@@ -142,6 +150,14 @@ void * device_malloc(size_t size, int verbose) ...@@ -142,6 +150,14 @@ void * device_malloc(size_t size, int verbose)
int device_free(void *ptr) int device_free(void *ptr)
{ {
#if PRECHECK_ERROR
cudaThreadSynchronize();
cudaError_t prevError = cudaGetLastError();
if (cudaSuccess != prevError)
{
fprintf(stderr, "Error existed before calling device_free.\n");
}
#endif
#if PRINT_FREE_MALLOC #if PRINT_FREE_MALLOC
size_t free = 0, total = 0; size_t free = 0, total = 0;
cudaError_t err2 = cudaMemGetInfo(&free, &total); cudaError_t err2 = cudaMemGetInfo(&free, &total);
...@@ -151,17 +167,23 @@ int device_free(void *ptr) ...@@ -151,17 +167,23 @@ int device_free(void *ptr)
"Error when tring to find the memory information" "Error when tring to find the memory information"
" on the GPU: %s\n", cudaGetErrorString(err2)); " on the GPU: %s\n", cudaGetErrorString(err2));
} }
fprintf(stderr, "device_free %p" #if COMPUTE_GPU_MEM_USED
{
int i = 0;
for(;i<TABLE_SIZE;i++)
if(_alloc_size_table[i].ptr==ptr){
break;
}
assert(i<TABLE_SIZE);
fprintf(stderr, "device_free %p of size %d."
" Driver report %d bytes free and %d bytes total \n",
ptr, _alloc_size_table[i].size, free, total);
}
#else
fprintf(stderr, "device_free %p."
" Driver report %d bytes free and %d bytes total \n", " Driver report %d bytes free and %d bytes total \n",
ptr, free, total); ptr, free, total);
#endif #endif
#if PRECHECK_ERROR
cudaError_t prevError = cudaGetLastError();
if (cudaSuccess != prevError)
{
fprintf(stderr, "Error existed before calling device_free.\n");
}
#endif #endif
// if there is no gpu context, the call to cudaFree will fail; skip it entirely // if there is no gpu context, the call to cudaFree will fail; skip it entirely
...@@ -190,6 +212,7 @@ int device_free(void *ptr) ...@@ -190,6 +212,7 @@ int device_free(void *ptr)
" on the GPU: %s\n", cudaGetErrorString(err2)); " on the GPU: %s\n", cudaGetErrorString(err2));
} }
#if COMPUTE_GPU_MEM_USED #if COMPUTE_GPU_MEM_USED
{
int i = 0; int i = 0;
for(;i<TABLE_SIZE;i++) for(;i<TABLE_SIZE;i++)
if(_alloc_size_table[i].ptr==ptr){ if(_alloc_size_table[i].ptr==ptr){
...@@ -201,6 +224,7 @@ int device_free(void *ptr) ...@@ -201,6 +224,7 @@ int device_free(void *ptr)
" Driver report %d bytes free and %d bytes total \n", " Driver report %d bytes free and %d bytes total \n",
ptr, cudaGetErrorString(err), ptr, cudaGetErrorString(err),
_alloc_size_table[i].size, _allocated_size, free, total); _alloc_size_table[i].size, _allocated_size, free, total);
}
#else #else
fprintf(stderr, fprintf(stderr,
"Error freeing device pointer %p (%s)." "Error freeing device pointer %p (%s)."
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论