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/idamax #2980

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions lib/node_modules/@stdlib/blas/base/idamax/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,130 @@ 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/idamax.h"
```

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

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

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

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

The function accepts the following arguments:

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

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

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

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

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

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

The function accepts the following arguments:

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

```c
CBLAS_INT c_idamax_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/idamax.h"
#include <stdio.h>

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

// 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_idamax( N, x, strideX );

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

// Compute the index of the maximum absolute value:
idx = c_idamax_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 double rand_double( 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;
double 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;
double x[ len ];
double t;
int idx;
int i;

for ( i = 0; i < len; i++ ) {
x[ i ] = ( rand_double()*20000.0 ) - 10000.0;
}
idx = -1;
t = tic();
for ( i = 0; i < iterations; i++ ) {
idx = c_idamax_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_idamax_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 IDAMAX_H
#define IDAMAX_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_idamax( const int N, const double *X, const int strideX );
CBLAS_INT API_SUFFIX(c_idamax)( const CBLAS_INT N, const double *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_idamax_ndarray)( const CBLAS_INT N, const double *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 IDAMAX_CLBAS_H
#define IDAMAX_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_idamax( const int N, const double *X, const int strideX );
CBLAS_INT API_SUFFIX(cblas_idamax)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );

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

// MODULES //

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


// MAIN //
Expand All @@ -42,43 +43,8 @@ var abs = require( '@stdlib/math/base/special/abs' );
* // returns 4
*/
function idamax( N, x, strideX ) {
var dmax;
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`...
dmax = abs( x[ 0 ] );
for ( i = 1; i < N; i++ ) {
v = abs( x[ i ] );
if ( v > dmax ) {
idx = i;
dmax = v;
}
}
return idx;
}
// Code for stride not equal to `1`...
dmax = abs( x[ 0 ] );
ix = strideX;
for ( i = 1; i < N; i++ ) {
v = abs( x[ ix ] );
if ( v > dmax ) {
idx = i;
dmax = 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/idamax/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( './idamax.native.js' );
var addon = require( './../src/addon.node' );


// MAIN //
Expand All @@ -45,13 +43,7 @@ var addon = require( './idamax.native.js' );
* // returns 3
*/
function idamax( 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