Skip to content

Commit f08a71f

Browse files
ShabiShett07kgrytestdlib-bot
authored
feat: add blas/base/ggemm
PR-URL: #7847 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent f928b2f commit f08a71f

File tree

110 files changed

+13262
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

110 files changed

+13262
-0
lines changed
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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 -->
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var floor = require( '@stdlib/math/base/special/floor' );
28+
var pkg = require( './../package.json' ).name;
29+
var ggemm = require( './../lib/ndarray.js' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'generic'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} N - array dimension size
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( N ) {
49+
var A = uniform( N*N, -10.0, 10.0, options );
50+
var B = uniform( N*N, -10.0, 10.0, options );
51+
var C = uniform( N*N, -10.0, 10.0, options );
52+
return benchmark;
53+
54+
/**
55+
* Benchmark function.
56+
*
57+
* @private
58+
* @param {Benchmark} b - benchmark instance
59+
*/
60+
function benchmark( b ) {
61+
var z;
62+
var i;
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
z = ggemm( 'no-transpose', 'no-transpose', N, N, N, 1.0, A, 1, N, 0, B, 1, N, 0, 1.0, C, 1, N, 0 );
67+
if ( isnan( z[ i%z.length ] ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
}
71+
b.toc();
72+
if ( isnan( z[ i%z.length ] ) ) {
73+
b.fail( 'should not return NaN' );
74+
}
75+
b.pass( 'benchmark finished' );
76+
b.end();
77+
}
78+
}
79+
80+
81+
// MAIN //
82+
83+
/**
84+
* Main execution sequence.
85+
*
86+
* @private
87+
*/
88+
function main() {
89+
var min;
90+
var max;
91+
var N;
92+
var f;
93+
var i;
94+
95+
min = 1; // 10^min
96+
max = 5; // 10^max
97+
98+
for ( i = min; i <= max; i++ ) {
99+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
100+
f = createBenchmark( N );
101+
bench( pkg+':ndarray:order(A)=column-major,order(B)=column-major,order(C)=column-major,trans(A)=false,trans(B)=false,size='+(N*N), f );
102+
}
103+
}
104+
105+
main();
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var floor = require( '@stdlib/math/base/special/floor' );
28+
var pkg = require( './../package.json' ).name;
29+
var ggemm = require( './../lib/ndarray.js' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'generic'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} N - array dimension size
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( N ) {
49+
var A = uniform( N*N, -10.0, 10.0, options );
50+
var B = uniform( N*N, -10.0, 10.0, options );
51+
var C = uniform( N*N, -10.0, 10.0, options );
52+
return benchmark;
53+
54+
/**
55+
* Benchmark function.
56+
*
57+
* @private
58+
* @param {Benchmark} b - benchmark instance
59+
*/
60+
function benchmark( b ) {
61+
var z;
62+
var i;
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
z = ggemm( 'no-transpose', 'transpose', N, N, N, 1.0, A, 1, N, 0, B, 1, N, 0, 1.0, C, 1, N, 0 );
67+
if ( isnan( z[ i%z.length ] ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
}
71+
b.toc();
72+
if ( isnan( z[ i%z.length ] ) ) {
73+
b.fail( 'should not return NaN' );
74+
}
75+
b.pass( 'benchmark finished' );
76+
b.end();
77+
}
78+
}
79+
80+
81+
// MAIN //
82+
83+
/**
84+
* Main execution sequence.
85+
*
86+
* @private
87+
*/
88+
function main() {
89+
var min;
90+
var max;
91+
var N;
92+
var f;
93+
var i;
94+
95+
min = 1; // 10^min
96+
max = 5; // 10^max
97+
98+
for ( i = min; i <= max; i++ ) {
99+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
100+
f = createBenchmark( N );
101+
bench( pkg+':ndarray:order(A)=column-major,order(B)=column-major,order(C)=column-major,trans(A)=false,trans(B)=true,size='+(N*N), f );
102+
}
103+
}
104+
105+
main();

0 commit comments

Comments
 (0)