Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -94,9 +94,12 @@ 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 out[ 2 ];
if(len <= 0){
return 0.0;
}
double x[ len ];
double t;
int i;
Expand All @@ -122,6 +125,44 @@ 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 out[ 2 ];
if(len <= 0){
return 0.0;
}
double x[ len ];
double t;
int i;

for ( i = 0; i < len; i++ ) {
x[ i ] = ( rand_double() * 20000.0 ) - 10000.0;
}
out[ 0 ] = 0.0;
out[ 1 ] = 0.0;

t = tic();
for ( i = 0; i < iterations; i++ ) {
stdlib_strided_dmeanvarpn_ndarray( len, 1, x, 1, 0, out, 1, 0 );
if ( out[ i%2 ] != out[ i%2 ] ) {
printf( "should not return NaN\n" );
break;
}
}
elapsed = tic() - t;
if ( out[ i%2 ] != out[ i%2 ] ) {
printf( "should not return NaN\n" );
}
return elapsed;
}

/**
* Main execution sequence.
*/
Expand All @@ -144,7 +185,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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#ifndef STDLIB_STATS_BASE_DMEANVARPN_H
#define STDLIB_STATS_BASE_DMEANVARPN_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,11 @@ extern "C" {
/**
* Computes the mean and variance of a double-precision floating-point strided array using a two-pass algorithm.
*/
void stdlib_strided_dmeanvarpn( const int64_t N, const double correction, const double *X, const int64_t strideX, double *Out, const int64_t strideOut );
void API_SUFFIX( stdlib_strided_dmeanvarpn )( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX, double *Out, const CBLAS_INT strideOut );
/**
* Computes the mean and variance of a double-precision floating-point strided array using a two-pass algorithm.
*/
void API_SUFFIX( stdlib_strided_dmeanvarpn_ndarray )( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, double *Out, const CBLAS_INT strideOut, const CBLAS_INT offsetOut );

#ifdef __cplusplus
}
Expand Down
67 changes: 5 additions & 62 deletions lib/node_modules/@stdlib/stats/base/dmeanvarpn/lib/dmeanvarpn.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

// MODULES //

var isnan = require( '@stdlib/math/base/assert/is-nan' );
var dsumpw = require( '@stdlib/blas/ext/base/dsumpw' );
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
var ndarray = require( './ndarray.js' );


