cmake/Tests/Cuda/Complex/dynamic.cu

74 lines
1.7 KiB
Plaintext
Raw Normal View History

2017-04-14 19:02:05 +02:00
#include <iostream>
#include <string>
2020-02-01 23:06:01 +01:00
#include <cuda.h>
2017-04-14 19:02:05 +02:00
#ifdef _WIN32
2018-08-09 18:06:22 +02:00
# define EXPORT __declspec(dllexport)
2017-04-14 19:02:05 +02:00
#else
2018-08-09 18:06:22 +02:00
# define EXPORT
2017-04-14 19:02:05 +02:00
#endif
int dynamic_base_func(int);
EXPORT int __host__ cuda_dynamic_host_func(int x)
{
return dynamic_base_func(x);
}
static __global__ void DetermineIfValidCudaDevice()
{
}
2017-07-20 19:35:53 +02:00
EXPORT int choose_cuda_device()
{
int nDevices = 0;
cudaError_t err = cudaGetDeviceCount(&nDevices);
if (err != cudaSuccess) {
std::cerr << "Failed to retrieve the number of CUDA enabled devices"
<< std::endl;
return 1;
}
for (int i = 0; i < nDevices; ++i) {
cudaDeviceProp prop;
cudaError_t err = cudaGetDeviceProperties(&prop, i);
if (err != cudaSuccess) {
std::cerr << "Could not retrieve properties from CUDA device " << i
<< std::endl;
return 1;
}
if (prop.major >= 3) {
err = cudaSetDevice(i);
if (err != cudaSuccess) {
std::cout << "Could not select CUDA device " << i << std::endl;
} else {
return 0;
}
}
}
std::cout << "Could not find a CUDA enabled card supporting compute >=3.0"
<< std::endl;
return 1;
}
2020-08-30 11:54:41 +02:00
EXPORT bool cuda_dynamic_lib_func()
2017-04-14 19:02:05 +02:00
{
cudaError_t err = cudaGetLastError();
2017-07-20 19:35:53 +02:00
if (err != cudaSuccess) {
2020-08-30 11:54:41 +02:00
std::cerr << "DetermineIfValidCudaDevice [Per Launch] failed: "
2017-07-20 19:35:53 +02:00
<< cudaGetErrorString(err) << std::endl;
2020-08-30 11:54:41 +02:00
return false;
2017-07-20 19:35:53 +02:00
}
2020-08-30 11:54:41 +02:00
DetermineIfValidCudaDevice<<<1, 1>>>();
2017-07-20 19:35:53 +02:00
err = cudaDeviceSynchronize();
if (err != cudaSuccess) {
2020-08-30 11:54:41 +02:00
std::cerr << "DetermineIfValidCudaDevice [SYNC] failed: "
2017-07-20 19:35:53 +02:00
<< cudaGetErrorString(cudaGetLastError()) << std::endl;
2020-08-30 11:54:41 +02:00
return false;
2017-04-14 19:02:05 +02:00
}
2020-08-30 11:54:41 +02:00
return true;
2017-04-14 19:02:05 +02:00
}