main.cu 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <iostream>
  2. #include "file1.h"
  3. #include "file2.h"
  4. int file2_launch_kernel(int x);
  5. result_type_dynamic __device__ file2_func(int x);
  6. static __global__ void main_kernel(result_type_dynamic& r, int x)
  7. {
  8. // call function that was not device linked to us, this will cause
  9. // a runtime failure of "invalid device function"
  10. r = file2_func(x);
  11. }
  12. int main_launch_kernel(int x)
  13. {
  14. result_type_dynamic r;
  15. main_kernel<<<1, 1>>>(r, x);
  16. return r.sum;
  17. }
  18. int choose_cuda_device()
  19. {
  20. int nDevices = 0;
  21. cudaError_t err = cudaGetDeviceCount(&nDevices);
  22. if (err != cudaSuccess) {
  23. std::cerr << "Failed to retrieve the number of CUDA enabled devices"
  24. << std::endl;
  25. return 1;
  26. }
  27. for (int i = 0; i < nDevices; ++i) {
  28. cudaDeviceProp prop;
  29. cudaError_t err = cudaGetDeviceProperties(&prop, i);
  30. if (err != cudaSuccess) {
  31. std::cerr << "Could not retrieve properties from CUDA device " << i
  32. << std::endl;
  33. return 1;
  34. }
  35. std::cout << "prop.major: " << prop.major << std::endl;
  36. if (prop.major >= 3) {
  37. err = cudaSetDevice(i);
  38. if (err != cudaSuccess) {
  39. std::cout << "Could not select CUDA device " << i << std::endl;
  40. } else {
  41. return 0;
  42. }
  43. }
  44. }
  45. std::cout << "Could not find a CUDA enabled card supporting compute >=3.0"
  46. << std::endl;
  47. return 1;
  48. }
  49. int main(int argc, char** argv)
  50. {
  51. int ret = choose_cuda_device();
  52. if (ret) {
  53. return 0;
  54. }
  55. main_launch_kernel(1);
  56. cudaError_t err = cudaGetLastError();
  57. if (err == cudaSuccess) {
  58. // This kernel launch should fail as the file2_func was device linked
  59. // into the static library and is not usable by the executable
  60. std::cerr << "main_launch_kernel: kernel launch should have failed"
  61. << std::endl;
  62. return 1;
  63. }
  64. return 0;
  65. }