|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# ggemm |
| 22 | + |
| 23 | +> Perform the matrix-matrix operation `C = α*op(A)*op(B) + β*C` where `op(X)` is one of the `op(X) = X`, or `op(X) = X^T`. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var ggemm = require( '@stdlib/blas/base/ggemm' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### ggemm( ord, ta, tb, M, N, K, α, A, lda, B, ldb, β, C, ldc ) |
| 34 | + |
| 35 | +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. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var A = [ 1.0, 2.0, 3.0, 4.0 ]; |
| 39 | +var B = [ 1.0, 1.0, 0.0, 1.0 ]; |
| 40 | +var C = [ 1.0, 2.0, 3.0, 4.0 ]; |
| 41 | + |
| 42 | +ggemm( 'row-major', 'no-transpose', 'no-transpose', 2, 2, 2, 1.0, A, 2, B, 2, 1.0, C, 2 ); |
| 43 | +// C => [ 2.0, 5.0, 6.0, 11.0 ] |
| 44 | +``` |
| 45 | + |
| 46 | +The function has the following parameters: |
| 47 | + |
| 48 | +- **ord**: storage layout. |
| 49 | +- **ta**: specifies whether `A` should be transposed, conjugate-transposed, or not transposed. |
| 50 | +- **tb**: specifies whether `B` should be transposed, conjugate-transposed, or not transposed. |
| 51 | +- **M**: number of rows in the matrix `op(A)` and in the matrix `C`. |
| 52 | +- **N**: number of columns in the matrix `op(B)` and in the matrix `C`. |
| 53 | +- **K**: number of columns in the matrix `op(A)` and number of rows in the matrix `op(B)`. |
| 54 | +- **α**: scalar constant. |
| 55 | +- **A**: first input matrix stored in linear memory. |
| 56 | +- **lda**: stride of the first dimension of `A` (leading dimension of `A`). |
| 57 | +- **B**: second input matrix stored in linear memory. |
| 58 | +- **ldb**: stride of the first dimension of `B` (leading dimension of `B`). |
| 59 | +- **β**: scalar constant |
| 60 | +- **C**: third input matrix stored in linear memory. |
| 61 | +- **ldc**: stride of the first dimension of `C` (leading dimension of `C`). |
| 62 | + |
| 63 | +The stride parameters determine how elements in the input arrays are accessed at runtime. For example, to perform matrix multiplication of two subarrays |
| 64 | + |
| 65 | +```javascript |
| 66 | +var A = [ 1.0, 2.0, 0.0, 0.0, 3.0, 4.0, 0.0, 0.0 ]; |
| 67 | +var B = [ 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 ]; |
| 68 | +var C = [ 1.0, 2.0, 3.0, 4.0 ]; |
| 69 | + |
| 70 | +ggemm( 'row-major', 'no-transpose', 'no-transpose', 2, 2, 2, 1.0, A, 4, B, 4, 1.0, C, 2 ); |
| 71 | +// C => [ 2.0, 5.0, 6.0, 11.0 ] |
| 72 | +``` |
| 73 | + |
| 74 | +<!-- lint disable maximum-heading-length --> |
| 75 | + |
| 76 | +#### ggemm.ndarray( ta, tb, M, N, K, α, A, sa1, sa2, oa, B, sb1, sb2, ob, β, C, sc1, sc2, oc ) |
| 77 | + |
| 78 | +Performs the matrix-matrix operation `C = α*op(A)*op(B) + β*C`, using alternative indexing semantics and 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. |
| 79 | + |
| 80 | +```javascript |
| 81 | +var A = [ 1.0, 2.0, 3.0, 4.0 ]; |
| 82 | +var B = [ 1.0, 1.0, 0.0, 1.0 ]; |
| 83 | +var C = [ 1.0, 2.0, 3.0, 4.0 ]; |
| 84 | + |
| 85 | +ggemm.ndarray( 'no-transpose', 'no-transpose', 2, 2, 2, 1.0, A, 2, 1, 0, B, 2, 1, 0, 1.0, C, 2, 1, 0 ); |
| 86 | +// C => [ 2.0, 5.0, 6.0, 11.0 ] |
| 87 | +``` |
| 88 | + |
| 89 | +The function has the following additional parameters: |
| 90 | + |
| 91 | +- **sa1**: stride of the first dimension of `A`. |
| 92 | +- **sa2**: stride of the second dimension of `A`. |
| 93 | +- **oa**: starting index for `A`. |
| 94 | +- **sb1**: stride of the first dimension of `B`. |
| 95 | +- **sb2**: stride of the second dimension of `B`. |
| 96 | +- **ob**: starting index for `B`. |
| 97 | +- **sc1**: stride of the first dimension of `C`. |
| 98 | +- **sc2**: stride of the second dimension of `C`. |
| 99 | +- **oc**: starting index for `C`. |
| 100 | + |
| 101 | +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example, |
| 102 | + |
| 103 | +```javascript |
| 104 | +var A = [ 0.0, 0.0, 1.0, 3.0, 2.0, 4.0 ]; |
| 105 | +var B = [ 0.0, 1.0, 0.0, 1.0, 1.0 ]; |
| 106 | +var C = [ 0.0, 0.0, 0.0, 1.0, 3.0, 2.0, 4.0 ]; |
| 107 | + |
| 108 | +ggemm.ndarray( 'no-transpose', 'no-transpose', 2, 2, 2, 1.0, A, 1, 2, 2, B, 1, 2, 1, 1.0, C, 1, 2, 3 ); |
| 109 | +// C => [ 0.0, 0.0, 0.0, 2.0, 6.0, 5.0, 11.0 ] |
| 110 | +``` |
| 111 | + |
| 112 | +</section> |
| 113 | + |
| 114 | +<!-- /.usage --> |
| 115 | + |
| 116 | +<section class="notes"> |
| 117 | + |
| 118 | +## Notes |
| 119 | + |
| 120 | +- `ggemm()` corresponds to the [BLAS][blas] level 3 function [`dgemm`][dgemm] with the exception that this implementation works with any array type, not just Float64Arrays. Depending on the environment, the typed versions ([`dgemm`][@stdlib/blas/base/dgemm], [`sgemm`][@stdlib/blas/base/sgemm], etc.) are likely to be significantly more performant. |
| 121 | +- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). |
| 122 | + |
| 123 | +</section> |
| 124 | + |
| 125 | +<!-- /.notes --> |
| 126 | + |
| 127 | +<section class="examples"> |
| 128 | + |
| 129 | +## Examples |
| 130 | + |
| 131 | +<!-- eslint no-undef: "error" --> |
| 132 | + |
| 133 | +```javascript |
| 134 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 135 | +var ggemm = require( '@stdlib/blas/base/ggemm' ); |
| 136 | + |
| 137 | +var opts = { |
| 138 | + 'dtype': 'generic' |
| 139 | +}; |
| 140 | + |
| 141 | +var M = 3; |
| 142 | +var N = 4; |
| 143 | +var K = 2; |
| 144 | + |
| 145 | +var A = discreteUniform( M*K, 0, 10, opts ); // 3x2 |
| 146 | +var B = discreteUniform( K*N, 0, 10, opts ); // 2x4 |
| 147 | +var C = discreteUniform( M*N, 0, 10, opts ); // 3x4 |
| 148 | + |
| 149 | +ggemm( 'row-major', 'no-transpose', 'no-transpose', M, N, K, 1.0, A, K, B, N, 1.0, C, N ); |
| 150 | +console.log( C ); |
| 151 | + |
| 152 | +ggemm.ndarray( 'no-transpose', 'no-transpose', M, N, K, 1.0, A, K, 1, 0, B, N, 1, 0, 1.0, C, N, 1, 0 ); |
| 153 | +console.log( C ); |
| 154 | +``` |
| 155 | + |
| 156 | +</section> |
| 157 | + |
| 158 | +<!-- /.examples --> |
| 159 | + |
| 160 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 161 | + |
| 162 | +<section class="related"> |
| 163 | + |
| 164 | +</section> |
| 165 | + |
| 166 | +<!-- /.related --> |
| 167 | + |
| 168 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 169 | + |
| 170 | +<section class="links"> |
| 171 | + |
| 172 | +[blas]: http://www.netlib.org/blas |
| 173 | + |
| 174 | +[dgemm]: https://www.netlib.org/lapack/explore-html/dd/d09/group__gemm_ga1e899f8453bcbfde78e91a86a2dab984.html |
| 175 | + |
| 176 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 177 | + |
| 178 | +[@stdlib/blas/base/dgemm]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/dgemm |
| 179 | + |
| 180 | +[@stdlib/blas/base/sgemm]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/sgemm |
| 181 | + |
| 182 | +[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor |
| 183 | + |
| 184 | +</section> |
| 185 | + |
| 186 | +<!-- /.links --> |
0 commit comments