Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update JavaScript implementation and add C ndarray implementation for blas/base/isamax #2931

Merged
merged 11 commits into from
Oct 2, 2024
123 changes: 123 additions & 0 deletions lib/node_modules/@stdlib/blas/base/isamax/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,129 @@ console.log( idx );

<!-- /.examples -->

<!-- C interface documentation. -->

* * *

<section class="c">

## C APIs

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- C usage documentation. -->

<section class="usage">

### Usage

```c
#include "stdlib/blas/base/isamax.h"
```

#### c_isamax( N, \*X, strideX )

Finds the index of the first element having the maximum absolute value.

```c
const float x[] = { 4.0f, 2.0f, -3.0f, 5.0f, -1.0f };

int idx = c_isamax( 5, x, 1 );
// returns 3
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **X**: `[in] float*` first input array.
- **strideX**: `[in] CBLAS_INT` index increment for `X`.

```c
CBLAS_INT c_isamax( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );
```

#### c_isamax_ndarray( N, \*X, strideX, offsetX )

Finds the index of the first element having the maximum absolute value using alternative indexing semantics.

```c
const float x[] = { 4.0f, 2.0f, -3.0f, 5.0f, -1.0f };

int idx = c_isamax_ndarray( 5, x, 1, 0 );
// returns 3
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **X**: `[in] float*` first input array.
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.

```c
CBLAS_INT c_isamax_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
```

</section>

<!-- /.usage -->

<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- C API usage examples. -->

<section class="examples">

### Examples

```c
#include "stdlib/blas/base/isamax.h"
#include <stdio.h>

int main( void ) {
// Create strided array:
const float x[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f };

// Specify the number of element:
const int N = 8;

// Specify stride:
const int strideX = 1;

// Compute the index of the maximum absolute value:
int idx = c_isamax( N, x, strideX );

// Print the result:
printf( "index value: %d\n", idx );

// Compute the index of the maximum absolute value:
idx = c_isamax_ndarray( N, x, -strideX, N-1 );

// Print the result:
printf( "index value: %d\n", idx );
}
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ static float rand_float( void ) {
* @param len array length
* @return elapsed time in seconds
*/
static double benchmark( int iterations, int len ) {
static double benchmark1( int iterations, int len ) {
double elapsed;
float x[ len ];
double t;
Expand All @@ -120,6 +120,39 @@ static double benchmark( int iterations, int len ) {
return elapsed;
}

/**
* Runs a benchmark.
*
* @param iterations number of iterations
* @param len array length
* @return elapsed time in seconds
*/
static double benchmark2( int iterations, int len ) {
double elapsed;
float x[ len ];
double t;
int idx;
int i;

for ( i = 0; i < len; i++ ) {
x[ i ] = ( rand_float()*20000.0f ) - 10000.0f;
}
idx = -1;
t = tic();
for ( i = 0; i < iterations; i++ ) {
idx = c_isamax_ndarray( len, x, 1, 0 );
if ( idx < -2 ) {
printf( "unexpected result\n" );
break;
}
}
elapsed = tic() - t;
if ( idx < -2 ) {
printf( "unexpected result\n" );
}
return elapsed;
}

/**
* Main execution sequence.
*/
Expand All @@ -142,7 +175,14 @@ int main( void ) {
for ( j = 0; j < REPEATS; j++ ) {
count += 1;
printf( "# c::%s:len=%d\n", NAME, len );
elapsed = benchmark( iter, len );
elapsed = benchmark1( iter, len );
print_results( iter, elapsed );
printf( "ok %d benchmark finished\n", count );
}
for ( j = 0; j < REPEATS; j++ ) {
count += 1;
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
elapsed = benchmark2( iter, len );
print_results( iter, elapsed );
printf( "ok %d benchmark finished\n", count );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ int main( void ) {

// Print the result:
printf( "index value: %d\n", idx );

// Compute the index of the maximum absolute value:
idx = c_isamax_ndarray( N, x, -strideX, N-1 );

// Print the result:
printf( "index value: %d\n", idx );
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#ifndef ISAMAX_H
#define ISAMAX_H

#include "stdlib/blas/base/shared.h"

/*
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
*/
Expand All @@ -32,7 +34,12 @@ extern "C" {
/**
* Finds the index of the first element having the maximum absolute value.
*/
int c_isamax( const int N, const float *X, const int strideX );
CBLAS_INT API_SUFFIX(c_isamax)( const CBLAS_INT N, const float *X, const CBLAS_INT strideX );

/**
* Finds the index of the first element having the maximum absolute value using alternative indexing semantics.
*/
CBLAS_INT API_SUFFIX(c_isamax_ndarray)( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );

#ifdef __cplusplus
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#ifndef ISAMAX_CLBAS_H
#define ISAMAX_CBLAS_H

#include "stdlib/blas/base/shared.h"

/*
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
*/
Expand All @@ -32,7 +34,7 @@ extern "C" {
/**
* Finds the index of the first element having the maximum absolute value.
*/
int cblas_ISAMAX( const int N, const float *X, const int strideX );
CBLAS_INT API_SUFFIX(cblas_isamax)( const CBLAS_INT N, const float *X, const CBLAS_INT strideX );

#ifdef __cplusplus
}
Expand Down
42 changes: 4 additions & 38 deletions lib/node_modules/@stdlib/blas/base/isamax/lib/isamax.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

// MODULES //

var absf = require( '@stdlib/math/base/special/absf' );
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
var ndarray = require( './ndarray.js' );


// MAIN //
Expand All @@ -42,43 +43,8 @@ var absf = require( '@stdlib/math/base/special/absf' );
* // returns 4
*/
function isamax( N, x, strideX ) {
var smax;
var idx;
var ix;
var v;
var i;

if ( N < 1 || strideX <= 0 ) {
return -1;
}
idx = 0;
if ( N === 1 ) {
return idx;
}
if (strideX === 1 ) {
// Code for stride equal to `1`...
smax = absf( x[ 0 ] );
for ( i = 1; i < N; i++ ) {
v = absf( x[ i ] );
if ( v > smax ) {
idx = i;
smax = v;
}
}
return idx;
}
// Code for stride not equal to `1`...
smax = absf( x[ 0 ] );
ix = strideX;
for ( i = 1; i < N; i++ ) {
v = absf( x[ ix ] );
if ( v > smax ) {
idx = i;
smax = v;
}
ix += strideX;
}
return idx;
var ox = stride2offset( N, strideX );
return ndarray( N, x, strideX, ox );
}


Expand Down
12 changes: 2 additions & 10 deletions lib/node_modules/@stdlib/blas/base/isamax/lib/ndarray.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@

// MODULES //

var minViewBufferIndex = require( '@stdlib/strided/base/min-view-buffer-index' );
var offsetView = require( '@stdlib/strided/base/offset-view' );
var addon = require( './isamax.native.js' );
var addon = require( './../src/addon.node' );


// MAIN //
Expand All @@ -45,13 +43,7 @@ var addon = require( './isamax.native.js' );
* // returns 3
*/
function isamax( N, x, strideX, offsetX ) {
var viewX;
offsetX = minViewBufferIndex( N, strideX, offsetX );
viewX = offsetView( x, offsetX );
if ( strideX < 0 ) {
return N - 1 - addon( N, viewX, -strideX );
}
return addon( N, viewX, strideX );
return addon.ndarray( N, x, strideX, offsetX );
}


Expand Down
Loading
Loading