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 14 kB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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)
  99. //set parameters for different number of threads.
  100. blas_set_parameter();
  101. #endif
  102. }
  103. void openblas_set_num_threads(int num_threads) {
  104. goto_set_num_threads(num_threads);
  105. }
  106. int blas_thread_init(void){
  107. #if defined(__FreeBSD__) && defined(__clang__)
  108. extern int openblas_omp_num_threads_env(void);
  109. if(blas_omp_number_max <= 0)
  110. blas_omp_number_max= openblas_omp_num_threads_env();
  111. if (blas_omp_number_max <= 0)
  112. blas_omp_number_max=MAX_CPU_NUMBER;
  113. #else
  114. blas_omp_number_max = omp_get_max_threads();
  115. #endif
  116. blas_get_cpu_number();
  117. adjust_thread_buffers();
  118. blas_server_avail = 1;
  119. return 0;
  120. }
  121. int BLASFUNC(blas_thread_shutdown)(void){
  122. int i=0, j=0;
  123. blas_server_avail = 0;
  124. for(i=0; i<MAX_PARALLEL_NUMBER; i++) {
  125. for(j=0; j<MAX_CPU_NUMBER; j++){
  126. if(blas_thread_buffer[i][j]!=NULL){
  127. blas_memory_free(blas_thread_buffer[i][j]);
  128. blas_thread_buffer[i][j]=NULL;
  129. }
  130. }
  131. }
  132. return 0;
  133. }
  134. static void legacy_exec(void *func, int mode, blas_arg_t *args, void *sb){
  135. if (!(mode & BLAS_COMPLEX)){
  136. #ifdef EXPRECISION
  137. if ((mode & BLAS_PREC) == BLAS_XDOUBLE){
  138. /* REAL / Extended Double */
  139. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, xdouble,
  140. xdouble *, BLASLONG, xdouble *, BLASLONG,
  141. xdouble *, BLASLONG, void *) = func;
  142. afunc(args -> m, args -> n, args -> k,
  143. ((xdouble *)args -> alpha)[0],
  144. args -> a, args -> lda,
  145. args -> b, args -> ldb,
  146. args -> c, args -> ldc, sb);
  147. } else
  148. #endif
  149. if ((mode & BLAS_PREC) == BLAS_DOUBLE){
  150. /* REAL / Double */
  151. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, double,
  152. double *, BLASLONG, double *, BLASLONG,
  153. double *, BLASLONG, void *) = func;
  154. afunc(args -> m, args -> n, args -> k,
  155. ((double *)args -> alpha)[0],
  156. args -> a, args -> lda,
  157. args -> b, args -> ldb,
  158. args -> c, args -> ldc, sb);
  159. } else if ((mode & BLAS_PREC) == BLAS_SINGLE){
  160. /* REAL / Single */
  161. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, float,
  162. float *, BLASLONG, float *, BLASLONG,
  163. float *, BLASLONG, void *) = func;
  164. afunc(args -> m, args -> n, args -> k,
  165. ((float *)args -> alpha)[0],
  166. args -> a, args -> lda,
  167. args -> b, args -> ldb,
  168. args -> c, args -> ldc, sb);
  169. #ifdef BUILD_BFLOAT16
  170. } else if ((mode & BLAS_PREC) == BLAS_BFLOAT16){
  171. /* REAL / BFLOAT16 */
  172. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, bfloat16,
  173. bfloat16 *, BLASLONG, bfloat16 *, BLASLONG,
  174. bfloat16 *, BLASLONG, void *) = func;
  175. afunc(args -> m, args -> n, args -> k,
  176. ((bfloat16 *)args -> alpha)[0],
  177. args -> a, args -> lda,
  178. args -> b, args -> ldb,
  179. args -> c, args -> ldc, sb);
  180. } else if ((mode & BLAS_PREC) == BLAS_STOBF16){
  181. /* REAL / BLAS_STOBF16 */
  182. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, float,
  183. float *, BLASLONG, bfloat16 *, BLASLONG,
  184. float *, BLASLONG, void *) = func;
  185. afunc(args -> m, args -> n, args -> k,
  186. ((float *)args -> alpha)[0],
  187. args -> a, args -> lda,
  188. args -> b, args -> ldb,
  189. args -> c, args -> ldc, sb);
  190. } else if ((mode & BLAS_PREC) == BLAS_DTOBF16){
  191. /* REAL / BLAS_DTOBF16 */
  192. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, double,
  193. double *, BLASLONG, bfloat16 *, BLASLONG,
  194. double *, BLASLONG, void *) = func;
  195. afunc(args -> m, args -> n, args -> k,
  196. ((double *)args -> alpha)[0],
  197. args -> a, args -> lda,
  198. args -> b, args -> ldb,
  199. args -> c, args -> ldc, sb);
  200. #endif
  201. } else {
  202. /* REAL / Other types in future */
  203. }
  204. } else {
  205. #ifdef EXPRECISION
  206. if ((mode & BLAS_PREC) == BLAS_XDOUBLE){
  207. /* COMPLEX / Extended Double */
  208. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, xdouble, xdouble,
  209. xdouble *, BLASLONG, xdouble *, BLASLONG,
  210. xdouble *, BLASLONG, void *) = func;
  211. afunc(args -> m, args -> n, args -> k,
  212. ((xdouble *)args -> alpha)[0],
  213. ((xdouble *)args -> alpha)[1],
  214. args -> a, args -> lda,
  215. args -> b, args -> ldb,
  216. args -> c, args -> ldc, sb);
  217. } else
  218. #endif
  219. if ((mode & BLAS_PREC) == BLAS_DOUBLE){
  220. /* COMPLEX / Double */
  221. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, double, double,
  222. double *, BLASLONG, double *, BLASLONG,
  223. double *, BLASLONG, void *) = func;
  224. afunc(args -> m, args -> n, args -> k,
  225. ((double *)args -> alpha)[0],
  226. ((double *)args -> alpha)[1],
  227. args -> a, args -> lda,
  228. args -> b, args -> ldb,
  229. args -> c, args -> ldc, sb);
  230. } else if ((mode & BLAS_PREC) == BLAS_SINGLE){
  231. /* COMPLEX / Single */
  232. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, float, float,
  233. float *, BLASLONG, float *, BLASLONG,
  234. float *, BLASLONG, void *) = func;
  235. afunc(args -> m, args -> n, args -> k,
  236. ((float *)args -> alpha)[0],
  237. ((float *)args -> alpha)[1],
  238. args -> a, args -> lda,
  239. args -> b, args -> ldb,
  240. args -> c, args -> ldc, sb);
  241. } else {
  242. /* COMPLEX / Other types in future */
  243. }
  244. }
  245. }
  246. static void exec_threads(int thread_num, blas_queue_t *queue, int buf_index){
  247. void *buffer, *sa, *sb;
  248. int pos=0, release_flag=0;
  249. buffer = NULL;
  250. sa = queue -> sa;
  251. sb = queue -> sb;
  252. #ifdef CONSISTENT_FPCSR
  253. #ifdef __aarch64__
  254. __asm__ __volatile__ ("msr fpcr, %0" : : "r" (queue -> sse_mode));
  255. #else
  256. __asm__ __volatile__ ("ldmxcsr %0" : : "m" (queue -> sse_mode));
  257. __asm__ __volatile__ ("fldcw %0" : : "m" (queue -> x87_mode));
  258. #endif
  259. #endif
  260. if ((sa == NULL) && (sb == NULL) && ((queue -> mode & BLAS_PTHREAD) == 0)) {
  261. pos= thread_num;
  262. buffer = blas_thread_buffer[buf_index][pos];
  263. //fallback
  264. if(buffer==NULL) {
  265. buffer = blas_memory_alloc(2);
  266. release_flag=1;
  267. }
  268. if (sa == NULL) {
  269. sa = (void *)((BLASLONG)buffer + GEMM_OFFSET_A);
  270. queue->sa=sa;
  271. }
  272. if (sb == NULL) {
  273. if (!(queue -> mode & BLAS_COMPLEX)){
  274. #ifdef EXPRECISION
  275. if ((queue -> mode & BLAS_PREC) == BLAS_XDOUBLE){
  276. sb = (void *)(((BLASLONG)sa + ((QGEMM_P * QGEMM_Q * sizeof(xdouble)
  277. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  278. } else
  279. #endif
  280. if ((queue -> mode & BLAS_PREC) == BLAS_DOUBLE){
  281. #if defined ( BUILD_DOUBLE) || defined (BUILD_COMPLEX16)
  282. sb = (void *)(((BLASLONG)sa + ((DGEMM_P * DGEMM_Q * sizeof(double)
  283. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  284. #endif
  285. } else if ((queue -> mode & BLAS_PREC) == BLAS_SINGLE){
  286. #if defined (BUILD_SINGLE) || defined (BUILD_COMPLEX)
  287. sb = (void *)(((BLASLONG)sa + ((SGEMM_P * SGEMM_Q * sizeof(float)
  288. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  289. #endif
  290. } else {
  291. /* Other types in future */
  292. }
  293. } else {
  294. #ifdef EXPRECISION
  295. if ((queue -> mode & BLAS_PREC) == BLAS_XDOUBLE){
  296. sb = (void *)(((BLASLONG)sa + ((XGEMM_P * XGEMM_Q * 2 * sizeof(xdouble)
  297. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  298. } else
  299. #endif
  300. if ((queue -> mode & BLAS_PREC) == BLAS_DOUBLE){
  301. #ifdef BUILD_COMPLEX16
  302. sb = (void *)(((BLASLONG)sa + ((ZGEMM_P * ZGEMM_Q * 2 * sizeof(double)
  303. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  304. #else
  305. fprintf(stderr,"UNHANDLED COMPLEX16\n");
  306. #endif
  307. } else if ((queue -> mode & BLAS_PREC) == BLAS_SINGLE) {
  308. #ifdef BUILD_COMPLEX
  309. sb = (void *)(((BLASLONG)sa + ((CGEMM_P * CGEMM_Q * 2 * sizeof(float)
  310. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  311. #else
  312. fprintf(stderr,"UNHANDLED COMPLEX\n");
  313. #endif
  314. } else {
  315. /* Other types in future */
  316. }
  317. }
  318. queue->sb=sb;
  319. }
  320. }
  321. if (queue -> mode & BLAS_LEGACY) {
  322. legacy_exec(queue -> routine, queue -> mode, queue -> args, sb);
  323. } else
  324. if (queue -> mode & BLAS_PTHREAD) {
  325. void (*pthreadcompat)(void *) = queue -> routine;
  326. (pthreadcompat)(queue -> args);
  327. } else {
  328. int (*routine)(blas_arg_t *, void *, void *, void *, void *, BLASLONG) = queue -> routine;
  329. (routine)(queue -> args, queue -> range_m, queue -> range_n, sa, sb, queue -> position);
  330. }
  331. if (release_flag) blas_memory_free(buffer);
  332. }
  333. int exec_blas(BLASLONG num, blas_queue_t *queue){
  334. // Handle lazy re-init of the thread-pool after a POSIX fork
  335. if (unlikely(blas_server_avail == 0)) blas_thread_init();
  336. BLASLONG i, buf_index;
  337. if ((num <= 0) || (queue == NULL)) return 0;
  338. #ifdef CONSISTENT_FPCSR
  339. for (i = 0; i < num; i ++) {
  340. #ifdef __aarch64__
  341. __asm__ __volatile__ ("mrs %0, fpcr" : "=r" (queue[i].sse_mode));
  342. #else
  343. __asm__ __volatile__ ("fnstcw %0" : "=m" (queue[i].x87_mode));
  344. __asm__ __volatile__ ("stmxcsr %0" : "=m" (queue[i].sse_mode));
  345. #endif
  346. }
  347. #endif
  348. while (true) {
  349. for(i=0; i < MAX_PARALLEL_NUMBER; i++) {
  350. #ifdef HAVE_C11
  351. _Bool inuse = false;
  352. if(atomic_compare_exchange_weak(&blas_buffer_inuse[i], &inuse, true)) {
  353. #else
  354. if(blas_buffer_inuse[i] == false) {
  355. blas_buffer_inuse[i] = true;
  356. #endif
  357. buf_index = i;
  358. break;
  359. }
  360. }
  361. if(i != MAX_PARALLEL_NUMBER)
  362. break;
  363. }
  364. /*For caller-managed threading, if caller has registered the callback, pass exec_thread as callback function*/
  365. if (openblas_threads_callback_) {
  366. #ifndef USE_SIMPLE_THREADED_LEVEL3
  367. for (i = 0; i < num; i ++)
  368. queue[i].position = i;
  369. #endif
  370. openblas_threads_callback_(1, (openblas_dojob_callback) exec_threads, num, sizeof(blas_queue_t), (void*) queue, buf_index);
  371. } else {
  372. if (openblas_omp_adaptive_env() != 0) {
  373. #pragma omp parallel for num_threads(num) schedule(OMP_SCHED)
  374. for (i = 0; i < num; i ++) {
  375. #ifndef USE_SIMPLE_THREADED_LEVEL3
  376. queue[i].position = i;
  377. #endif
  378. exec_threads(omp_get_thread_num(), &queue[i], buf_index);
  379. }
  380. } else {
  381. #pragma omp parallel for schedule(OMP_SCHED)
  382. for (i = 0; i < num; i ++) {
  383. #ifndef USE_SIMPLE_THREADED_LEVEL3
  384. queue[i].position = i;
  385. #endif
  386. exec_threads(omp_get_thread_num(), &queue[i], buf_index);
  387. }
  388. }
  389. }
  390. #ifdef HAVE_C11
  391. atomic_store(&blas_buffer_inuse[buf_index], false);
  392. #else
  393. blas_buffer_inuse[buf_index] = false;
  394. #endif
  395. return 0;
  396. }
  397. #endif