Skip to content

Commit 47043f4

Browse files
0PrashantYadav0kgrytestdlib-bot
authored
feat: add blas/ext/base/wasm/dapx
PR-URL: #5643 Closes: #5504 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent fcda856 commit 47043f4

34 files changed

+4444
-1
lines changed

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

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

0 commit comments

Comments
 (0)