Skip to content

Commit 6177f03

Browse files
ShabiShett07kgrytestdlib-bot
authored
feat: add blas/base/ggemv
PR-URL: #7697 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 295ec84 commit 6177f03

Some content is hidden

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

46 files changed

+6424
-0
lines changed
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
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+
# ggemv
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 ggemv = require( '@stdlib/blas/base/ggemv' );
31+
```
32+
33+
#### ggemv( order, 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 A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
39+
var x = [ 1.0, 1.0, 1.0 ];
40+
var y = [ 1.0, 1.0 ];
41+
42+
ggemv( 'row-major', 'no-transpose', 2, 3, 1.0, A, 3, x, 1, 1.0, y, 1 );
43+
// y => [ 7.0, 16.0 ]
44+
```
45+
46+
The function has the following parameters:
47+
48+
- **order**: storage layout.
49+
- **trans**: specifies whether `A` should be transposed, conjugate-transposed, or not transposed.
50+
- **M**: number of rows in the matrix `A`.
51+
- **N**: number of columns in the matrix `A`.
52+
- **α**: scalar constant.
53+
- **A**: input matrix stored in linear memory.
54+
- **LDA**: stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`).
55+
- **x**: first input vector.
56+
- **sx**: stride length for `x`.
57+
- **β**: scalar constant.
58+
- **y**: second input vector.
59+
- **sy**: stride length for `y`.
60+
61+
The stride parameters determine how operations are performed. For example, to iterate over every other element in `x` and `y`,
62+
63+
```javascript
64+
var A = [ 1.0, 2.0, 3.0, 4.0 ];
65+
var x = [ 1.0, 0.0, 1.0, 0.0 ];
66+
var y = [ 1.0, 0.0, 1.0, 0.0 ];
67+
68+
ggemv( 'row-major', 'no-transpose', 2, 2, 1.0, A, 2, x, 2, 1.0, y, 2 );
69+
// y => [ 4.0, 0.0, 8.0, 0.0 ]
70+
```
71+
72+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
73+
74+
<!-- eslint-disable stdlib/capitalized-comments -->
75+
76+
```javascript
77+
var Float64Array = require( '@stdlib/array/float64' );
78+
79+
// Initial arrays...
80+
var x0 = new Float64Array( [ 0.0, 1.0, 1.0 ] );
81+
var y0 = new Float64Array( [ 0.0, 1.0, 1.0 ] );
82+
var A = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
83+
84+
// Create offset views...
85+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
86+
var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
87+
88+
ggemv( 'row-major', 'no-transpose', 2, 2, 1.0, A, 2, x1, -1, 1.0, y1, -1 );
89+
// y0 => <Float64Array>[ 0.0, 8.0, 4.0 ]
90+
```
91+
92+
#### ggemv.ndarray( trans, M, N, α, A, sa1, sa2, oa, x, sx, ox, β, y, sy, oy )
93+
94+
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.
95+
96+
```javascript
97+
var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
98+
var x = [ 1.0, 1.0, 1.0 ];
99+
var y = [ 1.0, 1.0 ];
100+
101+
ggemv.ndarray( 'no-transpose', 2, 3, 1.0, A, 3, 1, 0, x, 1, 0, 1.0, y, 1, 0 );
102+
// y => [ 7.0, 16.0 ]
103+
```
104+
105+
The function has the following additional parameters:
106+
107+
- **sa1**: stride of the first dimension of `A`.
108+
- **sa2**: stride of the second dimension of `A`.
109+
- **oa**: starting index for `A`.
110+
- **ox**: starting index for `x`.
111+
- **oy**: starting index for `y`.
112+
113+
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,
114+
115+
```javascript
116+
var Float64Array = require( '@stdlib/array/float64' );
117+
118+
var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
119+
var x = [ 0.0, 1.0, 2.0, 3.0 ];
120+
var y = [ 7.0, 8.0, 9.0, 10.0 ];
121+
122+
ggemv.ndarray( 'no-transpose', 2, 3, 1.0, A, 3, 1, 0, x, 1, 1, 1.0, y, -2, 2 );
123+
// y => [ 39.0, 8.0, 23.0, 10.0 ]
124+
```
125+
126+
</section>
127+
128+
<!-- /.usage -->
129+
130+
<section class="notes">
131+
132+
## Notes
133+
134+
- `ggemv()` corresponds to the [BLAS][blas] level 2 function [`dgemv`][dgemv] with the exception that this implementation works with any array type, not just Float64Arrays. Depending on the environment, the typed versions ([`dgemv`][@stdlib/blas/base/dgemv], [`sgemv`][@stdlib/blas/base/sgemv], etc.) are likely to be significantly more performant.
135+
- 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]).
136+
137+
</section>
138+
139+
<!-- /.notes -->
140+
141+
<section class="examples">
142+
143+
## Examples
144+
145+
<!-- eslint no-undef: "error" -->
146+
147+
```javascript
148+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
149+
var ggemv = require( '@stdlib/blas/base/ggemv' );
150+
151+
var opts = {
152+
'dtype': 'generic'
153+
};
154+
155+
var M = 3;
156+
var N = 3;
157+
158+
var A = discreteUniform( M*N, 0, 255, opts );
159+
var x = discreteUniform( N, 0, 255, opts );
160+
var y = discreteUniform( M, 0, 255, opts );
161+
162+
ggemv( 'row-major', 'no-transpose', M, N, 1.0, A, N, x, 1, 1.0, y, 1 );
163+
console.log( y );
164+
165+
ggemv.ndarray( 'no-transpose', M, N, 1.0, A, N, 1, 0, x, 1, 0, 1.0, y, 1, 0 );
166+
console.log( y );
167+
```
168+
169+
</section>
170+
171+
<!-- /.examples -->
172+
173+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
174+
175+
<section class="related">
176+
177+
</section>
178+
179+
<!-- /.related -->
180+
181+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
182+
183+
<section class="links">
184+
185+
[blas]: http://www.netlib.org/blas
186+
187+
[dgemv]: https://netlib.org/lapack/explore-html-3.6.1/d7/d15/group__double__blas__level2_gadd421a107a488d524859b4a64c1901a9.html
188+
189+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
190+
191+
[@stdlib/blas/base/dgemv]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/dgemv
192+
193+
[@stdlib/blas/base/sgemv]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/sgemv
194+
195+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
196+
197+
<!-- <related-links> -->
198+
199+
<!-- </related-links> -->
200+
201+
</section>
202+
203+
<!-- /.links -->
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 ggemv = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'generic'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Create a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} N - array length
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+
function benchmark( b ) {
55+
var z;
56+
var i;
57+
58+
b.tic();
59+
for ( i = 0; i < b.iterations; i++ ) {
60+
z = ggemv( 'row-major', 'no-transpose', N, N, 1.0, A, N, x, 1, 1.0, y, 1 );
61+
if ( isnan( z[ i%z.length ] ) ) {
62+
b.fail( 'should not return NaN' );
63+
}
64+
}
65+
b.toc();
66+
if ( isnan( z[ i%z.length ] ) ) {
67+
b.fail( 'should not return NaN' );
68+
}
69+
b.pass( 'benchmark finished' );
70+
b.end();
71+
}
72+
}
73+
74+
75+
// MAIN //
76+
77+
function main() {
78+
var min;
79+
var max;
80+
var N;
81+
var f;
82+
var i;
83+
84+
min = 1; // 10^min
85+
max = 6; // 10^max
86+
87+
for ( i = min; i <= max; i++ ) {
88+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
89+
f = createBenchmark( N );
90+
bench( pkg+':size='+(N*N), f );
91+
}
92+
}
93+
94+
main();
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 ggemv = require( './../lib' ).ndarray;
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'generic'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Create 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+
function benchmark( b ) {
55+
var z;
56+
var i;
57+
58+
b.tic();
59+
for ( i = 0; i < b.iterations; i++ ) {
60+
z = ggemv( 'no-transpose', N, N, 1.0, A, N, 1, 0, x, 1, 0, 1.0, y, 1, 0 );
61+
if ( isnan( z[ i%z.length ] ) ) {
62+
b.fail( 'should not return NaN' );
63+
}
64+
}
65+
b.toc();
66+
if ( isnan( z[ i%z.length ] ) ) {
67+
b.fail( 'should not return NaN' );
68+
}
69+
b.pass( 'benchmark finished' );
70+
b.end();
71+
}
72+
}
73+
74+
75+
// MAIN //
76+
77+
function main() {
78+
var min;
79+
var max;
80+
var N;
81+
var f;
82+
var i;
83+
84+
min = 1; // 10^min
85+
max = 6; // 10^max
86+
87+
for ( i = min; i <= max; i++ ) {
88+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
89+
f = createBenchmark( N );
90+
bench( pkg+':ndarray:size='+(N*N), f );
91+
}
92+
}
93+
94+
main();

0 commit comments

Comments
 (0)