// MAIN //
Expand Down Expand Up @@ -56,66 +56,9 @@ var dsumpw = require( '@stdlib/blas/ext/base/dsumpw' );
* // returns <Float64Array>[ ~0.3333, ~4.3333 ]
*/
function dmeanvarpn( N, correction, x, strideX, out, strideOut ) {
var mu;
var ix;
var io;
var M2;
var M;
var d;
var c;
var n;
var i;

if ( strideX < 0 ) {
ix = (1-N) * strideX;
} else {
ix = 0;
}
if ( strideOut < 0 ) {
io = -strideOut;
} else {
io = 0;
}
if ( N <= 0 ) {
out[ io ] = NaN;
out[ io+strideOut ] = NaN;
return out;
}
n = N - correction;
if ( N === 1 || strideX === 0 ) {
out[ io ] = x[ ix ];
if ( n <= 0.0 ) {
out[ io+strideOut ] = NaN;
} else {
out[ io+strideOut ] = 0.0;
}
return out;
}
// Compute an estimate for the mean:
mu = dsumpw( N, x, strideX ) / N;
if ( isnan( mu ) ) {
out[ io ] = NaN;
out[ io+strideOut ] = NaN;
return out;
}
// Compute the sum of squared differences from the mean...
M2 = 0.0;
M = 0.0;
for ( i = 0; i < N; i++ ) {
d = x[ ix ] - mu;
M2 += d * d;
M += d;
ix += strideX;
}
// Compute an error term for the mean:
c = M / N;

out[ io ] = mu + c;
if ( n <= 0.0 ) {
out[ io+strideOut ] = NaN;
} else {
out[ io+strideOut ] = (M2/n) - (c*(M/n));
}
var offsetOut = stride2offset( 2, strideX );
var offsetX = stride2offset( N, strideX );
ndarray( N, correction, x, strideX, offsetX, out, strideOut, offsetOut );
return out;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var addon = require( './../src/addon.node' );
* var out = new Float64Array( 2 );
*
* var v = dmeanvarpn( x.length, 1, x, 1, out, 1 );
* // returns <Float64Array>[ ~0.3333, ~4.3333 ]
* //returns <Float64Array>[ ~0.3333, ~4.3333 ]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* //returns <Float64Array>[ ~0.3333, ~4.3333 ]
* // returns <Float64Array>[ ~0.3333, ~4.3333 ]

*/
function dmeanvarpn( N, correction, x, strideX, out, strideOut ) {
addon( N, correction, x, strideX, out, strideOut );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@

// MODULES //

var Float64Array = require( '@stdlib/array/float64' );
var addon = require( './dmeanvarpn.native.js' );
var addon = require( './../src/addon.node' );


// MAIN //
Expand Down Expand Up @@ -49,20 +48,10 @@
* var N = floor( x.length / 2 );
*
* var v = dmeanvarpn( N, 1, x, 2, 1, out, 1, 0 );
* // returns <Float64Array>[ 1.25, 6.25 ]
* //returns <Float64Array>[ 1.25, 6.25 ]
*/
function dmeanvarpn( N, correction, x, strideX, offsetX, out, strideOut, offsetOut ) { // eslint-disable-line max-len
var viewOut;
var viewX;
if ( strideX < 0 ) {
offsetX += (N-1) * strideX;
}
if ( strideOut < 0 ) {
offsetOut += strideOut;
}
viewX = new Float64Array( x.buffer, x.byteOffset+(x.BYTES_PER_ELEMENT*offsetX), x.length-offsetX ); // eslint-disable-line max-len
viewOut = new Float64Array( out.buffer, out.byteOffset+(out.BYTES_PER_ELEMENT*offsetOut), out.length-offsetOut ); // eslint-disable-line max-len
addon( N, correction, viewX, strideX, viewOut, strideOut );
addon.ndarray( N, correction, x, strideX, offsetX, out, strideOut, offsetOut );

Check warning on line 54 in lib/node_modules/@stdlib/stats/base/dmeanvarpn/lib/ndarray.native.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 83. Maximum allowed is 80
return out;
}

Expand Down
4 changes: 4 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dmeanvarpn/manifest.json
Copy link
Member

Choose a reason for hiding this comment

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

this is an incorrect format for manifest.json for this type of implementation, please see an existing implementation to get an idea of what we're looking for

Copy link
Member

Choose a reason for hiding this comment

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

Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
"@stdlib/napi/argv-double",
"@stdlib/napi/argv-strided-float64array",
"@stdlib/blas/base/shared",
"@stdlib/math/base/assert/is-nan",
"@stdlib/strided/base/stride2offset",
"@stdlib/blas/ext/base/dsumpw"
]
},
Expand All @@ -60,6 +62,7 @@
],
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-nan",
"@stdlib/blas/ext/base/dsumpw"
]
},
Expand All @@ -76,6 +79,7 @@
],
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-nan",
"@stdlib/blas/ext/base/dsumpw"
]
}
Expand Down
22 changes: 21 additions & 1 deletion lib/node_modules/@stdlib/stats/base/dmeanvarpn/src/addon.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,25 @@ static napi_value addon( napi_env env, napi_callback_info info ) {
API_SUFFIX(stdlib_strided_dmeanvarpn)( N, correction, X, strideX, Out, strideOut );
return NULL;
}
/**
* Receives JavaScript callback invocation data.
*
* @param env environment under which the function is invoked
* @param info callback data
* @return Node-API value
*/
static napi_value addon_method( napi_env env, napi_callback_info info ) {
STDLIB_NAPI_ARGV( env, info, argv, argc, 8 );
STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 );
STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 3 );
STDLIB_NAPI_ARGV_INT64( env, offsetX, argv, 4 );
STDLIB_NAPI_ARGV_INT64( env, offsetOut, argv, 7 );
STDLIB_NAPI_ARGV_INT64( env, strideOut, argv, 6 );
STDLIB_NAPI_ARGV_DOUBLE( env, correction, argv, 1 );
STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, strideX, argv, 2 );
STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, Out, 2, strideOut, argv, 5 );
API_SUFFIX( stdlib_strided_dmeanvarpn_ndarray )( N, correction, X, strideX, offsetX, Out, strideOut, offsetOut );
return NULL;
}

STDLIB_NAPI_MODULE_EXPORT_FCN( addon )
STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "ndarray", addon_method )
Loading
Loading