diff --git a/TVALGPU/src/CudaEnvInit.cpp b/TVALGPU/src/CudaEnvInit.cpp new file mode 100644 index 0000000..f20549e --- /dev/null +++ b/TVALGPU/src/CudaEnvInit.cpp @@ -0,0 +1,44 @@ +#include"CudaEnvInit.h" + +#include "cuda_runtime.h" +#include + +extern bool resetAllGPUDevice() +{ + int deviceCount; + if(cudaGetDeviceCount(&deviceCount)!= cudaSuccess) return false; + cudaDeviceProp deviceProp; + for (size_t i = 0; i < deviceCount; i++) + { + if(cudaSetDevice(i)!= cudaSuccess) return false; + if(cudaDeviceReset()!= cudaSuccess) return false; + } + return true; +} + +extern bool resetGPUDevice(int device, std::string& msg) +{ + int deviceCount; + if(cudaGetDeviceCount(&deviceCount)!= cudaSuccess) { + msg = "get device count fail!"; + return false; + } + if (device>=deviceCount || device < 0){ + msg = "bad device index!"; + return false; + } + auto error = cudaSetDevice(device); + if(error == cudaErrorInvalidDevice){ + msg = "Invalid Device!"; + return false; + } + if(error == cudaErrorDevicesUnavailable){ + msg = "Devices Unavailable!"; + return false; + } + if(cudaDeviceReset()!= cudaSuccess) { + msg = "Reset GPUDevice fail!"; + return false; + } + return true; +} diff --git a/TVALGPU/src/CudaEnvInit.h b/TVALGPU/src/CudaEnvInit.h new file mode 100644 index 0000000..98783b9 --- /dev/null +++ b/TVALGPU/src/CudaEnvInit.h @@ -0,0 +1,8 @@ +#ifndef __CUDAENVINIT_H__ +#define __CUDAENVINIT_H__ + +#include +extern bool resetAllGPUDevice(); +extern bool resetGPUDevice(int device,std::string& msg); + +#endif // __CUDAENVINIT_H__ \ No newline at end of file