Skip to content

Commit c29abc2

Browse files
feat: add C implementation for blas/ext/base/ndarray/sxsa
PR-URL: #15404 Reviewed-by: Athan Reines <kgryte@gmail.com> Ref: #14819
1 parent 0e93ce8 commit c29abc2

20 files changed

Lines changed: 1860 additions & 114 deletions

File tree

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

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,174 @@ console.log( ndarray2array( x ) );
106106

107107
<!-- /.examples -->
108108

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
2727
var format = require( '@stdlib/string/format' );
2828
var pkg = require( './../package.json' ).name;
29-
var sxsa = require( './../lib' );
29+
var sxsa = require( './../lib/main.js' );
3030

3131

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

0 commit comments

Comments
 (0)