You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

libcuda.cpp 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "megbrain_build_config.h"
  2. #pragma GCC visibility push(default)
  3. #include <cstdio>
  4. #define LOGE(fmt, v...) fprintf(stderr, "err: " fmt "\n", ##v)
  5. extern "C" {
  6. #include "cuda.h"
  7. }
  8. #include "cudaProfiler.h"
  9. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  10. static void log_failed_load(int func_idx);
  11. namespace {
  12. template <typename T>
  13. T on_init_failed(int func_idx);
  14. template <>
  15. CUresult on_init_failed(int func_idx) {
  16. log_failed_load(func_idx);
  17. return CUDA_ERROR_UNKNOWN;
  18. }
  19. } // namespace
  20. #define _WRAPLIB_API_CALL CUDAAPI
  21. #define _WRAPLIB_CALLBACK CUDA_CB
  22. #if CUDA_VERSION == 10010
  23. #include "./libcuda-wrap_10.1.h"
  24. //! as some symbols link from cuda lib, but used at other module, export here
  25. #ifdef WIN32
  26. #pragma comment(linker, "/export:cudaSetDevice")
  27. #pragma comment(linker, "/export:cuCtxGetCurrent")
  28. #pragma comment(linker, "/export:cudaGetDeviceCount")
  29. #pragma comment(linker, "/export:cudaGetDeviceProperties")
  30. #pragma comment(linker, "/export:cudaRuntimeGetVersion")
  31. #pragma comment(linker, "/export:cudaGetDevice")
  32. #pragma comment(linker, "/export:cudaDeviceSynchronize")
  33. #endif
  34. #elif CUDA_VERSION == 10020
  35. #include "./libcuda-wrap_10.2.h"
  36. //! as some symbols link from cuda lib, but used at other module, export here
  37. #ifdef WIN32
  38. #pragma comment(linker, "/export:cudaSetDevice")
  39. #pragma comment(linker, "/export:cuCtxGetCurrent")
  40. #pragma comment(linker, "/export:cudaGetDeviceCount")
  41. #pragma comment(linker, "/export:cudaGetDeviceProperties")
  42. #pragma comment(linker, "/export:cudaRuntimeGetVersion")
  43. #pragma comment(linker, "/export:cudaGetDevice")
  44. #pragma comment(linker, "/export:cudaDeviceSynchronize")
  45. #endif
  46. #elif CUDA_VERSION == 11010
  47. #include "./libcuda-wrap_11.1.h"
  48. #elif CUDA_VERSION == 11020
  49. #include "./libcuda-wrap_11.2.h"
  50. #elif CUDA_VERSION == 11040
  51. #include "./libcuda-wrap_11.4.h"
  52. #else
  53. #error "cuda stub not support this cuda version, you can close cuda stub to passby"
  54. #endif
  55. #undef _WRAPLIB_CALLBACK
  56. #undef _WRAPLIB_API_CALL
  57. static const char* default_so_name =
  58. #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
  59. "nvcuda.dll";
  60. #elif defined(__APPLE__) || defined(__MACOSX)
  61. "libcuda.dylib";
  62. #else
  63. "libcuda.so.1";
  64. #endif
  65. // Harvested from cuda_drvapi_dynlink.c
  66. static const char* default_so_paths[] = {
  67. #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
  68. "nvcuda.dll",
  69. #elif defined(__unix__) || defined(__QNX__) || defined(__APPLE__) || \
  70. defined(__MACOSX)
  71. #if defined(__APPLE__) || defined(__MACOSX)
  72. "/usr/local/cuda/lib/libcuda.dylib",
  73. #elif defined(__ANDROID__)
  74. #if defined(__aarch64__)
  75. "/system/vendor/lib64/libcuda.so",
  76. #elif defined(__arm__)
  77. "/system/vendor/lib/libcuda.so",
  78. #endif
  79. #else
  80. "libcuda.so.1",
  81. #endif
  82. #else
  83. #error "Unknown platform"
  84. #endif
  85. };
  86. static const char* extra_so_paths[] = {
  87. "/usr/lib/x86_64-linux-gnu/libcuda.so",
  88. "/usr/local/nvidia/lib64/libcuda.so",
  89. };
  90. static const char* g_default_api_name = "cuda";
  91. #include "./dlopen_helper.h"