platform.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #ifndef NCNN_PLATFORM_H
  15. #define NCNN_PLATFORM_H
  16. #define NCNN_STDIO 1
  17. #define NCNN_STRING 1
  18. #define NCNN_SIMPLEOCV 1
  19. #define NCNN_SIMPLEOMP 0
  20. #define NCNN_SIMPLESTL 0
  21. #define NCNN_THREADS 0
  22. #define NCNN_BENCHMARK 0
  23. #define NCNN_C_API 1
  24. #define NCNN_PLATFORM_API 1
  25. #define NCNN_PIXEL 1
  26. #define NCNN_PIXEL_ROTATE 1
  27. #define NCNN_PIXEL_AFFINE 1
  28. #define NCNN_PIXEL_DRAWING 1
  29. #define NCNN_VULKAN 0
  30. #define NCNN_SYSTEM_GLSLANG 0
  31. #define NCNN_RUNTIME_CPU 1
  32. #define NCNN_GNU_INLINE_ASM 1
  33. #define NCNN_AVX 0
  34. #define NCNN_XOP 0
  35. #define NCNN_FMA 0
  36. #define NCNN_F16C 0
  37. #define NCNN_AVX2 0
  38. #define NCNN_AVXVNNI 0
  39. #define NCNN_AVX512 0
  40. #define NCNN_AVX512VNNI 0
  41. #define NCNN_AVX512BF16 0
  42. #define NCNN_AVX512FP16 0
  43. #define NCNN_VFPV4 0
  44. #if __aarch64__
  45. #define NCNN_ARM82 0
  46. #define NCNN_ARM82DOT 0
  47. #define NCNN_ARM82FP16FML 0
  48. #define NCNN_ARM84BF16 0
  49. #define NCNN_ARM84I8MM 0
  50. #define NCNN_ARM86SVE 0
  51. #define NCNN_ARM86SVE2 0
  52. #define NCNN_ARM86SVEBF16 0
  53. #define NCNN_ARM86SVEI8MM 0
  54. #define NCNN_ARM86SVEF32MM 0
  55. #endif // __aarch64__
  56. #define NCNN_MSA 0
  57. #define NCNN_LSX 0
  58. #define NCNN_MMI 0
  59. #define NCNN_RVV 0
  60. #define NCNN_INT8 1
  61. #define NCNN_BF16 1
  62. #define NCNN_FORCE_INLINE 1
  63. #define NCNN_VERSION_STRING "1.0.20230418"
  64. #include "ncnn_export.h"
  65. #ifdef __cplusplus
  66. #if NCNN_THREADS
  67. #if (defined _WIN32 && !(defined __MINGW32__))
  68. #define WIN32_LEAN_AND_MEAN
  69. #include <windows.h>
  70. #include <process.h>
  71. #else
  72. #include <pthread.h>
  73. #endif
  74. #endif // NCNN_THREADS
  75. #if __ANDROID_API__ >= 26
  76. #define VK_USE_PLATFORM_ANDROID_KHR
  77. #endif // __ANDROID_API__ >= 26
  78. namespace ncnn {
  79. #if NCNN_THREADS
  80. #if (defined _WIN32 && !(defined __MINGW32__))
  81. class NCNN_EXPORT Mutex
  82. {
  83. public:
  84. Mutex() { InitializeSRWLock(&srwlock); }
  85. ~Mutex() {}
  86. void lock() { AcquireSRWLockExclusive(&srwlock); }
  87. void unlock() { ReleaseSRWLockExclusive(&srwlock); }
  88. private:
  89. friend class ConditionVariable;
  90. // NOTE SRWLock is available from windows vista
  91. SRWLOCK srwlock;
  92. };
  93. class NCNN_EXPORT ConditionVariable
  94. {
  95. public:
  96. ConditionVariable() { InitializeConditionVariable(&condvar); }
  97. ~ConditionVariable() {}
  98. void wait(Mutex& mutex) { SleepConditionVariableSRW(&condvar, &mutex.srwlock, INFINITE, 0); }
  99. void broadcast() { WakeAllConditionVariable(&condvar); }
  100. void signal() { WakeConditionVariable(&condvar); }
  101. private:
  102. CONDITION_VARIABLE condvar;
  103. };
  104. static unsigned __stdcall start_wrapper(void* args);
  105. class NCNN_EXPORT Thread
  106. {
  107. public:
  108. Thread(void* (*start)(void*), void* args = 0) { _start = start; _args = args; handle = (HANDLE)_beginthreadex(0, 0, start_wrapper, this, 0, 0); }
  109. ~Thread() {}
  110. void join() { WaitForSingleObject(handle, INFINITE); CloseHandle(handle); }
  111. private:
  112. friend unsigned __stdcall start_wrapper(void* args)
  113. {
  114. Thread* t = (Thread*)args;
  115. t->_start(t->_args);
  116. return 0;
  117. }
  118. HANDLE handle;
  119. void* (*_start)(void*);
  120. void* _args;
  121. };
  122. class NCNN_EXPORT ThreadLocalStorage
  123. {
  124. public:
  125. ThreadLocalStorage() { key = TlsAlloc(); }
  126. ~ThreadLocalStorage() { TlsFree(key); }
  127. void set(void* value) { TlsSetValue(key, (LPVOID)value); }
  128. void* get() { return (void*)TlsGetValue(key); }
  129. private:
  130. DWORD key;
  131. };
  132. #else // (defined _WIN32 && !(defined __MINGW32__))
  133. class NCNN_EXPORT Mutex
  134. {
  135. public:
  136. Mutex() { pthread_mutex_init(&mutex, 0); }
  137. ~Mutex() { pthread_mutex_destroy(&mutex); }
  138. void lock() { pthread_mutex_lock(&mutex); }
  139. void unlock() { pthread_mutex_unlock(&mutex); }
  140. private:
  141. friend class ConditionVariable;
  142. pthread_mutex_t mutex;
  143. };
  144. class NCNN_EXPORT ConditionVariable
  145. {
  146. public:
  147. ConditionVariable() { pthread_cond_init(&cond, 0); }
  148. ~ConditionVariable() { pthread_cond_destroy(&cond); }
  149. void wait(Mutex& mutex) { pthread_cond_wait(&cond, &mutex.mutex); }
  150. void broadcast() { pthread_cond_broadcast(&cond); }
  151. void signal() { pthread_cond_signal(&cond); }
  152. private:
  153. pthread_cond_t cond;
  154. };
  155. class NCNN_EXPORT Thread
  156. {
  157. public:
  158. Thread(void* (*start)(void*), void* args = 0) { pthread_create(&t, 0, start, args); }
  159. ~Thread() {}
  160. void join() { pthread_join(t, 0); }
  161. private:
  162. pthread_t t;
  163. };
  164. class NCNN_EXPORT ThreadLocalStorage
  165. {
  166. public:
  167. ThreadLocalStorage() { pthread_key_create(&key, 0); }
  168. ~ThreadLocalStorage() { pthread_key_delete(key); }
  169. void set(void* value) { pthread_setspecific(key, value); }
  170. void* get() { return pthread_getspecific(key); }
  171. private:
  172. pthread_key_t key;
  173. };
  174. #endif // (defined _WIN32 && !(defined __MINGW32__))
  175. #else // NCNN_THREADS
  176. class NCNN_EXPORT Mutex
  177. {
  178. public:
  179. Mutex() {}
  180. ~Mutex() {}
  181. void lock() {}
  182. void unlock() {}
  183. };
  184. class NCNN_EXPORT ConditionVariable
  185. {
  186. public:
  187. ConditionVariable() {}
  188. ~ConditionVariable() {}
  189. void wait(Mutex& /*mutex*/) {}
  190. void broadcast() {}
  191. void signal() {}
  192. };
  193. class NCNN_EXPORT Thread
  194. {
  195. public:
  196. Thread(void* (*/*start*/)(void*), void* /*args*/ = 0) {}
  197. ~Thread() {}
  198. void join() {}
  199. };
  200. class NCNN_EXPORT ThreadLocalStorage
  201. {
  202. public:
  203. ThreadLocalStorage() { data = 0; }
  204. ~ThreadLocalStorage() {}
  205. void set(void* value) { data = value; }
  206. void* get() { return data; }
  207. private:
  208. void* data;
  209. };
  210. #endif // NCNN_THREADS
  211. class NCNN_EXPORT MutexLockGuard
  212. {
  213. public:
  214. MutexLockGuard(Mutex& _mutex) : mutex(_mutex) { mutex.lock(); }
  215. ~MutexLockGuard() { mutex.unlock(); }
  216. private:
  217. Mutex& mutex;
  218. };
  219. } // namespace ncnn
  220. #if NCNN_SIMPLESTL
  221. #include "simplestl.h"
  222. #else
  223. #include <algorithm>
  224. #include <list>
  225. #include <vector>
  226. #include <string>
  227. #endif
  228. #endif // __cplusplus
  229. #if NCNN_STDIO
  230. #if NCNN_PLATFORM_API && __ANDROID_API__ >= 8
  231. #include <android/log.h>
  232. #define NCNN_LOGE(...) do { \
  233. fprintf(stderr, ##__VA_ARGS__); fprintf(stderr, "\n"); \
  234. __android_log_print(ANDROID_LOG_WARN, "ncnn", ##__VA_ARGS__); } while(0)
  235. #else // NCNN_PLATFORM_API && __ANDROID_API__ >= 8
  236. #include <stdio.h>
  237. #define NCNN_LOGE(...) do { \
  238. fprintf(stderr, ##__VA_ARGS__); fprintf(stderr, "\n"); } while(0)
  239. #endif // NCNN_PLATFORM_API && __ANDROID_API__ >= 8
  240. #else
  241. #define NCNN_LOGE(...)
  242. #endif
  243. #if NCNN_FORCE_INLINE
  244. #ifdef _MSC_VER
  245. #define NCNN_FORCEINLINE __forceinline
  246. #elif defined(__GNUC__)
  247. #define NCNN_FORCEINLINE inline __attribute__((__always_inline__))
  248. #elif defined(__CLANG__)
  249. #if __has_attribute(__always_inline__)
  250. #define NCNN_FORCEINLINE inline __attribute__((__always_inline__))
  251. #else
  252. #define NCNN_FORCEINLINE inline
  253. #endif
  254. #else
  255. #define NCNN_FORCEINLINE inline
  256. #endif
  257. #else
  258. #define NCNN_FORCEINLINE inline
  259. #endif
  260. #endif // NCNN_PLATFORM_H