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

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