Skip to content

Commit 86b103d

Browse files
aman-095kgryte
andauthored
feat: update JavaScript implementation and add C ndarray implementation for blas/base/idamax
PR-URL: #2980 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent 206c660 commit 86b103d

File tree

15 files changed

+523
-148
lines changed

15 files changed

+523
-148
lines changed

lib/node_modules/@stdlib/blas/base/idamax/README.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,130 @@ console.log( idx );
143143

144144
<!-- /.examples -->
145145

146+
<!-- C interface documentation. -->
147+
148+
* * *
149+
150+
<section class="c">
151+
152+
## C APIs
153+
154+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
155+
156+
<section class="intro">
157+
158+
</section>
159+
160+
<!-- /.intro -->
161+
162+
<!-- C usage documentation. -->
163+
164+
<section class="usage">
165+
166+
### Usage
167+
168+
```c
169+
#include "stdlib/blas/base/idamax.h"
170+
```
171+
172+
#### c_idamax( N, \*X, strideX )
173+
174+
Finds the index of the first element having the maximum absolute value.
175+
176+
```c
177+
const double x[] = { 4.0, 2.0, -3.0, 5.0, -1.0 };
178+
179+
int idx = c_idamax( 5, x, 1 );
180+
// returns 3
181+
```
182+
183+
The function accepts the following arguments:
184+
185+
- **N**: `[in] CBLAS_INT` number of indexed elements.
186+
- **X**: `[in] double*` input array.
187+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
188+
189+
```c
190+
CBLAS_INT c_idamax( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );
191+
```
192+
193+
#### c_idamax_ndarray( N, \*X, strideX, offsetX )
194+
195+
Finds the index of the first element having the maximum absolute value using alternative indexing semantics.
196+
197+
```c
198+
const double x[] = { 4.0, 2.0, -3.0, 5.0, -1.0 };
199+
200+
int idx = c_idamax_ndarray( 5, x, 1, 0 );
201+
// returns 3
202+
```
203+
204+
The function accepts the following arguments:
205+
206+
- **N**: `[in] CBLAS_INT` number of indexed elements.
207+
- **X**: `[in] double*` input array.
208+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
209+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
210+
211+
```c
212+
CBLAS_INT c_idamax_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
213+
```
214+
215+
</section>
216+
217+
<!-- /.usage -->
218+
219+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
220+
221+
<section class="notes">
222+
223+
</section>
224+
225+
<!-- /.notes -->
226+
227+
<!-- C API usage examples. -->
228+
229+
<section class="examples">
230+
231+
### Examples
232+
233+
```c
234+
#include "stdlib/blas/base/idamax.h"
235+
#include <stdio.h>
236+
237+
int main( void ) {
238+
// Create strided array:
239+
const double x[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 };
240+
241+
// Specify the number of element:
242+
const int N = 8;
243+
244+
// Specify stride:
245+
const int strideX = 1;
246+
247+
// Compute the index of the maximum absolute value:
248+
int idx = c_idamax( N, x, strideX );
249+
250+
// Print the result:
251+
printf( "index value: %d\n", idx );
252+
253+
// Compute the index of the maximum absolute value:
254+
idx = c_idamax_ndarray( N, x, -strideX, N-1 );
255+
256+
// Print the result:
257+
printf( "index value: %d\n", idx );
258+
}
259+
```
260+
261+
</section>
262+
263+
<!-- /.examples -->
264+
265+
</section>
266+
267+
<!-- /.c -->
268+
269+
146270
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
147271
148272
<section class="related">

