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

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