Skip to content

Commit 0f8405c

Browse files
committed
feat: add C ndarray interface and refactor implementation
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 5e05d49 commit 0f8405c

File tree

22 files changed

+350
-254
lines changed

22 files changed

+350
-254
lines changed

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

Lines changed: 133 additions & 14 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 [`Float64Array`][@stdlib/array/float64].
57-
- **strideX**: index increment for `x`.
57+
- **strideX**: stride length for `x`.
5858
- **y**: output [`Float64Array`][@stdlib/array/float64].
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 Float64Array = require( '@stdlib/array/float64' );
@@ -141,21 +141,16 @@ dcumaxabs.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 Float64Array = require( '@stdlib/array/float64' );
147146
var dcumaxabs = require( '@stdlib/stats/base/dcumaxabs' );
148147

149-
var y;
150-
var x;
151-
var i;
152-
153-
x = new Float64Array( 10 );
154-
y = new Float64Array( 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': 'float64'
150+
});
158151
console.log( x );
152+
153+
var y = new Float64Array( x.length );
159154
console.log( y );
160155

161156
dcumaxabs( x.length, x, 1, y, -1 );
@@ -166,6 +161,130 @@ console.log( y );
166161

167162
<!-- /.examples -->
168163

164+
<!-- C interface documentation. -->
165+
166+
* * *
167+
168+
<section class="c">
169+
170+
## C APIs
171+
172+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
173+
174+
<section class="intro">
175+
176+
</section>
177+
178+
<!-- /.intro -->
179+
180+
<!-- C usage documentation. -->
181+
182+
<section class="usage">
183+
184+
### Usage
185+
186+
```c
187+
#include "stdlib/stats/base/dcumaxabs.h"
188+
```
189+
190+
#### stdlib_strided_dcumaxabs( N, \*X, strideX, \*Y, strideY )
191+
192+
Computes the cumulative maximum absolute value of double-precision floating-point strided array elements.
193+
194+
```c
195+
const double x[] = { 1.0, 2.0, -3.0, 4.0, -5.0, 6.0, 7.0, 8.0 };
196+
double y[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
197+
stdlib_strided_dcumaxabs( 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] double*` input array.
204+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
205+
- **Y**: `[in] double*` output array.
206+
- **strideY**: `[in] CBLAS_INT` stride length for `Y`.
207+
208+
```c
209+
void stdlib_strided_dcumaxabs( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, double *Y, const CBLAS_INT strideY );
210+
```
211+
212+
#### stdlib_strided_dcumaxabs_ndarray(N, \*X, strideX, offsetX, \*Y, strideY, offsetY)
213+
214+
Computes the cumulative maximum absolute value of double-precision floating-point strided array elements using alternative indexing semantics.
215+
216+
```c
217+
const double x[] = { 1.0, 2.0, -3.0, 4.0, -5.0, 6.0, 7.0, 8.0 };
218+
double y[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
219+
stdlib_strided_dcumaxabs_ndarray( 4, x, 2, 0, y, -2, 0 );
220+
```
221+
222+
The function accepts the following arguments:
223+
224+
- **N**: `[in] CBLAS_INT` number of indexed elements.
225+
- **X**: `[in] double*` input array.
226+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
227+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
228+
- **Y**: `[in] double*` output array.
229+
- **strideY**: `[in] CBLAS_INT` stride length for `Y`.
230+
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.
231+
232+
```c
233+
void stdlib_strided_dmax_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, double *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY );
234+
```
235+
236+
</section>
237+
238+
<!-- /.usage -->
239+
240+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
241+
242+
<section class="notes">
243+
244+
</section>
245+
246+
<!-- /.notes -->
247+
248+
<!-- C API usage examples. -->
249+
250+
<section class="examples">
251+
252+
### Examples
253+
254+
```c
255+
#include "stdlib/stats/base/dcumaxabs.h"
256+
#include <stdio.h>
257+
258+
int main( void ) {
259+
// Create strided arrays:
260+
const double x[] = { 1.0, 2.0, -3.0, 4.0, -5.0, 6.0, 7.0, 8.0 };
261+
double y[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
262+
263+
// Specify the number of elements:
264+
const int N = 4;
265+
266+
// Specify stride lengths:
267+
const int strideX = 2;
268+
const int strideY = -2;
269+
270+
// Compute the cumulative maximum absolute value:
271+
stdlib_strided_dcumaxabs( N, x, strideX, y, strideY );
272+
273+
// Print the result:
274+
for ( int i = 0; i < 8; i++ ) {
275+
printf( "y[ %d ] = %lf\n", i, y[ i ] );
276+
}
277+
}
278+
```
279+
280+
</section>
281+
282+
<!-- /.examples -->
283+
284+
</section>
285+
286+
<!-- /.c -->
287+
169288
<section class="references">
170289
171290
</section>

