Skip to content

Commit b18f473

Browse files
committed
feat: add ndarray/base/unary-addon-dispatch
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent ece831b commit b18f473

File tree

10 files changed

+1358
-0
lines changed

10 files changed

+1358
-0
lines changed
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# dispatch
22+
23+
> Dispatch to a native add-on applying a unary function to an input ndarray.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var dispatch = require( '@stdlib/ndarray/base/unary-addon-dispatch' );
41+
```
42+
43+
#### dispatch( addon, fallback )
44+
45+
Returns a function which dispatches to a native add-on applying a unary function to an input ndarray.
46+
47+
```javascript
48+
var array = require( '@stdlib/ndarray/array' );
49+
var zeros = require( '@stdlib/ndarray/zeros' );
50+
var dispatch = require( '@stdlib/ndarray/base/unary-addon-dispatch' );
51+
52+
function addon( x, metaX, y, metaY ) {
53+
// Call into native add-on...
54+
}
55+
56+
function fallback( x, y ) {
57+
// Fallback JavaScript implementation...
58+
}
59+
60+
// Create a dispatch function:
61+
var f = dispatch( addon, fallback );
62+
63+
// ...
64+
65+
// Invoke the dispatch function with ndarray arguments:
66+
var x = array( [ [ 1, 2], [ 3, 4 ] ] );
67+
var y = zeros( [ 2, 2 ] );
68+
f( x, y );
69+
```
70+
71+
The returned function has the following signature:
72+
73+
```text
74+
f( x, y )
75+
```
76+
77+
where
78+
79+
- **x**: input ndarray.
80+
- **y**: output ndarray.
81+
82+
The `addon` function should have the following signature:
83+
84+
```text
85+
f( xbuf, metaX, ybuf, metaY )
86+
```
87+
88+
where
89+
90+
- **xbuf**: input ndarray data buffer.
91+
- **metaX**: [serialized][@stdlib/ndarray/base/serialize-meta-data] input ndarray meta data.
92+
- **ybuf**: output ndarray data buffer.
93+
- **metaY**: [serialized][@stdlib/ndarray/base/serialize-meta-data] output ndarray meta data.
94+
95+
The `fallback` function should have the following signature:
96+
97+
```text
98+
f( x, y )
99+
```
100+
101+
where
102+
103+
- **x**: input ndarray.
104+
- **y**: output ndarray.
105+
106+
</section>
107+
108+
<!-- /.usage -->
109+
110+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
111+
112+
<section class="notes">
113+
114+
## Notes
115+
116+
- To determine whether to dispatch to the `addon` function, the returned dispatch function checks whether the underlying ndarray data buffers are typed arrays. If the data buffers are typed arrays, the dispatch function invokes the `addon` function; otherwise, the dispatch function invokes the `fallback` function.
117+
118+
</section>
119+
120+
<!-- /.notes -->
121+
122+
<!-- Package usage examples. -->
123+
124+
<section class="examples">
125+
126+
## Examples
127+
128+
<!-- eslint-disable max-len -->
129+
130+
<!-- eslint no-undef: "error" -->
131+
132+
```javascript
133+
var array = require( '@stdlib/ndarray/array' );
134+
var zeros = require( '@stdlib/ndarray/zeros' );
135+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
136+
var dispatch = require( '@stdlib/ndarray/base/unary-addon-dispatch' );
137+
138+
function addon( xbuf, metaX, ybuf, metaY ) {
139+
console.log( xbuf );
140+
// => <Float64Array>[ 1, 2, 3, 4 ]
141+
142+
console.log( ybuf );
143+
// => <Float64Array>[ 0, 0, 0, 0 ]
144+
}
145+
146+
function fallback( x, y ) {
147+
console.log( ndarray2array( x ) );
148+
// => [ [ 1, 2 ], [ 3, 4 ] ]
149+
150+
console.log( ndarray2array( y ) );
151+
// => [ [ 0, 0 ], [ 0, 0 ] ]
152+
}
153+
154+
// Create a dispatch function:
155+
var f = dispatch( addon, fallback );
156+
157+
// Create ndarrays:
158+
var opts = {
159+
'dtype': 'float64',
160+
'casting': 'unsafe'
161+
};
162+
var x = array( [ [ 1, 2 ], [ 3, 4 ] ], opts );
163+
var y = zeros( [ 2, 2 ], opts );
164+
165+
// Dispatch to the add-on function:
166+
f( x, y );
167+
168+
// Define new ndarrays:
169+
opts = {
170+
'dtype': 'generic'
171+
};
172+
x = array( [ [ 1, 2 ], [ 3, 4 ] ], opts );
173+
y = zeros( [ 2, 2 ], opts );
174+
175+
// Dispatch to the fallback function:
176+
f( x, y );
177+
```
178+
179+
</section>
180+
181+
<!-- /.examples -->
182+
183+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
184+
185+
<section class="references">
186+
187+
</section>
188+
189+
<!-- /.references -->
190+
191+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
192+
193+
<section class="related">
194+
195+
</section>
196+
197+
<!-- /.related -->
198+
199+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
200+
201+
<section class="links">
202+
203+
[@stdlib/ndarray/base/serialize-meta-data]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/serialize-meta-data
204+
205+
</section>
206+
207+
<!-- /.links -->
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) 2025 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 bench = require( '@stdlib/bench' );
24+
var isFunction = require( '@stdlib/assert/is-function' );
25+
var uniform = require( '@stdlib/random/uniform' );
26+
var zeros = require( '@stdlib/ndarray/zeros' );
27+
var pkg = require( './../package.json' ).name;
28+
var dispatch = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
function addon( xbuf, metaX, ybuf, metaY ) {
34+
if ( !metaX || !metaY ) {
35+
return null;
36+
}
37+
xbuf[ 0 ] += xbuf.length;
38+
ybuf[ 0 ] = xbuf[ 0 ];
39+
}
40+
41+
function fallback( x, y ) {
42+
y.set( 0, x.get( 0 ) );
43+
}
44+
45+
46+
// MAIN //
47+
48+
bench( pkg+'::create', function benchmark( b ) {
49+
var f;
50+
var i;
51+
52+
b.tic();
53+
for ( i = 0; i < b.iterations; i++ ) {
54+
f = dispatch( addon, fallback );
55+
if ( typeof f !== 'function' ) {
56+
b.fail( 'should return a function' );
57+
}
58+
}
59+
b.toc();
60+
if ( !isFunction( f ) ) {
61+
b.fail( 'should return a function' );
62+
}
63+
b.pass( 'benchmark finished' );
64+
b.end();
65+
});
66+
67+
bench( pkg+'::dispatch,addon', function benchmark( b ) {
68+
var f;
69+
var x;
70+
var y;
71+
var v;
72+
var i;
73+
74+
f = dispatch( addon, fallback );
75+
x = uniform( [ 10 ], -10.0, 10.0, {
76+
'dtype': 'float64'
77+
});
78+
y = zeros( [ 10 ], {
79+
'dtype': 'float64'
80+
});
81+
82+
b.tic();
83+
for ( i = 0; i < b.iterations; i++ ) {
84+
x.set( 0, i );
85+
f( x, y );
86+
v = y.get( i%10 );
87+
if ( v !== v ) {
88+
b.fail( 'should not return NaN' );
89+
}
90+
}
91+
b.toc();
92+
if ( v !== v ) {
93+
b.fail( 'should not return NaN' );
94+
}
95+
b.pass( 'benchmark finished' );
96+
b.end();
97+
});
98+
99+
bench( pkg+'::dispatch,fallback', function benchmark( b ) {
100+
var f;
101+
var x;
102+
var y;
103+
var v;
104+
var i;
105+
106+
f = dispatch( addon, fallback );
107+
x = uniform( [ 10 ], -10.0, 10.0, {
108+
'dtype': 'generic'
109+
});
110+
y = zeros( [ 10 ], {
111+
'dtype': 'generic'
112+
});
113+
114+
b.tic();
115+
for ( i = 0; i < b.iterations; i++ ) {
116+
x.set( 0, i );
117+
f( x, y );
118+
v = y.get( i%10 );
119+
if ( v !== v ) {
120+
b.fail( 'should not return NaN' );
121+
}
122+
}
123+
b.toc();
124+
if ( v !== v ) {
125+
b.fail( 'should not return NaN' );
126+
}
127+
b.pass( 'benchmark finished' );
128+
b.end();
129+
});

0 commit comments

Comments
 (0)