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: add C ndarray interface and refactor implementation for stats/base/scuminabs #4698

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,21 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/array/uniform' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var pow = require( '@stdlib/math/base/special/pow' );
var Float32Array = require( '@stdlib/array/float32' );
var pkg = require( './../package.json' ).name;
var scuminabs = require( './../lib/scuminabs.js' );


// VARIABLES //

var options = {
'dtype': 'float32'
};


// FUNCTIONS //

/**
Expand All @@ -39,15 +46,8 @@ var scuminabs = require( './../lib/scuminabs.js' );
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var y;
var x;
var i;

x = new Float32Array( len );
y = new Float32Array( len );
for ( i = 0; i < x.length; i++ ) {
x[ i ] = ( randu()*20.0 ) - 10.0;
}
var x = uniform( len, -10.0, 10.0, options );
var y = new Float32Array( len );
return benchmark;

function benchmark( b ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/array/uniform' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var pow = require( '@stdlib/math/base/special/pow' );
var Float32Array = require( '@stdlib/array/float32' );
Expand All @@ -36,6 +36,9 @@ var scuminabs = tryRequire( resolve( __dirname, './../lib/scuminabs.native.js' )
var opts = {
'skip': ( scuminabs instanceof Error )
};
var options = {
'dtype': 'float32'
};


// FUNCTIONS //
Expand All @@ -48,15 +51,8 @@ var opts = {
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x;
var y;
var i;

x = new Float32Array( len );
y = new Float32Array( len );
for ( i = 0; i < x.length; i++ ) {
x[ i ] = ( randu()*20.0 ) - 10.0;
}
var x = uniform( len, -10.0, 10.0, options );
var y = new Float32Array( len );
return benchmark;

function benchmark( b ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,21 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/array/uniform' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var pow = require( '@stdlib/math/base/special/pow' );
var Float32Array = require( '@stdlib/array/float32' );
var pkg = require( './../package.json' ).name;
var scuminabs = require( './../lib/ndarray.js' );


// VARIABLES //

var options = {
'dtype': 'float32'
};


// FUNCTIONS //

/**
Expand All @@ -39,15 +46,8 @@ var scuminabs = require( './../lib/ndarray.js' );
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x;
var y;
var i;

x = new Float32Array( len );
y = new Float32Array( len );
for ( i = 0; i < x.length; i++ ) {
x[ i ] = ( randu()*20.0 ) - 10.0;
}
var x = uniform( len, -10.0, 10.0, options );
var y = new Float32Array( len );
return benchmark;

function benchmark( b ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/array/uniform' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var pow = require( '@stdlib/math/base/special/pow' );
var Float32Array = require( '@stdlib/array/float32' );
Expand All @@ -36,6 +36,9 @@ var scuminabs = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) )
var opts = {
'skip': ( scuminabs instanceof Error )
};
var options = {
'dtype': 'float32'
};


// FUNCTIONS //
Expand All @@ -48,15 +51,8 @@ var opts = {
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x;
var y;
var i;

x = new Float32Array( len );
y = new Float32Array( len );
for ( i = 0; i < x.length; i++ ) {
x[ i ] = ( randu()*20.0 ) - 10.0;
}
var x = uniform( len, -10.0, 10.0, options );
var y = new Float32Array( len );
return benchmark;

function benchmark( b ) {
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 ];
float y[ len ];
Expand All @@ -121,6 +121,40 @@ 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 ];
float y[ len ];
double t;
int i;

for ( i = 0; i < len; i++ ) {
x[ i ] = ( rand_float() * 20000.0f ) - 10000.0f;
y[ i ] = 0.0f;
}
t = tic();
for ( i = 0; i < iterations; i++ ) {
x[ 0 ] += 1.0f;
stdlib_strided_scuminabs_ndarray( len, x, 1, 0, y, 1, 0 );
if ( y[ 0 ] != y[ 0 ] ) {
printf( "should not return NaN\n" );
break;
}
}
elapsed = tic() - t;
if ( y[ len-1 ] != y[ len-1 ] ) {
printf( "should not return NaN\n" );
}
return elapsed;
}

/**
* Main execution sequence.
*/
Expand All @@ -143,7 +177,18 @@ 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 ( i = MIN; i <= MAX; i++ ) {
len = pow( 10, i );
iter = ITERATIONS / pow( 10, i-1 );
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
24 changes: 11 additions & 13 deletions lib/node_modules/@stdlib/stats/base/scuminabs/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
Computes the cumulative minimum absolute value of single-precision floating-
point strided array elements.

The `N` and `stride` parameters determine which elements in `x` and `y` are
accessed at runtime.
The `N` and stride parameters determine which elements in the strided arrays
are accessed at runtime.

Indexing is relative to the first index. To introduce an offset, use a typed
array view.
Expand All @@ -20,13 +20,13 @@
Input array.

strideX: integer
Index increment for `x`.
Stride Length for `x`.

y: Float32Array
Output array.

strideY: integer
Index increment for `y`.
Stride Length for `y`.

Returns
-------
Expand All @@ -41,24 +41,23 @@
> {{alias}}( x.length, x, 1, y, 1 )
<Float32Array>[ 1.0, 1.0, 1.0 ]

// Using `N` and `stride` parameters:
// Using `N` and stride parameters:
> x = new {{alias:@stdlib/array/float32}}( [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0 ] );
> y = new {{alias:@stdlib/array/float32}}( x.length );
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
> {{alias}}( N, x, 2, y, 2 )
> {{alias}}( 3, x, 2, y, 2 )
<Float32Array>[ 2.0, 0.0, 1.0, 0.0, 1.0, 0.0 ]

// Using view offsets:
> var x0 = new {{alias:@stdlib/array/float32}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ] );
> var y0 = new {{alias:@stdlib/array/float32}}( x0.length );
> var x1 = new {{alias:@stdlib/array/float32}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 );
> var y1 = new {{alias:@stdlib/array/float32}}( y0.buffer, y0.BYTES_PER_ELEMENT*3 );
> N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 );
> {{alias}}( N, x1, 2, y1, 1 )
> {{alias}}( 3, x1, 2, y1, 1 )
<Float32Array>[ 2.0, 2.0, 1.0 ]
> y0
<Float32Array>[ 0.0, 0.0, 0.0, 2.0, 2.0, 1.0 ]


