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.

blas_server_omp.c 15 kB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*********************************************************************/
  2. /* Copyright 2009, 2010 The University of Texas at Austin. */
  3. /* All rights reserved. */
  4. /* */
  5. /* Redistribution and use in source and binary forms, with or */
  6. /* without modification, are permitted provided that the following */
  7. /* conditions are met: */
  8. /* */
  9. /* 1. Redistributions of source code must retain the above */
  10. /* copyright notice, this list of conditions and the following */
  11. /* disclaimer. */
  12. /* */
  13. /* 2. Redistributions in binary form must reproduce the above */
  14. /* copyright notice, this list of conditions and the following */
  15. /* disclaimer in the documentation and/or other materials */
  16. /* provided with the distribution. */
  17. /* */
  18. /* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */
  19. /* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */
  20. /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
  21. /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
  22. /* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */
  23. /* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */
  24. /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
  25. /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */
  26. /* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */
  27. /* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
  28. /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
  29. /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */
  30. /* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
  31. /* POSSIBILITY OF SUCH DAMAGE. */
  32. /* */
  33. /* The views and conclusions contained in the software and */
  34. /* documentation are those of the authors and should not be */
  35. /* interpreted as representing official policies, either expressed */
  36. /* or implied, of The University of Texas at Austin. */
  37. /*********************************************************************/
  38. #include <stdbool.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. //#include <sys/mman.h>
  42. #include "common.h"
  43. #ifndef USE_OPENMP
  44. #include "blas_server.c"
  45. #else
  46. #ifndef likely
  47. #ifdef __GNUC__
  48. #define likely(x) __builtin_expect(!!(x), 1)
  49. #else
  50. #define likely(x) (x)
  51. #endif
  52. #endif
  53. #ifndef unlikely
  54. #ifdef __GNUC__
  55. #define unlikely(x) __builtin_expect(!!(x), 0)
  56. #else
  57. #define unlikely(x) (x)
  58. #endif
  59. #endif
  60. #ifndef OMP_SCHED
  61. #define OMP_SCHED static
  62. #endif
  63. int blas_server_avail = 0;
  64. int blas_omp_number_max = 0;
  65. int blas_omp_threads_local = 1;
  66. extern int openblas_omp_adaptive_env(void);
  67. static void * blas_thread_buffer[MAX_PARALLEL_NUMBER][MAX_CPU_NUMBER];
  68. #ifdef HAVE_C11
  69. static atomic_bool blas_buffer_inuse[MAX_PARALLEL_NUMBER];
  70. #else
  71. static _Bool blas_buffer_inuse[MAX_PARALLEL_NUMBER];
  72. #endif
  73. static void adjust_thread_buffers(void) {
  74. int i=0, j=0;
  75. //adjust buffer for each thread
  76. for(i=0; i < MAX_PARALLEL_NUMBER; i++) {
  77. for(j=0; j < blas_cpu_number; j++){
  78. if(blas_thread_buffer[i][j] == NULL){
  79. blas_thread_buffer[i][j] = blas_memory_alloc(2);
  80. }
  81. }
  82. for(; j < MAX_CPU_NUMBER; j++){
  83. if(blas_thread_buffer[i][j] != NULL){
  84. blas_memory_free(blas_thread_buffer[i][j]);
  85. blas_thread_buffer[i][j] = NULL;
  86. }
  87. }
  88. }
  89. }
  90. void goto_set_num_threads(int num_threads) {
  91. if (num_threads < 1) num_threads = blas_num_threads;
  92. if (num_threads > MAX_CPU_NUMBER) num_threads = MAX_CPU_NUMBER;
  93. if (num_threads > blas_num_threads) {
  94. blas_num_threads = num_threads;
  95. }
  96. blas_cpu_number = num_threads;
  97. adjust_thread_buffers();
  98. #if defined(ARCH_MIPS64) || defined(ARCH_LOONGARCH64)
  99. #ifndef DYNAMIC_ARCH
  100. //set parameters for different number of threads.
  101. blas_set_parameter();
  102. #endif
  103. #endif
  104. }
  105. void openblas_set_num_threads(int num_threads) {
  106. goto_set_num_threads(num_threads);
  107. }
  108. #ifdef OS_LINUX
  109. int openblas_setaffinity(int thread_idx, size_t cpusetsize, cpu_set_t* cpu_set) {
  110. fprintf(stderr,"OpenBLAS: use OpenMP environment variables for setting cpu affinity\n");
  111. return -1;
  112. }
  113. int openblas_getaffinity(int thread_idx, size_t cpusetsize, cpu_set_t* cpu_set) {
  114. fprintf(stderr,"OpenBLAS: use OpenMP environment variables for querying cpu affinity\n");
  115. return -1;
  116. }
  117. #endif
  118. int blas_thread_init(void){
  119. #if defined(__FreeBSD__) && defined(__clang__)
  120. extern int openblas_omp_num_threads_env(void);
  121. if(blas_omp_number_max <= 0)
  122. blas_omp_number_max= openblas_omp_num_threads_env();
  123. if (blas_omp_number_max <= 0)
  124. blas_omp_number_max=MAX_CPU_NUMBER;
  125. #else
  126. blas_omp_number_max = omp_get_max_threads();
  127. #endif
  128. blas_get_cpu_number();
  129. adjust_thread_buffers();
  130. blas_server_avail = 1;
  131. return 0;
  132. }
  133. int BLASFUNC(blas_thread_shutdown)(void){
  134. int i=0, j=0;
  135. blas_server_avail = 0;
  136. for(i=0; i<MAX_PARALLEL_NUMBER; i++) {
  137. for(j=0; j<MAX_CPU_NUMBER; j++){
  138. if(blas_thread_buffer[i][j]!=NULL){
  139. blas_memory_free(blas_thread_buffer[i][j]);
  140. blas_thread_buffer[i][j]=NULL;
  141. }
  142. }
  143. }
  144. return 0;
  145. }
  146. static void legacy_exec(void *func, int mode, blas_arg_t *args, void *sb){
  147. if (!(mode & BLAS_COMPLEX)){
  148. #ifdef EXPRECISION
  149. if ((mode & BLAS_PREC) == BLAS_XDOUBLE){
  150. /* REAL / Extended Double */
  151. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, xdouble,
  152. xdouble *, BLASLONG, xdouble *, BLASLONG,
  153. xdouble *, BLASLONG, void *) = func;
  154. afunc(args -> m, args -> n, args -> k,
  155. ((xdouble *)args -> alpha)[0],
  156. args -> a, args -> lda,
  157. args -> b, args -> ldb,
  158. args -> c, args -> ldc, sb);
  159. } else
  160. #endif
  161. if ((mode & BLAS_PREC) == BLAS_DOUBLE){
  162. /* REAL / Double */
  163. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, double,
  164. double *, BLASLONG, double *, BLASLONG,
  165. double *, BLASLONG, void *) = func;
  166. afunc(args -> m, args -> n, args -> k,
  167. ((double *)args -> alpha)[0],
  168. args -> a, args -> lda,
  169. args -> b, args -> ldb,
  170. args -> c, args -> ldc, sb);
  171. } else if ((mode & BLAS_PREC) == BLAS_SINGLE){
  172. /* REAL / Single */
  173. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, float,
  174. float *, BLASLONG, float *, BLASLONG,
  175. float *, BLASLONG, void *) = func;
  176. afunc(args -> m, args -> n, args -> k,
  177. ((float *)args -> alpha)[0],
  178. args -> a, args -> lda,
  179. args -> b, args -> ldb,
  180. args -> c, args -> ldc, sb);
  181. #ifdef BUILD_BFLOAT16
  182. } else if ((mode & BLAS_PREC) == BLAS_BFLOAT16){
  183. /* REAL / BFLOAT16 */
  184. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, bfloat16,
  185. bfloat16 *, BLASLONG, bfloat16 *, BLASLONG,
  186. bfloat16 *, BLASLONG, void *) = func;
  187. afunc(args -> m, args -> n, args -> k,
  188. ((bfloat16 *)args -> alpha)[0],
  189. args -> a, args -> lda,
  190. args -> b, args -> ldb,
  191. args -> c, args -> ldc, sb);
  192. } else if ((mode & BLAS_PREC) == BLAS_STOBF16){
  193. /* REAL / BLAS_STOBF16 */
  194. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, float,
  195. float *, BLASLONG, bfloat16 *, BLASLONG,
  196. float *, BLASLONG, void *) = func;
  197. afunc(args -> m, args -> n, args -> k,
  198. ((float *)args -> alpha)[0],
  199. args -> a, args -> lda,
  200. args -> b, args -> ldb,
  201. args -> c, args -> ldc, sb);
  202. } else if ((mode & BLAS_PREC) == BLAS_DTOBF16){
  203. /* REAL / BLAS_DTOBF16 */
  204. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, double,
  205. double *, BLASLONG, bfloat16 *, BLASLONG,
  206. double *, BLASLONG, void *) = func;
  207. afunc(args -> m, args -> n, args -> k,
  208. ((double *)args -> alpha)[0],
  209. args -> a, args -> lda,
  210. args -> b, args -> ldb,
  211. args -> c, args -> ldc, sb);
  212. #endif
  213. } else {
  214. /* REAL / Other types in future */
  215. }
  216. } else {
  217. #ifdef EXPRECISION
  218. if ((mode & BLAS_PREC) == BLAS_XDOUBLE){
  219. /* COMPLEX / Extended Double */
  220. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, xdouble, xdouble,
  221. xdouble *, BLASLONG, xdouble *, BLASLONG,
  222. xdouble *, BLASLONG, void *) = func;
  223. afunc(args -> m, args -> n, args -> k,
  224. ((xdouble *)args -> alpha)[0],
  225. ((xdouble *)args -> alpha)[1],
  226. args -> a, args -> lda,
  227. args -> b, args -> ldb,
  228. args -> c, args -> ldc, sb);
  229. } else
  230. #endif
  231. if ((mode & BLAS_PREC) == BLAS_DOUBLE){
  232. /* COMPLEX / Double */
  233. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, double, double,
  234. double *, BLASLONG, double *, BLASLONG,
  235. double *, BLASLONG, void *) = func;
  236. afunc(args -> m, args -> n, args -> k,
  237. ((double *)args -> alpha)[0],
  238. ((double *)args -> alpha)[1],
  239. args -> a, args -> lda,
  240. args -> b, args -> ldb,
  241. args -> c, args -> ldc, sb);
  242. } else if ((mode & BLAS_PREC) == BLAS_SINGLE){
  243. /* COMPLEX / Single */
  244. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, float, float,
  245. float *, BLASLONG, float *, BLASLONG,
  246. float *, BLASLONG, void *) = func;
  247. afunc(args -> m, args -> n, args -> k,
  248. ((float *)args -> alpha)[0],
  249. ((float *)args -> alpha)[1],
  250. args -> a, args -> lda,
  251. args -> b, args -> ldb,
  252. args -> c, args -> ldc, sb);
  253. } else {
  254. /* COMPLEX / Other types in future */
  255. }
  256. }
  257. }
  258. static void exec_threads(int thread_num, blas_queue_t *queue, int buf_index){
  259. void *buffer, *sa, *sb;
  260. int pos=0, release_flag=0;
  261. buffer = NULL;
  262. sa = queue -> sa;
  263. sb = queue -> sb;
  264. #ifdef CONSISTENT_FPCSR
  265. #ifdef __aarch64__
  266. __asm__ __volatile__ ("msr fpcr, %0" : : "r" (queue -> sse_mode));
  267. #else
  268. __asm__ __volatile__ ("ldmxcsr %0" : : "m" (queue -> sse_mode));
  269. __asm__ __volatile__ ("fldcw %0" : : "m" (queue -> x87_mode));
  270. #endif
  271. #endif
  272. if ((sa == NULL) && (sb == NULL) && ((queue -> mode & BLAS_PTHREAD) == 0)) {
  273. pos= thread_num;
  274. buffer = blas_thread_buffer[buf_index][pos];
  275. //fallback
  276. if(buffer==NULL) {
  277. buffer = blas_memory_alloc(2);
  278. release_flag=1;
  279. }
  280. if (sa == NULL) {
  281. sa = (void *)((BLASLONG)buffer + GEMM_OFFSET_A);
  282. queue->sa=sa;
  283. }
  284. if (sb == NULL) {
  285. if (!(queue -> mode & BLAS_COMPLEX)){
  286. #ifdef EXPRECISION
  287. if ((queue -> mode & BLAS_PREC) == BLAS_XDOUBLE){
  288. sb = (void *)(((BLASLONG)sa + ((QGEMM_P * QGEMM_Q * sizeof(xdouble)
  289. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  290. } else
  291. #endif
  292. if ((queue -> mode & BLAS_PREC) == BLAS_DOUBLE){
  293. #if defined ( BUILD_DOUBLE) || defined (BUILD_COMPLEX16)
  294. sb = (void *)(((BLASLONG)sa + ((DGEMM_P * DGEMM_Q * sizeof(double)
  295. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  296. #endif
  297. } else if ((queue -> mode & BLAS_PREC) == BLAS_SINGLE){
  298. #if defined (BUILD_SINGLE) || defined (BUILD_COMPLEX)
  299. sb = (void *)(((BLASLONG)sa + ((SGEMM_P * SGEMM_Q * sizeof(float)
  300. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  301. #endif
  302. } else {
  303. /* Other types in future */
  304. }
  305. } else {
  306. #ifdef EXPRECISION
  307. if ((queue -> mode & BLAS_PREC) == BLAS_XDOUBLE){
  308. sb = (void *)(((BLASLONG)sa + ((XGEMM_P * XGEMM_Q * 2 * sizeof(xdouble)
  309. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  310. } else
  311. #endif
  312. if ((queue -> mode & BLAS_PREC) == BLAS_DOUBLE){
  313. #ifdef BUILD_COMPLEX16
  314. sb = (void *)(((BLASLONG)sa + ((ZGEMM_P * ZGEMM_Q * 2 * sizeof(double)
  315. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  316. #else
  317. fprintf(stderr,"UNHANDLED COMPLEX16\n");
  318. #endif
  319. } else if ((queue -> mode & BLAS_PREC) == BLAS_SINGLE) {
  320. #ifdef BUILD_COMPLEX
  321. sb = (void *)(((BLASLONG)sa + ((CGEMM_P * CGEMM_Q * 2 * sizeof(float)
  322. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  323. #else
  324. fprintf(stderr,"UNHANDLED COMPLEX\n");
  325. #endif
  326. } else {
  327. /* Other types in future */
  328. }
  329. }
  330. queue->sb=sb;
  331. }
  332. }
  333. if (queue -> mode & BLAS_LEGACY) {
  334. legacy_exec(queue -> routine, queue -> mode, queue -> args, sb);
  335. } else
  336. if (queue -> mode & BLAS_PTHREAD) {
  337. void (*pthreadcompat)(void *) = queue -> routine;
  338. (pthreadcompat)(queue -> args);
  339. } else {
  340. int (*routine)(blas_arg_t *, void *, void *, void *, void *, BLASLONG) = queue -> routine;
  341. (routine)(queue -> args, queue -> range_m, queue -> range_n, sa, sb, queue -> position);
  342. }
  343. if (release_flag) blas_memory_free(buffer);
  344. }
  345. int exec_blas(BLASLONG num, blas_queue_t *queue){
  346. // Handle lazy re-init of the thread-pool after a POSIX fork
  347. if (unlikely(blas_server_avail == 0)) blas_thread_init();
  348. BLASLONG i, buf_index;
  349. if ((num <= 0) || (queue == NULL)) return 0;
  350. #ifdef CONSISTENT_FPCSR
  351. for (i = 0; i < num; i ++) {
  352. #ifdef __aarch64__
  353. __asm__ __volatile__ ("mrs %0, fpcr" : "=r" (queue[i].sse_mode));
  354. #else
  355. __asm__ __volatile__ ("fnstcw %0" : "=m" (queue[i].x87_mode));
  356. __asm__ __volatile__ ("stmxcsr %0" : "=m" (queue[i].sse_mode));
  357. #endif
  358. }
  359. #endif
  360. while (true) {
  361. for(i=0; i < MAX_PARALLEL_NUMBER; i++) {
  362. #ifdef HAVE_C11
  363. _Bool inuse = false;
  364. if(atomic_compare_exchange_weak(&blas_buffer_inuse[i], &inuse, true)) {
  365. #else
  366. if(blas_buffer_inuse[i] == false) {
  367. blas_buffer_inuse[i] = true;
  368. #endif
  369. buf_index = i;
  370. break;
  371. }
  372. }
  373. if(i != MAX_PARALLEL_NUMBER)
  374. break;
  375. }
  376. /*For caller-managed threading, if caller has registered the callback, pass exec_thread as callback function*/
  377. if (openblas_threads_callback_) {
  378. #ifndef USE_SIMPLE_THREADED_LEVEL3
  379. for (i = 0; i < num; i ++)
  380. queue[i].position = i;
  381. #endif
  382. openblas_threads_callback_(1, (openblas_dojob_callback) exec_threads, num, sizeof(blas_queue_t), (void*) queue, buf_index);
  383. } else {
  384. if (openblas_omp_adaptive_env() != 0) {
  385. #pragma omp parallel for num_threads(num) schedule(OMP_SCHED)
  386. for (i = 0; i < num; i ++) {
  387. #ifndef USE_SIMPLE_THREADED_LEVEL3
  388. queue[i].position = i;
  389. #endif
  390. exec_threads(omp_get_thread_num(), &queue[i], buf_index);
  391. }
  392. } else {
  393. #pragma omp parallel for schedule(OMP_SCHED)
  394. for (i = 0; i < num; i ++) {
  395. #ifndef USE_SIMPLE_THREADED_LEVEL3
  396. queue[i].position = i;
  397. #endif
  398. exec_threads(omp_get_thread_num(), &queue[i], buf_index);
  399. }
  400. }
  401. }
  402. #ifdef HAVE_C11
  403. atomic_store(&blas_buffer_inuse[buf_index], false);
  404. #else
  405. blas_buffer_inuse[buf_index] = false;
  406. #endif
  407. return 0;
  408. }
  409. #endif