Add cuda init to TVALGPU
This commit is contained in:
44
TVALGPU/src/CudaEnvInit.cpp
Normal file
44
TVALGPU/src/CudaEnvInit.cpp
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
#include"CudaEnvInit.h"
|
||||||
|
|
||||||
|
#include "cuda_runtime.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
8
TVALGPU/src/CudaEnvInit.h
Normal file
8
TVALGPU/src/CudaEnvInit.h
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#ifndef __CUDAENVINIT_H__
|
||||||
|
#define __CUDAENVINIT_H__
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
extern bool resetAllGPUDevice();
|
||||||
|
extern bool resetGPUDevice(int device,std::string& msg);
|
||||||
|
|
||||||
|
#endif // __CUDAENVINIT_H__
|
||||||
Reference in New Issue
Block a user