Skip to content

Commit ca1c1d3

Browse files
committed
feat(blas/ext/base/ndarray/dtril): implement native C add-on
1 parent ad21e7c commit ca1c1d3

20 files changed

Lines changed: 2059 additions & 189 deletions

File tree

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

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,188 @@ console.log( ndarray2array( out ) );
114114

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

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

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

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

3232

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

0 commit comments

Comments
 (0)