|
| 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 <vecintrin.h> |
| 34 | + |
| 35 | +#include <stdbool.h> |
| 36 | +#include <stdio.h> |
| 37 | +#include <stdlib.h> |
| 38 | + |
| 39 | +#ifdef COMPLEX |
| 40 | +#error "Handling for complex numbers is not supported in this kernel" |
| 41 | +#endif |
| 42 | + |
| 43 | +#ifdef DOUBLE |
| 44 | +#define UNROLL_M DGEMM_DEFAULT_UNROLL_M |
| 45 | +#define UNROLL_N DGEMM_DEFAULT_UNROLL_N |
| 46 | +#else |
| 47 | +#define UNROLL_M SGEMM_DEFAULT_UNROLL_M |
| 48 | +#define UNROLL_N SGEMM_DEFAULT_UNROLL_N |
| 49 | +#endif |
| 50 | + |
| 51 | +static const size_t unroll_m = UNROLL_M; |
| 52 | +static const size_t unroll_n = UNROLL_N; |
| 53 | + |
| 54 | +/* |
| 55 | + * Background: |
| 56 | + * |
| 57 | + * The algorithm of GotoBLAS / OpenBLAS breaks down the matrix multiplication |
| 58 | + * problem by splitting all matrices into partitions multiple times, so that the |
| 59 | + * submatrices fit into the L1 or L2 caches. As a result, each multiplication of |
| 60 | + * submatrices can stream data fast from L1 and L2 caches. Inbetween, it copies |
| 61 | + * and rearranges the submatrices to enable contiguous memory accesses to |
| 62 | + * improve locality in both caches and TLBs. |
| 63 | + * |
| 64 | + * At the heart of the algorithm is this kernel, which multiplies, a "Block |
| 65 | + * matrix" A (small dimensions) with a "Panel matrix" B (number of rows is |
| 66 | + * small) and adds the result into a "Panel matrix" C; GotoBLAS calls this |
| 67 | + * operation GEBP. This kernel further partitions GEBP twice, such that (1) |
| 68 | + * submatrices of C and B fit into the L1 caches (GEBP_column_block) and (2) a |
| 69 | + * block of C fits into the registers, while multiplying panels from A and B |
| 70 | + * streamed from the L2 and L1 cache, respectively (GEBP_block). |
| 71 | + * |
| 72 | + * |
| 73 | + * Algorithm GEBP(A, B, C, m, n, k, alpha): |
| 74 | + * |
| 75 | + * The problem is calculating C += alpha * (A * B) |
| 76 | + * C is an m x n matrix, A is an m x k matrix, B is an k x n matrix. |
| 77 | + * |
| 78 | + * - C is in column-major-order, with an offset of ldc to the element in the |
| 79 | + * next column (same row). |
| 80 | + * - A is in row-major-order yet stores SGEMM_UNROLL_M elements of each column |
| 81 | + * contiguously while walking along rows. |
| 82 | + * - B is in column-major-order but packs SGEMM_UNROLL_N elements of a row |
| 83 | + * contiguously. |
| 84 | + * If the numbers of rows and columns are not multiples of SGEMM_UNROLL_M or |
| 85 | + * SGEMM_UNROLL_N, the remaining elements are arranged in blocks with power-of-2 |
| 86 | + * dimensions (e.g., 5 remaining columns would be in a block-of-4 and a |
| 87 | + * block-of-1). |
| 88 | + * |
| 89 | + * Note that packing A and B into that form is taken care of by the caller in |
| 90 | + * driver/level3/level3.c (actually done by "copy kernels"). |
| 91 | + * |
| 92 | + * Steps: |
| 93 | + * - Partition C and B into blocks of n_r (SGEMM_UNROLL_N) columns, C_j and B_j. |
| 94 | + * Now, B_j should fit into the L1 cache. |
| 95 | + * - For each partition, calculate C_j += alpha * (A * B_j) by |
| 96 | + * (1) Calculate C_aux := A * B_j (see below) |
| 97 | + * (2) unpack C_j = C_j + alpha * C_aux |
| 98 | + * |
| 99 | + * |
| 100 | + * Algorithm for Calculating C_aux: |
| 101 | + * |
| 102 | + * - Further partition C_aux and A into groups of m_r (SGEMM_UNROLL_M) rows, |
| 103 | + * such that the m_r x n_r-submatrix of C_aux can be held in registers. Each |
| 104 | + * submatrix of C_aux can be calculated independently, and the registers are |
| 105 | + * added back into C_j. |
| 106 | + * |
| 107 | + * - For each row-block of C_aux: |
| 108 | + * (uses a row block of A and full B_j) |
| 109 | + * - stream over all columns of A, multiply with elements from B and |
| 110 | + * accumulate in registers. (use different inner-kernels to exploit |
| 111 | + * vectorization for varying block sizes) |
| 112 | + * - add alpha * row block of C_aux back into C_j. |
| 113 | + * |
| 114 | + * Reference: |
| 115 | + * |
| 116 | + * The summary above is based on staring at various kernel implementations and: |
| 117 | + * K. Goto and R. A. Van de Geijn, Anatomy of High-Performance Matrix |
| 118 | + * Multiplication, in ACM Transactions of Mathematical Software, Vol. 34, No. |
| 119 | + * 3, May 2008. |
| 120 | + */ |
| 121 | + |
| 122 | +#define VLEN_BYTES 16 |
| 123 | +#define VLEN_FLOATS (VLEN_BYTES / sizeof(FLOAT)) |
| 124 | + |
| 125 | +typedef FLOAT vector_float __attribute__ ((vector_size (16))); |
| 126 | + |
| 127 | +/** |
| 128 | + * Calculate for a row-block in C_i of size ROWSxCOLS using vector intrinsics. |
| 129 | + * |
| 130 | + * @param[in] A Pointer current block of input matrix A. |
| 131 | + * @param[in] k Number of columns in A. |
| 132 | + * @param[in] B Pointer current block of input matrix B. |
| 133 | + * @param[inout] C Pointer current block of output matrix C. |
| 134 | + * @param[in] ldc Offset between elements in adjacent columns in C. |
| 135 | + * @param[in] alpha Scalar factor. |
| 136 | + */ |
| 137 | +#define VECTOR_BLOCK(ROWS, COLS) \ |
| 138 | + static inline void GEBP_block_##ROWS##_##COLS( \ |
| 139 | + FLOAT const *restrict A, BLASLONG bk, FLOAT const *restrict B, \ |
| 140 | + FLOAT *restrict C, BLASLONG ldc, FLOAT alpha) { \ |
| 141 | + _Static_assert( \ |
| 142 | + ROWS % VLEN_FLOATS == 0, \ |
| 143 | + "rows in block must be multiples of vector length"); \ |
| 144 | + vector_float Caux[ROWS / VLEN_FLOATS][COLS]; \ |
| 145 | + \ |
| 146 | + for (BLASLONG i = 0; i < ROWS / VLEN_FLOATS; i++) \ |
| 147 | + for (BLASLONG j = 0; j < COLS; j++) \ |
| 148 | + Caux[i][j] = vec_splats(ZERO); \ |
| 149 | + \ |
| 150 | + /* \ |
| 151 | + * Stream over the row-block of A, which is packed \ |
| 152 | + * column-by-column, multiply by coefficients in B and add up \ |
| 153 | + * into temporaries Caux (which the compiler will hold in \ |
| 154 | + * registers). Vectorization: Multiply column vectors from A \ |
| 155 | + * with scalars from B and add up in column vectors of Caux. \ |
| 156 | + * That equates to unrolling the loop over rows (in i) and \ |
| 157 | + * executing each unrolled iteration as a vector element. \ |
| 158 | + */ \ |
| 159 | + for (BLASLONG k = 0; k < bk; k++) { \ |
| 160 | + for (BLASLONG i = 0; i < ROWS / VLEN_FLOATS; i++) { \ |
| 161 | + vector_float Ak = \ |
| 162 | + *(vector_float *)(A + i * VLEN_FLOATS + \ |
| 163 | + k * ROWS); \ |
| 164 | + \ |
| 165 | + for (BLASLONG j = 0; j < COLS; j++) \ |
| 166 | + Caux[i][j] += Ak * B[j + k * COLS]; \ |
| 167 | + } \ |
| 168 | + } \ |
| 169 | + \ |
| 170 | + /* \ |
| 171 | + * Unpack row-block of C_aux into outer C_i, multiply by \ |
| 172 | + * alpha and add up. \ |
| 173 | + */ \ |
| 174 | + for (BLASLONG j = 0; j < COLS; j++) { \ |
| 175 | + for (BLASLONG i = 0; i < ROWS / VLEN_FLOATS; i++) { \ |
| 176 | + vector_float *C_ij = \ |
| 177 | + (vector_float *)(C + i * VLEN_FLOATS + \ |
| 178 | + j * ldc); \ |
| 179 | + *C_ij += alpha * Caux[i][j]; \ |
| 180 | + } \ |
| 181 | + } \ |
| 182 | + } |
| 183 | + |
| 184 | + |
| 185 | +VECTOR_BLOCK(8, 4) |
| 186 | +VECTOR_BLOCK(8, 2) |
| 187 | +VECTOR_BLOCK(8, 1) |
| 188 | +VECTOR_BLOCK(4, 4) |
| 189 | +VECTOR_BLOCK(4, 2) |
| 190 | +VECTOR_BLOCK(4, 1) |
| 191 | + |
| 192 | +#ifdef DOUBLE |
| 193 | +VECTOR_BLOCK(2, 4) |
| 194 | +VECTOR_BLOCK(2, 2) |
| 195 | +#endif |
| 196 | + |
| 197 | +/** |
| 198 | + * Handle calculation for row blocks in C_i of any size by dispatching into |
| 199 | + * macro-defined (inline) functions or by deferring to a simple generic |
| 200 | + * implementation. Note that the compiler can remove this awkward-looking |
| 201 | + * dispatching code while inlineing. |
| 202 | + * |
| 203 | + * @param[in] m Number of rows in block C_i. |
| 204 | + * @param[in] n Number of columns in block C_i. |
| 205 | + * @param[in] first_row Index of first row of the block C_i (relative to C). |
| 206 | + * @param[in] A Pointer to input matrix A (note: all of it). |
| 207 | + * @param[in] k Number of columns in A and rows in B. |
| 208 | + * @param[in] B Pointer to current column block (panel) of input matrix B. |
| 209 | + * @param[inout] C Pointer to current column block (panel) of output matrix C. |
| 210 | + * @param[in] ldc Offset between elements in adjacent columns in C. |
| 211 | + * @param[in] alpha Scalar factor. |
| 212 | + */ |
| 213 | +static inline void GEBP_block(BLASLONG m, BLASLONG n, |
| 214 | + BLASLONG first_row, |
| 215 | + const FLOAT * restrict A, BLASLONG k, |
| 216 | + const FLOAT * restrict B, |
| 217 | + FLOAT *restrict C, BLASLONG ldc, |
| 218 | + FLOAT alpha) |
| 219 | +{ |
| 220 | + A += first_row * k; |
| 221 | + C += first_row; |
| 222 | + |
| 223 | +#define BLOCK(bm, bn) \ |
| 224 | + if (m == bm && n == bn) { \ |
| 225 | + GEBP_block_##bm##_##bn(A, k, B, C, ldc, alpha); \ |
| 226 | + return; \ |
| 227 | + } |
| 228 | + |
| 229 | + BLOCK(8, 4); BLOCK(8, 2); BLOCK(8, 1); |
| 230 | + BLOCK(4, 4); BLOCK(4, 2); BLOCK(4, 1); |
| 231 | + |
| 232 | + #ifdef DOUBLE |
| 233 | + BLOCK(2, 4); |
| 234 | + BLOCK(2, 2); |
| 235 | + #endif |
| 236 | + |
| 237 | +#undef BLOCK |
| 238 | + |
| 239 | + /* simple implementation for smaller block sizes: */ |
| 240 | + FLOAT Caux[m][n] __attribute__ ((aligned (16))); |
| 241 | + |
| 242 | + /* |
| 243 | + * Peel off first iteration (i.e., column of A) for initializing Caux |
| 244 | + */ |
| 245 | + for (BLASLONG i = 0; i < m; i++) |
| 246 | + for (BLASLONG j = 0; j < n; j++) |
| 247 | + Caux[i][j] = A[i] * B[j]; |
| 248 | + |
| 249 | + for (BLASLONG kk = 1; kk < k; kk++) |
| 250 | + for (BLASLONG i = 0; i < m; i++) |
| 251 | + for (BLASLONG j = 0; j < n; j++) |
| 252 | + Caux[i][j] += A[i + kk * m] * B[j + kk * n]; |
| 253 | + |
| 254 | + for (BLASLONG i = 0; i < m; i++) |
| 255 | + for (BLASLONG j = 0; j < n; j++) |
| 256 | + C[i + j * ldc] += alpha * Caux[i][j]; |
| 257 | +} |
| 258 | + |
| 259 | +/** |
| 260 | + * Handle a column block (panel) of C and B while calculating C += alpha(A * B). |
| 261 | + * |
| 262 | + * @param[in] num_cols Number of columns in the block (in C and B). |
| 263 | + * @param[in] first_col First column of the current block (in C and B). |
| 264 | + * @param[in] A Pointer to input matrix A. |
| 265 | + * @param[in] bk Number of columns in A and rows in B. |
| 266 | + * @param[in] B Pointer to input matrix B (note: all of it). |
| 267 | + * @param[in] bm Number of rows in C and A. |
| 268 | + * @param[inout] C Pointer to output matrix C (note: all of it). |
| 269 | + * @param[in] ldc Offset between elements in adjacent columns in C. |
| 270 | + * @param[in] alpha Scalar factor. |
| 271 | + */ |
| 272 | +static inline void GEBP_column_block(BLASLONG num_cols, BLASLONG first_col, |
| 273 | + const FLOAT *restrict A, BLASLONG bk, |
| 274 | + const FLOAT *restrict B, BLASLONG bm, |
| 275 | + FLOAT *restrict C, BLASLONG ldc, |
| 276 | + FLOAT alpha) { |
| 277 | + FLOAT *restrict C_i = C + first_col * ldc; |
| 278 | + /* |
| 279 | + * B is in column-order with n_r packed row elements, which does |
| 280 | + * not matter -- we always move in full such blocks of |
| 281 | + * column*pack |
| 282 | + */ |
| 283 | + const FLOAT *restrict B_i = B + first_col * bk; |
| 284 | + |
| 285 | + /* |
| 286 | + * Calculate C_aux := A * B_j |
| 287 | + * then unpack C_i += alpha * C_aux. |
| 288 | + * |
| 289 | + * For that purpose, further partition C_aux and A into blocks |
| 290 | + * of m_r (unroll_m) rows, or powers-of-2 if smaller. |
| 291 | + */ |
| 292 | + BLASLONG row = 0; |
| 293 | + for (BLASLONG block_size = unroll_m; block_size > 0; block_size /= 2) |
| 294 | + for (; bm - row >= block_size; row += block_size) |
| 295 | + GEBP_block(block_size, num_cols, row, A, bk, B_i, C_i, |
| 296 | + ldc, alpha); |
| 297 | +} |
| 298 | + |
| 299 | +/** |
| 300 | + * Inner kernel for matrix-matrix multiplication. C += alpha (A * B) |
| 301 | + * where C is an m-by-n matrix, A is m-by-k and B is k-by-n. Note that A, B, and |
| 302 | + * C are pointers to submatrices of the actual matrices. |
| 303 | + * |
| 304 | + * @param[in] bm Number of rows in C and A. |
| 305 | + * @param[in] bn Number of columns in C and B. |
| 306 | + * @param[in] bk Number of columns in A and rows in B. |
| 307 | + * @param[in] alpha Scalar factor. |
| 308 | + * @param[in] ba Pointer to input matrix A. |
| 309 | + * @param[in] bb Pointer to input matrix B. |
| 310 | + * @param[inout] C Pointer to output matrix C. |
| 311 | + * @param[in] ldc Offset between elements in adjacent columns in C. |
| 312 | + * @returns 0 on success. |
| 313 | + */ |
| 314 | +int CNAME(BLASLONG bm, BLASLONG bn, BLASLONG bk, FLOAT alpha, |
| 315 | + FLOAT *restrict ba, FLOAT *restrict bb, |
| 316 | + FLOAT *restrict C, BLASLONG ldc) |
| 317 | +{ |
| 318 | + if ( (bm == 0) || (bn == 0) || (bk == 0) || (alpha == ZERO)) |
| 319 | + return 0; |
| 320 | + |
| 321 | + /* |
| 322 | + * interface code allocates buffers for ba and bb at page |
| 323 | + * granularity (i.e., using mmap(MAP_ANONYMOUS), so enable the compiler |
| 324 | + * to make use of the fact in vector load operations. |
| 325 | + */ |
| 326 | + ba = __builtin_assume_aligned(ba, 16); |
| 327 | + bb = __builtin_assume_aligned(bb, 16); |
| 328 | + |
| 329 | + /* |
| 330 | + * Partition B and C into blocks of n_r (unroll_n) columns, called B_i |
| 331 | + * and C_i. For each partition, calculate C_i += alpha * (A * B_j). |
| 332 | + * |
| 333 | + * For remaining columns that do not fill up a block of n_r, iteratively |
| 334 | + * use smaller block sizes of powers of 2. |
| 335 | + */ |
| 336 | + BLASLONG col = 0; |
| 337 | + for (BLASLONG block_size = unroll_n; block_size > 0; block_size /= 2) |
| 338 | + for (; bn - col >= block_size; col += block_size) |
| 339 | + GEBP_column_block(block_size, col, ba, bk, bb, bm, C, ldc, alpha); |
| 340 | + |
| 341 | + return 0; |
| 342 | +} |
0 commit comments