Skip to content

Commit a25eb35

Browse files
authored
feat: add C implementation for blas/ext/base/ndarray/dwxsy
PR-URL: #16133 Reviewed-by: Athan Reines <kgryte@gmail.com> Ref: #14819
1 parent a1c2797 commit a25eb35

20 files changed

Lines changed: 2228 additions & 245 deletions

File tree

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

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,171 @@ console.log( ndarray2array( w ) );
115115

116116
<!-- /.examples -->
117117

118+
<!-- C interface documentation. -->
119+
120+
* * *
121+
122+
<section class="c">
123+
124+
## C APIs
125+
126+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
127+
128+
<section class="intro">
129+
130+
</section>
131+
132+
<!-- /.intro -->
133+
134+
<!-- C usage documentation. -->
135+
136+
<section class="usage">
137+
138+
### Usage
139+
140+
```c
141+
#include "stdlib/blas/ext/base/ndarray/dwxsy.h"
142+
```
143+
144+
#### stdlib_blas_ext_dwxsy( arrays )
145+
146+
Subtracts elements of a second one-dimensional double-precision floating-point ndarray from the corresponding elements of a first one-dimensional double-precision floating-point ndarray and assigns the results to elements in a third one-dimensional double-precision floating-point ndarray.
147+
148+
```c
149+
#include "stdlib/ndarray/ctor.h"
150+
#include "stdlib/ndarray/dtypes.h"
151+
#include "stdlib/ndarray/index_modes.h"
152+
#include "stdlib/ndarray/orders.h"
153+
#include "stdlib/ndarray/base/bytes_per_element.h"
154+
#include <stdint.h>
155+
156+
// Create ndarrays:
157+
const double dataX[] = { 1.0, 2.0, 3.0, 4.0, 5.0 };
158+
const double dataY[] = { 5.0, 4.0, 3.0, 2.0, 1.0 };
159+
double dataW[] = { 0.0, 0.0, 0.0, 0.0, 0.0 };
160+
int64_t shape[] = { 5 };
161+
int64_t strides[] = { STDLIB_NDARRAY_FLOAT64_BYTES_PER_ELEMENT };
162+
int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR };
163+
164+
struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)dataX, 1, shape, strides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes );
165+
struct ndarray *y = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)dataY, 1, shape, strides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes );
166+
struct ndarray *w = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)dataW, 1, shape, strides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes );
167+
168+
// Perform computation:
169+
const struct ndarray *arrays[] = { x, y, w };
170+
stdlib_blas_ext_dwxsy( arrays );
171+
172+
// Free allocated memory:
173+
stdlib_ndarray_free( x );
174+
stdlib_ndarray_free( y );
175+
stdlib_ndarray_free( w );
176+
```
177+
178+
The function accepts the following arguments:
179+
180+
- **arrays**: `[in] struct ndarray**` list containing the following ndarrays:
181+
182+
- `[in] struct ndarray*` first one-dimensional input ndarray.
183+
- `[in] struct ndarray*` second one-dimensional input ndarray.
184+
- `[out] struct ndarray*` a one-dimensional output ndarray.
185+
186+
```c
187+
void stdlib_blas_ext_dwxsy( const struct ndarray *arrays[] );
188+
```
189+
190+
</section>
191+
192+
<!-- /.usage -->
193+
194+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
195+
196+
<section class="notes">
197+
198+
</section>
199+
200+
<!-- /.notes -->
201+
202+
<!-- C API usage examples. -->
203+
204+
<section class="examples">
205+
206+
### Examples
207+
208+
```c
209+
#include "stdlib/blas/ext/base/ndarray/dwxsy.h"
210+
#include "stdlib/ndarray/ctor.h"
211+
#include "stdlib/ndarray/dtypes.h"
212+
#include "stdlib/ndarray/index_modes.h"
213+
#include "stdlib/ndarray/orders.h"
214+
#include "stdlib/ndarray/base/bytes_per_element.h"
215+
#include <stdint.h>
216+
#include <stdlib.h>
217+
#include <stdio.h>
218+
219+
int main( void ) {
220+
// Create data buffers:
221+
const double dataX[] = { 1.0, 2.0, 3.0, 4.0, 5.0 };
222+
const double dataY[] = { 5.0, 4.0, 3.0, 2.0, 1.0 };
223+
double dataW[] = { 0.0, 0.0, 0.0, 0.0, 0.0 };
224+
225+
// Specify the number of array dimensions:
226+
const int64_t ndims = 1;
227+
228+
// Specify the array shape:
229+
int64_t shape[] = { 5 };
230+
231+
// Specify the array strides:
232+
int64_t strides[] = { STDLIB_NDARRAY_FLOAT64_BYTES_PER_ELEMENT };
233+
234+
// Specify the byte offset:
235+
const int64_t offset = 0;
236+
237+
// Specify the array order:
238+
const enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR;
239+
240+
// Specify the index mode:
241+
const enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR;
242+
243+
// Specify the subscript index modes:
244+
int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR };
245+
const int64_t nsubmodes = 1;
246+
247+
// Create ndarrays:
248+
struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)dataX, ndims, shape, strides, offset, order, imode, nsubmodes, submodes );
249+
struct ndarray *y = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)dataY, ndims, shape, strides, offset, order, imode, nsubmodes, submodes );
250+
struct ndarray *w = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)dataW, ndims, shape, strides, offset, order, imode, nsubmodes, submodes );
251+
252+
if ( x == NULL || y == NULL || w == NULL ) {
253+
fprintf( stderr, "Error allocating memory.\n" );
254+
exit( 1 );
255+
}
256+
257+
// Define a list of ndarrays:
258+
const struct ndarray *arrays[] = { x, y, w };
259+
260+
// Perform computation:
261+
stdlib_blas_ext_dwxsy( arrays );
262+
263+
// Print the result:
264+
for ( int i = 0; i < 5; i++ ) {
265+
printf( "w[ %i ] = %lf\n", i, dataW[ i ] );
266+
}
267+
268+
// Free allocated memory:
269+
stdlib_ndarray_free( x );
270+
stdlib_ndarray_free( y );
271+
stdlib_ndarray_free( w );
272+
}
273+
```
274+
275+
</section>
276+
277+
<!-- /.examples -->
278+
279+
</section>
280+
281+
<!-- /.c -->
282+
118283
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
119284
120285
<section class="related">

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var uniform = require( '@stdlib/random/uniform' );
2525
var pow = require( '@stdlib/math/base/special/pow' );
2626
var format = require( '@stdlib/string/format' );
2727
var pkg = require( './../package.json' ).name;
28-
var dwxsy = require( './../lib' );
28+
var dwxsy = require( './../lib/main.js' );
2929

