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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. #if __STDC_VERSION__ >= 201112L
  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_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_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 {
  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. }
  150. } else {
  151. #ifdef EXPRECISION
  152. if (mode & BLAS_XDOUBLE){
  153. /* COMPLEX / Extended Double */
  154. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, xdouble, xdouble,
  155. xdouble *, BLASLONG, xdouble *, BLASLONG,
  156. xdouble *, BLASLONG, void *) = func;
  157. afunc(args -> m, args -> n, args -> k,
  158. ((xdouble *)args -> alpha)[0],
  159. ((xdouble *)args -> alpha)[1],
  160. args -> a, args -> lda,
  161. args -> b, args -> ldb,
  162. args -> c, args -> ldc, sb);
  163. } else
  164. #endif
  165. if (mode & BLAS_DOUBLE){
  166. /* COMPLEX / Double */
  167. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, double, double,
  168. double *, BLASLONG, double *, BLASLONG,
  169. double *, BLASLONG, void *) = func;
  170. afunc(args -> m, args -> n, args -> k,
  171. ((double *)args -> alpha)[0],
  172. ((double *)args -> alpha)[1],
  173. args -> a, args -> lda,
  174. args -> b, args -> ldb,
  175. args -> c, args -> ldc, sb);
  176. } else {
  177. /* COMPLEX / Single */
  178. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, float, float,
  179. float *, BLASLONG, float *, BLASLONG,
  180. float *, BLASLONG, void *) = func;
  181. afunc(args -> m, args -> n, args -> k,
  182. ((float *)args -> alpha)[0],
  183. ((float *)args -> alpha)[1],
  184. args -> a, args -> lda,
  185. args -> b, args -> ldb,
  186. args -> c, args -> ldc, sb);
  187. }
  188. }
  189. }
  190. static void exec_threads(blas_queue_t *queue, int buf_index){
  191. void *buffer, *sa, *sb;
  192. int pos=0, release_flag=0;
  193. buffer = NULL;
  194. sa = queue -> sa;
  195. sb = queue -> sb;
  196. #ifdef CONSISTENT_FPCSR
  197. __asm__ __volatile__ ("ldmxcsr %0" : : "m" (queue -> sse_mode));
  198. __asm__ __volatile__ ("fldcw %0" : : "m" (queue -> x87_mode));
  199. #endif
  200. if ((sa == NULL) && (sb == NULL) && ((queue -> mode & BLAS_PTHREAD) == 0)) {
  201. pos = omp_get_thread_num();
  202. buffer = blas_thread_buffer[buf_index][pos];
  203. //fallback
  204. if(buffer==NULL) {
  205. buffer = blas_memory_alloc(2);
  206. release_flag=1;
  207. }
  208. if (sa == NULL) {
  209. sa = (void *)((BLASLONG)buffer + GEMM_OFFSET_A);
  210. queue->sa=sa;
  211. }
  212. if (sb == NULL) {
  213. if (!(queue -> mode & BLAS_COMPLEX)){
  214. #ifdef EXPRECISION
  215. if (queue -> mode & BLAS_XDOUBLE){
  216. sb = (void *)(((BLASLONG)sa + ((QGEMM_P * QGEMM_Q * sizeof(xdouble)
  217. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  218. } else
  219. #endif
  220. if (queue -> mode & BLAS_DOUBLE){
  221. sb = (void *)(((BLASLONG)sa + ((DGEMM_P * DGEMM_Q * sizeof(double)
  222. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  223. } else {
  224. sb = (void *)(((BLASLONG)sa + ((SGEMM_P * SGEMM_Q * sizeof(float)
  225. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  226. }
  227. } else {
  228. #ifdef EXPRECISION
  229. if (queue -> mode & BLAS_XDOUBLE){
  230. sb = (void *)(((BLASLONG)sa + ((XGEMM_P * XGEMM_Q * 2 * sizeof(xdouble)
  231. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  232. } else
  233. #endif
  234. if (queue -> mode & BLAS_DOUBLE){
  235. sb = (void *)(((BLASLONG)sa + ((ZGEMM_P * ZGEMM_Q * 2 * sizeof(double)
  236. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  237. } else {
  238. sb = (void *)(((BLASLONG)sa + ((CGEMM_P * CGEMM_Q * 2 * sizeof(float)
  239. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  240. }
  241. }
  242. queue->sb=sb;
  243. }
  244. }
  245. if (queue -> mode & BLAS_LEGACY) {
  246. legacy_exec(queue -> routine, queue -> mode, queue -> args, sb);
  247. } else
  248. if (queue -> mode & BLAS_PTHREAD) {
  249. void (*pthreadcompat)(void *) = queue -> routine;
  250. (pthreadcompat)(queue -> args);
  251. } else {
  252. int (*routine)(blas_arg_t *, void *, void *, void *, void *, BLASLONG) = queue -> routine;
  253. (routine)(queue -> args, queue -> range_m, queue -> range_n, sa, sb, queue -> position);
  254. }
  255. if (release_flag) blas_memory_free(buffer);
  256. }
  257. int exec_blas(BLASLONG num, blas_queue_t *queue){
  258. BLASLONG i, buf_index;
  259. if ((num <= 0) || (queue == NULL)) return 0;
  260. #ifdef CONSISTENT_FPCSR
  261. for (i = 0; i < num; i ++) {
  262. __asm__ __volatile__ ("fnstcw %0" : "=m" (queue[i].x87_mode));
  263. __asm__ __volatile__ ("stmxcsr %0" : "=m" (queue[i].sse_mode));
  264. }
  265. #endif
  266. while(true) {
  267. for(i=0; i < MAX_PARALLEL_NUMBER; i++) {
  268. #if __STDC_VERSION__ >= 201112L
  269. _Bool inuse = false;
  270. if(atomic_compare_exchange_weak(&blas_buffer_inuse[i], &inuse, true)) {
  271. #else
  272. if(blas_buffer_inuse[i] == false) {
  273. blas_buffer_inuse[i] = true;
  274. #endif
  275. buf_index = i;
  276. break;
  277. }
  278. }
  279. if(i != MAX_PARALLEL_NUMBER)
  280. break;
  281. }
  282. #pragma omp parallel for schedule(OMP_SCHED)
  283. for (i = 0; i < num; i ++) {
  284. #ifndef USE_SIMPLE_THREADED_LEVEL3
  285. queue[i].position = i;
  286. #endif
  287. exec_threads(&queue[i], buf_index);
  288. }
  289. #if __STDC_VERSION__ >= 201112L
  290. atomic_store(&blas_buffer_inuse[buf_index], false);
  291. #else
  292. blas_buffer_inuse[buf_index] = false;
  293. #endif
  294. return 0;
  295. }
  296. #endif