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

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