lib/node_modules/@stdlib/blas/base/idamax/benchmark/c/benchmark.length.c

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static double rand_double( void ) {
9494
* @param len array length
9595
* @return elapsed time in seconds
9696
*/
97-
static double benchmark( int iterations, int len ) {
97+
static double benchmark1( int iterations, int len ) {
9898
double elapsed;
9999
double x[ len ];
100100
double t;
@@ -120,6 +120,39 @@ static double benchmark( int iterations, int len ) {
120120
return elapsed;
121121
}
122122

123+
/**
124+
* Runs a benchmark.
125+
*
126+
* @param iterations number of iterations
127+
* @param len array length
128+
* @return elapsed time in seconds
129+
*/
130+
static double benchmark2( int iterations, int len ) {
131+
double elapsed;
132+
double x[ len ];
133+
double t;
134+
int idx;
135+
int i;
136+
137+
for ( i = 0; i < len; i++ ) {
138+
x[ i ] = ( rand_double()*20000.0 ) - 10000.0;
139+
}
140+
idx = -1;
141+
t = tic();
142+
for ( i = 0; i < iterations; i++ ) {
143+
idx = c_idamax_ndarray( len, x, 1, 0 );
144+
if ( idx < -2 ) {
145+
printf( "unexpected result\n" );
146+
break;
147+
}
148+
}
149+
elapsed = tic() - t;
150+
if ( idx < -2 ) {
151+
printf( "unexpected result\n" );
152+
}
153+
return elapsed;
154+
}
155+
123156
/**
124157
* Main execution sequence.
125158
*/
@@ -142,7 +175,14 @@ int main( void ) {
142175
for ( j = 0; j < REPEATS; j++ ) {
143176
count += 1;
144177
printf( "# c::%s:len=%d\n", NAME, len );
145-
elapsed = benchmark( iter, len );
178+
elapsed = benchmark1( iter, len );
179+
print_results( iter, elapsed );
180+
printf( "ok %d benchmark finished\n", count );
181+
}
182+
for ( j = 0; j < REPEATS; j++ ) {
183+
count += 1;
184+
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
185+
elapsed = benchmark2( iter, len );
146186
print_results( iter, elapsed );
147187
printf( "ok %d benchmark finished\n", count );
148188
}

lib/node_modules/@stdlib/blas/base/idamax/examples/c/example.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,10 @@ int main( void ) {
3434

3535
// Print the result:
3636
printf( "index value: %d\n", idx );
37+
38+
// Compute the index of the maximum absolute value:
39+
idx = c_idamax_ndarray( N, x, -strideX, N-1 );
40+
41+
// Print the result:
42+
printf( "index value: %d\n", idx );
3743
}

lib/node_modules/@stdlib/blas/base/idamax/include/stdlib/blas/base/idamax.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#ifndef IDAMAX_H
2323
#define IDAMAX_H
2424

25+
#include "stdlib/blas/base/shared.h"
26+
2527
/*
2628
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
2729
*/
@@ -32,7 +34,12 @@ extern "C" {
3234
/**
3335
* Finds the index of the first element having the maximum absolute value.
3436
*/
35-
int c_idamax( const int N, const double *X, const int strideX );
37+
CBLAS_INT API_SUFFIX(c_idamax)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );
38+
39+
/**
40+
* Finds the index of the first element having the maximum absolute value using alternative indexing semantics.
41+
*/
42+
CBLAS_INT API_SUFFIX(c_idamax_ndarray)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
3643

3744
#ifdef __cplusplus
3845
}

lib/node_modules/@stdlib/blas/base/idamax/include/stdlib/blas/base/idamax_cblas.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#ifndef IDAMAX_CLBAS_H
2323
#define IDAMAX_CBLAS_H
2424

25+
#include "stdlib/blas/base/shared.h"
26+
2527
/*
2628
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
2729
*/
@@ -32,7 +34,7 @@ extern "C" {
3234
/**
3335
* Finds the index of the first element having the maximum absolute value.
3436
*/
35-
int cblas_idamax( const int N, const double *X, const int strideX );
37+
CBLAS_INT API_SUFFIX(cblas_idamax)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );
3638

3739
#ifdef __cplusplus
3840
}

lib/node_modules/@stdlib/blas/base/idamax/lib/idamax.js

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020

2121
// MODULES //
2222

23-
var abs = require( '@stdlib/math/base/special/abs' );
23+
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
24+
var ndarray = require( './ndarray.js' );
2425

2526

2627
// MAIN //
@@ -42,43 +43,8 @@ var abs = require( '@stdlib/math/base/special/abs' );
4243
* // returns 4
4344
*/
4445
function idamax( N, x, strideX ) {
45-
var dmax;
46-
var idx;
47-
var ix;
48-
var v;
49-
var i;
50-
51-
if ( N < 1 || strideX <= 0 ) {
52-
return -1;
53-
}
54-
idx = 0;
55-
if ( N === 1 ) {
56-
return idx;
57-
}
58-
if (strideX === 1 ) {
59-
// Code for stride equal to `1`...
60-
dmax = abs( x[ 0 ] );
61-
for ( i = 1; i < N; i++ ) {
62-
v = abs( x[ i ] );
63-
if ( v > dmax ) {
64-
idx = i;
65-
dmax = v;
66-
}
67-
}
68-
return idx;
69-
}
70-
// Code for stride not equal to `1`...
71-
dmax = abs( x[ 0 ] );
72-
ix = strideX;
73-
for ( i = 1; i < N; i++ ) {
74-
v = abs( x[ ix ] );
75-
if ( v > dmax ) {
76-
idx = i;
77-
dmax = v;
78-
}
79-
ix += strideX;
80-
}
81-
return idx;
46+
var ox = stride2offset( N, strideX );
47+
return ndarray( N, x, strideX, ox );
8248
}
8349

8450

lib/node_modules/@stdlib/blas/base/idamax/lib/ndarray.native.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020

2121
// MODULES //
2222

23-
var minViewBufferIndex = require( '@stdlib/strided/base/min-view-buffer-index' );
24-
var offsetView = require( '@stdlib/strided/base/offset-view' );
25-
var addon = require( './idamax.native.js' );
23+
var addon = require( './../src/addon.node' );
2624

2725

2826
// MAIN //
@@ -45,13 +43,7 @@ var addon = require( './idamax.native.js' );
4543
* // returns 3
4644
*/
4745
function idamax( N, x, strideX, offsetX ) {
48-
var viewX;
49-
offsetX = minViewBufferIndex( N, strideX, offsetX );
50-
viewX = offsetView( x, offsetX );
51-
if ( strideX < 0 ) {
52-
return N - 1 - addon( N, viewX, -strideX );
53-
}
54-
return addon( N, viewX, strideX );
46+
return addon.ndarray( N, x, strideX, offsetX );
5547
}
5648

5749

0 commit comments

Comments
 (0)