Skip to content

Commit 0cb0f64

Browse files
fix: addon.c and main.c
--- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: passed - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na ---
1 parent 0ca2f07 commit 0cb0f64

File tree

5 files changed

+355
-3
lines changed

5 files changed

+355
-3
lines changed
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "stdlib/stats/base/cumax.h"
20+
#include <stdlib.h>
21+
#include <stdio.h>
22+
#include <math.h>
23+
#include <time.h>
24+
#include <sys/time.h>
25+
26+
#define NAME "cumax"
27+
#define ITERATIONS 1000000
28+
#define REPEATS 3
29+
#define MIN 1
30+
#define MAX 6
31+
32+
/**
33+
* Prints the TAP version.
34+
*/
35+
static void print_version( void ) {
36+
printf( "TAP version 13\n" );
37+
}
38+
39+
/**
40+
* Prints the TAP summary.
41+
*
42+
* @param total total number of tests
43+
* @param passing total number of passing tests
44+
*/
45+
static void print_summary( int total, int passing ) {
46+
printf( "#\n" );
47+
printf( "1..%d\n", total ); // TAP plan
48+
printf( "# total %d\n", total );
49+
printf( "# pass %d\n", passing );
50+
printf( "#\n" );
51+
printf( "# ok\n" );
52+
}
53+
54+
/**
55+
* Prints benchmarks results.
56+
*
57+
* @param iterations number of iterations
58+
* @param elapsed elapsed time in seconds
59+
*/
60+
static void print_results( int iterations, double elapsed ) {
61+
double rate = (double)iterations / elapsed;
62+
printf( " ---\n" );
63+
printf( " iterations: %d\n", iterations );
64+
printf( " elapsed: %0.9f\n", elapsed );
65+
printf( " rate: %0.9f\n", rate );
66+
printf( " ...\n" );
67+
}
68+
69+
/**
70+
* Returns a clock time.
71+
*
72+
* @return clock time
73+
*/
74+
static double tic( void ) {
75+
struct timeval now;
76+
gettimeofday( &now, NULL );
77+
return (double)now.tv_sec + (double)now.tv_usec/1.0e6;
78+
}
79+
80+
/**
81+
* Generates a random number on the interval [0,1).
82+
*
83+
* @return random number
84+
*/
85+
static double rand_double( void ) {
86+
int r = rand();
87+
return (double)r / ( (double)RAND_MAX + 1.0 );
88+
}
89+
90+
/**
91+
* Runs a benchmark.
92+
*
93+
* @param iterations number of iterations
94+
* @param len array length
95+
* @return elapsed time in seconds
96+
*/
97+
static double benchmark1( int iterations, int len ) {
98+
double elapsed;
99+
float x[ len ];
100+
float y[ len ];
101+
double t;
102+
int i;
103+
104+
for ( i = 0; i < len; i++ ) {
105+
x[ i ] = ( rand_double() * 20000.0 ) - 10000.0;
106+
y[ i ] = 0.0;
107+
}
108+
t = tic();
109+
for ( i = 0; i < iterations; i++ ) {
110+
x[ 0 ] += 1.0;
111+
stdlib_strided_cumax( len, x, 1, y, 1 );
112+
if ( y[ 0 ] != y[ 0 ] ) {
113+
printf( "should not return NaN\n" );
114+
break;
115+
}
116+
}
117+
elapsed = tic() - t;
118+
if ( y[ len-1 ] != y[ len-1 ] ) {
119+
printf( "should not return NaN\n" );
120+
}
121+
return elapsed;
122+
}
123+
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_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_cumax_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+
158+
/**
159+
* Main execution sequence.
160+
*/
161+
int main( void ) {
162+
double elapsed;
163+
int count;
164+
int iter;
165+
int len;
166+
int i;
167+
int j;
168+
169+
// Use the current time to seed the random number generator:
170+
srand( time( NULL ) );
171+
172+
print_version();
173+
count = 0;
174+
for ( i = MIN; i <= MAX; i++ ) {
175+
len = pow( 10, i );
176+
iter = ITERATIONS / pow( 10, i-1 );
177+
for ( j = 0; j < REPEATS; j++ ) {
178+
count += 1;
179+
printf( "# c::%s:len=%d\n", NAME, 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 );
192+
print_results( iter, elapsed );
193+
printf( "ok %d benchmark finished\n", count );
194+
}
195+
}
196+
print_summary( count, count );
197+
}

