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.

laed3_parallel.c 7.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /***************************************************************************
  2. Copyright (c) 2025, The OpenBLAS Project
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. 3. Neither the name of the OpenBLAS project nor the names of
  14. its contributors may be used to endorse or promote products
  15. derived from this software without specific prior written
  16. permission.
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  26. USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *****************************************************************************/
  28. #include <stdio.h>
  29. #include "common.h"
  30. #define max(a,b) ((a) > (b) ? (a) : (b))
  31. #define copysign(x,y) ((y) < 0 ? ((x) < 0 ? (x) : -(x)) : ((x) < 0 ? -(x) : (x)))
  32. #if defined(DOUBLE)
  33. #define LAMC3 BLASFUNC(dlamc3)
  34. #define LAED4 BLASFUNC(dlaed4)
  35. #define GEMM BLASFUNC(dgemm)
  36. #define NRM2 BLASFUNC(dnrm2)
  37. #define COPY BLASFUNC(dcopy)
  38. #define LACPY BLASFUNC(dlacpy)
  39. #define LASET BLASFUNC(dlaset)
  40. #else
  41. #define LAMC3 BLASFUNC(slamc3)
  42. #define LAED4 BLASFUNC(slaed4)
  43. #define GEMM BLASFUNC(sgemm)
  44. #define NRM2 BLASFUNC(snrm2)
  45. #define COPY BLASFUNC(scopy)
  46. #define LACPY BLASFUNC(slacpy)
  47. #define LASET BLASFUNC(slaset)
  48. #endif
  49. FLOAT LAMC3(FLOAT *, FLOAT *);
  50. void LAED4(blasint *, blasint *, FLOAT *, FLOAT *, FLOAT *, FLOAT *, FLOAT *, blasint *);
  51. void LACPY(char *, blasint *, blasint *, FLOAT *, blasint *, FLOAT *, blasint *);
  52. void LASET(char *, blasint *, blasint *, FLOAT *, FLOAT *, FLOAT *, blasint *);
  53. /* Table of constant values */
  54. static blasint c1 = 1;
  55. static FLOAT c1f = 1.;
  56. static FLOAT c0f = 0.;
  57. static void inner_laed4_thread(blas_arg_t *args, BLASLONG *range_m, BLASLONG *range_n, FLOAT *sa, FLOAT *sb, BLASLONG mypos){
  58. blasint kval = args -> m;
  59. blasint j, j_from, j_to;
  60. FLOAT *dlamda = (FLOAT *)args -> a;
  61. FLOAT *w = (FLOAT *)args -> b;
  62. FLOAT *q = (FLOAT *)args -> c;
  63. BLASLONG qdim = args -> ldc;
  64. FLOAT *d = (FLOAT *)args -> d;
  65. FLOAT rho = *(FLOAT *)args -> alpha;
  66. blasint *info = &((blasint*)args -> beta)[mypos];
  67. j_from = range_m[0] + 1;
  68. j_to = range_m[1];
  69. for (j = j_from; j <= j_to; j++) {
  70. LAED4(&kval, &j, dlamda, w, &q[(j - 1) * qdim], &rho, &d[j - 1], info);
  71. if(*info != 0) break;
  72. }
  73. }
  74. static void inner_wloop_thread(blas_arg_t *args, BLASLONG *range_m, BLASLONG *range_n, FLOAT *sa, FLOAT *sb, BLASLONG mypos){
  75. blasint kval = args -> m;
  76. blasint i, j, i_from, i_to;
  77. FLOAT *dlamda = (FLOAT *)args -> a;
  78. FLOAT *w = (FLOAT *)args -> b;
  79. FLOAT *q = (FLOAT *)args -> c;
  80. BLASLONG qdim = args -> ldc;
  81. i_from = range_m[0];
  82. i_to = range_m[1];
  83. for (j = 0; j < kval; j++) {
  84. for (i = i_from; i < i_to; i++) {
  85. if (i != j) w[i] *= q[j * qdim + i] / (dlamda[i] - dlamda[j]);
  86. }
  87. }
  88. }
  89. /* ===================================================================== */
  90. blasint CNAME(blasint *k, blasint *n, blasint *n1, FLOAT *d,
  91. FLOAT *q, blasint *ldq, FLOAT *rho, FLOAT *dlamda,
  92. FLOAT *q2, blasint *indx, blasint *ctot, FLOAT *w,
  93. FLOAT *s, blasint *info)
  94. {
  95. FLOAT temp;
  96. blasint kval, qdim;
  97. blasint i, j, itmp;
  98. blasint n2, n12, ii, n23, iq2;
  99. blas_queue_t queue[MAX_CPU_NUMBER];
  100. blas_arg_t args;
  101. BLASLONG range[MAX_CPU_NUMBER + 1];
  102. blasint infoarray[MAX_CPU_NUMBER];
  103. int width, num_cpu, mode, nthreads;
  104. qdim = *ldq;
  105. kval = *k;
  106. /* Modify values DLAMDA(i) to make sure all DLAMDA(i)-DLAMDA(j) can */
  107. /* be computed with high relative accuracy (barring over/underflow). */
  108. for (i = 0; i < kval; i++) {
  109. dlamda[i] = LAMC3(&dlamda[i], &dlamda[i]) - dlamda[i];
  110. }
  111. nthreads = num_cpu_avail(4);
  112. #if defined(DOUBLE)
  113. mode = BLAS_DOUBLE | BLAS_REAL;
  114. #else
  115. mode = BLAS_SINGLE | BLAS_REAL;
  116. #endif
  117. args.m = kval;
  118. args.a = (void *)dlamda;
  119. args.b = (void *)w;
  120. args.c = (void *)q;
  121. args.ldc = qdim;
  122. args.d = (void *)d;
  123. args.alpha = (void *)rho;
  124. args.beta = (void *)infoarray;
  125. num_cpu = 0;
  126. range[0] = 0;
  127. i = kval;
  128. while (i > 0) {
  129. width = blas_quickdivide(i + nthreads - num_cpu - 1, nthreads - num_cpu);
  130. range[num_cpu + 1] = range[num_cpu] + width;
  131. queue[num_cpu].range_m = &range[num_cpu];
  132. queue[num_cpu].range_n = NULL;
  133. queue[num_cpu].routine = inner_laed4_thread;
  134. queue[num_cpu].args = &args;
  135. queue[num_cpu].sa = NULL;
  136. queue[num_cpu].sb = NULL;
  137. queue[num_cpu].mode = mode;
  138. queue[num_cpu].next = &queue[num_cpu + 1];
  139. infoarray[num_cpu] = 0;
  140. num_cpu ++;
  141. i -= width;
  142. }
  143. if (num_cpu) {
  144. queue[num_cpu - 1].next = NULL;
  145. exec_blas(num_cpu, queue);
  146. }
  147. for (i = 0; i < num_cpu; i++) {
  148. *info = max(infoarray[i], *info);
  149. }
  150. /* If the zero finder fails, the computation is terminated. */
  151. if (*info != 0) {
  152. return 0;
  153. }
  154. if (kval == 2) {
  155. for (j = 0; j < kval; j++) {
  156. w[0] = q[j * qdim];
  157. w[1] = q[j * qdim + 1];
  158. ii = indx[0] - 1;
  159. q[j * qdim] = w[ii];
  160. ii = indx[1] - 1;
  161. q[j * qdim + 1] = w[ii];
  162. }
  163. } else if (kval != 1) {
  164. /* Compute updated W. */
  165. COPY(k, w, &c1, s, &c1);
  166. /* Initialize W(I) = Q(I,I) */
  167. itmp = qdim + 1;
  168. COPY(k, q, &itmp, w, &c1);
  169. for (i = 0; i < num_cpu; i++) {
  170. queue[i].routine = inner_wloop_thread;
  171. }
  172. if (num_cpu) {
  173. exec_blas(num_cpu, queue);
  174. }
  175. for (i = 0; i < kval; i++) {
  176. temp = sqrt(-w[i]);
  177. w[i] = copysign(temp, s[i]);
  178. }
  179. /* Compute eigenvectors of the modified rank-1 modification. */
  180. for (j = 0; j < kval; j++) {
  181. for (i = 0; i < kval; i++) {
  182. s[i] = w[i] / q[j * qdim + i];
  183. }
  184. temp = NRM2(k, s, &c1);
  185. for (i = 0; i < kval; i++) {
  186. ii = indx[i] - 1;
  187. q[j * qdim + i] = s[ii] / temp;
  188. }
  189. }
  190. }
  191. /* Compute the updated eigenvectors. */
  192. n2 = *n - *n1;
  193. n12 = ctot[0] + ctot[1];
  194. n23 = ctot[1] + ctot[2];
  195. LACPY("A", &n23, k, &q[ctot[0]], ldq, s, &n23);
  196. iq2 = *n1 * n12;
  197. if (n23 != 0) {
  198. GEMM("N", "N", &n2, k, &n23, &c1f, &q2[iq2], &n2, s, &n23, &c0f, &q[*n1], ldq);
  199. } else {
  200. LASET("A", &n2, k, &c0f, &c0f, &q[*n1], ldq);
  201. }
  202. LACPY("A", &n12, k, q, ldq, s, &n12);
  203. if (n12 != 0) {
  204. GEMM("N", "N", n1, k, &n12, &c1f, q2, n1, s, &n12, &c0f, q, ldq);
  205. } else {
  206. LASET("A", n1, k, &c0f, &c0f, q, ldq);
  207. }
  208. return 0;
  209. }