Skip to content

Commit 57975f3

Browse files
aman-095kgryte
authored andcommitted
feat: add blas/base/sgemv
PR-URL: stdlib-js#2365 Ref: stdlib-js#2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 83516ea commit 57975f3

40 files changed

+4007
-0
lines changed
Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
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+
# sgemv
22+
23+
> Perform one of the matrix-vector operations `y = α*A*x + β*y` or `y = α*A**T*x + β*y`.
24+
25+
<section class = "usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var sgemv = require( '@stdlib/blas/base/sgemv' );
31+
```
32+
33+
#### sgemv( ord, trans, M, N, α, A, LDA, x, sx, β, y, sy )
34+
35+
Performs one of the matrix-vector operations `y = α*A*x + β*y` or `y = α*A**T*x + β*y`, where `α` and `β` are scalars, `x` and `y` are vectors, and `A` is 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, 5.0, 6.0 ] );
41+
var x = new Float32Array( [ 1.0, 1.0, 1.0 ] );
42+
var y = new Float32Array( [ 1.0, 1.0 ] );
43+
44+
sgemv( 'row-major', 'no-transpose', 2, 3, 1.0, A, 3, x, 1, 1.0, y, 1 );
45+
// y => <Float32Array>[ 7.0, 16.0 ]
46+
```
47+
48+
The function has the following parameters:
49+
50+
- **ord**: storage layout.
51+
- **trans**: specifies whether `A` should be transposed, conjugate-transposed, or not transposed.
52+
- **M**: number of rows in the matrix `A`.
53+
- **N**: number of columns in the matrix `A`.
54+
- **α**: scalar constant.
55+
- **A**: input matrix stored in linear memory as a [`Float32Array`][mdn-float32array].
56+
- **lda**: stride of the first dimension of `A` (leading dimension of `A`).
57+
- **x**: input [`Float32Array`][mdn-float32array].
58+
- **sx**: index increment for `x`.
59+
- **β**: scalar constant.
60+
- **y**: output [`Float32Array`][mdn-float32array].
61+
- **sy**: index increment for `y`.
62+
63+
The stride parameters determine how operations are performed. For example, to iterate over every other element in `x` and `y`,
64+
65+
```javascript
66+
var Float32Array = require( '@stdlib/array/float32' );
67+
68+
var A = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] );
69+
var x = new Float32Array( [ 1.0, 0.0, 1.0, 0.0 ] );
70+
var y = new Float32Array( [ 1.0, 0.0, 1.0, 0.0 ] );
71+
72+
sgemv( 'row-major', 'no-transpose', 2, 2, 1.0, A, 2, x, 2, 1.0, y, 2 );
73+
// y => <Float32Array>[ 4.0, 0.0, 8.0, 0.0 ]
74+
```
75+
76+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
77+
78+
<!-- eslint-disable stdlib/capitalized-comments -->
79+
80+
```javascript
81+
var Float32Array = require( '@stdlib/array/float32' );
82+
83+
// Initial arrays...
84+
var x0 = new Float32Array( [ 0.0, 1.0, 1.0 ] );
85+
var y0 = new Float32Array( [ 0.0, 1.0, 1.0 ] );
86+
var A = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
87+
88+
// Create offset views...
89+
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
90+
var y1 = new Float32Array( y0.buffer, y0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
91+
92+
sgemv( 'row-major', 'no-transpose', 2, 2, 1.0, A, 2, x1, -1, 1.0, y1, -1 );
93+
// y0 => <Float32Array>[ 0.0, 8.0, 4.0 ]
94+
```
95+
96+
#### sgemv.ndarray( trans, M, N, α, A, sa1, sa2, oa, x, sx, ox, β, y, sy, oy )
97+
98+
Performs one of the matrix-vector operations `y = α*A*x + β*y` or `y = α*A**T*x + β*y`, using alternative indexing semantics and where `α` and `β` are scalars, `x` and `y` are vectors, and `A` is an `M` by `N` matrix.
99+
100+
```javascript
101+
var Float32Array = require( '@stdlib/array/float32' );
102+
103+
var A = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
104+
var x = new Float32Array( [ 1.0, 1.0, 1.0 ] );
105+
var y = new Float32Array( [ 1.0, 1.0 ] );
106+
107+
sgemv.ndarray( 'no-transpose', 2, 3, 1.0, A, 3, 1, 0, x, 1, 0, 1.0, y, 1, 0 );
108+
// y => <Float32Array>[ 7.0, 16.0 ]
109+
```
110+
111+
The function has the following additional parameters:
112+
113+
- **sa1**: stride of the first dimension of `A`.
114+
- **sa2**: stride of the second dimension of `A`.
115+
- **oa**: starting index for `A`.
116+
- **ox**: starting index for `x`.
117+
- **oy**: starting index for `y`.
118+
119+
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,
120+
121+
```javascript
122+
var Float32Array = require( '@stdlib/array/float32' );
123+
124+
var A = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
125+
var x = new Float32Array( [ 0.0, 1.0, 2.0, 3.0 ] );
126+
var y = new Float32Array( [ 7.0, 8.0, 9.0, 10.0 ] );
127+
128+
sgemv.ndarray( 'no-transpose', 2, 3, 1.0, A, 3, 1, 0, x, 1, 1, 1.0, y, -2, 2 );
129+
// y => <Float32Array>[ 39, 8, 23, 10 ]
130+
```
131+
132+
</section>
133+
134+
<!-- /.usage -->
135+
136+
<section class="notes">
137+
138+
## Notes
139+
140+
- `sgemv()` corresponds to the [BLAS][blas] level 2 function [`sgemv`][blas-sgemv].
141+
142+
</section>
143+
144+
<!-- /.notes -->
145+
146+
<section class="examples">
147+
148+
## Examples
149+
150+
<!-- eslint no-undef: "error" -->
151+
152+
```javascript
153+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
154+
var sgemv = require( '@stdlib/blas/base/sgemv' );
155+
156+
var opts = {
157+
'dtype': 'float32'
158+
};
159+
160+
var M = 3;
161+
var N = 3;
162+
163+
var A = discreteUniform( M*N, 0, 255, opts );
164+
var x = discreteUniform( N, 0, 255, opts );
165+
var y = discreteUniform( M, 0, 255, opts );
166+
167+
sgemv( 'row-major', 'no-transpose', M, N, 1.0, A, N, x, -1, 1.0, y, -1 );
168+
console.log( y );
169+
170+
```
171+
172+
</section>
173+
174+
<!-- /.examples -->
175+
176+
<!-- C interface documentation. -->
177+
178+
* * *
179+
180+
<section class="c">
181+
182+
## C APIs
183+
184+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
185+
186+
<section class="intro">
187+
188+
</section>
189+
190+
<!-- /.intro -->
191+
192+
<!-- C usage documentation. -->
193+
194+
<section class="usage">
195+
196+
### Usage
197+
198+
```c
199+
#include "stdlib/blas/base/sgemv.h"
200+
```
201+
202+
#### TODO
203+
204+
TODO.
205+
206+
```c
207+
TODO
208+
```
209+
210+
TODO
211+
212+
```c
213+
TODO
214+
```
215+
216+
</section>
217+
218+
<!-- /.usage -->
219+
220+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
221+
222+
<section class="notes">
223+
224+
</section>
225+
226+
<!-- /.notes -->
227+
228+
<!-- C API usage examples. -->
229+
230+
<section class="examples">
231+
232+
### Examples
233+
234+
```c
235+
TODO
236+
```
237+
238+
</section>
239+
240+
<!-- /.examples -->
241+
242+
</section>
243+
244+
<!-- /.c -->
245+
246+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
247+
248+
<section class="related">
249+
250+
</section>
251+
252+
<!-- /.related -->
253+
254+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
255+
256+
<section class="links">
257+
258+
[blas]: http://www.netlib.org/blas
259+
260+
[blas-sgemv]: https://www.netlib.org/lapack/explore-html/d7/dda/group__gemv_ga0d35d880b663ad18204bb23bd186e380.html#ga0d35d880b663ad18204bb23bd186e380
261+
262+
[mdn-float32array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array
263+
264+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
265+
266+
</section>
267+
268+
<!-- /.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) 2018 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 sgemv = require( './../lib/sgemv.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 x = uniform( N, -10.0, 10.0, options );
50+
var y = uniform( N, -10.0, 10.0, options );
51+
var A = 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 = sgemv( 'row-major', 'no-transpose', N, N, 1.0, A, N, x, 1, 1.0, y, 1 );
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 = 6; // 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+':size='+(len*len), f );
102+
}
103+
}
104+
105+
main();

0 commit comments

Comments
 (0)