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.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #else
  51. #error "cuda stub not support this cuda version, you can close cuda stub to passby"
  52. #endif
  53. #undef _WRAPLIB_CALLBACK
  54. #undef _WRAPLIB_API_CALL
  55. static const char* default_so_name =
  56. #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
  57. "nvcuda.dll";
  58. #elif defined(__APPLE__) || defined(__MACOSX)
  59. "libcuda.dylib";
  60. #else
  61. "libcuda.so.1";
  62. #endif
  63. // Harvested from cuda_drvapi_dynlink.c
  64. static const char* default_so_paths[] = {
  65. #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
  66. "nvcuda.dll",
  67. #elif defined(__unix__) || defined(__QNX__) || defined(__APPLE__) || \
  68. defined(__MACOSX)
  69. #if defined(__APPLE__) || defined(__MACOSX)
  70. "/usr/local/cuda/lib/libcuda.dylib",
  71. #elif defined(__ANDROID__)
  72. #if defined(__aarch64__)
  73. "/system/vendor/lib64/libcuda.so",
  74. #elif defined(__arm__)
  75. "/system/vendor/lib/libcuda.so",
  76. #endif
  77. #else
  78. "libcuda.so.1",
  79. #endif
  80. #else
  81. #error "Unknown platform"
  82. #endif
  83. };
  84. static const char* extra_so_paths[] = {
  85. "/usr/lib/x86_64-linux-gnu/libcuda.so",
  86. "/usr/local/nvidia/lib64/libcuda.so",
  87. };
  88. static const char* g_default_api_name = "cuda";
  89. #include "./dlopen_helper.h"

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台