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.

syrk_thread.c 6.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 <math.h>
  41. #include "common.h"
  42. int CNAME(int mode, blas_arg_t *arg, BLASLONG *range_m, BLASLONG *range_n, int (*function)(blas_arg_t*, BLASLONG*, BLASLONG*, FLOAT *, FLOAT *, BLASLONG), void *sa, void *sb, BLASLONG nthreads) {
  43. blas_queue_t queue[MAX_CPU_NUMBER];
  44. BLASLONG range[MAX_CPU_NUMBER + 1];
  45. BLASLONG width, i;
  46. BLASLONG n_from, n_to;
  47. double dnum, nf, nt, di, dinum;
  48. int num_cpu;
  49. int mask = 0;
  50. if (!(mode & BLAS_COMPLEX)) {
  51. switch (mode & BLAS_PREC) {
  52. #if defined(BUILD_SINGLE) || defined(BUILD_COMPLEX)
  53. case BLAS_SINGLE:
  54. mask = SGEMM_UNROLL_MN - 1;
  55. break;
  56. #endif
  57. #if defined(BUILD_DOUBLE) || defined(BUILD_COMPLEX16)
  58. case BLAS_DOUBLE:
  59. mask = DGEMM_UNROLL_MN - 1;
  60. break;
  61. #endif
  62. #ifdef EXPRECISION
  63. case BLAS_XDOUBLE:
  64. mask = MAX(QGEMM_UNROLL_M, QGEMM_UNROLL_N) - 1;
  65. break;
  66. #endif
  67. }
  68. } else {
  69. switch (mode & BLAS_PREC) {
  70. #ifdef BUILD_COMPLEX
  71. case BLAS_SINGLE:
  72. mask = CGEMM_UNROLL_MN - 1;
  73. break;
  74. #endif
  75. #ifdef BUILD_COMPLEX16
  76. case BLAS_DOUBLE:
  77. mask = ZGEMM_UNROLL_MN - 1;
  78. break;
  79. #endif
  80. #ifdef EXPRECISION
  81. case BLAS_XDOUBLE:
  82. mask = MAX(XGEMM_UNROLL_M, XGEMM_UNROLL_N) - 1;
  83. break;
  84. #endif
  85. }
  86. }
  87. n_from = 0;
  88. n_to = arg -> n;
  89. if (range_n) {
  90. n_from = *(range_n + 0);
  91. n_to = *(range_n + 1);
  92. }
  93. if (!(mode & BLAS_UPLO)) {
  94. nf = (double)(n_from);
  95. nt = (double)(n_to);
  96. dnum = (nt * nt - nf * nf) / (double)nthreads;
  97. num_cpu = 0;
  98. range[0] = n_from;
  99. i = n_from;
  100. while (i < n_to){
  101. if (nthreads - num_cpu > 1) {
  102. di = (double)i;
  103. dinum = di * di +dnum;
  104. if (dinum <0)
  105. width = (BLASLONG)(( - di + mask)/(mask+1)) * (mask+1);
  106. else
  107. width = (BLASLONG)(( sqrt(dinum) - di + mask)/(mask+1)) * (mask+1);
  108. if ((width <= 0) || (width > n_to - i)) width = n_to - i;
  109. } else {
  110. width = n_to - i;
  111. }
  112. range[num_cpu + 1] = range[num_cpu] + width;
  113. queue[num_cpu].mode = mode;
  114. queue[num_cpu].routine = function;
  115. queue[num_cpu].args = arg;
  116. queue[num_cpu].range_m = range_m;
  117. queue[num_cpu].range_n = &range[num_cpu];
  118. queue[num_cpu].sa = NULL;
  119. queue[num_cpu].sb = NULL;
  120. queue[num_cpu].next = &queue[num_cpu + 1];
  121. num_cpu ++;
  122. i += width;
  123. }
  124. } else {
  125. nf = (double)(arg -> n - n_from);
  126. nt = (double)(arg -> n - n_to);
  127. dnum = (nt * nt - nf * nf) / (double)nthreads;
  128. num_cpu = 0;
  129. range[0] = n_from;
  130. i = n_from;
  131. while (i < n_to){
  132. if (nthreads - num_cpu > 1) {
  133. di = (double)(arg -> n - i);
  134. dinum = di * di + dnum;
  135. if (dinum<0)
  136. width = ((BLASLONG)(di + mask)/(mask+1)) * (mask+1);
  137. else
  138. width = ((BLASLONG)((-sqrt(dinum) + di) + mask)/(mask+1)) * (mask+1);
  139. if ((width <= 0) || (width > n_to - i)) width = n_to - i;
  140. } else {
  141. width = n_to - i;
  142. }
  143. range[num_cpu + 1] = range[num_cpu] + width;
  144. queue[num_cpu].mode = mode;
  145. queue[num_cpu].routine = function;
  146. queue[num_cpu].args = arg;
  147. queue[num_cpu].range_m = range_m;
  148. queue[num_cpu].range_n = &range[num_cpu];
  149. queue[num_cpu].sa = NULL;
  150. queue[num_cpu].sb = NULL;
  151. queue[num_cpu].next = &queue[num_cpu + 1];
  152. num_cpu ++;
  153. i += width;
  154. }
  155. }
  156. if (num_cpu) {
  157. queue[0].sa = sa;
  158. queue[0].sb = sb;
  159. queue[num_cpu - 1].next = NULL;
  160. exec_blas(num_cpu, queue);
  161. }
  162. return 0;
  163. }