3030

3131
// VARIABLES //
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 uniform = require( '@stdlib/random/uniform' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var format = require( '@stdlib/string/format' );
28+
var tryRequire = require( '@stdlib/utils/try-require' );
29+
var pkg = require( './../package.json' ).name;
30+
31+
32+
// VARIABLES //
33+
34+
var dwxsy = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( dwxsy instanceof Error )
37+
};
38+
var options = {
39+
'dtype': 'float64'
40+
};
41+
42+
43+
// FUNCTIONS //
44+
45+
/**
46+
* Creates a benchmark function.
47+
*
48+
* @private
49+
* @param {PositiveInteger} len - ndarray length
50+
* @returns {Function} benchmark function
51+
*/
52+
function createBenchmark( len ) {
53+
var x = uniform( [ len ], -100.0, 100.0, options );
54+
var y = uniform( [ len ], -100.0, 100.0, options );
55+
var w = uniform( [ len ], -100.0, 100.0, options );
56+
return benchmark;
57+
58+
/**
59+
* Benchmark function.
60+
*
61+
* @private
62+
* @param {Benchmark} b - benchmark instance
63+
*/
64+
function benchmark( b ) {
65+
var out;
66+
var i;
67+
68+
b.tic();
69+
for ( i = 0; i < b.iterations; i++ ) {
70+
out = dwxsy( [ x, y, w ] );
71+
if ( typeof out !== 'object' ) {
72+
b.fail( 'should return an ndarray' );
73+
}
74+
}
75+
b.toc();
76+
if ( typeof out !== 'object' ) {
77+
b.fail( 'should return an ndarray' );
78+
}
79+
b.pass( 'benchmark finished' );
80+
b.end();
81+
}
82+
}
83+
84+
85+
// MAIN //
86+
87+
/**
88+
* Main execution sequence.
89+
*
90+
* @private
91+
*/
92+
function main() {
93+
var len;
94+
var min;
95+
var max;
96+
var f;
97+
var i;
98+
99+
min = 1; // 10^min
100+
max = 6; // 10^max
101+
102+
for ( i = min; i <= max; i++ ) {
103+
len = pow( 10, i );
104+
f = createBenchmark( len );
105+
bench( format( '%s::native:len=%d', pkg, len ), opts, f );
106+
}
107+
}
108+
109+
main();

0 commit comments

Comments
 (0)