Skip to content

Commit 3409af0

Browse files
aman-095kgryte
andauthored
feat: add blas/base/srot-wasm
PR-URL: #3028 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent e2ed2a3 commit 3409af0

32 files changed

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

0 commit comments

Comments
 (0)