Add cuda init to TVALGPU

This commit is contained in:
kradchen
2023-06-06 09:04:58 +08:00
parent 1c63e89e08
commit 6bd93590f8
2 changed files with 52 additions and 0 deletions

View 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;
}

View 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__