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.

gemm_vec.c 25 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /*
  2. * Copyright (c) IBM Corporation 2020.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. * 3. Neither the name of the OpenBLAS project nor the names of
  17. * its contributors may be used to endorse or promote products
  18. * derived from this software without specific prior written
  19. * permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  30. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "common.h"
  33. #include "vector-common.h"
  34. #include <stdbool.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #ifdef COMPLEX
  38. #error "Handling for complex numbers is not supported in this kernel"
  39. #endif
  40. #ifdef DOUBLE
  41. #define UNROLL_M DGEMM_DEFAULT_UNROLL_M
  42. #define UNROLL_N DGEMM_DEFAULT_UNROLL_N
  43. #else
  44. #define UNROLL_M SGEMM_DEFAULT_UNROLL_M
  45. #define UNROLL_N SGEMM_DEFAULT_UNROLL_N
  46. #endif
  47. static const size_t unroll_m = UNROLL_M;
  48. static const size_t unroll_n = UNROLL_N;
  49. /* Handling of triangular matrices */
  50. #ifdef TRMMKERNEL
  51. static const bool trmm = true;
  52. static const bool left =
  53. #ifdef LEFT
  54. true;
  55. #else
  56. false;
  57. #endif
  58. static const bool backwards =
  59. #if defined(LEFT) != defined(TRANSA)
  60. true;
  61. #else
  62. false;
  63. #endif
  64. #else
  65. static const bool trmm = false;
  66. static const bool left = false;
  67. static const bool backwards = false;
  68. #endif /* TRMMKERNEL */
  69. /*
  70. * Background:
  71. *
  72. * The algorithm of GotoBLAS / OpenBLAS breaks down the matrix multiplication
  73. * problem by splitting all matrices into partitions multiple times, so that the
  74. * submatrices fit into the L1 or L2 caches. As a result, each multiplication of
  75. * submatrices can stream data fast from L1 and L2 caches. Inbetween, it copies
  76. * and rearranges the submatrices to enable contiguous memory accesses to
  77. * improve locality in both caches and TLBs.
  78. *
  79. * At the heart of the algorithm is this kernel, which multiplies, a "Block
  80. * matrix" A (small dimensions) with a "Panel matrix" B (number of rows is
  81. * small) and adds the result into a "Panel matrix" C; GotoBLAS calls this
  82. * operation GEBP. This kernel further partitions GEBP twice, such that (1)
  83. * submatrices of C and B fit into the L1 caches (GEBP_column_block) and (2) a
  84. * block of C fits into the registers, while multiplying panels from A and B
  85. * streamed from the L2 and L1 cache, respectively (GEBP_block).
  86. *
  87. *
  88. * Algorithm GEBP(A, B, C, m, n, k, alpha):
  89. *
  90. * The problem is calculating C += alpha * (A * B)
  91. * C is an m x n matrix, A is an m x k matrix, B is an k x n matrix.
  92. *
  93. * - C is in column-major-order, with an offset of ldc to the element in the
  94. * next column (same row).
  95. * - A is in row-major-order yet stores SGEMM_UNROLL_M elements of each column
  96. * contiguously while walking along rows.
  97. * - B is in column-major-order but packs SGEMM_UNROLL_N elements of a row
  98. * contiguously.
  99. * If the numbers of rows and columns are not multiples of SGEMM_UNROLL_M or
  100. * SGEMM_UNROLL_N, the remaining elements are arranged in blocks with power-of-2
  101. * dimensions (e.g., 5 remaining columns would be in a block-of-4 and a
  102. * block-of-1).
  103. *
  104. * Note that packing A and B into that form is taken care of by the caller in
  105. * driver/level3/level3.c (actually done by "copy kernels").
  106. *
  107. * Steps:
  108. * - Partition C and B into blocks of n_r (SGEMM_UNROLL_N) columns, C_j and B_j.
  109. * Now, B_j should fit into the L1 cache.
  110. * - For each partition, calculate C_j += alpha * (A * B_j) by
  111. * (1) Calculate C_aux := A * B_j (see below)
  112. * (2) unpack C_j = C_j + alpha * C_aux
  113. *
  114. *
  115. * Algorithm for Calculating C_aux:
  116. *
  117. * - Further partition C_aux and A into groups of m_r (SGEMM_UNROLL_M) rows,
  118. * such that the m_r x n_r-submatrix of C_aux can be held in registers. Each
  119. * submatrix of C_aux can be calculated independently, and the registers are
  120. * added back into C_j.
  121. *
  122. * - For each row-block of C_aux:
  123. * (uses a row block of A and full B_j)
  124. * - stream over all columns of A, multiply with elements from B and
  125. * accumulate in registers. (use different inner-kernels to exploit
  126. * vectorization for varying block sizes)
  127. * - add alpha * row block of C_aux back into C_j.
  128. *
  129. * Note that there are additional mechanics for handling triangular matrices,
  130. * calculating B := alpha (A * B) where either of the matrices A or B can be
  131. * triangular. In case of A, the macro "LEFT" is defined. In addition, A can
  132. * optionally be transposed.
  133. * The code effectively skips an "offset" number of columns in A and rows of B
  134. * in each block, to save unnecessary work by exploiting the triangular nature.
  135. * To handle all cases, the code discerns (1) a "left" mode when A is triangular
  136. * and (2) "forward" / "backwards" modes where only the first "offset"
  137. * columns/rows of A/B are used or where the first "offset" columns/rows are
  138. * skipped, respectively.
  139. *
  140. * Reference:
  141. *
  142. * The summary above is based on staring at various kernel implementations and:
  143. * K. Goto and R. A. Van de Geijn, Anatomy of High-Performance Matrix
  144. * Multiplication, in ACM Transactions of Mathematical Software, Vol. 34, No.
  145. * 3, May 2008.
  146. */
  147. /**
  148. * Calculate for a row-block in C_i of size ROWSxCOLS using vector intrinsics.
  149. *
  150. * @param[in] A Pointer current block of input matrix A.
  151. * @param[in] k Number of columns in A.
  152. * @param[in] B Pointer current block of input matrix B.
  153. * @param[inout] C Pointer current block of output matrix C.
  154. * @param[in] ldc Offset between elements in adjacent columns in C.
  155. * @param[in] alpha Scalar factor.
  156. */
  157. #define VECTOR_BLOCK(ROWS, COLS) \
  158. static inline void GEBP_block_##ROWS##_##COLS( \
  159. FLOAT const *restrict A, BLASLONG bk, FLOAT const *restrict B, \
  160. FLOAT *restrict C, BLASLONG ldc, FLOAT alpha) { \
  161. _Static_assert( \
  162. ROWS % VLEN_FLOATS == 0, \
  163. "rows in block must be multiples of vector length"); \
  164. vector_float Caux[ROWS / VLEN_FLOATS][COLS]; \
  165. \
  166. for (BLASLONG i = 0; i < ROWS / VLEN_FLOATS; i++) { \
  167. vector_float A0 = \
  168. vec_load_hinted(A + i * VLEN_FLOATS); \
  169. for (BLASLONG j = 0; j < COLS; j++) \
  170. Caux[i][j] = A0 * B[j]; \
  171. } \
  172. \
  173. /* \
  174. * Stream over the row-block of A, which is packed \
  175. * column-by-column, multiply by coefficients in B and add up \
  176. * into temporaries Caux (which the compiler will hold in \
  177. * registers). Vectorization: Multiply column vectors from A \
  178. * with scalars from B and add up in column vectors of Caux. \
  179. * That equates to unrolling the loop over rows (in i) and \
  180. * executing each unrolled iteration as a vector element. \
  181. */ \
  182. for (BLASLONG k = 1; k < bk; k++) { \
  183. for (BLASLONG i = 0; i < ROWS / VLEN_FLOATS; i++) { \
  184. vector_float Ak = vec_load_hinted( \
  185. A + i * VLEN_FLOATS + k * ROWS); \
  186. \
  187. for (BLASLONG j = 0; j < COLS; j++) \
  188. Caux[i][j] += Ak * B[j + k * COLS]; \
  189. } \
  190. } \
  191. \
  192. /* \
  193. * Unpack row-block of C_aux into outer C_i, multiply by \
  194. * alpha and add up. \
  195. */ \
  196. for (BLASLONG j = 0; j < COLS; j++) { \
  197. for (BLASLONG i = 0; i < ROWS / VLEN_FLOATS; i++) { \
  198. vector_float *C_ij = \
  199. (vector_float *)(C + i * VLEN_FLOATS + \
  200. j * ldc); \
  201. if (trmm) { \
  202. *C_ij = alpha * Caux[i][j]; \
  203. } else { \
  204. *C_ij += alpha * Caux[i][j]; \
  205. } \
  206. } \
  207. } \
  208. }
  209. #if UNROLL_M == 16
  210. VECTOR_BLOCK(16, 2)
  211. VECTOR_BLOCK(16, 1)
  212. #endif
  213. #if UNROLL_N == 8
  214. VECTOR_BLOCK(8, 8)
  215. VECTOR_BLOCK(4, 8)
  216. #endif
  217. #ifndef DOUBLE
  218. VECTOR_BLOCK(8, 4)
  219. #endif
  220. VECTOR_BLOCK(8, 2)
  221. VECTOR_BLOCK(8, 1)
  222. VECTOR_BLOCK(4, 4)
  223. VECTOR_BLOCK(4, 2)
  224. VECTOR_BLOCK(4, 1)
  225. /**
  226. * Calculate for a row-block in C_i of size ROWSxCOLS using scalar operations.
  227. * Simple implementation for smaller block sizes
  228. *
  229. * @param[in] A Pointer current block of input matrix A.
  230. * @param[in] k Number of columns in A.
  231. * @param[in] B Pointer current block of input matrix B.
  232. * @param[inout] C Pointer current block of output matrix C.
  233. * @param[in] ldc Offset between elements in adjacent columns in C.
  234. * @param[in] alpha Scalar factor.
  235. */
  236. #define SCALAR_BLOCK(ROWS, COLS) \
  237. static inline void GEBP_block_##ROWS##_##COLS( \
  238. FLOAT const *restrict A, BLASLONG k, FLOAT const *restrict B, \
  239. FLOAT *restrict C, BLASLONG ldc, FLOAT alpha) { \
  240. FLOAT Caux[ROWS][COLS] __attribute__((aligned(16))); \
  241. \
  242. /* \
  243. * Peel off first iteration (i.e., column of A) for \
  244. * initializing Caux \
  245. */ \
  246. for (BLASLONG i = 0; i < ROWS; i++) \
  247. for (BLASLONG j = 0; j < COLS; j++) Caux[i][j] = A[i] * B[j]; \
  248. \
  249. for (BLASLONG kk = 1; kk < k; kk++) \
  250. for (BLASLONG i = 0; i < ROWS; i++) \
  251. for (BLASLONG j = 0; j < COLS; j++) \
  252. Caux[i][j] += A[i + kk * ROWS] * B[j + kk * COLS]; \
  253. \
  254. for (BLASLONG i = 0; i < ROWS; i++) \
  255. for (BLASLONG j = 0; j < COLS; j++) \
  256. if (trmm) { \
  257. C[i + j * ldc] = alpha * Caux[i][j]; \
  258. } else { \
  259. C[i + j * ldc] += alpha * Caux[i][j]; \
  260. } \
  261. }
  262. #ifdef DOUBLE
  263. VECTOR_BLOCK(2, 4)
  264. VECTOR_BLOCK(2, 2)
  265. VECTOR_BLOCK(2, 1)
  266. #else
  267. SCALAR_BLOCK(2, 4)
  268. SCALAR_BLOCK(2, 2)
  269. SCALAR_BLOCK(2, 1)
  270. #endif
  271. SCALAR_BLOCK(1, 4)
  272. SCALAR_BLOCK(1, 2)
  273. SCALAR_BLOCK(1, 1)
  274. /**
  275. * Calculate a row-block that fits 4x4 vector registers using a loop
  276. * unrolled-by-2 with explicit interleaving to better overlap loads and
  277. * computation.
  278. * This function fits 16x4 blocks for SGEMM and 8x4 blocks for DGEMM.
  279. */
  280. #ifdef DOUBLE
  281. static inline void GEBP_block_8_4(
  282. #else // float
  283. static inline void GEBP_block_16_4(
  284. #endif
  285. FLOAT const *restrict A, BLASLONG bk, FLOAT const *restrict B,
  286. FLOAT *restrict C, BLASLONG ldc, FLOAT alpha) {
  287. #define VEC_ROWS 4
  288. #define VEC_COLS 4
  289. #define ROWS VEC_ROWS * VLEN_FLOATS
  290. #define COLS (VEC_COLS)
  291. /*
  292. * Hold intermediate results in vector registers.
  293. * Since we need to force the compiler's hand in places, we need to use
  294. * individual variables in contrast to the generic implementation's
  295. * arrays.
  296. */
  297. #define INIT_ROW_OF_C(ROW) \
  298. vector_float A##ROW = vec_load_hinted(A + ROW * VLEN_FLOATS); \
  299. vector_float C_##ROW##_0 = A##ROW * B[0]; \
  300. vector_float C_##ROW##_1 = A##ROW * B[1]; \
  301. vector_float C_##ROW##_2 = A##ROW * B[2]; \
  302. vector_float C_##ROW##_3 = A##ROW * B[3];
  303. INIT_ROW_OF_C(0)
  304. INIT_ROW_OF_C(1)
  305. INIT_ROW_OF_C(2)
  306. INIT_ROW_OF_C(3)
  307. #undef INIT_ROW_OF_C
  308. if (bk > 1) {
  309. BLASLONG k = 1;
  310. vector_float Ak[VEC_ROWS], Aknext[VEC_ROWS];
  311. vector_float Bk[VEC_COLS], Bknext[VEC_COLS];
  312. /*
  313. * Note that in several places, we enforce an instruction
  314. * sequence that we identified empirically by utilizing dummy
  315. * asm statements.
  316. */
  317. for (BLASLONG j = 0; j < VEC_COLS; j++)
  318. Bk[j] = vec_splats(B[j + k * COLS]);
  319. asm("");
  320. for (BLASLONG i = 0; i < VEC_ROWS; i++)
  321. Ak[i] = vec_load_hinted(A + i * VLEN_FLOATS + k * ROWS);
  322. for (; k < (bk - 2); k += 2) {
  323. /*
  324. * Load inputs for (k+1) into registers.
  325. * Loading from B first is advantageous.
  326. */
  327. for (BLASLONG j = 0; j < VEC_COLS; j++)
  328. Bknext[j] = vec_splats(B[j + (k + 1) * COLS]);
  329. asm("");
  330. for (BLASLONG i = 0; i < VEC_ROWS; i++)
  331. Aknext[i] = vec_load_hinted(A + i * VLEN_FLOATS +
  332. (k + 1) * ROWS);
  333. /*
  334. * To achieve better instruction-level parallelism,
  335. * make sure to first load input data for (k+1) before
  336. * initiating compute for k. We enforce that ordering
  337. * with a pseudo asm statement.
  338. * Note that we need to massage this particular "barrier"
  339. * depending on the gcc version.
  340. */
  341. #if __GNUC__ > 7 || defined(__clang__)
  342. #define BARRIER_READ_BEFORE_COMPUTE(SUFFIX) \
  343. do { \
  344. asm("" \
  345. : "+v"(C_0_0), "+v"(C_0_1), "+v"(C_0_2), "+v"(C_0_3), "+v"(C_1_0), \
  346. "+v"(C_1_1), "+v"(C_1_2), "+v"(C_1_3) \
  347. : "v"(B##SUFFIX[0]), "v"(B##SUFFIX[1]), "v"(B##SUFFIX[2]), \
  348. "v"(B##SUFFIX[3]), "v"(A##SUFFIX[0]), "v"(A##SUFFIX[1]), \
  349. "v"(A##SUFFIX[2]), "v"(A##SUFFIX[3])); \
  350. asm("" \
  351. : "+v"(C_2_0), "+v"(C_2_1), "+v"(C_2_2), "+v"(C_2_3), "+v"(C_3_0), \
  352. "+v"(C_3_1), "+v"(C_3_2), "+v"(C_3_3) \
  353. : "v"(B##SUFFIX[0]), "v"(B##SUFFIX[1]), "v"(B##SUFFIX[2]), \
  354. "v"(B##SUFFIX[3]), "v"(A##SUFFIX[0]), "v"(A##SUFFIX[1]), \
  355. "v"(A##SUFFIX[2]), "v"(A##SUFFIX[3])); \
  356. } while (0)
  357. #else // __GNUC__ <= 7
  358. #define BARRIER_READ_BEFORE_COMPUTE(SUFFIX) \
  359. do { \
  360. asm(""); \
  361. } while (0)
  362. #endif
  363. BARRIER_READ_BEFORE_COMPUTE(knext);
  364. /* Compute for (k) */
  365. C_0_0 += Ak[0] * Bk[0];
  366. C_1_0 += Ak[1] * Bk[0];
  367. C_2_0 += Ak[2] * Bk[0];
  368. C_3_0 += Ak[3] * Bk[0];
  369. C_0_1 += Ak[0] * Bk[1];
  370. C_1_1 += Ak[1] * Bk[1];
  371. C_2_1 += Ak[2] * Bk[1];
  372. C_3_1 += Ak[3] * Bk[1];
  373. C_0_2 += Ak[0] * Bk[2];
  374. C_1_2 += Ak[1] * Bk[2];
  375. C_2_2 += Ak[2] * Bk[2];
  376. C_3_2 += Ak[3] * Bk[2];
  377. C_0_3 += Ak[0] * Bk[3];
  378. C_1_3 += Ak[1] * Bk[3];
  379. C_2_3 += Ak[2] * Bk[3];
  380. C_3_3 += Ak[3] * Bk[3];
  381. asm("");
  382. /*
  383. * Load inputs for (k+2) into registers.
  384. * First load from B.
  385. */
  386. for (BLASLONG j = 0; j < VEC_COLS; j++)
  387. Bk[j] = vec_splats(B[j + (k + 2) * COLS]);
  388. asm("");
  389. for (BLASLONG i = 0; i < VEC_ROWS; i++)
  390. Ak[i] = vec_load_hinted(A + i * VLEN_FLOATS + (k + 2) * ROWS);
  391. /*
  392. * As above, make sure to first schedule the loads for (k+2)
  393. * before compute for (k+1).
  394. */
  395. BARRIER_READ_BEFORE_COMPUTE(k);
  396. /* Compute on (k+1) */
  397. C_0_0 += Aknext[0] * Bknext[0];
  398. C_1_0 += Aknext[1] * Bknext[0];
  399. C_2_0 += Aknext[2] * Bknext[0];
  400. C_3_0 += Aknext[3] * Bknext[0];
  401. C_0_1 += Aknext[0] * Bknext[1];
  402. C_1_1 += Aknext[1] * Bknext[1];
  403. C_2_1 += Aknext[2] * Bknext[1];
  404. C_3_1 += Aknext[3] * Bknext[1];
  405. C_0_2 += Aknext[0] * Bknext[2];
  406. C_1_2 += Aknext[1] * Bknext[2];
  407. C_2_2 += Aknext[2] * Bknext[2];
  408. C_3_2 += Aknext[3] * Bknext[2];
  409. C_0_3 += Aknext[0] * Bknext[3];
  410. C_1_3 += Aknext[1] * Bknext[3];
  411. C_2_3 += Aknext[2] * Bknext[3];
  412. C_3_3 += Aknext[3] * Bknext[3];
  413. }
  414. /* Wrapup remaining k's */
  415. for (; k < bk; k++) {
  416. vector_float Ak;
  417. #define COMPUTE_WRAPUP_ROW(ROW) \
  418. Ak = vec_load_hinted(A + ROW * VLEN_FLOATS + k * ROWS); \
  419. C_##ROW##_0 += Ak * B[0 + k * COLS]; \
  420. C_##ROW##_1 += Ak * B[1 + k * COLS]; \
  421. C_##ROW##_2 += Ak * B[2 + k * COLS]; \
  422. C_##ROW##_3 += Ak * B[3 + k * COLS];
  423. COMPUTE_WRAPUP_ROW(0)
  424. COMPUTE_WRAPUP_ROW(1)
  425. COMPUTE_WRAPUP_ROW(2)
  426. COMPUTE_WRAPUP_ROW(3)
  427. #undef COMPUTE_WRAPUP_ROW
  428. }
  429. }
  430. /*
  431. * Unpack row-block of C_aux into outer C_i, multiply by
  432. * alpha and add up (or assign for TRMM).
  433. */
  434. #define WRITE_BACK_C(ROW, COL) \
  435. do { \
  436. vector_float *Cij = \
  437. (vector_float *)(C + ROW * VLEN_FLOATS + COL * ldc); \
  438. if (trmm) { \
  439. *Cij = alpha * C_##ROW##_##COL; \
  440. } else { \
  441. *Cij += alpha * C_##ROW##_##COL; \
  442. } \
  443. } while (0)
  444. WRITE_BACK_C(0, 0); WRITE_BACK_C(0, 1); WRITE_BACK_C(0, 2); WRITE_BACK_C(0, 3);
  445. WRITE_BACK_C(1, 0); WRITE_BACK_C(1, 1); WRITE_BACK_C(1, 2); WRITE_BACK_C(1, 3);
  446. WRITE_BACK_C(2, 0); WRITE_BACK_C(2, 1); WRITE_BACK_C(2, 2); WRITE_BACK_C(2, 3);
  447. WRITE_BACK_C(3, 0); WRITE_BACK_C(3, 1); WRITE_BACK_C(3, 2); WRITE_BACK_C(3, 3);
  448. #undef WRITE_BACK_C
  449. #undef ROWS
  450. #undef VEC_ROWS
  451. #undef COLS
  452. #undef VEC_COLS
  453. #undef BARRIER_READ_BEFORE_COMPUTE
  454. }
  455. /**
  456. * Handle calculation for row blocks in C_i of any size by dispatching into
  457. * macro-defined (inline) functions or by deferring to a simple generic
  458. * implementation. Note that the compiler can remove this awkward-looking
  459. * dispatching code while inlineing.
  460. *
  461. * @param[in] m Number of rows in block C_i.
  462. * @param[in] n Number of columns in block C_i.
  463. * @param[in] first_row Index of first row of the block C_i (relative to C).
  464. * @param[in] A Pointer to input matrix A (note: all of it).
  465. * @param[in] k Number of columns in A and rows in B.
  466. * @param[in] B Pointer to current column block (panel) of input matrix B.
  467. * @param[inout] C Pointer to current column block (panel) of output matrix C.
  468. * @param[in] ldc Offset between elements in adjacent columns in C.
  469. * @param[in] alpha Scalar factor.
  470. * @param[in] offset Number of columns of A and rows of B to skip (for triangular matrices).
  471. * @param[in] off Running offset for handling triangular matrices.
  472. */
  473. static inline void GEBP_block(BLASLONG m, BLASLONG n,
  474. BLASLONG first_row,
  475. const FLOAT * restrict A, BLASLONG k,
  476. const FLOAT * restrict B,
  477. FLOAT *restrict C, BLASLONG ldc,
  478. FLOAT alpha,
  479. BLASLONG offset, BLASLONG off)
  480. {
  481. if (trmm && left)
  482. off = offset + first_row;
  483. A += first_row * k;
  484. C += first_row;
  485. if (trmm) {
  486. if (backwards) {
  487. A += off * m;
  488. B += off * n;
  489. k -= off;
  490. } else {
  491. if (left) {
  492. k = off + m;
  493. } else {
  494. k = off + n;
  495. }
  496. }
  497. }
  498. /* Dispatch into the implementation for each block size: */
  499. #define BLOCK(bm, bn) \
  500. if (m == bm && n == bn) { \
  501. GEBP_block_##bm##_##bn(A, k, B, C, ldc, alpha); \
  502. return; \
  503. }
  504. #if UNROLL_M == 16
  505. BLOCK(16, 4); BLOCK(16, 2); BLOCK(16, 1);
  506. #endif
  507. #if UNROLL_N == 8
  508. BLOCK(8, 8); BLOCK(4, 8);
  509. #endif
  510. BLOCK(8, 4); BLOCK(8, 2); BLOCK(8, 1);
  511. BLOCK(4, 4); BLOCK(4, 2); BLOCK(4, 1);
  512. BLOCK(2, 4); BLOCK(2, 2); BLOCK(2, 1);
  513. BLOCK(1, 4); BLOCK(1, 2); BLOCK(1, 1);
  514. #undef BLOCK
  515. }
  516. /**
  517. * Handle a column block (panel) of C and B while calculating C += alpha(A * B).
  518. *
  519. * @param[in] num_cols Number of columns in the block (in C and B).
  520. * @param[in] first_col First column of the current block (in C and B).
  521. * @param[in] A Pointer to input matrix A.
  522. * @param[in] bk Number of columns in A and rows in B.
  523. * @param[in] B Pointer to input matrix B (note: all of it).
  524. * @param[in] bm Number of rows in C and A.
  525. * @param[inout] C Pointer to output matrix C (note: all of it).
  526. * @param[in] ldc Offset between elements in adjacent columns in C.
  527. * @param[in] alpha Scalar factor.
  528. * @param[in] offset Number of columns of A and rows of B to skip (for triangular matrices).
  529. */
  530. static inline void GEBP_column_block(BLASLONG num_cols, BLASLONG first_col,
  531. const FLOAT *restrict A, BLASLONG bk,
  532. const FLOAT *restrict B, BLASLONG bm,
  533. FLOAT *restrict C, BLASLONG ldc,
  534. FLOAT alpha,
  535. BLASLONG const offset) {
  536. FLOAT *restrict C_i = C + first_col * ldc;
  537. /*
  538. * B is in column-order with n_r packed row elements, which does
  539. * not matter -- we always move in full such blocks of
  540. * column*pack
  541. */
  542. const FLOAT *restrict B_i = B + first_col * bk;
  543. BLASLONG off = 0;
  544. if (trmm) {
  545. if (left) {
  546. off = offset;
  547. } else {
  548. off = -offset + first_col;
  549. }
  550. }
  551. /*
  552. * Calculate C_aux := A * B_j
  553. * then unpack C_i += alpha * C_aux.
  554. *
  555. * For that purpose, further partition C_aux and A into blocks
  556. * of m_r (unroll_m) rows, or powers-of-2 if smaller.
  557. */
  558. BLASLONG row = 0;
  559. for (BLASLONG block_size = unroll_m; block_size > 0; block_size /= 2)
  560. for (; bm - row >= block_size; row += block_size)
  561. GEBP_block(block_size, num_cols, row, A, bk, B_i, C_i,
  562. ldc, alpha, offset, off);
  563. }
  564. /**
  565. * Inner kernel for matrix-matrix multiplication. C += alpha (A * B)
  566. * where C is an m-by-n matrix, A is m-by-k and B is k-by-n. Note that A, B, and
  567. * C are pointers to submatrices of the actual matrices.
  568. *
  569. * For triangular matrix multiplication, calculate B := alpha (A * B) where A
  570. * or B can be triangular (in case of A, the macro LEFT will be defined).
  571. *
  572. * @param[in] bm Number of rows in C and A.
  573. * @param[in] bn Number of columns in C and B.
  574. * @param[in] bk Number of columns in A and rows in B.
  575. * @param[in] alpha Scalar factor.
  576. * @param[in] ba Pointer to input matrix A.
  577. * @param[in] bb Pointer to input matrix B.
  578. * @param[inout] C Pointer to output matrix C.
  579. * @param[in] ldc Offset between elements in adjacent columns in C.
  580. * @param[in] offset Number of columns of A and rows of B to skip (for triangular matrices).
  581. * @returns 0 on success.
  582. */
  583. int CNAME(BLASLONG bm, BLASLONG bn, BLASLONG bk, FLOAT alpha,
  584. FLOAT *restrict ba, FLOAT *restrict bb,
  585. FLOAT *restrict C, BLASLONG ldc
  586. #ifdef TRMMKERNEL
  587. , BLASLONG offset
  588. #endif
  589. )
  590. {
  591. if ( (bm == 0) || (bn == 0) || (bk == 0) || (alpha == ZERO))
  592. return 0;
  593. /*
  594. * interface code allocates buffers for ba and bb at page
  595. * granularity (i.e., using mmap(MAP_ANONYMOUS), so enable the compiler
  596. * to make use of the fact in vector load operations.
  597. */
  598. ba = __builtin_assume_aligned(ba, 16);
  599. bb = __builtin_assume_aligned(bb, 16);
  600. /*
  601. * Use offset and off even when compiled as SGEMMKERNEL to simplify
  602. * function signatures and function calls.
  603. */
  604. #ifndef TRMMKERNEL
  605. BLASLONG const offset = 0;
  606. #endif
  607. /*
  608. * Partition B and C into blocks of n_r (unroll_n) columns, called B_i
  609. * and C_i. For each partition, calculate C_i += alpha * (A * B_j).
  610. *
  611. * For remaining columns that do not fill up a block of n_r, iteratively
  612. * use smaller block sizes of powers of 2.
  613. */
  614. BLASLONG col = 0;
  615. for (BLASLONG block_size = unroll_n; block_size > 0; block_size /= 2)
  616. for (; bn - col >= block_size; col += block_size)
  617. GEBP_column_block(block_size, col, ba, bk, bb, bm, C, ldc, alpha, offset);
  618. return 0;
  619. }