lib/node_modules/@stdlib/stats/base/cumax/include/stdlib/stats/base/cumax.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ extern "C" {
2929
#endif
3030

3131
/**
32-
* Computes the cumulative maximum of a strided array.
32+
* Computes the cumulative maximum of single-precision floating-point strided array elements.
3333
*/
3434
void API_SUFFIX(stdlib_strided_cumax)( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, float *Y, const CBLAS_INT strideY );
3535

3636
/**
37-
* Computes the cumulative maximum of a strided array.
37+
* Computes the cumulative maximum of single-precision floating-point strided array elements using alternative indexing semantics.
3838
*/
3939
void API_SUFFIX(stdlib_strided_cumax_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 );
4040

lib/node_modules/@stdlib/stats/base/cumax/manifest.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
"libpath": [],
4040
"dependencies": [
4141
"@stdlib/blas/base/shared",
42-
"@stdlib/blas/base/cumax",
4342
"@stdlib/strided/base/stride2offset",
4443
"@stdlib/math/base/assert/is-positive-zero",
4544
"@stdlib/math/base/assert/is-nan",
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "stdlib/stats/base/cumax.h"
20+
#include "stdlib/napi/export.h"
21+
#include "stdlib/napi/argv.h"
22+
#include "stdlib/napi/argv_int64.h"
23+
#include "stdlib/blas/base/shared.h"
24+
#include "stdlib/napi/argv_strided_float32array.h"
25+
#include <node_api.h>
26+
27+
/**
28+
* Receives JavaScript callback invocation data.
29+
*
30+
* @param env environment under which the function is invoked
31+
* @param info callback data
32+
* @return Node-API value
33+
*/
34+
static napi_value addon( napi_env env, napi_callback_info info ) {
35+
STDLIB_NAPI_ARGV( env, info, argv, argc, 5 );
36+
STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 );
37+
STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 2 );
38+
STDLIB_NAPI_ARGV_INT64( env, strideY, argv, 4 );
39+
STDLIB_NAPI_ARGV_STRIDED_FLOAT32ARRAY( env, X, N, strideX, argv, 1 );
40+
STDLIB_NAPI_ARGV_STRIDED_FLOAT32ARRAY( env, Y, N, strideY, argv, 3 );
41+
API_SUFFIX(stdlib_strided_cumax)( N, X, strideX, Y, strideY );
42+
return NULL;
43+
}
44+
45+
/**
46+
* Receives JavaScript callback invocation data.
47+
*
48+
* @param env environment under which the function is invoked
49+
* @param info callback data
50+
* @return Node-API value
51+
*/
52+
static napi_value addon_method( napi_env env, napi_callback_info info ) {
53+
STDLIB_NAPI_ARGV( env, info, argv, argc, 7 );
54+
STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 );
55+
STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 2 );
56+
STDLIB_NAPI_ARGV_INT64( env, strideY, argv, 5 );
57+
STDLIB_NAPI_ARGV_INT64( env, offsetX, argv, 3 );
58+
STDLIB_NAPI_ARGV_INT64( env, offsetY, argv, 6 );
59+
STDLIB_NAPI_ARGV_STRIDED_FLOAT32ARRAY( env, X, N, strideX, argv, 1 );
60+
STDLIB_NAPI_ARGV_STRIDED_FLOAT32ARRAY( env, Y, N, strideY, argv, 4 );
61+
API_SUFFIX(stdlib_strided_cumax_ndarray)( N, X, strideX, offsetX, Y, strideY, offsetY );
62+
return NULL;
63+
}
64+
65+
STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "ndarray", addon_method )
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "stdlib/stats/base/cumax.h"
20+
#include "stdlib/blas/base/shared.h"
21+
#include "stdlib/math/base/assert/is_nan.h"
22+
#include "stdlib/math/base/assert/is_positive_zero.h"
23+
#include "stdlib/strided/base/stride2offset.h"
24+
25+
/**
26+
* Computes the cumulative maximum of single-precision floating-point strided array elements.
27+
*
28+
* @param N number of indexed elements
29+
* @param X input array
30+
* @param strideX X stride length
31+
* @param Y output array
32+
* @param strideY Y stride length
33+
*/
34+
void API_SUFFIX(stdlib_strided_cumax)( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, float *Y, const CBLAS_INT strideY ) {
35+
const CBLAS_INT ox = stdlib_strided_stride2offset( N, strideX );
36+
const CBLAS_INT oy = stdlib_strided_stride2offset( N, strideY );
37+
API_SUFFIX(stdlib_strided_cumax_ndarray)( N, X, strideX, ox, Y, strideY, oy );
38+
return;
39+
}
40+
41+
/**
42+
* Computes the cumulative maximum of single-precision floating-point strided array elements using alternative indexing semantics.
43+
*
44+
* @param N number of indexed elements
45+
* @param X input array
46+
* @param strideX X stride length
47+
* @param offsetX starting index for X
48+
* @param Y output array
49+
* @param strideY Y stride length
50+
* @param offsetY starting index for Y
51+
*/
52+
void API_SUFFIX(stdlib_strided_cumax_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 ) {
53+
CBLAS_INT ix;
54+
CBLAS_INT iy;
55+
CBLAS_INT i;
56+
float max;
57+
float v;
58+
59+
if ( N <= 0 ) {
60+
return;
61+
}
62+
ix = offsetX;
63+
iy = offsetY;
64+
max = X[ ix ];
65+
Y[ iy ] = max;
66+
67+
iy += strideY;
68+
i = 1;
69+
if ( !stdlib_base_is_nan( max ) ) {
70+
for (; i < N; i++ ) {
71+
ix += strideX;
72+
v = X[ ix ];
73+
if ( stdlib_base_is_nan( v ) ) {
74+
max = v;
75+
break;
76+
}
77+
if ( v > max || ( v == max && stdlib_base_is_positive_zero( v ) ) ) {
78+
max = v;
79+
}
80+
Y[ iy ] = max;
81+
iy += strideY;
82+
}
83+
}
84+
if ( stdlib_base_is_nan( max ) ) {
85+
for (; i < N; i++ ) {
86+
Y[ iy ] = max;
87+
iy += strideY;
88+
}
89+
}
90+
return;
91+
}

0 commit comments

Comments
 (0)