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

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