Skip to content

Commit e2cb754

Browse files
MeKaustubh07kgrytegururaj1512
authored
feat: add C implementation for blas/ext/base/ndarray/dnansum
PR-URL: #10715 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: Gururaj Gurram <gururajgurram1512@gmail.com> Reviewed-by: Gururaj Gurram <gururajgurram1512@gmail.com>
1 parent b78ba63 commit e2cb754

File tree

20 files changed

+1839
-134
lines changed

20 files changed

+1839
-134
lines changed

lib/node_modules/@stdlib/blas/ext/base/ndarray/dnansum/README.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,153 @@ console.log( v );
102102

103103
<!-- /.examples -->
104104

105+
<!-- C interface documentation. -->
106+
107+
* * *
108+
109+
<section class="c">
110+
111+
## C APIs
112+
113+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
114+
115+
<section class="intro">
116+
117+
</section>
118+
119+
<!-- /.intro -->
120+
121+
<!-- C usage documentation. -->
122+
123+
<section class="usage">
124+
125+
### Usage
126+
127+
```c
128+
#include "stdlib/blas/ext/base/ndarray/dnansum.h"
129+
```
130+
131+
#### stdlib_blas_ext_dnansum( arrays )
132+
133+
Computes the sum of a one-dimensional double-precision floating-point ndarray, ignoring `NaN` values.
134+
135+
```c
136+
#include "stdlib/ndarray/ctor.h"
137+
#include "stdlib/ndarray/dtypes.h"
138+
#include "stdlib/ndarray/index_modes.h"
139+
#include "stdlib/ndarray/orders.h"
140+
#include "stdlib/ndarray/base/bytes_per_element.h"
141+
#include <stdint.h>
142+
143+
// Create an ndarray:
144+
const double data[] = { 1.0, 0.0/0.0, 3.0, 4.0 };
145+
int64_t shape[] = { 4 };
146+
int64_t strides[] = { STDLIB_NDARRAY_FLOAT64_BYTES_PER_ELEMENT };
147+
int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR };
148+
149+
struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)data, 1, shape, strides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes );
150+
151+
// Compute the sum:
152+
const struct ndarray *arrays[] = { x };
153+
double v = stdlib_blas_ext_dnansum( arrays );
154+
// returns 8.0
155+
156+
// Free allocated memory:
157+
stdlib_ndarray_free( x );
158+
```
159+
160+
The function accepts the following arguments:
161+
162+
- **arrays**: `[in] struct ndarray**` list containing a one-dimensional input ndarray.
163+
164+
```c
165+
double stdlib_blas_ext_dnansum( const struct ndarray *arrays[] );
166+
```
167+
168+
</section>
169+
170+
<!-- /.usage -->
171+
172+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
173+
174+
<section class="notes">
175+
176+
</section>
177+
178+
<!-- /.notes -->
179+
180+
<!-- C API usage examples. -->
181+
182+
<section class="examples">
183+
184+
### Examples
185+
186+
```c
187+
#include "stdlib/blas/ext/base/ndarray/dnansum.h"
188+
#include "stdlib/ndarray/ctor.h"
189+
#include "stdlib/ndarray/dtypes.h"
190+
#include "stdlib/ndarray/index_modes.h"
191+
#include "stdlib/ndarray/orders.h"
192+
#include "stdlib/ndarray/base/bytes_per_element.h"
193+
#include <stdint.h>
194+
#include <stdlib.h>
195+
#include <stdio.h>
196+
197+
int main( void ) {
198+
// Create a data buffer:
199+
const double data[] = { 1.0, -2.0, 0.0/0.0, -4.0, 5.0, -6.0, 0.0/0.0, -8.0 };
200+
201+
// Specify the number of array dimensions:
202+
const int64_t ndims = 1;
203+
204+
// Specify the array shape:
205+
int64_t shape[] = { 4 };
206+
207+
// Specify the array strides:
208+
int64_t strides[] = { 2*STDLIB_NDARRAY_FLOAT64_BYTES_PER_ELEMENT };
209+
210+
// Specify the byte offset:
211+
const int64_t offset = 0;
212+
213+
// Specify the array order:
214+
const enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR;
215+
216+
// Specify the index mode:
217+
const enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR;
218+
219+
// Specify the subscript index modes:
220+
int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR };
221+
const int64_t nsubmodes = 1;
222+
223+
// Create an ndarray:
224+
struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)data, ndims, shape, strides, offset, order, imode, nsubmodes, submodes );
225+
if ( x == NULL ) {
226+
fprintf( stderr, "Error allocating memory.\n" );
227+
exit( 1 );
228+
}
229+
230+
// Define a list of ndarrays:
231+
const struct ndarray *arrays[] = { x };
232+
233+
// Compute the sum:
234+
double v = stdlib_blas_ext_dnansum( arrays );
235+
236+
// Print the result:
237+
printf( "sum: %lf\n", v );
238+
239+
// Free allocated memory:
240+
stdlib_ndarray_free( x );
241+
}
242+
```
243+
244+
</section>
245+
246+
<!-- /.examples -->
247+
248+
</section>
249+
250+
<!-- /.c -->
251+
105252
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
106253
107254
<section class="related">