{{alias}}.ndarray( N, x, strideX, offsetX, y, strideY, offsetY )
Computes the cumulative minimum absolute value of single-precision floating-
point strided array elements using alternative indexing semantics.
Expand All @@ -76,7 +75,7 @@
Input array.

strideX: integer
Index increment for `x`.
Stride Length for `x`.

offsetX: integer
Starting index for `x`.
Expand All @@ -85,7 +84,7 @@
Output array.

strideY: integer
Index increment for `y`.
Stride Length for `y`.

offsetY: integer
Starting index for `y`.
Expand All @@ -106,8 +105,7 @@
// Advanced indexing:
> x = new {{alias:@stdlib/array/float32}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ] );
> y = new {{alias:@stdlib/array/float32}}( x.length );
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
> {{alias}}.ndarray( N, x, 2, 1, y, -1, y.length-1 )
> {{alias}}.ndarray( 3, x, 2, 1, y, -1, y.length-1 )
<Float32Array>[ 0.0, 0.0, 0.0, 1.0, 2.0, 2.0 ]

See Also
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,25 @@
*/

#include "stdlib/stats/base/scuminabs.h"
#include <stdint.h>
#include <stdio.h>
#include <inttypes.h>

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

// Specify the number of elements:
int64_t N = 4;
const int N = 4;

// Specify stride lengths:
int64_t strideX = 2;
int64_t strideY = -2;
const int strideX = 2;
const int strideY = -2;

// Compute the cumulative minimum absolute value:
stdlib_strided_scuminabs( N, x, strideX, y, strideY );

// Print the result:
for ( int64_t i = 0; i < 8; i++ ) {
printf( "y[ %"PRId64" ] = %f\n", i, y[ i ] );
for ( int i = 0; i < 8; i++ ) {
printf( "y[ %d ] = %f\n", i, y[ i ] );
}
}
16 changes: 5 additions & 11 deletions lib/node_modules/@stdlib/stats/base/scuminabs/examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,14 @@

'use strict';

var randu = require( '@stdlib/random/base/randu' );
var round = require( '@stdlib/math/base/special/round' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var Float32Array = require( '@stdlib/array/float32' );
var scuminabs = require( './../lib' );

var y;
var x;
var i;

x = new Float32Array( 10 );
y = new Float32Array( x.length );
for ( i = 0; i < x.length; i++ ) {
x[ i ] = round( (randu()*100.0) - 50.0 );
}
var x = discreteUniform( 10, -50, 50, {
'dtype': 'float32'
});
var y = new Float32Array( x.length );
console.log( x );
console.log( y );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#ifndef STDLIB_STATS_BASE_SCUMINABS_H
#define STDLIB_STATS_BASE_SCUMINABS_H

#include <stdint.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 @@ -31,7 +31,12 @@ extern "C" {
/**
* Computes the cumulative minimum absolute value of single-precision floating-point strided array elements.
*/
void stdlib_strided_scuminabs( const int64_t N, const float *X, const int64_t strideX, float *Y, const int64_t strideY );
void API_SUFFIX(stdlib_strided_scuminabs)( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, float *Y, const CBLAS_INT strideY );

/**
* Computes the cumulative minimum absolute value of single-precision floating-point strided array elements using alternative indexing semantics.
*/
void API_SUFFIX(stdlib_strided_scuminabs_ndarray)( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, float *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY );

#ifdef __cplusplus
}
Expand Down
Loading
Loading