Skip to content

Commit 402bd40

Browse files
aman-095kgryte
andauthored
feat: add blas/base/wasm/sscal
PR-URL: #5224 Ref: #2039 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 86d5fea commit 402bd40

33 files changed

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

0 commit comments

Comments
 (0)