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.

libatlas-wrap.cpp 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #pragma GCC visibility push(default)
  2. #include <cstdio>
  3. #define LOGE(fmt, v...) fprintf(stderr, "err: " fmt "\n", ##v)
  4. #include "acl/acl.h"
  5. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  6. #if defined(_WIN32)
  7. #include <windows.h>
  8. #define RTLD_LAZY 0
  9. static void* dlopen(const char* file, int) {
  10. return static_cast<void*>(LoadLibraryA(file));
  11. }
  12. static void* dlerror() {
  13. const char* errmsg = "dlerror not aviable in windows";
  14. return const_cast<char*>(errmsg);
  15. }
  16. static void* dlsym(void* handle, const char* name) {
  17. FARPROC symbol = GetProcAddress((HMODULE)handle, name);
  18. return reinterpret_cast<void*>(symbol);
  19. }
  20. #else
  21. #include <dlfcn.h>
  22. #include <unistd.h>
  23. #endif
  24. static void log_failed_load(int func_idx);
  25. namespace {
  26. template <typename T>
  27. T on_init_failed(int func_idx);
  28. template <>
  29. float on_init_failed(int func_idx) {
  30. log_failed_load(func_idx);
  31. return 0.f;
  32. }
  33. template <>
  34. aclFloat16 on_init_failed(int func_idx) {
  35. log_failed_load(func_idx);
  36. return 0;
  37. }
  38. template <>
  39. aclDataBuffer* on_init_failed(int func_idx) {
  40. log_failed_load(func_idx);
  41. return nullptr;
  42. }
  43. template <>
  44. aclError on_init_failed(int func_idx) {
  45. log_failed_load(func_idx);
  46. return ACL_ERROR_INTERNAL_ERROR;
  47. }
  48. template <>
  49. void* on_init_failed(int func_idx) {
  50. log_failed_load(func_idx);
  51. return nullptr;
  52. }
  53. template <>
  54. uint32_t on_init_failed(int func_idx) {
  55. log_failed_load(func_idx);
  56. return 0;
  57. }
  58. template <>
  59. size_t on_init_failed(int func_idx) {
  60. log_failed_load(func_idx);
  61. return 0;
  62. }
  63. template <>
  64. void on_init_failed(int func_idx) {
  65. log_failed_load(func_idx);
  66. }
  67. template <>
  68. int64_t on_init_failed(int func_idx) {
  69. log_failed_load(func_idx);
  70. return 0;
  71. }
  72. template <>
  73. const char* on_init_failed(int func_idx) {
  74. log_failed_load(func_idx);
  75. return "load lib failed";
  76. }
  77. template <>
  78. aclopAttr* on_init_failed(int func_idx) {
  79. log_failed_load(func_idx);
  80. return nullptr;
  81. }
  82. template <>
  83. aclmdlDesc* on_init_failed(int func_idx) {
  84. log_failed_load(func_idx);
  85. return nullptr;
  86. }
  87. template <>
  88. aclmdlDataset* on_init_failed(int func_idx) {
  89. log_failed_load(func_idx);
  90. return nullptr;
  91. }
  92. template <>
  93. aclFormat on_init_failed(int func_idx) {
  94. log_failed_load(func_idx);
  95. return ACL_FORMAT_UNDEFINED;
  96. }
  97. template <>
  98. aclTensorDesc* on_init_failed(int func_idx) {
  99. log_failed_load(func_idx);
  100. return nullptr;
  101. }
  102. template <>
  103. aclDataType on_init_failed(int func_idx) {
  104. log_failed_load(func_idx);
  105. return ACL_DT_UNDEFINED;
  106. }
  107. template <>
  108. aclmdlAIPP* on_init_failed(int func_idx) {
  109. log_failed_load(func_idx);
  110. return nullptr;
  111. }
  112. template <>
  113. aclmdlConfigHandle* on_init_failed(int func_idx) {
  114. log_failed_load(func_idx);
  115. return nullptr;
  116. }
  117. template <>
  118. tagRtGroupInfo* on_init_failed(int func_idx) {
  119. log_failed_load(func_idx);
  120. return nullptr;
  121. }
  122. } // namespace
  123. //! atlas310
  124. #if !defined(ACL_MAJOR_VERSION)
  125. #include "./libatlas-wrap.h"
  126. //! atlas710
  127. #elif (ACL_MAJOR_VERSION == 1 && ACL_MINOR_VERSION == 1 && ACL_PATCH_VERSION == 0)
  128. #include "./libatlas-wrap_1.1.0.h"
  129. #endif
  130. static const char* default_so_paths[] = {
  131. "/usr/local/Ascend/acllib/lib64/libascendcl.so",
  132. "libascendcl.so",
  133. };
  134. static void* get_library_handle() {
  135. void* handle = nullptr;
  136. for (size_t i = 0; i < (sizeof(default_so_paths) / sizeof(char*)); i++) {
  137. handle = dlopen(default_so_paths[i], RTLD_LAZY);
  138. if (handle) {
  139. break;
  140. }
  141. }
  142. if (!handle) {
  143. LOGE("Failed to load atlas library");
  144. return nullptr;
  145. }
  146. return handle;
  147. }
  148. static void log_failed_load(int func_idx) {
  149. LOGE("failed to load atlas func: %s", g_func_name[func_idx]);
  150. }
  151. static void* resolve_library_func(void* handle, const char* func) {
  152. if (!handle) {
  153. LOGE("handle should not be nullptr!");
  154. return nullptr;
  155. }
  156. auto ret = dlsym(handle, func);
  157. return ret;
  158. }