Skip to content

Commit dae553b

Browse files
feat: add blas/base/wasm/dsdot
PR-URL: #6751 Ref: #2039 Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 8b784ff commit dae553b

33 files changed

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

0 commit comments

Comments
 (0)