Skip to content

Commit c1395e3

Browse files
committed
feat: add blas/base/wasm/sdsdot
--- 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 6b367a1 commit c1395e3

33 files changed

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

0 commit comments

Comments
 (0)