Skip to content

Commit

Permalink
feat: update JavaScript implementation and add C ndarray implementa…
Browse files Browse the repository at this point in the history
…tion for `blas/base/idamax`

PR-URL: #2980
Ref: #2039
Co-authored-by: Athan Reines <kgryte@gmail.com>
Reviewed-by: Athan Reines <kgryte@gmail.com> 
Signed-off-by: Athan Reines <kgryte@gmail.com>
  • Loading branch information
aman-095 and kgryte authored Oct 2, 2024
1 parent 206c660 commit 86b103d
Show file tree
Hide file tree
Showing 15 changed files with 523 additions and 148 deletions.
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

1 comment on commit 86b103d

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
blas/base/idamax $\color{green}369/369$
$\color{green}+100.00\%$
$\color{green}20/20$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}369/369$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.