Skip to content

Commit 18fef5f

Browse files
committed
feat: add blas/base/wasm/dnrm2
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent c4b589f commit 18fef5f

33 files changed

+4350
-0
lines changed
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+
# dnrm2
22+
23+
> Calculate the L2-norm of a double-precision floating-point vector.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var dnrm2 = require( '@stdlib/blas/base/wasm/dnrm2' );
31+
```
32+
33+
#### dnrm2.main( N, x, strideX )
34+
35+
Calculates the L2-norm of a double-precision floating-point vector.
36+
37+
```javascript
38+
var Float64Array = require( '@stdlib/array/float64' );
39+
40+
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
41+
42+
var z = dnrm2.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 [`Float64Array`][@stdlib/array/float64].
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 Float64Array = require( '@stdlib/array/float64' );
56+
57+
var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
58+
59+
var z = dnrm2.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 Float64Array = require( '@stdlib/array/float64' );
69+
70+
// Initial array:
71+
var x0 = new Float64Array( [ 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 Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
75+
76+
var z = dnrm2.main( 4, x1, 2 );
77+
// returns 5.0
78+
```
79+
80+
#### dnrm2.ndarray( N, x, strideX, offsetX )
81+
82+
Calculates the L2-norm of a double-precision floating-point vector using alternative indexing semantics.
83+
84+
```javascript
85+
var Float64Array = require( '@stdlib/array/float64' );
86+
87+
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
88+
89+
var z = dnrm2.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 Float64Array = require( '@stdlib/array/float64' );
101+
102+
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
103+
104+
var z = dnrm2.ndarray( 4, x, 2, 1 );
105+
// returns 5.0
106+
```
107+
108+
* * *
109+
110+
### Module
111+
112+
#### dnrm2.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 dnrm2.Module( mem );
129+
// returns <Module>
130+
131+
// Initialize the routine:
132+
mod.initializeSync();
133+
```
134+
135+
#### dnrm2.Module.prototype.main( N, xp, sx )
136+
137+
Computes the L2-norm of a double-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 dnrm2.Module( mem );
153+
// returns <Module>
154+
155+
// Initialize the routine:
156+
mod.initializeSync();
157+
158+
// Define a vector data type:
159+
var dtype = 'float64';
160+
161+
// Specify a vector length:
162+
var N = 5;
163+
164+
// Define a pointer (i.e., byte offset) 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 [`Float64Array`][@stdlib/array/float64] pointer (i.e., byte offset).
179+
- **sx**: index increment for `x`.
180+
181+
#### dnrm2.Module.prototype.ndarray( N, xp, sx, ox )
182+
183+
Computes the L2-norm of a double-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 dnrm2.Module( mem );
199+
// returns <Module>
200+
201+
// Initialize the routine:
202+
mod.initializeSync();
203+
204+
// Define a vector data type:
205+
var dtype = 'float64';
206+
207+
// Specify a vector length:
208+
var N = 5;
209+
210+
// Define a pointer (i.e., byte offset) 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 `dnrm2` 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/dnrm2`][@stdlib/blas/base/dnrm2]. 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/dnrm2`][@stdlib/blas/base/dnrm2]. 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+
- `dnrm2()` corresponds to the [BLAS][blas] level 1 function [`dnrm2`][dnrm2].
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 dnrm2 = require( '@stdlib/blas/base/wasm/dnrm2' );
254+
255+
var opts = {
256+
'dtype': 'float64'
257+
};
258+
var x = discreteUniform( 10, 0, 100, opts );
259+
console.log( x );
260+
261+
var out = dnrm2.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+
[dnrm2]: http://www.netlib.org/lapack/explore-html/de/da4/group__double__blas__level1.html
284+
285+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
286+
287+
[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64
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/dnrm2]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/dnrm2
294+
295+
</section>
296+
297+
<!-- /.links -->

0 commit comments

Comments
 (0)