Skip to content

Commit 243f857

Browse files
aman-095kgryte
andauthored
feat: add blas/base/snrm2-wasm
PR-URL: #3029 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 22b1ac8 commit 243f857

32 files changed

+4462
-0
lines changed
Lines changed: 297 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
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+
# snrm2
22+
23+
> Calculate the L2-norm of a single-precision floating-point vector.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var snrm2 = require( '@stdlib/blas/base/snrm2-wasm' );
31+
```
32+
33+
#### snrm2.main( N, x, strideX )
34+
35+
Calculates the L2-norm of a single-precision floating-point vector.
36+
37+
```javascript
38+
var Float32Array = require( '@stdlib/array/float32' );
39+
40+
var x = new Float32Array( [ 1.0, -2.0, 2.0 ] );
41+
42+
var z = snrm2.main( 3, x, 1 );
43+
// returns 3.0
44+
```
45+
46+
The function has the following parameters:
47+
48+
- **N**: number of indexed elements.
49+
- **x**: input [`Float32Array`][@stdlib/array/float32].
50+
- **strideX**: index increment for `x`.
51+
52+
The `N` and stride parameters determine which elements in the input strided array are accessed at runtime. For example, to compute the L2-norm of every other element in `x`,
53+
54+
```javascript
55+
var Float32Array = require( '@stdlib/array/float32' );
56+
57+
var x = new Float32Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
58+
59+
var z = snrm2.main( 4, x, 2 );
60+
// returns 5.0
61+
```
62+
63+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
64+
65+
<!-- eslint-disable stdlib/capitalized-comments -->
66+
67+
```javascript
68+
var Float32Array = require( '@stdlib/array/float32' );
69+
70+
// Initial array:
71+
var x0 = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
72+
73+
// Create a typed array view:
74+
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
75+
76+
var z = snrm2.main( 4, x1, 2 );
77+
// returns 5.0
78+
```
79+
80+
#### snrm2.ndarray( N, x, strideX, offsetX )
81+
82+
Calculates the L2-norm of a single-precision floating-point vector using alternative indexing semantics.
83+
84+
```javascript
85+
var Float32Array = require( '@stdlib/array/float32' );
86+
87+
var x = new Float32Array( [ 1.0, -2.0, 2.0 ] );
88+
89+
var z = snrm2.ndarray( 3, x, 1, 0 );
90+
// returns 3.0
91+
```
92+
93+
The function has the following additional parameters:
94+
95+
- **offsetX**: starting index for `x`.
96+
97+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to calculate the L2-norm for every other value in `x` starting from the second value,
98+
99+
```javascript
100+
var Float32Array = require( '@stdlib/array/float32' );
101+
102+
var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
103+
104+
var z = snrm2.ndarray( 4, x, 2, 1 );
105+
// returns 5.0
106+
```
107+
108+
* * *
109+
110+
### Module
111+
112+
#### snrm2.Module( memory )
113+
114+
Returns a new WebAssembly [module wrapper][@stdlib/wasm/module-wrapper] instance which uses the provided WebAssembly [memory][@stdlib/wasm/memory] instance as its underlying memory.
115+
116+
<!-- eslint-disable node/no-sync -->
117+
118+
```javascript
119+
var Memory = require( '@stdlib/wasm/memory' );
120+
121+
// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
122+
var mem = new Memory({
123+
'initial': 10,
124+
'maximum': 100
125+
});
126+
127+
// Create a BLAS routine:
128+
var mod = new snrm2.Module( mem );
129+
// returns <Module>
130+
131+
// Initialize the routine:
132+
mod.initializeSync();
133+
```
134+
135+
#### snrm2.Module.prototype.main( N, xp, sx )
136+
137+
Computes the L2-norm of a single-precision floating-point vector.
138+
139+
<!-- eslint-disable node/no-sync -->
140+
141+
```javascript
142+
var Memory = require( '@stdlib/wasm/memory' );
143+
var oneTo = require( '@stdlib/array/one-to' );
144+
145+
// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
146+
var mem = new Memory({
147+
'initial': 10,
148+
'maximum': 100
149+
});
150+
151+
// Create a BLAS routine:
152+
var mod = new snrm2.Module( mem );
153+
// returns <Module>
154+
155+
// Initialize the routine:
156+
mod.initializeSync();
157+
158+
// Define a vector data type:
159+
var dtype = 'float32';
160+
161+
// Specify a vector length:
162+
var N = 5;
163+
164+
// Define pointer (i.e., byte offsets) for storing the input vector:
165+
var xptr = 0;
166+
167+
// Write vector values to module memory:
168+
mod.write( xptr, oneTo( N, dtype ) );
169+
170+
// Perform computation:
171+
var out = mod.main( N, xptr, 1 );
172+
// returns ~7.42
173+
```
174+
175+
The function has the following parameters:
176+
177+
- **N**: number of indexed elements.
178+
- **xp**: input [`Float32Array`][@stdlib/array/float32] pointer (i.e., byte offset).
179+
- **sx**: index increment for `x`.
180+
181+
#### snrm2.Module.prototype.ndarray( N, xp, sx, ox )
182+
183+
Computes the L2-norm of a single-precision floating-point vector using alternative indexing semantics.
184+
185+
<!-- eslint-disable node/no-sync -->
186+
187+
```javascript
188+
var Memory = require( '@stdlib/wasm/memory' );
189+
var oneTo = require( '@stdlib/array/one-to' );
190+
191+
// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
192+
var mem = new Memory({
193+
'initial': 10,
194+
'maximum': 100
195+
});
196+
197+
// Create a BLAS routine:
198+
var mod = new snrm2.Module( mem );
199+
// returns <Module>
200+
201+
// Initialize the routine:
202+
mod.initializeSync();
203+
204+
// Define a vector data type:
205+
var dtype = 'float32';
206+
207+
// Specify a vector length:
208+
var N = 5;
209+
210+
// Define pointer (i.e., byte offsets) for storing the input vector:
211+
var xptr = 0;
212+
213+
// Write vector values to module memory:
214+
mod.write( xptr, oneTo( N, dtype ) );
215+
216+
// Perform computation:
217+
var out = mod.ndarray( N, xptr, 1, 0 );
218+
// returns ~7.42
219+
```
220+
221+
The function has the following additional parameters:
222+
223+
- **ox**: starting index for `x`.
224+
225+
</section>
226+
227+
<!-- /.usage -->
228+
229+
<section class="notes">
230+
231+
* * *
232+
233+
## Notes
234+
235+
- If `N <= 0`, both `main` and `ndarray` methods return `0.0`.
236+
- This package implements routines using WebAssembly. When provided arrays which are not allocated on a `snrm2` module memory instance, data must be explicitly copied to module memory prior to computation. Data movement may entail a performance cost, and, thus, if you are using arrays external to module memory, you should prefer using [`@stdlib/blas/base/snrm2`][@stdlib/blas/base/snrm2]. However, if working with arrays which are allocated and explicitly managed on module memory, you can achieve better performance when compared to the pure JavaScript implementations found in [`@stdlib/blas/base/snrm2`][@stdlib/blas/base/snrm2]. Beware that such performance gains may come at the cost of additional complexity when having to perform manual memory management. Choosing between implementations depends heavily on the particular needs and constraints of your application, with no one choice universally better than the other.
237+
- `snrm2()` corresponds to the [BLAS][blas] level 1 function [`snrm2`][snrm2].
238+
239+
</section>
240+
241+
<!-- /.notes -->
242+
243+
<section class="examples">
244+
245+
* * *
246+
247+
## Examples
248+
249+
<!-- eslint no-undef: "error" -->
250+
251+
```javascript
252+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
253+
var snrm2 = require( '@stdlib/blas/base/snrm2-wasm' );
254+
255+
var opts = {
256+
'dtype': 'float32'
257+
};
258+
var x = discreteUniform( 10, 0, 100, opts );
259+
console.log( x );
260+
261+
var out = snrm2.ndarray( x.length, x, 1, 0 );
262+
console.log( out );
263+
```
264+
265+
</section>
266+
267+
<!-- /.examples -->
268+
269+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
270+
271+
<section class="related">
272+
273+
</section>
274+
275+
<!-- /.related -->
276+
277+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
278+
279+
<section class="links">
280+
281+
[blas]: http://www.netlib.org/blas
282+
283+
[snrm2]: https://www.netlib.org/lapack/explore-html/d1/d2a/group__nrm2_gab5393665c8f0e7d5de9bd1dd2ff0d9d0.html#gab5393665c8f0e7d5de9bd1dd2ff0d9d0
284+
285+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
286+
287+
[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32
288+
289+
[@stdlib/wasm/memory]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/memory
290+
291+
[@stdlib/wasm/module-wrapper]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/module-wrapper
292+
293+
[@stdlib/blas/base/snrm2]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/snrm2
294+
295+
</section>
296+
297+
<!-- /.links -->

0 commit comments

Comments
 (0)