Skip to content

Commit ab0faa5

Browse files
aman-095kgrytestdlib-bot
authored
feat: add blas/base/sgemm
PR-URL: #2742 Ref: #2039 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <82920195+stdlib-bot@users.noreply.github.com>
1 parent 153bb1a commit ab0faa5

File tree

110 files changed

+9812
-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

+9812
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# sgemm
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 sgemm = require( '@stdlib/blas/base/sgemm' );
31+
```
32+
33+
#### sgemm( 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(A)` 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 Float32Array = require( '@stdlib/array/float32' );
39+
40+
var A = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] );
41+
var B = new Float32Array( [ 1.0, 1.0, 0.0, 1.0 ] );
42+
var C = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] );
43+
44+
sgemm( 'row-major', 'no-transpose', 'no-transpose', 2, 2, 2, 1.0, A, 2, B, 2, 1.0, C, 2 );
45+
// C => <Float32Array>[ 2.0, 5.0, 6.0, 11.0 ]
46+
```
47+
48+
The function has the following parameters:
49+
50+
- **ord**: storage layout.
51+
- **ta**: specifies whether `A` should be transposed, conjugate-transposed, or not transposed.
52+
- **tb**: specifies whether `B` should be transposed, conjugate-transposed, or not transposed.
53+
- **M**: number of rows in the matrix `op(A)` and in the matrix `C`.
54+
- **N**: number of columns in the matrix `op(B)` and in the matrix `C`.
55+
- **K**: number of columns in the matrix `op(A)` and number of rows in the matrix `op(B)`.
56+
- **α**: scalar constant.
57+
- **A**: first input matrix stored in linear memory as a [`Float32Array`][mdn-float32array].
58+
- **lda**: stride of the first dimension of `A` (leading dimension of `A`).
59+
- **B**: second input matrix stored in linear memory as a [`Float32Array`][mdn-float32array].
60+
- **ldb**: stride of the first dimension of `B` (leading dimension of `B`).
61+
- **β**: scalar constant
62+
- **C**: third input matrix stored in linear memory as a [`Float32Array`][mdn-float32array].
63+
- **ldc**: stride of the first dimension of `C` (leading dimension of `C`).
64+
65+
The stride parameters determine how elements in the input arrays are accessed at runtime. For example, to perform matrix multiplication of two subarrays
66+
67+
```javascript
68+
var Float32Array = require( '@stdlib/array/float32' );
69+
70+
var A = new Float32Array( [ 1.0, 2.0, 0.0, 0.0, 3.0, 4.0, 0.0, 0.0 ] );
71+
var B = new Float32Array( [ 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 ] );
72+
var C = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] );
73+
74+
sgemm( 'row-major', 'no-transpose', 'no-transpose', 2, 2, 2, 1.0, A, 4, B, 4, 1.0, C, 2 );
75+
// C => <Float32Array>[ 2.0, 5.0, 6.0, 11.0 ]
76+
```
77+
78+
<!-- lint disable maximum-heading-length -->
79+
80+
#### sgemm.ndarray( ta, tb, M, N, K, α, A, sa1, sa2, oa, B, sb1, sb2, ob, β, C, sc1, sc2, oc )
81+
82+
Performs the matrix-matrix operation `C = α*op(A)*op(B) + β*C`, using alternative indexing semantics and where `op(A)` 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.
83+
84+
```javascript
85+
var Float32Array = require( '@stdlib/array/float32' );
86+
87+
var A = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] );
88+
var B = new Float32Array( [ 1.0, 1.0, 0.0, 1.0 ] );
89+
var C = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] );
90+
91+
sgemm.ndarray( 'no-transpose', 'no-transpose', 2, 2, 2, 1.0, A, 2, 1, 0, B, 2, 1, 0, 1.0, C, 2, 1, 0 );
92+
// C => <Float32Array>[ 2.0, 5.0, 6.0, 11.0 ]
93+
```
94+
95+
The function has the following additional parameters:
96+
97+
- **sa1**: stride of the first dimension of `A`.
98+
- **sa2**: stride of the second dimension of `A`.
99+
- **oa**: starting index for `A`.
100+
- **sb1**: stride of the first dimension of `B`.
101+
- **sb2**: stride of the second dimension of `B`.
102+
- **ob**: starting index for `B`.
103+
- **sc1**: stride of the first dimension of `C`.
104+
- **sc2**: stride of the second dimension of `C`.
105+
- **oc**: starting index for `C`.
106+
107+
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,
108+
109+
```javascript
110+
var Float32Array = require( '@stdlib/array/float32' );
111+
112+
var A = new Float32Array( [ 0.0, 0.0, 1.0, 3.0, 2.0, 4.0 ] );
113+
var B = new Float32Array( [ 0.0, 1.0, 0.0, 1.0, 1.0 ] );
114+
var C = new Float32Array( [ 0.0, 0.0, 0.0, 1.0, 3.0, 2.0, 4.0 ] );
115+
116+
sgemm.ndarray( 'no-transpose', 'no-transpose', 2, 2, 2, 1.0, A, 1, 2, 2, B, 1, 2, 1, 1.0, C, 1, 2, 3 );
117+
// C => <Float32Array>[ 0.0, 0.0, 0.0, 2.0, 6.0, 5.0, 11.0 ]
118+
```
119+
120+
</section>
121+
122+
<!-- /.usage -->
123+
124+
<section class="notes">
125+
126+
## Notes
127+
128+
- `sgemm()` corresponds to the [BLAS][blas] level 3 function [`sgemm`][blas-sgemm].
129+
130+
</section>
131+
132+
<!-- /.notes -->
133+
134+
<section class="examples">
135+
136+
## Examples
137+
138+
<!-- eslint no-undef: "error" -->
139+
140+
```javascript
141+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
142+
var sgemm = require( '@stdlib/blas/base/sgemm' );
143+
144+
var opts = {
145+
'dtype': 'float32'
146+
};
147+
148+
var M = 3;
149+
var N = 4;
150+
var K = 2;
151+
152+
var A = discreteUniform( M*K, 0, 10, opts ); // 3x2
153+
var B = discreteUniform( K*N, 0, 10, opts ); // 2x4
154+
var C = discreteUniform( M*N, 0, 10, opts ); // 3x4
155+
156+
sgemm( 'row-major', 'no-transpose', 'no-transpose', M, N, K, 1.0, A, K, B, N, 1.0, C, N );
157+
console.log( C );
158+
159+
sgemm.ndarray( 'no-transpose', 'no-transpose', M, N, K, 1.0, A, K, 1, 0, B, N, 1, 0, 1.0, C, N, 1, 0 );
160+
console.log( C );
161+
```
162+
163+
</section>
164+
165+
<!-- /.examples -->
166+
167+
<!-- C interface documentation. -->
168+
169+
* * *
170+
171+
<section class="c">
172+
173+
## C APIs
174+
175+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
176+
177+
<section class="intro">
178+
179+
</section>
180+
181+
<!-- /.intro -->
182+
183+
<!-- C usage documentation. -->
184+
185+
<section class="usage">
186+
187+
### Usage
188+
189+
```c
190+
#include "stdlib/blas/base/sgemm.h"
191+
```
192+
193+
#### TODO
194+
195+
TODO.
196+
197+
```c
198+
TODO
199+
```
200+
201+
TODO
202+
203+
```c
204+
TODO
205+
```
206+
207+
</section>
208+
209+
<!-- /.usage -->
210+
211+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
212+
213+
<section class="notes">
214+
215+
</section>
216+
217+
<!-- /.notes -->
218+
219+
<!-- C API usage examples. -->
220+
221+
<section class="examples">
222+
223+
### Examples
224+
225+
```c
226+
TODO
227+
```
228+
229+
</section>
230+
231+
<!-- /.examples -->
232+
233+
</section>
234+
235+
<!-- /.c -->
236+
237+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
238+
239+
<section class="related">
240+
241+
</section>
242+
243+
<!-- /.related -->
244+
245+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
246+
247+
<section class="links">
248+
249+
[blas]: http://www.netlib.org/blas
250+
251+
[blas-sgemm]: https://www.netlib.org/lapack/explore-html/dd/d09/group__gemm_ga8cad871c590600454d22564eff4fed6b.html#ga8cad871c590600454d22564eff4fed6b
252+
253+
[mdn-float32array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array
254+
255+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
256+
257+
</section>
258+
259+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
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 sgemm = require( './../lib/ndarray.js' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'float32'
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 = sgemm( '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 ( isnanf( z[ i%z.length ] ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
}
71+
b.toc();
72+
if ( isnanf( 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 len;
90+
var min;
91+
var max;
92+
var f;
93+
var i;
94+
95+
min = 1; // 10^min
96+
max = 4; // 10^max
97+
98+
for ( i = min; i <= max; i++ ) {
99+
len = floor( pow( pow( 10, i ), 1.0/2.0 ) );
100+
f = createBenchmark( len );
101+
bench( pkg+':ndarray:order(A)=column-major,order(B)=column-major,order(C)=column-major,trans(A)=false,trans(B)=false,size='+(len*len), f );
102+
}
103+
}
104+
105+
main();

0 commit comments

Comments
 (0)