Skip to content

Commit f73aeb5

Browse files
aman-095kgryte
andauthored
feat: add blas/base/sspmv
PR-URL: #2414 Ref: #2039 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent d2cd4c3 commit f73aeb5

25 files changed

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

0 commit comments

Comments
 (0)