Skip to content

Commit 8dc3f35

Browse files
gururaj1512kgryte
andauthored
feat: add blas/base/wasm/srotm
PR-URL: https://github.com/stodlib-js/stdlib/pull/5824 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Gururaj Gurram <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent b419e98 commit 8dc3f35

33 files changed

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

0 commit comments

Comments
 (0)