lib/node_modules/@stdlib/blas/ext/base/ndarray/dnansum/benchmark/benchmark.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ var filledarrayBy = require( '@stdlib/array/filled-by' );
2727
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2828
var pow = require( '@stdlib/math/base/special/pow' );
2929
var ndarray = require( '@stdlib/ndarray/base/ctor' );
30+
var format = require( '@stdlib/string/format' );
3031
var pkg = require( './../package.json' ).name;
31-
var dnansum = require( './../lib' );
32+
var dnansum = require( './../lib/main.js' );
3233

3334

3435
// VARIABLES //
@@ -116,7 +117,7 @@ function main() {
116117
for ( i = min; i <= max; i++ ) {
117118
len = pow( 10, i );
118119
f = createBenchmark( len );
119-
bench( pkg+':len='+len, f );
120+
bench( format( '%s:len=%d', pkg, len ), f );
120121
}
121122
}
122123

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
26+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
27+
var filledarrayBy = require( '@stdlib/array/filled-by' );
28+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
29+
var pow = require( '@stdlib/math/base/special/pow' );
30+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
31+
var format = require( '@stdlib/string/format' );
32+
var tryRequire = require( '@stdlib/utils/try-require' );
33+
var pkg = require( './../package.json' ).name;
34+
35+
36+
// VARIABLES //
37+
38+
var dnansum = tryRequire( resolve( __dirname, './../lib/native.js' ) );
39+
var opts = {
40+
'skip': ( dnansum instanceof Error )
41+
};
42+
var options = {
43+
'dtype': 'float64'
44+
};
45+
46+
47+
// FUNCTIONS //
48+
49+
/**
50+
* Returns a random number.
51+
*
52+
* @private
53+
* @returns {number} random number
54+
*/
55+
function rand() {
56+
if ( bernoulli( 0.7 ) > 0 ) {
57+
return discreteUniform( -10.0, 10.0 );
58+
}
59+
return NaN;
60+
}
61+
62+
/**
63+
* Creates a benchmark function.
64+
*
65+
* @private
66+
* @param {PositiveInteger} len - array length
67+
* @returns {Function} benchmark function
68+
*/
69+
function createBenchmark( len ) {
70+
var xbuf;
71+
var x;
72+
73+
xbuf = filledarrayBy( len, options.dtype, rand );
74+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
75+
76+
return benchmark;
77+
78+
/**
79+
* Benchmark function.
80+
*
81+
* @private
82+
* @param {Benchmark} b - benchmark instance
83+
*/
84+
function benchmark( b ) {
85+
var v;
86+
var i;
87+
88+
b.tic();
89+
for ( i = 0; i < b.iterations; i++ ) {
90+
v = dnansum( [ x ] );
91+
if ( isnan( v ) ) {
92+
b.fail( 'should not return NaN' );
93+
}
94+
}
95+
b.toc();
96+
if ( isnan( v ) ) {
97+
b.fail( 'should not return NaN' );
98+
}
99+
b.pass( 'benchmark finished' );
100+
b.end();
101+
}
102+
}
103+
104+
105+
// MAIN //
106+
107+
/**
108+
* Main execution sequence.
109+
*
110+
* @private
111+
*/
112+
function main() {
113+
var len;
114+
var min;
115+
var max;
116+
var f;
117+
var i;
118+
119+
min = 1; // 10^min
120+
max = 6; // 10^max
121+
122+
for ( i = min; i <= max; i++ ) {
123+
len = pow( 10, i );
124+
f = createBenchmark( len );
125+
bench( format( '%s::native:len=%d', pkg, len ), opts, f );
126+
}
127+
}
128+
129+
main();

0 commit comments

Comments
 (0)