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

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