Skip to content

Commit aa99f10

Browse files
aayush0325Neerajpathak07
authored andcommitted
feat: add C ndarray interface and refactor implementation for stats/base/scumaxabs
PR-URL: stdlib-js#4684 Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 4b1354d commit aa99f10

22 files changed

+334
-277
lines changed

lib/node_modules/@stdlib/stats/base/scumaxabs/README.md

Lines changed: 96 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ The function has the following parameters:
5454

5555
- **N**: number of indexed elements.
5656
- **x**: input [`Float32Array`][@stdlib/array/float32].
57-
- **strideX**: index increment for `x`.
57+
- **strideX**: stride length for `x`.
5858
- **y**: output [`Float32Array`][@stdlib/array/float32].
59-
- **strideY**: index increment for `y`.
59+
- **strideY**: stride length for `y`.
6060

61-
The `N` and `stride` parameters determine which elements in `x` and `y` are accessed at runtime. For example, to compute the cumulative maximum absolute value of every other element in `x`,
61+
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to compute the cumulative maximum absolute value of every other element in `x`,
6262

6363
```javascript
6464
var Float32Array = require( '@stdlib/array/float32' );
@@ -108,7 +108,7 @@ The function has the following additional parameters:
108108
- **offsetX**: starting index for `x`.
109109
- **offsetY**: starting index for `y`.
110110

111-
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, `offsetX` and `offsetY` parameters support indexing semantics based on a starting indices. For example, to calculate the cumulative maximum absolute value of every other value in `x` starting from the second value and to store in the last `N` elements of `y` starting from the last element
111+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, offset parameters support indexing semantics based on a starting indices. For example, to calculate the cumulative maximum absolute value of every other element in `x` starting from the second element and to store in the last `N` elements of `y` starting from the last element
112112

113113
```javascript
114114
var Float32Array = require( '@stdlib/array/float32' );
@@ -141,20 +141,14 @@ scumaxabs.ndarray( 4, x, 2, 1, y, -1, y.length-1 );
141141
<!-- eslint no-undef: "error" -->
142142

