Skip to content

Commit d7c6f8e

Browse files
ShabiShett07kgrytestdlib-bot
authored
feat: add blas/base/wasm/sswap
PR-URL: #6582 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 9a3b220 commit d7c6f8e

33 files changed

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

0 commit comments

Comments
 (0)