cuda.cu 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //===============================================================================================================================================================================================================200
  2. // INCLUDE/DEFINE
  3. //===============================================================================================================================================================================================================200
  4. #include "cuda.h" // (in library path specified to compiler)
  5. //===============================================================================================================================================================================================================200
  6. // SET DEVICE FUNCTION
  7. //===============================================================================================================================================================================================================200
  8. void setdevice(void)
  9. {
  10. // variables
  11. int num_devices;
  12. int device;
  13. // work
  14. cudaGetDeviceCount(&num_devices);
  15. if (num_devices > 1) {
  16. // variables
  17. int max_multiprocessors;
  18. int max_device;
  19. cudaDeviceProp properties;
  20. // initialize variables
  21. max_multiprocessors = 0;
  22. max_device = 0;
  23. for (device = 0; device < num_devices; device++) {
  24. cudaGetDeviceProperties(&properties, device);
  25. if (max_multiprocessors < properties.multiProcessorCount) {
  26. max_multiprocessors = properties.multiProcessorCount;
  27. max_device = device;
  28. }
  29. }
  30. cudaSetDevice(max_device);
  31. }
  32. }
  33. //===============================================================================================================================================================================================================200
  34. // GET LAST ERROR FUNCTION
  35. //===============================================================================================================================================================================================================200
  36. void checkCUDAError(const char *msg)
  37. {
  38. cudaError_t err = cudaGetLastError();
  39. if( cudaSuccess != err) {
  40. // fprintf(stderr, "Cuda error: %s: %s.\n", msg, cudaGetErrorString( err) );
  41. printf("Cuda error: %s: %s.\n", msg, cudaGetErrorString( err) );
  42. fflush(NULL);
  43. exit(EXIT_FAILURE);
  44. }
  45. }
  46. //===============================================================================================================================================================================================================200
  47. // END
  48. //===============================================================================================================================================================================================================200