Skip to content

Commit 58d9f60

Browse files
committed
feat: add C implementation for blas/ext/base/ndarray/sxmy
1 parent f468ab7 commit 58d9f60

19 files changed

Lines changed: 1981 additions & 186 deletions

File tree

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

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,163 @@ console.log( ndarray2array( y ) );
112112

113113
<!-- /.examples -->
114114

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

0 commit comments

Comments
 (0)