Skip to content

Commit 6bbedfa

Browse files
aman-095kgrytestdlib-bot
authored andcommitted
feat: add blas/base/wasm/isamax
PR-URL: stdlib-js#3984 Ref: stdlib-js#2039 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent 8cb5cec commit 6bbedfa

33 files changed

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

0 commit comments

Comments
 (0)