dynamic.cu 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <cuda.h>
  2. #include <iostream>
  3. #include <string>
  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 void cuda_dynamic_lib_func()
  48. {
  49. DetermineIfValidCudaDevice<<<1, 1>>>();
  50. cudaError_t err = cudaGetLastError();
  51. if (err != cudaSuccess) {
  52. std::cerr << "DetermineIfValidCudaDevice [SYNC] failed: "
  53. << cudaGetErrorString(err) << std::endl;
  54. }
  55. err = cudaDeviceSynchronize();
  56. if (err != cudaSuccess) {
  57. std::cerr << "DetermineIfValidCudaDevice [ASYNC] failed: "
  58. << cudaGetErrorString(cudaGetLastError()) << std::endl;
  59. }
  60. }