Skip to content

Commit c807834

Browse files
ShabiShett07kgrytestdlib-bot
authored
feat: add blas/base/gger
PR-URL: #7629 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 92d4b33 commit c807834

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

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