dynamic.cu 1.7 KB

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