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.

gemv_thread.c 6.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #ifndef TRANSA
  42. #if !defined(CONJ) && !defined(XCONJ)
  43. #define GEMV GEMV_N
  44. #elif defined(CONJ) && !defined(XCONJ)
  45. #define GEMV GEMV_R
  46. #elif !defined(CONJ) && defined(XCONJ)
  47. #define GEMV GEMV_O
  48. #else
  49. #define GEMV GEMV_S
  50. #endif
  51. #else
  52. #if !defined(CONJ) && !defined(XCONJ)
  53. #define GEMV GEMV_T
  54. #elif defined(CONJ) && !defined(XCONJ)
  55. #define GEMV GEMV_C
  56. #elif !defined(CONJ) && defined(XCONJ)
  57. #define GEMV GEMV_U
  58. #else
  59. #define GEMV GEMV_D
  60. #endif
  61. #endif
  62. static int gemv_kernel(blas_arg_t *args, BLASLONG *range_m, BLASLONG *range_n, FLOAT *dummy1, FLOAT *buffer, BLASLONG pos){
  63. FLOAT *a, *x, *y;
  64. BLASLONG lda, incx, incy;
  65. BLASLONG m_from, m_to, n_from, n_to;
  66. a = (FLOAT *)args -> a;
  67. x = (FLOAT *)args -> b;
  68. y = (FLOAT *)args -> c;
  69. lda = args -> lda;
  70. incx = args -> ldb;
  71. incy = args -> ldc;
  72. m_from = 0;
  73. m_to = args -> m;
  74. if (range_m) {
  75. m_from = *(range_m + 0);
  76. m_to = *(range_m + 1);
  77. a += m_from * COMPSIZE;
  78. #ifndef TRANSA
  79. y += m_from * incy * COMPSIZE;
  80. #endif
  81. }
  82. n_from = 0;
  83. n_to = args -> n;
  84. if (range_n) {
  85. n_from = *(range_n + 0);
  86. n_to = *(range_n + 1);
  87. a += n_from * lda * COMPSIZE;
  88. #ifdef TRANSA
  89. y += n_from * incy * COMPSIZE;
  90. #endif
  91. }
  92. // fprintf(stderr, "M_From = %d M_To = %d N_From = %d N_To = %d\n", m_from, m_to, n_from, n_to);
  93. GEMV(m_to - m_from, n_to - n_from, 0,
  94. *((FLOAT *)args -> alpha + 0),
  95. #ifdef COMPLEX
  96. *((FLOAT *)args -> alpha + 1),
  97. #endif
  98. a, lda, x, incx, y, incy, buffer);
  99. return 0;
  100. }
  101. #ifndef COMPLEX
  102. int CNAME(BLASLONG m, BLASLONG n, FLOAT alpha, FLOAT *a, BLASLONG lda, FLOAT *x, BLASLONG incx, FLOAT *y, BLASLONG incy, FLOAT *buffer, int nthreads){
  103. #else
  104. int CNAME(BLASLONG m, BLASLONG n, FLOAT *alpha, FLOAT *a, BLASLONG lda, FLOAT *x, BLASLONG incx, FLOAT *y, BLASLONG incy, FLOAT *buffer, int nthreads){
  105. #endif
  106. blas_arg_t args;
  107. blas_queue_t queue[MAX_CPU_NUMBER];
  108. BLASLONG range[MAX_CPU_NUMBER + 1];
  109. BLASLONG width, i, num_cpu;
  110. #ifdef SMP
  111. #ifndef COMPLEX
  112. #ifdef XDOUBLE
  113. int mode = BLAS_XDOUBLE | BLAS_REAL;
  114. #elif defined(DOUBLE)
  115. int mode = BLAS_DOUBLE | BLAS_REAL;
  116. #else
  117. int mode = BLAS_SINGLE | BLAS_REAL;
  118. #endif
  119. #else
  120. #ifdef XDOUBLE
  121. int mode = BLAS_XDOUBLE | BLAS_COMPLEX;
  122. #elif defined(DOUBLE)
  123. int mode = BLAS_DOUBLE | BLAS_COMPLEX;
  124. #else
  125. int mode = BLAS_SINGLE | BLAS_COMPLEX;
  126. #endif
  127. #endif
  128. #endif
  129. args.m = m;
  130. args.n = n;
  131. args.a = (void *)a;
  132. args.b = (void *)x;
  133. args.c = (void *)y;
  134. args.lda = lda;
  135. args.ldb = incx;
  136. args.ldc = incy;
  137. #ifndef COMPLEX
  138. args.alpha = (void *)&alpha;
  139. #else
  140. args.alpha = (void *) alpha;
  141. #endif
  142. num_cpu = 0;
  143. range[0] = 0;
  144. #ifndef TRANSA
  145. i = m;
  146. #else
  147. i = n;
  148. #endif
  149. while (i > 0){
  150. width = blas_quickdivide(i + nthreads - num_cpu - 1, nthreads - num_cpu);
  151. if (width < 4) width = 4;
  152. if (i < width) width = i;
  153. range[num_cpu + 1] = range[num_cpu] + width;
  154. queue[num_cpu].mode = mode;
  155. queue[num_cpu].routine = gemv_kernel;
  156. queue[num_cpu].args = &args;
  157. #ifndef TRANSA
  158. queue[num_cpu].range_m = &range[num_cpu];
  159. queue[num_cpu].range_n = NULL;
  160. #else
  161. queue[num_cpu].range_m = NULL;
  162. queue[num_cpu].range_n = &range[num_cpu];
  163. #endif
  164. queue[num_cpu].sa = NULL;
  165. queue[num_cpu].sb = NULL;
  166. queue[num_cpu].next = &queue[num_cpu + 1];
  167. num_cpu ++;
  168. i -= width;
  169. }
  170. if (num_cpu) {
  171. queue[0].sa = NULL;
  172. queue[0].sb = buffer;
  173. queue[num_cpu - 1].next = NULL;
  174. exec_blas(num_cpu, queue);
  175. }
  176. return 0;
  177. }