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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. void goto_set_num_threads(int num_threads) {
  47. if (num_threads < 1) num_threads = blas_num_threads;
  48. if (num_threads > MAX_CPU_NUMBER) num_threads = MAX_CPU_NUMBER;
  49. if (num_threads > blas_num_threads) {
  50. blas_num_threads = num_threads;
  51. }
  52. blas_cpu_number = num_threads;
  53. omp_set_num_threads(blas_cpu_number);
  54. }
  55. void openblas_set_num_threads(int num_threads) {
  56. goto_set_num_threads(num_threads);
  57. }
  58. int blas_thread_init(void){
  59. blas_get_cpu_number();
  60. blas_server_avail = 1;
  61. return 0;
  62. }
  63. int BLASFUNC(blas_thread_shutdown)(void){
  64. blas_server_avail = 0;
  65. return 0;
  66. }
  67. static void legacy_exec(void *func, int mode, blas_arg_t *args, void *sb){
  68. if (!(mode & BLAS_COMPLEX)){
  69. #ifdef EXPRECISION
  70. if (mode & BLAS_XDOUBLE){
  71. /* REAL / Extended Double */
  72. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, xdouble,
  73. xdouble *, BLASLONG, xdouble *, BLASLONG,
  74. xdouble *, BLASLONG, void *) = func;
  75. afunc(args -> m, args -> n, args -> k,
  76. ((xdouble *)args -> alpha)[0],
  77. args -> a, args -> lda,
  78. args -> b, args -> ldb,
  79. args -> c, args -> ldc, sb);
  80. } else
  81. #endif
  82. if (mode & BLAS_DOUBLE){
  83. /* REAL / Double */
  84. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, double,
  85. double *, BLASLONG, double *, BLASLONG,
  86. double *, BLASLONG, void *) = func;
  87. afunc(args -> m, args -> n, args -> k,
  88. ((double *)args -> alpha)[0],
  89. args -> a, args -> lda,
  90. args -> b, args -> ldb,
  91. args -> c, args -> ldc, sb);
  92. } else {
  93. /* REAL / Single */
  94. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, float,
  95. float *, BLASLONG, float *, BLASLONG,
  96. float *, BLASLONG, void *) = func;
  97. afunc(args -> m, args -> n, args -> k,
  98. ((float *)args -> alpha)[0],
  99. args -> a, args -> lda,
  100. args -> b, args -> ldb,
  101. args -> c, args -> ldc, sb);
  102. }
  103. } else {
  104. #ifdef EXPRECISION
  105. if (mode & BLAS_XDOUBLE){
  106. /* COMPLEX / Extended Double */
  107. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, xdouble, xdouble,
  108. xdouble *, BLASLONG, xdouble *, BLASLONG,
  109. xdouble *, BLASLONG, void *) = func;
  110. afunc(args -> m, args -> n, args -> k,
  111. ((xdouble *)args -> alpha)[0],
  112. ((xdouble *)args -> alpha)[1],
  113. args -> a, args -> lda,
  114. args -> b, args -> ldb,
  115. args -> c, args -> ldc, sb);
  116. } else
  117. #endif
  118. if (mode & BLAS_DOUBLE){
  119. /* COMPLEX / Double */
  120. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, double, double,
  121. double *, BLASLONG, double *, BLASLONG,
  122. double *, BLASLONG, void *) = func;
  123. afunc(args -> m, args -> n, args -> k,
  124. ((double *)args -> alpha)[0],
  125. ((double *)args -> alpha)[1],
  126. args -> a, args -> lda,
  127. args -> b, args -> ldb,
  128. args -> c, args -> ldc, sb);
  129. } else {
  130. /* COMPLEX / Single */
  131. void (*afunc)(BLASLONG, BLASLONG, BLASLONG, float, float,
  132. float *, BLASLONG, float *, BLASLONG,
  133. float *, BLASLONG, void *) = func;
  134. afunc(args -> m, args -> n, args -> k,
  135. ((float *)args -> alpha)[0],
  136. ((float *)args -> alpha)[1],
  137. args -> a, args -> lda,
  138. args -> b, args -> ldb,
  139. args -> c, args -> ldc, sb);
  140. }
  141. }
  142. }
  143. static void exec_threads(blas_queue_t *queue){
  144. void *buffer, *sa, *sb;
  145. buffer = NULL;
  146. sa = queue -> sa;
  147. sb = queue -> sb;
  148. #ifdef CONSISTENT_FPCSR
  149. __asm__ __volatile__ ("ldmxcsr %0" : : "m" (queue -> sse_mode));
  150. __asm__ __volatile__ ("fldcw %0" : : "m" (queue -> x87_mode));
  151. #endif
  152. if ((sa == NULL) && (sb == NULL) && ((queue -> mode & BLAS_PTHREAD) == 0)) {
  153. buffer = blas_memory_alloc(2);
  154. if (sa == NULL) sa = (void *)((BLASLONG)buffer + GEMM_OFFSET_A);
  155. if (sb == NULL) {
  156. if (!(queue -> mode & BLAS_COMPLEX)){
  157. #ifdef EXPRECISION
  158. if (queue -> mode & BLAS_XDOUBLE){
  159. sb = (void *)(((BLASLONG)sa + ((QGEMM_P * QGEMM_Q * sizeof(xdouble)
  160. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  161. } else
  162. #endif
  163. if (queue -> mode & BLAS_DOUBLE){
  164. sb = (void *)(((BLASLONG)sa + ((DGEMM_P * DGEMM_Q * sizeof(double)
  165. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  166. } else {
  167. sb = (void *)(((BLASLONG)sa + ((SGEMM_P * SGEMM_Q * sizeof(float)
  168. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  169. }
  170. } else {
  171. #ifdef EXPRECISION
  172. if (queue -> mode & BLAS_XDOUBLE){
  173. sb = (void *)(((BLASLONG)sa + ((XGEMM_P * XGEMM_Q * 2 * sizeof(xdouble)
  174. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  175. } else
  176. #endif
  177. if (queue -> mode & BLAS_DOUBLE){
  178. sb = (void *)(((BLASLONG)sa + ((ZGEMM_P * ZGEMM_Q * 2 * sizeof(double)
  179. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  180. } else {
  181. sb = (void *)(((BLASLONG)sa + ((CGEMM_P * CGEMM_Q * 2 * sizeof(float)
  182. + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
  183. }
  184. }
  185. }
  186. }
  187. if (queue -> mode & BLAS_LEGACY) {
  188. legacy_exec(queue -> routine, queue -> mode, queue -> args, sb);
  189. } else
  190. if (queue -> mode & BLAS_PTHREAD) {
  191. void (*pthreadcompat)(void *) = queue -> routine;
  192. (pthreadcompat)(queue -> args);
  193. } else {
  194. int (*routine)(blas_arg_t *, void *, void *, void *, void *, BLASLONG) = queue -> routine;
  195. (routine)(queue -> args, queue -> range_m, queue -> range_n, sa, sb, queue -> position);
  196. }
  197. if (buffer != NULL) blas_memory_free(buffer);
  198. }
  199. int exec_blas(BLASLONG num, blas_queue_t *queue){
  200. BLASLONG i;
  201. if ((num <= 0) || (queue == NULL)) return 0;
  202. #ifdef CONSISTENT_FPCSR
  203. for (i = 0; i < num; i ++) {
  204. __asm__ __volatile__ ("fnstcw %0" : "=m" (queue[i].x87_mode));
  205. __asm__ __volatile__ ("stmxcsr %0" : "=m" (queue[i].sse_mode));
  206. }
  207. #endif
  208. #pragma omp parallel for schedule(static)
  209. for (i = 0; i < num; i ++) {
  210. #ifndef USE_SIMPLE_THREADED_LEVEL3
  211. queue[i].position = i;
  212. #endif
  213. exec_threads(&queue[i]);
  214. }
  215. return 0;
  216. }
  217. #endif

OpenBLAS is an optimized BLAS library based on GotoBLAS2 1.13 BSD version.