| 1 |
|
| 2 |
#include <iostream> |
| 3 |
#include <string> |
| 4 |
|
| 5 |
#include <cuda.h> |
| 6 |
|
| 7 |
#ifdef _WIN32 |
| 8 |
# define EXPORT __declspec(dllexport) |
| 9 |
#else |
| 10 |
# define EXPORT |
| 11 |
#endif |
| 12 |
|
| 13 |
int dynamic_base_func(int); |
| 14 |
|
| 15 |
EXPORT int __host__ cuda_dynamic_host_func(int x) |
| 16 |
{ |
| 17 |
return dynamic_base_func(x); |
| 18 |
} |
| 19 |
|
| 20 |
static __global__ void DetermineIfValidCudaDevice() |
| 21 |
{ |
| 22 |
} |
| 23 |
|
| 24 |
EXPORT int choose_cuda_device() |
| 25 |
{ |
| 26 |
int nDevices = 0; |
| 27 |
cudaError_t err = cudaGetDeviceCount(&nDevices); |
| 28 |
if (err != cudaSuccess) { |
| 29 |
std::cerr << "Failed to retrieve the number of CUDA enabled devices" |
| 30 |
<< std::endl; |
| 31 |
return 1; |
| 32 |
} |
| 33 |
for (int i = 0; i < nDevices; ++i) { |
| 34 |
cudaDeviceProp prop; |
| 35 |
cudaError_t err = cudaGetDeviceProperties(&prop, i); |
| 36 |
if (err != cudaSuccess) { |
| 37 |
std::cerr << "Could not retrieve properties from CUDA device " << i |
| 38 |
<< std::endl; |
| 39 |
return 1; |
| 40 |
} |
| 41 |
if (prop.major >= 3) { |
| 42 |
err = cudaSetDevice(i); |
| 43 |
if (err != cudaSuccess) { |
| 44 |
std::cout << "Could not select CUDA device " << i << std::endl; |
| 45 |
} else { |
| 46 |
return 0; |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
std::cout << "Could not find a CUDA enabled card supporting compute >=3.0" |
| 52 |
<< std::endl; |
| 53 |
|
| 54 |
return 1; |
| 55 |
} |
| 56 |
|
| 57 |
EXPORT bool cuda_dynamic_lib_func() |
| 58 |
{ |
| 59 |
cudaError_t err = cudaGetLastError(); |
| 60 |
if (err != cudaSuccess) { |
| 61 |
std::cerr << "DetermineIfValidCudaDevice [Per Launch] failed: " |
| 62 |
<< cudaGetErrorString(err) << std::endl; |
| 63 |
return false; |
| 64 |
} |
| 65 |
DetermineIfValidCudaDevice<<<1, 1>>>(); |
| 66 |
err = cudaDeviceSynchronize(); |
| 67 |
if (err != cudaSuccess) { |
| 68 |
std::cerr << "DetermineIfValidCudaDevice [SYNC] failed: " |
| 69 |
<< cudaGetErrorString(cudaGetLastError()) << std::endl; |
| 70 |
return false; |
| 71 |
} |
| 72 |
return true; |
| 73 |
} |