Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 198x 198x 198x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 3x 3x 8x 24x 24x 24x 8x 8x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 3x 3x 8x 24x 24x 24x 8x 8x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 20x 20x 80x 80x 80x 80x 20x 20x 10x 10x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 75x 51x 51x 75x 24x 24x 24x 75x 75x 75x 387x 75x 75x 387x 312x 312x 312x 387x 387x 387x 387x 4443x 387x 387x 4443x 4056x 4056x 4056x 4443x 4443x 4443x 4443x 33898x 33898x 260392x 260392x 260392x 260392x 33898x 33898x 4443x 387x 75x 51x 51x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 79x 79x 79x 79x 79x 79x 79x 79x 79x 12x 12x 67x 79x 3x 79x 3x 3x 67x 79x 6x 6x 61x 61x 61x 61x 61x 79x 35x 20x 2x 2x 2x 20x 3x 3x 3x 20x 79x 15x 2x 2x 2x 15x 3x 3x 3x 15x 51x 79x 25x 25x 79x 26x 26x 26x 79x 21x 21x 79x 30x 30x 30x 51x 51x 79x 3x 3x 3x 3x 3x | /** * @license Apache-2.0 * * Copyright (c) 2024 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* eslint-disable max-len, max-params */ 'use strict'; // MODULES // var isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major' ); var sdot = require( '@stdlib/blas/base/sdot' ).ndarray; var blockSize = require( '@stdlib/ndarray/base/unary-tiling-block-size' ); var f32 = require( '@stdlib/number/float64/base/to-float32' ); // VARIABLES // var bsize = blockSize( 'float32' ); // TODO: consider using a larger block size // FUNCTIONS // /** * Tests whether a provided string indicates to transpose a matrix. * * @private * @param {string} str - input string * @returns {boolean} boolean indicating whether to transpose a matrix * * @example * var bool = isTransposed( 'transpose' ); * // returns true * * @example * var bool = isTransposed( 'conjugate-transpose' ); * // returns true * * @example * var bool = isTransposed( 'no-transpose' ); * // returns false */ function isTransposed( str ) { // TODO: consider moving to a separate helper utility package return ( str !== 'no-transpose' ); } /** * Fills a matrix with zeros. * * @private * @param {NonNegativeInteger} M - number of rows * @param {NonNegativeInteger} N - number of columns * @param {Float32Array} X - matrix to fill * @param {integer} strideX1 - stride of the first dimension of `X` * @param {integer} strideX2 - stride of the second dimension of `X` * @param {NonNegativeInteger} offsetX - starting index for `X` * @returns {Float32Array} input matrix * * @example * var Float32Array = require( '@stdlib/array/float32' ); * * var X = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); * * zeros( 2, 3, X, 3, 1, 0 ); * // X => <Float32Array>[ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] * * @example * var Float32Array = require( '@stdlib/array/float32' ); * * var X = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); * * zeros( 2, 3, X, 1, 2, 0 ); * // X => <Float32Array>[ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] */ function zeros( M, N, X, strideX1, strideX2, offsetX ) { // TODO: consider moving to a separate package var dx0; var dx1; var S0; var S1; var i0; var i1; var ix; if ( isRowMajor( [ strideX1, strideX2 ] ) ) { // For row-major matrices, the last dimension has the fastest changing index... S0 = N; S1 = M; dx0 = strideX2; // offset increment for innermost loop dx1 = strideX1 - ( S0*strideX2 ); // offset increment for outermost loop } else { // column-major // For column-major matrices, the first dimension has the fastest changing index... S0 = M; S1 = N; dx0 = strideX1; // offset increment for innermost loop dx1 = strideX2 - ( S0*strideX1 ); // offset increment for outermost loop } ix = offsetX; for ( i1 = 0; i1 < S1; i1++ ) { for ( i0 = 0; i0 < S0; i0++ ) { X[ ix ] = 0.0; ix += dx0; } ix += dx1; } return X; } /** * Scales each element in a matrix by a scalar `β`. * * @private * @param {NonNegativeInteger} M - number of rows * @param {NonNegativeInteger} N - number of columns * @param {number} beta - scalar * @param {Float32Array} X - matrix to fill * @param {integer} strideX1 - stride of the first dimension of `X` * @param {integer} strideX2 - stride of the second dimension of `X` * @param {NonNegativeInteger} offsetX - starting index for `X` * @returns {Float32Array} input matrix * * @example * var Float32Array = require( '@stdlib/array/float32' ); * * var X = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); * * scal( 2, 3, 5.0, X, 3, 1, 0 ); * // X => <Float32Array>[ 5.0, 10.0, 15.0, 20.0, 25.0, 30.0 ] * * @example * var Float32Array = require( '@stdlib/array/float32' ); * * var X = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); * * scal( 2, 3, 5.0, X, 1, 2, 0 ); * // X => <Float32Array>[ 5.0, 10.0, 15.0, 20.0, 25.0, 30.0 ] */ function scal( M, N, beta, X, strideX1, strideX2, offsetX ) { // TODO: consider moving to a separate package var dx0; var dx1; var S0; var S1; var i0; var i1; var ix; if ( isRowMajor( [ strideX1, strideX2 ] ) ) { // For row-major matrices, the last dimension has the fastest changing index... S0 = N; S1 = M; dx0 = strideX2; // offset increment for innermost loop dx1 = strideX1 - ( S0*strideX2 ); // offset increment for outermost loop } else { // column-major // For column-major matrices, the first dimension has the fastest changing index... S0 = M; S1 = N; dx0 = strideX1; // offset increment for innermost loop dx1 = strideX2 - ( S0*strideX1 ); // offset increment for outermost loop } ix = offsetX; for ( i1 = 0; i1 < S1; i1++ ) { for ( i0 = 0; i0 < S0; i0++ ) { X[ ix ] *= beta; ix += dx0; } ix += dx1; } return X; } /** * Performs matrix multiplication using a naive algorithm which is cache-optimal when `A` is row-major and `B` is column-major. * * @private * @param {NonNegativeInteger} M - number of rows in the matrix `op(A)` and in the matrix `C` * @param {NonNegativeInteger} N - number of columns in the matrix `op(B)` and in the matrix `C` * @param {NonNegativeInteger} K - number of columns in the matrix `op(A)` and number of rows in the matrix `op(B)` * @param {number} alpha - scalar constant * @param {Float32Array} A - first matrix * @param {integer} strideA1 - stride of the first dimension of `A` * @param {integer} strideA2 - stride of the second dimension of `A` * @param {NonNegativeInteger} offsetA - starting index for `A` * @param {Float32Array} B - second matrix * @param {integer} strideB1 - stride of the first dimension of `B` * @param {integer} strideB2 - stride of the second dimension of `B` * @param {NonNegativeInteger} offsetB - starting index for `B` * @param {Float32Array} C - third matrix * @param {integer} strideC1 - stride of the first dimension of `C` * @param {integer} strideC2 - stride of the second dimension of `C` * @param {NonNegativeInteger} offsetC - starting index for `C` * @returns {Float32Array} `C` * * @example * var Float32Array = require( '@stdlib/array/float32' ); * * var A = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); * var B = new Float32Array( [ 1.0, 1.0, 0.0, 1.0 ] ); * var C = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); * * naive( 2, 2, 2, 1.0, A, 2, 1, 0, B, 2, 1, 0, C, 2, 1, 0 ); * // C => <Float32Array>[ 2.0, 5.0, 6.0, 11.0 ] */ function naive( M, N, K, alpha, A, strideA1, strideA2, offsetA, B, strideB1, strideB2, offsetB, C, strideC1, strideC2, offsetC ) { var da0; var db0; var dc0; var dc1; var S0; var S1; var i0; var i1; var ia; var ib; var ic; // Note on variable naming convention: S#, da#, db#, dc#, i# where # corresponds to the loop number, with `0` being the innermost loop... S0 = N; S1 = M; da0 = strideA2; db0 = strideB1; dc0 = strideC2; // offset increment for innermost loop dc1 = strideC1 - ( S0*strideC2 ); // offset increment for outermost loop ic = offsetC; for ( i1 = 0; i1 < S1; i1++ ) { ia = offsetA + ( i1*strideA1 ); for ( i0 = 0; i0 < S0; i0++ ) { ib = offsetB + ( i0*strideB2 ); C[ ic ] += f32( alpha * sdot( K, A, da0, ia, B, db0, ib ) ); ic += dc0; } ic += dc1; } return C; } /** * Performs matrix multiplication using loop tiling. * * @private * @param {NonNegativeInteger} M - number of rows in the matrix `op(A)` and in the matrix `C` * @param {NonNegativeInteger} N - number of columns in the matrix `op(B)` and in the matrix `C` * @param {NonNegativeInteger} K - number of columns in the matrix `op(A)` and number of rows in the matrix `op(B)` * @param {number} alpha - scalar constant * @param {Float32Array} A - first matrix * @param {integer} strideA1 - stride of the first dimension of `A` * @param {integer} strideA2 - stride of the second dimension of `A` * @param {NonNegativeInteger} offsetA - starting index for `A` * @param {Float32Array} B - second matrix * @param {integer} strideB1 - stride of the first dimension of `B` * @param {integer} strideB2 - stride of the second dimension of `B` * @param {NonNegativeInteger} offsetB - starting index for `B` * @param {Float32Array} C - third matrix * @param {integer} strideC1 - stride of the first dimension of `C` * @param {integer} strideC2 - stride of the second dimension of `C` * @param {NonNegativeInteger} offsetC - starting index for `C` * @returns {Float32Array} `C` * * @example * var Float32Array = require( '@stdlib/array/float32' ); * * var A = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); * var B = new Float32Array( [ 1.0, 1.0, 0.0, 1.0 ] ); * var C = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); * * blocked( 2, 2, 2, 1.0, A, 2, 1, 0, B, 2, 1, 0, C, 2, 1, 0 ); * // C => <Float32Array>[ 2.0, 5.0, 6.0, 11.0 ] */ function blocked( M, N, K, alpha, A, strideA1, strideA2, offsetA, B, strideB1, strideB2, offsetB, C, strideC1, strideC2, offsetC ) { var da0; var db0; var dc0; var dc1; var oa1; var ob0; var oc0; var oc1; var S0; var S1; var s0; var s1; var sk; var i0; var i1; var j0; var j1; var ia; var ib; var ic; var oa; var ob; var k; // Note on variable naming convention: S#, da#, db#, dc#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop... S0 = N; S1 = M; // Define increments for the innermost loop: da0 = strideA2; db0 = strideB1; dc0 = strideC2; // Iterate over blocks... for ( j1 = S1; j1 > 0; ) { if ( j1 < bsize ) { s1 = j1; j1 = 0; } else { s1 = bsize; j1 -= bsize; } oa1 = offsetA + ( j1*strideA1 ); oc1 = offsetC + ( j1*strideC1 ); for ( j0 = S0; j0 > 0; ) { if ( j0 < bsize ) { s0 = j0; j0 = 0; } else { s0 = bsize; j0 -= bsize; } ob0 = offsetB + ( j0*strideB2 ); oc0 = oc1 + ( j0*strideC2 ); // index offset for `C` for the current block dc1 = strideC1 - ( s0*strideC2 ); // loop offset increment for `C` for ( k = K; k > 0; ) { if ( k < bsize ) { sk = k; k = 0; } else { sk = bsize; k -= bsize; } oa = oa1 + ( k*strideA2 ); ob = ob0 + ( k*strideB1 ); ic = oc0; for ( i1 = 0; i1 < s1; i1++ ) { ia = oa + ( i1*strideA1 ); for ( i0 = 0; i0 < s0; i0++ ) { ib = ob + ( i0*strideB2 ); C[ ic ] += f32( alpha * sdot( sk, A, da0, ia, B, db0, ib ) ); ic += dc0; } ic += dc1; } } } } return C; } // MAIN // /** * Performs the matrix-matrix operation `C = α*op(A)*op(B) + β*C` where `op(X)` is either `op(X) = X` or `op(X) = X^T`, `α` and `β` are scalars, `A`, `B`, and `C` are matrices, with `op(A)` an `M` by `K` matrix, `op(B)` a `K` by `N` matrix, and `C` an `M` by `N` matrix. * * @private * @param {string} transA - specifies whether `A` should be transposed, conjugate-transposed, or not transposed * @param {string} transB - specifies whether `B` should be transposed, conjugate-transposed, or not transposed * @param {NonNegativeInteger} M - number of rows in the matrix `op(A)` and in the matrix `C` * @param {NonNegativeInteger} N - number of columns in the matrix `op(B)` and in the matrix `C` * @param {NonNegativeInteger} K - number of columns in the matrix `op(A)` and number of rows in the matrix `op(B)` * @param {number} alpha - scalar constant * @param {Float32Array} A - first matrix * @param {integer} strideA1 - stride of the first dimension of `A` * @param {integer} strideA2 - stride of the second dimension of `A` * @param {NonNegativeInteger} offsetA - starting index for `A` * @param {Float32Array} B - second matrix * @param {integer} strideB1 - stride of the first dimension of `B` * @param {integer} strideB2 - stride of the second dimension of `B` * @param {NonNegativeInteger} offsetB - starting index for `B` * @param {number} beta - scalar constant * @param {Float32Array} C - third matrix * @param {integer} strideC1 - stride of the first dimension of `C` * @param {integer} strideC2 - stride of the second dimension of `C` * @param {NonNegativeInteger} offsetC - starting index for `C` * @returns {Float32Array} `C` * * @example * var Float32Array = require( '@stdlib/array/float32' ); * * var A = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); * var B = new Float32Array( [ 1.0, 1.0, 0.0, 1.0 ] ); * var C = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); * * sgemm( 'no-transpose', 'no-transpose', 2, 2, 2, 1.0, A, 2, 1, 0, B, 2, 1, 0, 1.0, C, 2, 1, 0 ); * // C => <Float32Array>[ 2.0, 5.0, 6.0, 11.0 ] */ function sgemm( transA, transB, M, N, K, alpha, A, strideA1, strideA2, offsetA, B, strideB1, strideB2, offsetB, beta, C, strideC1, strideC2, offsetC ) { var isrma; var isrmb; var sa1; var sa2; var sb1; var sb2; if ( M === 0 || N === 0 || ( ( beta === 1.0 ) && ( ( alpha === 0.0 ) || ( K === 0 ) ) ) ) { return C; } // Form: C = β⋅C if ( beta === 0.0 ) { C = zeros( M, N, C, strideC1, strideC2, offsetC ); } else if ( beta !== 1.0 ) { C = scal( M, N, beta, C, strideC1, strideC2, offsetC ); } // Check whether we can early return... if ( alpha === 0.0 ) { return C; } // Determine the memory layouts of `A` and `B`... isrma = isRowMajor( [ strideA1, strideA2 ] ); isrmb = isRowMajor( [ strideB1, strideB2 ] ); // Check whether we can avoid loop tiling and simply use the "naive" (cache-optimal) algorithm for performing matrix multiplication... if ( isrma ) { // orderA === 'row-major' if ( !isTransposed( transA ) ) { if ( !isrmb && !isTransposed( transB ) ) { // orderB === 'column-major' // Form: C = α⋅A⋅B + C return naive( M, N, K, alpha, A, strideA1, strideA2, offsetA, B, strideB1, strideB2, offsetB, C, strideC1, strideC2, offsetC ); } if ( isrmb && isTransposed( transB ) ) { // orderB === 'row-major' // Form: C = α⋅A⋅B^T + C return naive( M, N, K, alpha, A, strideA1, strideA2, offsetA, B, strideB2, strideB1, offsetB, C, strideC1, strideC2, offsetC ); } } } else if ( isTransposed( transA ) ) { // orderA === 'column-major' if ( isrmb && isTransposed( transB ) ) { // orderB === 'row-major' // Form: C = α⋅A^T⋅B^T + C return naive( M, N, K, alpha, A, strideA2, strideA1, offsetA, B, strideB2, strideB1, offsetB, C, strideC1, strideC2, offsetC ); } if ( !isrmb && !isTransposed( transB ) ) { // orderB === 'column-major' // Form: C = α⋅A^T⋅B + C return naive( M, N, K, alpha, A, strideA2, strideA1, offsetA, B, strideB1, strideB2, offsetB, C, strideC1, strideC2, offsetC ); } } // Swap strides to perform transposes... if ( isTransposed( transA ) ) { sa1 = strideA2; sa2 = strideA1; } else { sa1 = strideA1; sa2 = strideA2; } if ( isTransposed( transB ) ) { sb1 = strideB2; sb2 = strideB1; } else { sb1 = strideB1; sb2 = strideB2; } // Perform loop tiling to promote cache locality: return blocked( M, N, K, alpha, A, sa1, sa2, offsetA, B, sb1, sb2, offsetB, C, strideC1, strideC2, offsetC ); } // EXPORTS // module.exports = sgemm; |