143143
```javascript
144-
var randu = require( '@stdlib/random/base/randu' );
145-
var round = require( '@stdlib/math/base/special/round' );
144+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
146145
var Float32Array = require( '@stdlib/array/float32' );
147146
var scumaxabs = require( '@stdlib/stats/base/scumaxabs' );
148147

149-
var y;
150-
var x;
151-
var i;
152-
153-
x = new Float32Array( 10 );
154-
y = new Float32Array( x.length );
155-
for ( i = 0; i < x.length; i++ ) {
156-
x[ i ] = round( (randu()*100.0) - 50.0 );
157-
}
148+
var x = discreteUniform( 10, -50, 50, {
149+
'dtype': 'float32'
150+
});
151+
var y = new Float32Array( x.length );
158152
console.log( x );
159153
console.log( y );
160154

@@ -166,6 +160,93 @@ console.log( y );
166160

167161
<!-- /.examples -->
168162

163+
<!-- C interface documentation. -->
164+
165+
* * *
166+
167+
<section class="c">
168+
169+
## C APIs
170+
171+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
172+
173+
<section class="intro">
174+
175+
</section>
176+
177+
<!-- /.intro -->
178+
179+
<!-- C usage documentation. -->
180+
181+
<section class="usage">
182+
183+
### Usage
184+
185+
```c
186+
#include "stdlib/stats/base/scumaxabs.h"
187+
```
188+
189+
#### stdlib_strided_scumaxabs( N, \*X, strideX, \*Y, strideY )
190+
191+
Computes the cumulative maximum absolute value of single-precision floating-point strided array elements.
192+
193+
```c
194+
const float x[] = { 1.0f, 2.0f, -3.0f, 4.0f, -5.0f, 6.0f, 7.0f, 8.0f };
195+
float y[] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
196+
197+
stdlib_strided_scumaxabs( 4, x, 2, y, -2 );
198+
```
199+
200+
The function accepts the following arguments:
201+
202+
- **N**: `[in] CBLAS_INT` number of indexed elements.
203+
- **X**: `[in] float*` input array.
204+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
205+
- **Y**: `[out] float*` output array.
206+
- **strideY**: `[in] CBLAS_INT` stride length for `Y`.
207+
208+
```c
209+
void stdlib_strided_scumaxabs( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, float *Y, const CBLAS_INT strideY );
210+
```
211+
212+
#### stdlib_strided_scumaxabs_ndarray(N, \*X, strideX, offsetX, \*Y, strideY, offsetY )
213+
214+
Computes the cumulative maximum absolute value of single-precision floating-point strided array elements using alternative indexing semantics.
215+
216+
```c
217+
#include "stdlib/stats/base/scumaxabs.h"
218+
#include <stdio.h>
219+
220+
int main( void ) {
221+
// Create strided arrays:
222+
const float x[] = { 1.0f, 2.0f, -3.0f, 4.0f, -5.0f, 6.0f, 7.0f, 8.0f };
223+
const float y[] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
224+
225+
// Specify the number of elements:
226+
const int N = 4;
227+
228+
// Specify stride lengths:
229+
const int strideX = 2;
230+
const int strideY = -2;
231+
232+
// Compute the cumulative maximum absolute value:
233+
stdlib_strided_scumaxabs( N, x, strideX, y, strideY );
234+
235+
// Print the result:
236+
for ( int i = 0; i < 8; i++ ) {
237+
printf( "y[ %d ] = %f\n", i, y[ i ] );
238+
}
239+
}
240+
```
241+
242+
</section>
243+
244+
<!-- /.examples -->
245+
246+
</section>
247+
248+
<!-- /.c -->
249+
169250
<section class="references">
170251
171252
</section>

lib/node_modules/@stdlib/stats/base/scumaxabs/benchmark/benchmark.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,21 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
2525
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
2727
var Float32Array = require( '@stdlib/array/float32' );
2828
var pkg = require( './../package.json' ).name;
2929
var scumaxabs = require( './../lib/scumaxabs.js' );
3030

3131

32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'float32'
36+
};
37+
38+
3239
// FUNCTIONS //
3340

3441
/**
@@ -39,15 +46,8 @@ var scumaxabs = require( './../lib/scumaxabs.js' );
3946
* @returns {Function} benchmark function
4047
*/
4148
function createBenchmark( len ) {
42-
var y;
43-
var x;
44-
var i;
45-
46-
x = new Float32Array( len );
47-
y = new Float32Array( len );
48-
for ( i = 0; i < x.length; i++ ) {
49-
x[ i ] = ( randu()*20.0 ) - 10.0;
50-
}
49+
var x = uniform( len, -10.0, 10.0, options );
50+
var y = new Float32Array( len );
5151
return benchmark;
5252

5353
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/scumaxabs/benchmark/benchmark.native.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var randu = require( '@stdlib/random/base/randu' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
2626
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2727
var pow = require( '@stdlib/math/base/special/pow' );
2828
var Float32Array = require( '@stdlib/array/float32' );
@@ -36,6 +36,9 @@ var scumaxabs = tryRequire( resolve( __dirname, './../lib/scumaxabs.native.js' )
3636
var opts = {
3737
'skip': ( scumaxabs instanceof Error )
3838
};
39+
var options = {
40+
'dtype': 'float32'
41+
};
3942

4043

4144
// FUNCTIONS //
@@ -48,15 +51,8 @@ var opts = {
4851
* @returns {Function} benchmark function
4952
*/
5053
function createBenchmark( len ) {
51-
var x;
52-
var y;
53-
var i;
54-
55-
x = new Float32Array( len );
56-
y = new Float32Array( len );
57-
for ( i = 0; i < x.length; i++ ) {
58-
x[ i ] = ( randu()*20.0 ) - 10.0;
59-
}
54+
var x = uniform( len, -10.0, 10.0, options );
55+
var y = new Float32Array( len );
6056
return benchmark;
6157

6258
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/scumaxabs/benchmark/benchmark.ndarray.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,21 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
2525
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
2727
var Float32Array = require( '@stdlib/array/float32' );
2828
var pkg = require( './../package.json' ).name;
2929
var scumaxabs = require( './../lib/ndarray.js' );
3030

3131

32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'float32'
36+
};
37+
38+
3239
// FUNCTIONS //
3340

3441
/**
@@ -39,15 +46,8 @@ var scumaxabs = require( './../lib/ndarray.js' );
3946
* @returns {Function} benchmark function
4047
*/
4148
function createBenchmark( len ) {
42-
var x;
43-
var y;
44-
var i;
45-
46-
x = new Float32Array( len );
47-
y = new Float32Array( len );
48-
for ( i = 0; i < x.length; i++ ) {
49-
x[ i ] = ( randu()*20.0 ) - 10.0;
50-
}
49+
var x = uniform( len, -10.0, 10.0, options );
50+
var y = new Float32Array( len );
5151
return benchmark;
5252

5353
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/scumaxabs/benchmark/benchmark.ndarray.native.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var randu = require( '@stdlib/random/base/randu' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
2626
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2727
var pow = require( '@stdlib/math/base/special/pow' );
2828
var Float32Array = require( '@stdlib/array/float32' );
@@ -36,6 +36,9 @@ var scumaxabs = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) )
3636
var opts = {
3737
'skip': ( scumaxabs instanceof Error )
3838
};
39+
var options = {
40+
'dtype': 'float32'
41+
};
3942

4043

4144
// FUNCTIONS //
@@ -48,15 +51,8 @@ var opts = {
4851
* @returns {Function} benchmark function
4952
*/
5053
function createBenchmark( len ) {
51-
var x;
52-
var y;
53-
var i;
54-
55-
x = new Float32Array( len );
56-
y = new Float32Array( len );
57-
for ( i = 0; i < x.length; i++ ) {
58-
x[ i ] = ( randu()*20.0 ) - 10.0;
59-
}
54+
var x = uniform( len, -10.0, 10.0, options );
55+
var y = new Float32Array( len );
6056
return benchmark;
6157

6258
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/scumaxabs/benchmark/c/benchmark.length.c

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static float rand_float( void ) {
9494
* @param len array length
9595
* @return elapsed time in seconds
9696
*/
97-
static double benchmark( int iterations, int len ) {
97+
static double benchmark1( int iterations, int len ) {
9898
double elapsed;
9999
float x[ len ];
100100
float y[ len ];
@@ -121,6 +121,40 @@ static double benchmark( int iterations, int len ) {
121121
return elapsed;
122122
}
123123

124+
/**
125+
* Runs a benchmark.
126+
*
127+
* @param iterations number of iterations
128+
* @param len array length
129+
* @return elapsed time in seconds
130+
*/
131+
static double benchmark2( int iterations, int len ) {
132+
double elapsed;
133+
float x[ len ];
134+
float y[ len ];
135+
double t;
136+
int i;
137+
138+
for ( i = 0; i < len; i++ ) {
139+
x[ i ] = ( rand_float() * 20000.0f ) - 10000.0f;
140+
y[ i ] = 0.0f;
141+
}
142+
t = tic();
143+
for ( i = 0; i < iterations; i++ ) {
144+
x[ 0 ] += 1.0f;
145+
stdlib_strided_scumaxabs_ndarray( len, x, 1, 0, y, 1, 0 );
146+
if ( y[ 0 ] != y[ 0 ] ) {
147+
printf( "should not return NaN\n" );
148+
break;
149+
}
150+
}
151+
elapsed = tic() - t;
152+
if ( y[ len-1 ] != y[ len-1 ] ) {
153+
printf( "should not return NaN\n" );
154+
}
155+
return elapsed;
156+
}
157+
124158
/**
125159
* Main execution sequence.
126160
*/
@@ -143,7 +177,18 @@ int main( void ) {
143177
for ( j = 0; j < REPEATS; j++ ) {
144178
count += 1;
145179
printf( "# c::%s:len=%d\n", NAME, len );
146-
elapsed = benchmark( iter, len );
180+
elapsed = benchmark1( iter, len );
181+
print_results( iter, elapsed );
182+
printf( "ok %d benchmark finished\n", count );
183+
}
184+
}
185+
for ( i = MIN; i <= MAX; i++ ) {
186+
len = pow( 10, i );
187+
iter = ITERATIONS / pow( 10, i-1 );
188+
for ( j = 0; j < REPEATS; j++ ) {
189+
count += 1;
190+
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
191+
elapsed = benchmark2( iter, len );
147192
print_results( iter, elapsed );
148193
printf( "ok %d benchmark finished\n", count );
149194
}

0 commit comments

Comments
 (0)