Skip to content

Commit b7ebe14

Browse files
ShabiShett07kgryte0PrashantYadav0stdlib-bot
authored
feat: add blas/ext/base/wasm/dnansumkbn2
PR-URL: #5735 Closes: #5732 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: JoyBoy <144602492+0PrashantYadav0@users.noreply.github.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent 6f8be0f commit b7ebe14

34 files changed

+4808
-1
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"options": {
3-
"task": "build"
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: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
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+
# dnansumkbn2
22+
23+
> Calculate the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using a second-order iterative Kahan–Babuška algorithm.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var dnansumkbn2 = require( '@stdlib/blas/ext/base/wasm/dnansumkbn2' );
37+
```
38+
39+
#### dnansumkbn2.main( N, x, strideX )
40+
41+
Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using a second-order iterative Kahan–Babuška algorithm.
42+
43+
```javascript
44+
var Float64Array = require( '@stdlib/array/float64' );
45+
46+
var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] );
47+
48+
var sum = dnansumkbn2.main( x.length, x, 1 );
49+
// returns 1.0
50+
```
51+
52+
The function has the following parameters:
53+
54+
- **N**: number of indexed elements.
55+
- **x**: input [`Float64Array`][@stdlib/array/float64].
56+
- **strideX**: stride length for `x`.
57+
58+
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to compute the sum of every other element in `x`,
59+
60+
```javascript
61+
var Float64Array = require( '@stdlib/array/float64' );
62+
63+
var x = new Float64Array( [ 1.0, 2.0, NaN, -7.0, NaN, 3.0, 4.0, 2.0 ] );
64+
65+
var v = dnansumkbn2.main( 4, x, 2 );
66+
// returns 5.0
67+
```
68+
69+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
70+
71+
<!-- eslint-disable stdlib/capitalized-comments -->
72+
73+
```javascript
74+
var Float64Array = require( '@stdlib/array/float64' );
75+
76+
var x0 = new Float64Array( [ 2.0, 1.0, NaN, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
77+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
78+
79+
var v = dnansumkbn2.main( 4, x1, 2 );
80+
// returns 5.0
81+
```
82+
83+
#### dnansumkbn2.ndarray( N, x, strideX, offsetX )
84+
85+
Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using a second-order iterative Kahan–Babuška algorithm and alternative indexing semantics.
86+
87+
```javascript
88+
var Float64Array = require( '@stdlib/array/float64' );
89+
90+
var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] );
91+
92+
var v = dnansumkbn2.ndarray( x.length, x, 1, 0 );
93+
// returns 1.0
94+
```
95+
96+
The function has the following additional parameters:
97+
98+
- **offsetX**: starting index for `x`.
99+
100+
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 every other element starting from the second element:
101+
102+
```javascript
103+
var Float64Array = require( '@stdlib/array/float64' );
104+
105+
var x = new Float64Array( [ 2.0, 1.0, NaN, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
106+
107+
var v = dnansumkbn2.ndarray( 4, x, 2, 1 );
108+
// returns 5.0
109+
```
110+
111+
* * *
112+
113+
### Module
114+
115+
#### dnansumkbn2.Module( memory )
116+
117+
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.
118+
119+
<!-- eslint-disable node/no-sync -->
120+
121+
```javascript
122+
var Memory = require( '@stdlib/wasm/memory' );
123+
124+
// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
125+
var mem = new Memory({
126+
'initial': 10,
127+
'maximum': 100
128+
});
129+
130+
// Create a BLAS routine:
131+
var mod = new dnansumkbn2.Module( mem );
132+
// returns <Module>
133+
134+
// Initialize the routine:
135+
mod.initializeSync();
136+
```
137+
138+
#### dnansumkbn2.Module.prototype.main( N, xp, sx )
139+
140+
Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using a second-order iterative Kahan–Babuška algorithm.
141+
142+
<!-- eslint-disable node/no-sync -->
143+
144+
```javascript
145+
var Float64Array = require( '@stdlib/array/float64' );
146+
var Memory = require( '@stdlib/wasm/memory' );
147+
148+
// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
149+
var mem = new Memory({
150+
'initial': 10,
151+
'maximum': 100
152+
});
153+
154+
// Create a BLAS routine:
155+
var mod = new dnansumkbn2.Module( mem );
156+
// returns <Module>
157+
158+
// Initialize the routine:
159+
mod.initializeSync();
160+
161+
// Specify a vector length:
162+
var N = 4;
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, new Float64Array( [ 1.0, 2.0, 3.0, NaN ] ) );
169+
170+
// Perform computation:
171+
var sum = mod.main( N, xptr, 1 );
172+
// returns 6.0
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**: stride length for `x`.
180+
181+
#### dnansumkbn2.Module.prototype.ndarray( N, xp, sx, ox )
182+
183+
Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using a second-order iterative Kahan–Babuška algorithm and alternative indexing semantics.
184+
185+
<!-- eslint-disable node/no-sync -->
186+
187+
```javascript
188+
var Float64Array = require( '@stdlib/array/float64' );
189+
var Memory = require( '@stdlib/wasm/memory' );
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 dnansumkbn2.Module( mem );
199+
// returns <Module>
200+
201+
// Initialize the routine:
202+
mod.initializeSync();
203+
204+
// Specify a vector length:
205+
var N = 4;
206+
207+
// Define a pointer (i.e., byte offset) for storing the input vector:
208+
var xptr = 0;
209+
210+
// Write vector values to module memory:
211+
mod.write( xptr, new Float64Array( [ 1.0, 2.0, 3.0, NaN ] ) );
212+
213+
// Perform computation:
214+
var sum = mod.ndarray( N, xptr, 1, 0 );
215+
// returns 6.0
216+
```
217+
218+
The function has the following additional parameters:
219+
220+
- **ox**: starting index for `x`.
221+
222+
</section>
223+
224+
<!-- /.usage -->
225+
226+
<section class="notes">
227+
228+
* * *
229+
230+
## Notes
231+
232+
- If `N <= 0`, both `main` and `ndarray` methods return `0.0`.
233+
- This package implements routines using WebAssembly. When provided arrays which are not allocated on a `dnansumkbn2` 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/dnansumkbn2`][@stdlib/blas/ext/base/dnansumkbn2]. 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/dnansumkbn2`][@stdlib/blas/ext/base/dnansumkbn2]. 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.
234+
235+
</section>
236+
237+
<!-- /.notes -->
238+
239+
<section class="examples">
240+
241+
* * *
242+
243+
## Examples
244+
245+
<!-- eslint no-undef: "error" -->
246+
247+
```javascript
248+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
249+
var dnansumkbn2 = require( '@stdlib/blas/ext/base/wasm/dnansumkbn2' );
250+
251+
var opts = {
252+
'dtype': 'float64'
253+
};
254+
var x = discreteUniform( 10, 0, 100, opts );
255+
console.log( x );
256+
257+
var sum = dnansumkbn2.ndarray( x.length, x, 1, 0 );
258+
console.log( sum );
259+
```
260+
261+
</section>
262+
263+
<!-- /.examples -->
264+
265+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
266+
267+
<section class="related">
268+
269+
</section>
270+
271+
<!-- /.related -->
272+
273+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
274+
275+
<section class="links">
276+
277+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
278+
279+
[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64
280+
281+
[@stdlib/wasm/memory]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/memory
282+
283+
[@stdlib/wasm/module-wrapper]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/module-wrapper
284+
285+
[@stdlib/blas/ext/base/dnansumkbn2]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/dnansumkbn2
286+
287+
</section>
288+
289+
<!-- /.links -->

0 commit comments

Comments
 (0)