lib/node_modules/@stdlib/stats/base/dcumaxabs/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 isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
2727
var Float64Array = require( '@stdlib/array/float64' );
2828
var pkg = require( './../package.json' ).name;
2929
var dcumaxabs = require( './../lib/dcumaxabs.js' );
3030

3131

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

3441
/**
@@ -39,15 +46,8 @@ var dcumaxabs = require( './../lib/dcumaxabs.js' );
3946
* @returns {Function} benchmark function
4047
*/
4148
function createBenchmark( len ) {
42-
var y;
43-
var x;
44-
var i;
45-
46-
x = new Float64Array( len );
47-
y = new Float64Array( 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 Float64Array( len );
5151
return benchmark;
5252

5353
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/dcumaxabs/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 isnan = require( '@stdlib/math/base/assert/is-nan' );
2727
var pow = require( '@stdlib/math/base/special/pow' );
2828
var Float64Array = require( '@stdlib/array/float64' );
@@ -36,6 +36,9 @@ var dcumaxabs = tryRequire( resolve( __dirname, './../lib/dcumaxabs.native.js' )
3636
var opts = {
3737
'skip': ( dcumaxabs instanceof Error )
3838
};
39+
var options = {
40+
'dtype': 'float64'
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 Float64Array( len );
56-
y = new Float64Array( 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 Float64Array( len );
6056
return benchmark;
6157

6258
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/dcumaxabs/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 isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
2727
var Float64Array = require( '@stdlib/array/float64' );
2828
var pkg = require( './../package.json' ).name;
2929
var dcumaxabs = require( './../lib/ndarray.js' );
3030

3131

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

3441
/**
@@ -39,15 +46,8 @@ var dcumaxabs = 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 Float64Array( len );
47-
y = new Float64Array( 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 Float64Array( len );
5151
return benchmark;
5252

5353
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/dcumaxabs/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 isnan = require( '@stdlib/math/base/assert/is-nan' );
2727
var pow = require( '@stdlib/math/base/special/pow' );
2828
var Float64Array = require( '@stdlib/array/float64' );
@@ -36,6 +36,9 @@ var dcumaxabs = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) )
3636
var opts = {
3737
'skip': ( dcumaxabs instanceof Error )
3838
};
39+
var options = {
40+
'dtype': 'float64'
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 Float64Array( len );
56-
y = new Float64Array( 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 Float64Array( len );
6056
return benchmark;
6157

6258
function benchmark( b ) {

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

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static double rand_double( 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
double x[ len ];
100100
double 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+
double x[ len ];
134+
double y[ len ];
135+
double t;
136+
int i;
137+
138+
for ( i = 0; i < len; i++ ) {
139+
x[ i ] = ( rand_double() * 20000.0 ) - 10000.0;
140+
y[ i ] = 0.0;
141+
}
142+
t = tic();
143+
for ( i = 0; i < iterations; i++ ) {
144+
x[ 0 ] += 1.0;
145+
stdlib_strided_dcumaxabs_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)