Skip to content

Commit

Permalink
improved CUDA calls wrappers w/ better error checking
Browse files Browse the repository at this point in the history
  • Loading branch information
eliazonta committed Nov 27, 2023
1 parent 0783139 commit cffd8eb
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions include/utils.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
///////////////////////////////////////////////////////////////
// CUDA error check
//////////////////////////////////////////////////////////////
static void cuda_check_status(cudaError_t status)
#define cuda_error_check(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
{
if (status != cudaSuccess)
{
std::cerr << "error : CUDA API call : "
<< cudaGetErrorString(status) << std::endl;
exit(1);
}
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}

//////////////////////////////////////////////////////////////
Expand All @@ -35,8 +35,7 @@ template <typename T>
T* malloc_managed(size_t n, T value = T())
{
T* p;
auto status = cudaMallocManaged(&p, n * sizeof(T));
cuda_check_status(status);
cuda_error_check(cudaMallocManaged(&p, n * sizeof(T)));
std::fill(p, p + n, value);
return p;
}
Expand All @@ -45,7 +44,7 @@ template <typename T>
T* malloc_pinned(size_t n, T value = T())
{
T* p = nullptr;
cudaHostAlloc((void**)&p, n * sizeof(T), 0);
cuda_error_check(((void**)&p, n * sizeof(T), 0));
std::fill(p, p + n, value);
return p;
}
Expand All @@ -57,27 +56,26 @@ T* malloc_pinned(size_t n, T value = T())
template <typename T>
void copy_to_device(T* from, T* to, size_t n)
{
cuda_check_status(cudaMemcpy(to, from, n * sizeof(T), cudaMemcpyHostToDevice));
cuda_error_check(cudaMemcpy(to, from, n * sizeof(T), cudaMemcpyHostToDevice));
}

template <typename T>
void copy_to_host(T* from, T* to, size_t n)
{
cuda_check_status(cudaMemcpy(to, from, n * sizeof(T), cudaMemcpyDeviceToHost));
cuda_error_check(cudaMemcpy(to, from, n * sizeof(T), cudaMemcpyDeviceToHost));
}

template <typename T>
void copy_to_device_async(const T* from, T* to, size_t n, cudaStream_t stream = NULL)
{
auto status = cudaMemcpyAsync(to, from, n * sizeof(T), cudaMemcpyHostToDevice, stream);
cuda_check_status(status);
cuda_error_check(cudaMemcpyAsync(to, from, n * sizeof(T), cudaMemcpyHostToDevice, stream));

}

template <typename T>
void copy_to_host_async(const T* from, T* to, size_t n, cudaStream_t stream = NULL)
{
auto status = cudaMemcpyAsync(to, from, n * sizeof(T), cudaMemcpyDeviceToHost, stream);
cuda_check_status(status);
cuda_error_check(cudaMemcpyAsync(to, from, n * sizeof(T), cudaMemcpyDeviceToHost, stream));
}

///////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit cffd8eb

Please sign in to comment.