-
-
Notifications
You must be signed in to change notification settings - Fork 912
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(cherry picked from commit 8d097a76ea1f74017fdd23794742df09133f3c35)
- Loading branch information
1 parent
adaeecc
commit a759b87
Showing
4 changed files
with
122 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
//////////////////////////////////////////////////////////////////// | ||
// UtilCUDADevice.h | ||
// | ||
// Copyright 2024 cDc@seacave | ||
// Distributed under the Boost Software License, Version 1.0 | ||
// (See http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#ifndef __SEACAVE_CUDA_DEVICE_H__ | ||
#define __SEACAVE_CUDA_DEVICE_H__ | ||
|
||
|
||
// I N C L U D E S ///////////////////////////////////////////////// | ||
|
||
#include "Config.h" | ||
|
||
// CUDA driver | ||
#include <cuda.h> | ||
|
||
// CUDA toolkit | ||
#include <cuda_runtime.h> | ||
|
||
#include <memory> | ||
|
||
|
||
// D E F I N E S /////////////////////////////////////////////////// | ||
|
||
#ifndef VERBOSE | ||
#define DEFINE_VERBOSE 1 | ||
#define VERBOSE(...) fprintf(stderr, __VA_ARGS__) | ||
#endif | ||
|
||
// check for CUDA errors following a CUDA call | ||
#define CUDA_CHECK(condition) SEACAVE::CUDA::checkCudaCall(condition) | ||
|
||
// check cudaGetLastError() for success | ||
#define CUDA_CHECK_LAST_ERROR CUDA_CHECK(cudaGetLastError()); | ||
|
||
|
||
// S T R U C T S /////////////////////////////////////////////////// | ||
|
||
namespace SEACAVE { | ||
|
||
namespace CUDA { | ||
|
||
inline void checkCudaCall(const cudaError_t error) { | ||
if (error == cudaSuccess) | ||
return; | ||
VERBOSE("CUDA error at %s:%d: %s (code %d)", __FILE__, __LINE__, cudaGetErrorString(error), error); | ||
ASSERT("CudaError" == NULL); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
// define smart pointers for CUDA stream | ||
struct CudaStreamDestructor { | ||
void operator()(cudaStream_t s) { | ||
if (s) | ||
CUDA_CHECK(cudaStreamDestroy(s)); | ||
} | ||
}; | ||
|
||
typedef std::unique_ptr<std::remove_pointer<cudaStream_t>::type, CudaStreamDestructor> CudaStreamPtr; | ||
inline CudaStreamPtr CreateStream() { | ||
cudaStream_t stream; | ||
CUDA_CHECK(cudaStreamCreate(&stream)); | ||
return CudaStreamPtr(stream, CudaStreamDestructor()); | ||
} | ||
|
||
typedef std::shared_ptr<std::remove_pointer<cudaStream_t>::type> CudaStreamSharedPtr; | ||
inline CudaStreamSharedPtr CreateSharedStream() { | ||
cudaStream_t stream; | ||
CUDA_CHECK(cudaStreamCreate(&stream)); | ||
return CudaStreamSharedPtr(stream, CudaStreamDestructor()); | ||
} | ||
/*----------------------------------------------------------------*/ | ||
|
||
} // namespace CUDA | ||
|
||
} // namespace SEACAVE | ||
|
||
#ifdef DEFINE_VERBOSE | ||
#undef DEFINE_VERBOSE | ||
#undef VERBOSE | ||
#endif | ||
|
||
#endif // __SEACAVE_CUDA_DEVICE_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters