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

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