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.

ctrsm_kernel_RT_bulldozer.c 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 "common.h"
  39. static FLOAT dm1 = -1.;
  40. #ifdef CONJ
  41. #define GEMM_KERNEL GEMM_KERNEL_R
  42. #else
  43. #define GEMM_KERNEL GEMM_KERNEL_N
  44. #endif
  45. #if GEMM_DEFAULT_UNROLL_M == 1
  46. #define GEMM_UNROLL_M_SHIFT 0
  47. #endif
  48. #if GEMM_DEFAULT_UNROLL_M == 2
  49. #define GEMM_UNROLL_M_SHIFT 1
  50. #endif
  51. #if GEMM_DEFAULT_UNROLL_M == 4
  52. #define GEMM_UNROLL_M_SHIFT 2
  53. #endif
  54. #if GEMM_DEFAULT_UNROLL_M == 6
  55. #define GEMM_UNROLL_M_SHIFT 2
  56. #endif
  57. #if GEMM_DEFAULT_UNROLL_M == 8
  58. #define GEMM_UNROLL_M_SHIFT 3
  59. #endif
  60. #if GEMM_DEFAULT_UNROLL_M == 16
  61. #define GEMM_UNROLL_M_SHIFT 4
  62. #endif
  63. #if GEMM_DEFAULT_UNROLL_N == 1
  64. #define GEMM_UNROLL_N_SHIFT 0
  65. #endif
  66. #if GEMM_DEFAULT_UNROLL_N == 2
  67. #define GEMM_UNROLL_N_SHIFT 1
  68. #endif
  69. #if GEMM_DEFAULT_UNROLL_N == 4
  70. #define GEMM_UNROLL_N_SHIFT 2
  71. #endif
  72. #if GEMM_DEFAULT_UNROLL_N == 8
  73. #define GEMM_UNROLL_N_SHIFT 3
  74. #endif
  75. #if GEMM_DEFAULT_UNROLL_N == 16
  76. #define GEMM_UNROLL_N_SHIFT 4
  77. #endif
  78. #ifndef CONJ
  79. static void ctrsm_RT_solve_opt(BLASLONG n, FLOAT *a, FLOAT *b, FLOAT *c, BLASLONG ldc, FLOAT *as, FLOAT *bs) __attribute__ ((noinline));
  80. static void ctrsm_RT_solve_opt(BLASLONG n, FLOAT *a, FLOAT *b, FLOAT *c, BLASLONG ldc, FLOAT *as, FLOAT *bs)
  81. {
  82. FLOAT *c1 = c + ldc*2 ;
  83. BLASLONG n1 = n * 4;
  84. BLASLONG i=0;
  85. __asm__ __volatile__
  86. (
  87. " vzeroupper \n\t"
  88. " prefetcht0 (%4) \n\t"
  89. " prefetcht0 (%5) \n\t"
  90. " vxorps %%xmm8 , %%xmm8 , %%xmm8 \n\t"
  91. " vxorps %%xmm9 , %%xmm9 , %%xmm9 \n\t"
  92. " vxorps %%xmm10, %%xmm10, %%xmm10 \n\t"
  93. " vxorps %%xmm11, %%xmm11, %%xmm11 \n\t"
  94. " vxorps %%xmm12, %%xmm12, %%xmm12 \n\t"
  95. " vxorps %%xmm13, %%xmm13, %%xmm13 \n\t"
  96. " vxorps %%xmm14, %%xmm14, %%xmm14 \n\t"
  97. " vxorps %%xmm15, %%xmm15, %%xmm15 \n\t"
  98. " cmpq $0, %0 \n\t"
  99. " je 3f \n\t"
  100. " .align 16 \n\t"
  101. "1: \n\t"
  102. " vbroadcastss (%3,%1,4), %%xmm0 \n\t" // b0 real, b0 real
  103. " vbroadcastss 4(%3,%1,4), %%xmm1 \n\t" // b0 imag, b0 imag
  104. " vbroadcastss 8(%3,%1,4), %%xmm2 \n\t" // b1 real, b1 real
  105. " vbroadcastss 12(%3,%1,4), %%xmm3 \n\t" // b1 imag, b1 imag
  106. " vmovups (%2,%1,8), %%xmm4 \n\t" // a0 real , a0 imag
  107. " vmovups 16(%2,%1,8), %%xmm5 \n\t" // a1 real , a1 imag
  108. " vfnmaddps %%xmm8 , %%xmm0 , %%xmm4 , %%xmm8 \n\t" // a_real * b_real , a_imag * b_real
  109. " vfnmaddps %%xmm9 , %%xmm1 , %%xmm4 , %%xmm9 \n\t" // a_real * b_imag , a_imag * b_imag
  110. " vfnmaddps %%xmm10, %%xmm0 , %%xmm5 , %%xmm10 \n\t" // a_real * b_real , a_imag * b_real
  111. " vfnmaddps %%xmm11, %%xmm1 , %%xmm5 , %%xmm11 \n\t" // a_real * b_imag , a_imag * b_imag
  112. " vfnmaddps %%xmm12, %%xmm2 , %%xmm4 , %%xmm12 \n\t" // a_real * b_real , a_imag * b_real
  113. " vfnmaddps %%xmm13, %%xmm3 , %%xmm4 , %%xmm13 \n\t" // a_real * b_imag , a_imag * b_imag
  114. " vfnmaddps %%xmm14, %%xmm2 , %%xmm5 , %%xmm14 \n\t" // a_real * b_real , a_imag * b_real
  115. " vfnmaddps %%xmm15, %%xmm3 , %%xmm5 , %%xmm15 \n\t" // a_real * b_imag , a_imag * b_imag
  116. " addq $4, %1 \n\t"
  117. " cmpq %1, %0 \n\t"
  118. " jnz 1b \n\t"
  119. "2: \n\t"
  120. " vshufps $0xb1 , %%xmm9 , %%xmm9, %%xmm9 \n\t"
  121. " vshufps $0xb1 , %%xmm11 , %%xmm11 , %%xmm11 \n\t"
  122. " vshufps $0xb1 , %%xmm13 , %%xmm13 , %%xmm13 \n\t"
  123. " vshufps $0xb1 , %%xmm15 , %%xmm15 , %%xmm15 \n\t"
  124. " vaddsubps %%xmm8 , %%xmm9 , %%xmm8 \n\t"
  125. " vaddsubps %%xmm10, %%xmm11, %%xmm10 \n\t"
  126. " vaddsubps %%xmm12, %%xmm13, %%xmm12 \n\t"
  127. " vaddsubps %%xmm14, %%xmm15, %%xmm14 \n\t"
  128. " vxorps %%xmm7 , %%xmm7 , %%xmm7 \n\t"
  129. " vaddsubps %%xmm8 , %%xmm7 , %%xmm8 \n\t"
  130. " vaddsubps %%xmm10, %%xmm7 , %%xmm10 \n\t"
  131. " vaddsubps %%xmm12, %%xmm7 , %%xmm12 \n\t"
  132. " vaddsubps %%xmm14, %%xmm7 , %%xmm14 \n\t"
  133. " vmovups (%4) , %%xmm0 \n\t"
  134. " vmovups 16(%4) , %%xmm1 \n\t"
  135. " vmovups (%5) , %%xmm4 \n\t"
  136. " vmovups 16(%5) , %%xmm5 \n\t"
  137. " vaddps %%xmm0 , %%xmm8 , %%xmm8 \n\t"
  138. " vaddps %%xmm1 , %%xmm10, %%xmm10 \n\t"
  139. " vaddps %%xmm4 , %%xmm12, %%xmm12 \n\t"
  140. " vaddps %%xmm5 , %%xmm14, %%xmm14 \n\t"
  141. " vmovups %%xmm8 , (%4) \n\t"
  142. " vmovups %%xmm10 ,16(%4) \n\t"
  143. " vmovups %%xmm12 , (%5) \n\t"
  144. " vmovups %%xmm14 ,16(%5) \n\t"
  145. "3: \n\t"
  146. " vzeroupper \n\t"
  147. :
  148. :
  149. "r" (n1), // 0
  150. "a" (i), // 1
  151. "r" (a), // 2
  152. "r" (b), // 3
  153. "r" (c), // 4
  154. "r" (c1), // 5
  155. "r" (as), // 6
  156. "r" (bs) // 7
  157. : "cc",
  158. "%xmm0", "%xmm1", "%xmm2", "%xmm3",
  159. "%xmm4", "%xmm5", "%xmm6", "%xmm7",
  160. "%xmm8", "%xmm9", "%xmm10", "%xmm11",
  161. "%xmm12", "%xmm13", "%xmm14", "%xmm15",
  162. "memory"
  163. );
  164. }
  165. #endif
  166. #ifndef COMPLEX
  167. static inline void solve(BLASLONG m, BLASLONG n, FLOAT *a, FLOAT *b, FLOAT *c, BLASLONG ldc) {
  168. FLOAT aa, bb;
  169. int i, j, k;
  170. a += (n - 1) * m;
  171. b += (n - 1) * n;
  172. for (i = n - 1; i >= 0; i--) {
  173. bb = *(b + i);
  174. for (j = 0; j < m; j ++) {
  175. aa = *(c + j + i * ldc);
  176. aa *= bb;
  177. *a = aa;
  178. *(c + j + i * ldc) = aa;
  179. a ++;
  180. for (k = 0; k < i; k ++){
  181. *(c + j + k * ldc) -= aa * *(b + k);
  182. }
  183. }
  184. b -= n;
  185. a -= 2 * m;
  186. }
  187. }
  188. #else
  189. static inline void solve(BLASLONG m, BLASLONG n, FLOAT *a, FLOAT *b, FLOAT *c, BLASLONG ldc) {
  190. FLOAT aa1, aa2;
  191. FLOAT bb1, bb2;
  192. FLOAT cc1, cc2;
  193. int i, j, k;
  194. ldc *= 2;
  195. a += (n - 1) * m * 2;
  196. b += (n - 1) * n * 2;
  197. for (i = n - 1; i >= 0; i--) {
  198. bb1 = *(b + i * 2 + 0);
  199. bb2 = *(b + i * 2 + 1);
  200. for (j = 0; j < m; j ++) {
  201. aa1 = *(c + j * 2 + 0 + i * ldc);
  202. aa2 = *(c + j * 2 + 1 + i * ldc);
  203. #ifndef CONJ
  204. cc1 = aa1 * bb1 - aa2 * bb2;
  205. cc2 = aa1 * bb2 + aa2 * bb1;
  206. #else
  207. cc1 = aa1 * bb1 + aa2 * bb2;
  208. cc2 = - aa1 * bb2 + aa2 * bb1;
  209. #endif
  210. *(a + 0) = cc1;
  211. *(a + 1) = cc2;
  212. *(c + j * 2 + 0 + i * ldc) = cc1;
  213. *(c + j * 2 + 1 + i * ldc) = cc2;
  214. a += 2;
  215. for (k = 0; k < i; k ++){
  216. #ifndef CONJ
  217. *(c + j * 2 + 0 + k * ldc) -= cc1 * *(b + k * 2 + 0) - cc2 * *(b + k * 2 + 1);
  218. *(c + j * 2 + 1 + k * ldc) -= cc1 * *(b + k * 2 + 1) + cc2 * *(b + k * 2 + 0);
  219. #else
  220. *(c + j * 2 + 0 + k * ldc) -= cc1 * *(b + k * 2 + 0) + cc2 * *(b + k * 2 + 1);
  221. *(c + j * 2 + 1 + k * ldc) -= -cc1 * *(b + k * 2 + 1) + cc2 * *(b + k * 2 + 0);
  222. #endif
  223. }
  224. }
  225. b -= n * 2;
  226. a -= 4 * m;
  227. }
  228. }
  229. #endif
  230. int CNAME(BLASLONG m, BLASLONG n, BLASLONG k, FLOAT dummy1,
  231. #ifdef COMPLEX
  232. FLOAT dummy2,
  233. #endif
  234. FLOAT *a, FLOAT *b, FLOAT *c, BLASLONG ldc, BLASLONG offset){
  235. BLASLONG i, j;
  236. FLOAT *aa, *cc;
  237. BLASLONG kk;
  238. #if 0
  239. fprintf(stderr, "TRSM RT KERNEL m = %3ld n = %3ld k = %3ld offset = %3ld\n",
  240. m, n, k, offset);
  241. #endif
  242. kk = n - offset;
  243. c += n * ldc * COMPSIZE;
  244. b += n * k * COMPSIZE;
  245. if (n & (GEMM_UNROLL_N - 1)) {
  246. j = 1;
  247. while (j < GEMM_UNROLL_N) {
  248. if (n & j) {
  249. aa = a;
  250. b -= j * k * COMPSIZE;
  251. c -= j * ldc* COMPSIZE;
  252. cc = c;
  253. i = (m >> GEMM_UNROLL_M_SHIFT);
  254. if (i > 0) {
  255. do {
  256. if (k - kk > 0) {
  257. GEMM_KERNEL(GEMM_UNROLL_M, j, k - kk, dm1,
  258. #ifdef COMPLEX
  259. ZERO,
  260. #endif
  261. aa + GEMM_UNROLL_M * kk * COMPSIZE,
  262. b + j * kk * COMPSIZE,
  263. cc,
  264. ldc);
  265. }
  266. solve(GEMM_UNROLL_M, j,
  267. aa + (kk - j) * GEMM_UNROLL_M * COMPSIZE,
  268. b + (kk - j) * j * COMPSIZE,
  269. cc, ldc);
  270. aa += GEMM_UNROLL_M * k * COMPSIZE;
  271. cc += GEMM_UNROLL_M * COMPSIZE;
  272. i --;
  273. } while (i > 0);
  274. }
  275. if (m & (GEMM_UNROLL_M - 1)) {
  276. i = (GEMM_UNROLL_M >> 1);
  277. do {
  278. if (m & i) {
  279. if (k - kk > 0) {
  280. GEMM_KERNEL(i, j, k - kk, dm1,
  281. #ifdef COMPLEX
  282. ZERO,
  283. #endif
  284. aa + i * kk * COMPSIZE,
  285. b + j * kk * COMPSIZE,
  286. cc, ldc);
  287. }
  288. solve(i, j,
  289. aa + (kk - j) * i * COMPSIZE,
  290. b + (kk - j) * j * COMPSIZE,
  291. cc, ldc);
  292. aa += i * k * COMPSIZE;
  293. cc += i * COMPSIZE;
  294. }
  295. i >>= 1;
  296. } while (i > 0);
  297. }
  298. kk -= j;
  299. }
  300. j <<= 1;
  301. }
  302. }
  303. j = (n >> GEMM_UNROLL_N_SHIFT);
  304. if (j > 0) {
  305. do {
  306. aa = a;
  307. b -= GEMM_UNROLL_N * k * COMPSIZE;
  308. c -= GEMM_UNROLL_N * ldc * COMPSIZE;
  309. cc = c;
  310. i = (m >> GEMM_UNROLL_M_SHIFT);
  311. if (i > 0) {
  312. do {
  313. #ifndef CONJ
  314. ctrsm_RT_solve_opt(k-kk, aa + GEMM_UNROLL_M * kk * COMPSIZE, b + GEMM_UNROLL_N * kk * COMPSIZE, cc, ldc,
  315. aa + (kk - GEMM_UNROLL_N) * GEMM_UNROLL_M * COMPSIZE, b + (kk - GEMM_UNROLL_N) * GEMM_UNROLL_N * COMPSIZE);
  316. solve(GEMM_UNROLL_M, GEMM_UNROLL_N,
  317. aa + (kk - GEMM_UNROLL_N) * GEMM_UNROLL_M * COMPSIZE,
  318. b + (kk - GEMM_UNROLL_N) * GEMM_UNROLL_N * COMPSIZE,
  319. cc, ldc);
  320. #else
  321. if (k - kk > 0) {
  322. GEMM_KERNEL(GEMM_UNROLL_M, GEMM_UNROLL_N, k - kk, dm1,
  323. #ifdef COMPLEX
  324. ZERO,
  325. #endif
  326. aa + GEMM_UNROLL_M * kk * COMPSIZE,
  327. b + GEMM_UNROLL_N * kk * COMPSIZE,
  328. cc,
  329. ldc);
  330. }
  331. solve(GEMM_UNROLL_M, GEMM_UNROLL_N,
  332. aa + (kk - GEMM_UNROLL_N) * GEMM_UNROLL_M * COMPSIZE,
  333. b + (kk - GEMM_UNROLL_N) * GEMM_UNROLL_N * COMPSIZE,
  334. cc, ldc);
  335. #endif
  336. aa += GEMM_UNROLL_M * k * COMPSIZE;
  337. cc += GEMM_UNROLL_M * COMPSIZE;
  338. i --;
  339. } while (i > 0);
  340. }
  341. if (m & (GEMM_UNROLL_M - 1)) {
  342. i = (GEMM_UNROLL_M >> 1);
  343. do {
  344. if (m & i) {
  345. if (k - kk > 0) {
  346. GEMM_KERNEL(i, GEMM_UNROLL_N, k - kk, dm1,
  347. #ifdef COMPLEX
  348. ZERO,
  349. #endif
  350. aa + i * kk * COMPSIZE,
  351. b + GEMM_UNROLL_N * kk * COMPSIZE,
  352. cc,
  353. ldc);
  354. }
  355. solve(i, GEMM_UNROLL_N,
  356. aa + (kk - GEMM_UNROLL_N) * i * COMPSIZE,
  357. b + (kk - GEMM_UNROLL_N) * GEMM_UNROLL_N * COMPSIZE,
  358. cc, ldc);
  359. aa += i * k * COMPSIZE;
  360. cc += i * COMPSIZE;
  361. }
  362. i >>= 1;
  363. } while (i > 0);
  364. }
  365. kk -= GEMM_UNROLL_N;
  366. j --;
  367. } while (j > 0);
  368. }
  369. return 0;
  370. }