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_l1_thread.c 7.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 "common.h"
  41. int blas_level1_thread(int mode, BLASLONG m, BLASLONG n, BLASLONG k, void *alpha,
  42. void *a, BLASLONG lda,
  43. void *b, BLASLONG ldb,
  44. void *c, BLASLONG ldc, int (*function)(void), int nthreads){
  45. blas_queue_t queue[MAX_CPU_NUMBER];
  46. blas_arg_t args [MAX_CPU_NUMBER];
  47. BLASLONG i, width, astride, bstride;
  48. int num_cpu, calc_type_a, calc_type_b;
  49. switch (mode & BLAS_PREC) {
  50. case BLAS_INT8 :
  51. case BLAS_BFLOAT16:
  52. case BLAS_SINGLE :
  53. case BLAS_DOUBLE :
  54. case BLAS_XDOUBLE :
  55. calc_type_a = calc_type_b = (mode & BLAS_PREC) + ((mode & BLAS_COMPLEX) != 0);
  56. break;
  57. case BLAS_STOBF16 :
  58. calc_type_a = 2 + ((mode & BLAS_COMPLEX) != 0);
  59. calc_type_b = 1 + ((mode & BLAS_COMPLEX) != 0);
  60. break;
  61. case BLAS_DTOBF16 :
  62. calc_type_a = 3 + ((mode & BLAS_COMPLEX) != 0);
  63. calc_type_b = 1 + ((mode & BLAS_COMPLEX) != 0);
  64. break;
  65. case BLAS_BF16TOS :
  66. calc_type_a = 1 + ((mode & BLAS_COMPLEX) != 0);
  67. calc_type_b = 2 + ((mode & BLAS_COMPLEX) != 0);
  68. break;
  69. case BLAS_BF16TOD :
  70. calc_type_a = 1 + ((mode & BLAS_COMPLEX) != 0);
  71. calc_type_b = 3 + ((mode & BLAS_COMPLEX) != 0);
  72. break;
  73. default:
  74. calc_type_a = calc_type_b = 0;
  75. break;
  76. }
  77. if(!(mode & BLAS_PTHREAD)) mode |= BLAS_LEGACY;
  78. for (i = 0; i < nthreads; i++) blas_queue_init(&queue[i]);
  79. num_cpu = 0;
  80. i = m;
  81. while (i > 0){
  82. /* Adjust Parameters */
  83. width = blas_quickdivide(i + nthreads - num_cpu - 1,
  84. nthreads - num_cpu);
  85. i -= width;
  86. if (i < 0) width = width + i;
  87. astride = width * lda;
  88. if (!(mode & BLAS_TRANSB_T)) {
  89. bstride = width * ldb;
  90. } else {
  91. bstride = width;
  92. }
  93. astride <<= calc_type_a;
  94. bstride <<= calc_type_b;
  95. args[num_cpu].m = width;
  96. args[num_cpu].n = n;
  97. args[num_cpu].k = k;
  98. args[num_cpu].a = (void *)a;
  99. args[num_cpu].b = (void *)b;
  100. args[num_cpu].c = (void *)c;
  101. args[num_cpu].lda = lda;
  102. args[num_cpu].ldb = ldb;
  103. args[num_cpu].ldc = ldc;
  104. args[num_cpu].alpha = alpha;
  105. queue[num_cpu].mode = mode;
  106. queue[num_cpu].routine = function;
  107. queue[num_cpu].args = &args[num_cpu];
  108. queue[num_cpu].next = &queue[num_cpu + 1];
  109. a = (void *)((BLASULONG)a + astride);
  110. b = (void *)((BLASULONG)b + bstride);
  111. num_cpu ++;
  112. }
  113. if (num_cpu) {
  114. queue[num_cpu - 1].next = NULL;
  115. exec_blas(num_cpu, queue);
  116. }
  117. return 0;
  118. }
  119. int blas_level1_thread_with_return_value(int mode, BLASLONG m, BLASLONG n, BLASLONG k, void *alpha,
  120. void *a, BLASLONG lda,
  121. void *b, BLASLONG ldb,
  122. void *c, BLASLONG ldc, int (*function)(void), int nthreads){
  123. blas_queue_t queue[MAX_CPU_NUMBER];
  124. blas_arg_t args [MAX_CPU_NUMBER];
  125. BLASLONG i, width, astride, bstride;
  126. int num_cpu, calc_type_a, calc_type_b;
  127. switch (mode & BLAS_PREC) {
  128. case BLAS_INT8 :
  129. case BLAS_BFLOAT16:
  130. case BLAS_SINGLE :
  131. case BLAS_DOUBLE :
  132. case BLAS_XDOUBLE :
  133. calc_type_a = calc_type_b = (mode & BLAS_PREC) + ((mode & BLAS_COMPLEX) != 0);
  134. break;
  135. case BLAS_STOBF16 :
  136. calc_type_a = 2 + ((mode & BLAS_COMPLEX) != 0);
  137. calc_type_b = 1 + ((mode & BLAS_COMPLEX) != 0);
  138. break;
  139. case BLAS_DTOBF16 :
  140. calc_type_a = 3 + ((mode & BLAS_COMPLEX) != 0);
  141. calc_type_b = 1 + ((mode & BLAS_COMPLEX) != 0);
  142. break;
  143. case BLAS_BF16TOS :
  144. calc_type_a = 1 + ((mode & BLAS_COMPLEX) != 0);
  145. calc_type_b = 2 + ((mode & BLAS_COMPLEX) != 0);
  146. break;
  147. case BLAS_BF16TOD :
  148. calc_type_a = 1 + ((mode & BLAS_COMPLEX) != 0);
  149. calc_type_b = 3 + ((mode & BLAS_COMPLEX) != 0);
  150. break;
  151. default:
  152. calc_type_a = calc_type_b = 0;
  153. break;
  154. }
  155. mode |= BLAS_LEGACY;
  156. for (i = 0; i < nthreads; i++) blas_queue_init(&queue[i]);
  157. num_cpu = 0;
  158. i = m;
  159. while (i > 0){
  160. /* Adjust Parameters */
  161. width = blas_quickdivide(i + nthreads - num_cpu - 1,
  162. nthreads - num_cpu);
  163. i -= width;
  164. if (i < 0) width = width + i;
  165. astride = width * lda;
  166. if (!(mode & BLAS_TRANSB_T)) {
  167. bstride = width * ldb;
  168. } else {
  169. bstride = width;
  170. }
  171. astride <<= calc_type_a;
  172. bstride <<= calc_type_b;
  173. args[num_cpu].m = width;
  174. args[num_cpu].n = n;
  175. args[num_cpu].k = k;
  176. args[num_cpu].a = (void *)a;
  177. args[num_cpu].b = (void *)b;
  178. args[num_cpu].c = (void *)((char *)c + num_cpu * sizeof(double)*2);
  179. args[num_cpu].lda = lda;
  180. args[num_cpu].ldb = ldb;
  181. args[num_cpu].ldc = ldc;
  182. args[num_cpu].alpha = alpha;
  183. queue[num_cpu].mode = mode;
  184. queue[num_cpu].routine = function;
  185. queue[num_cpu].args = &args[num_cpu];
  186. queue[num_cpu].next = &queue[num_cpu + 1];
  187. a = (void *)((BLASULONG)a + astride);
  188. b = (void *)((BLASULONG)b + bstride);
  189. num_cpu ++;
  190. }
  191. if (num_cpu) {
  192. queue[num_cpu - 1].next = NULL;
  193. exec_blas(num_cpu, queue);
  194. }
  195. return 0;
  196. }