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.

strsm_kernel_RT_bulldozer.c 13 kB

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