Skip to content

Commit bd0050a

Browse files
0PrashantYadav0kgrytestdlib-bot
authored
feat: add blas/ext/base/wasm/dnanasumors
PR-URL: #6110 Ref: #5809 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent 14fc1c1 commit bd0050a

34 files changed

+4542
-2
lines changed

lib/node_modules/@stdlib/blas/ext/base/dnanasumors/manifest.json

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
2-
"options": {
3-
"task": "build"
2+
"options": {
3+
"task": "build",
4+
"wasm": false
45
},
56
"fields": [
67
{
@@ -27,6 +28,7 @@
2728
"confs": [
2829
{
2930
"task": "build",
31+
"wasm": false,
3032
"src": [
3133
"./src/main.c"
3234
],
@@ -49,6 +51,7 @@
4951
},
5052
{
5153
"task": "benchmark",
54+
"wasm": false,
5255
"src": [
5356
"./src/main.c"
5457
],
@@ -66,6 +69,25 @@
6669
},
6770
{
6871
"task": "examples",
72+
"wasm": false,
73+
"src": [
74+
"./src/main.c"
75+
],
76+
"include": [
77+
"./include"
78+
],
79+
"libraries": [],
80+
"libpath": [],
81+
"dependencies": [
82+
"@stdlib/math/base/assert/is-nan",
83+
"@stdlib/math/base/special/abs",
84+
"@stdlib/blas/base/shared",
85+
"@stdlib/strided/base/stride2offset"
86+
]
87+
},
88+
{
89+
"task": "build",
90+
"wasm": true,
6991
"src": [
7092
"./src/main.c"
7193
],
Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
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+
# dnanasumors
22+
23+
> Calculate the sum of absolute values ([_L1_ norm][l1norm]) of double-precision floating-point strided array elements, ignoring `NaN` values and using ordinary recursive summation.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var dnanasumors = require( '@stdlib/blas/ext/base/wasm/dnanasumors' );
31+
```
32+
33+
#### dnanasumors.main( N, x, strideX )
34+
35+
Computes the sum of absolute values ([_L1_ norm][l1norm]) of double-precision floating-point strided array elements, ignoring `NaN` values and using ordinary recursive summation.
36+
37+
```javascript
38+
var Float64Array = require( '@stdlib/array/float64' );
39+
40+
var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] );
41+
42+
var v = dnanasumors.main( x.length, x, 1 );
43+
// returns 5.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**: stride length.
51+
52+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the sum of absolute values ([_L1_ norm][l1norm]) for every other element:
53+
54+
```javascript
55+
var Float64Array = require( '@stdlib/array/float64' );
56+
57+
var x = new Float64Array( [ 1.0, 2.0, NaN, -7.0, NaN, 3.0, 4.0, 2.0 ] );
58+
59+
var sum = dnanasumors.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+
var x0 = new Float64Array( [ 2.0, 1.0, NaN, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
71+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
72+
73+
var v = dnanasumors.main( 4, x1, 2 );
74+
// returns 9.0
75+
```
76+
77+
#### dnanasumors.ndarray( N, x, strideX, offsetX )
78+
79+
Computes the sum of absolute values ([_L1_ norm][l1norm]) of double-precision floating-point strided array elements, ignoring `NaN` values and using ordinary recursive summation and alternative indexing semantics.
80+
81+
```javascript
82+
var Float64Array = require( '@stdlib/array/float64' );
83+
84+
var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] );
85+
86+
var v = dnanasumors.ndarray( x.length, x, 1, 0 );
87+
// returns 5.0
88+
```
89+
90+
The function has the following additional parameters:
91+
92+
- **offsetX**: starting index.
93+
94+
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 sum of absolute values ([_L1_ norm][l1norm]) for every other value in the strided array starting from the second value:
95+
96+
```javascript
97+
var Float64Array = require( '@stdlib/array/float64' );
98+
99+
var x = new Float64Array( [ 2.0, 1.0, NaN, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
100+
101+
var v = dnanasumors.ndarray( 4, x, 2, 1 );
102+
// returns 9.0
103+
```
104+
105+
* * *
106+
107+
### Module
108+
109+
#### dnanasumors.Module( memory )
110+
111+
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.
112+
113+
<!-- eslint-disable node/no-sync -->
114+
115+
```javascript
116+
var Memory = require( '@stdlib/wasm/memory' );
117+
118+
// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
119+
var mem = new Memory({
120+
'initial': 10,
121+
'maximum': 100
122+
});
123+
124+
// Create a BLAS routine:
125+
var mod = new dnanasumors.Module( mem );
126+
// returns <Module>
127+
128+
// Initialize the routine:
129+
mod.initializeSync();
130+
```
131+
132+
#### dnanasumors.Module.prototype.main( N, xp, sx )
133+
134+
Computes the sum of absolute values ([_L1_ norm][l1norm]) of double-precision floating-point strided array elements, ignoring `NaN` values and using ordinary recursive summation.
135+
136+
<!-- eslint-disable node/no-sync -->
137+
138+
```javascript
139+
var Memory = require( '@stdlib/wasm/memory' );
140+
var oneTo = require( '@stdlib/array/one-to' );
141+
var zeros = require( '@stdlib/array/zeros' );
142+
143+
// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
144+
var mem = new Memory({
145+
'initial': 10,
146+
'maximum': 100
147+
});
148+
149+
// Create a BLAS routine:
150+
var mod = new dnanasumors.Module( mem );
151+
// returns <Module>
152+
153+
// Initialize the routine:
154+
mod.initializeSync();
155+
156+
// Define a vector data type:
157+
var dtype = 'float64';
158+
159+
// Specify a vector length:
160+
var N = 3;
161+
162+
// Define a pointer (i.e., byte offset) for storing the input vector:
163+
var xptr = 0;
164+
165+
// Write vector values to module memory:
166+
mod.write( xptr, oneTo( N, dtype ) );
167+
168+
// Perform computation:
169+
var v = mod.main( N, xptr, 1 );
170+
// returns 6.0
171+
```
172+
173+
The function has the following parameters:
174+
175+
- **N**: number of indexed elements.
176+
- **xp**: input [`Float64Array`][@stdlib/array/float64] pointer (i.e., byte offset).
177+
- **sx**: stride length.
178+
179+
#### dnanasumors.Module.prototype.ndarray( N, xp, sx, ox )
180+
181+
Computes the sum of absolute values ([_L1_ norm][l1norm]) of double-precision floating-point strided array elements, ignoring `NaN` values and using ordinary recursive summation and alternative indexing semantics.
182+
183+
<!-- eslint-disable node/no-sync -->
184+
185+
```javascript
186+
var Memory = require( '@stdlib/wasm/memory' );
187+
var oneTo = require( '@stdlib/array/one-to' );
188+
var zeros = require( '@stdlib/array/zeros' );
189+
190+
// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
191+
var mem = new Memory({
192+
'initial': 10,
193+
'maximum': 100
194+
});
195+
196+
// Create a BLAS routine:
197+
var mod = new dnanasumors.Module( mem );
198+
// returns <Module>
199+
200+
// Initialize the routine:
201+
mod.initializeSync();
202+
203+
// Define a vector data type:
204+
var dtype = 'float64';
205+
206+
// Specify a vector length:
207+
var N = 3;
208+
209+
// Define a pointer (i.e., byte offset) for storing the input vector:
210+
var xptr = 0;
211+
212+
// Write vector values to module memory:
213+
mod.write( xptr, oneTo( N, dtype ) );
214+
215+
// Perform computation:
216+
var v = mod.ndarray( N, xptr, 1, 0 );
217+
// returns 6.0
218+
```
219+
220+
The function has the following additional parameters:
221+
222+
- **ox**: starting index.
223+
224+
</section>
225+
226+
<!-- /.usage -->
227+
228+
<section class="notes">
229+
230+
* * *
231+
232+
## Notes
233+
234+
- If `N <= 0`, both functions return `0.0`.
235+
- Ordinary recursive summation (i.e., a "simple" sum) is performant, but can incur significant numerical error. If performance is paramount and error tolerated, using ordinary recursive summation is acceptable; in all other cases, exercise due caution.
236+
- This package implements routines using WebAssembly. When provided arrays which are not allocated on a `dnanasumors` 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/ext/base/dnanasumors`][@stdlib/blas/ext/base/dnanasumors]. 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/ext/base/dnanasumors`][@stdlib/blas/ext/base/dnanasumors]. 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+
238+
</section>
239+
240+
<!-- /.notes -->
241+
242+
<section class="examples">
243+
244+
* * *
245+
246+
## Examples
247+
248+
<!-- eslint no-undef: "error" -->
249+
250+
```javascript
251+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
252+
var dnanasumors = require( '@stdlib/blas/ext/base/wasm/dnanasumors' );
253+
254+
var opts = {
255+
'dtype': 'float64'
256+
};
257+
var x = discreteUniform( 10, 0, 100, opts );
258+
console.log( x );
259+
260+
var v = dnanasumors.ndarray( x.length, x, 1, 0 );
261+
console.log( v );
262+
```
263+
264+
</section>
265+
266+
<!-- /.examples -->
267+
268+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
269+
270+
<section class="related">
271+
272+
</section>
273+
274+
<!-- /.related -->
275+
276+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
277+
278+
<section class="links">
279+
280+
[l1norm]: https://en.wikipedia.org/wiki/Norm_%28mathematics%29
281+
282+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
283+
284+
[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64
285+
286+
[@stdlib/wasm/memory]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/memory
287+
288+
[@stdlib/wasm/module-wrapper]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/module-wrapper
289+
290+
[@stdlib/blas/ext/base/dnanasumors]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/dnanasumors
291+
292+
</section>
293+
294+
<!-- /.links -->

0 commit comments

Comments
 (0)