Skip to content

Commit 5db0f5f

Browse files
committed
feat: add 3d kernels
--- 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: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent f679f3a commit 5db0f5f

File tree

4 files changed

+992
-0
lines changed

4 files changed

+992
-0
lines changed
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
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+
/* eslint-disable max-params */
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var copyIndexed = require( '@stdlib/array/base/copy-indexed' );
26+
var incrementOffsets = require( './increment_offsets.js' );
27+
var setViewOffsets = require( './set_view_offsets.js' );
28+
var offsets = require( './offsets.js' );
29+
var wrap = require( './callback_wrapper.js' );
30+
31+
32+
// MAIN //
33+
34+
/**
35+
* Performs a reduction over an input ndarray according to callback function and assigns results to a provided output ndarray.
36+
*
37+
* @private
38+
* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function
39+
* @param {Array<Object>} arrays - ndarrays
40+
* @param {Function} strategy - input ndarray reshape strategy
41+
* @param {Array<Object>} views - initialized ndarray-like objects representing sub-array views
42+
* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices
43+
* @param {NonNegativeIntegerArray} ldims - list of loop dimensions
44+
* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions
45+
* @param {IntegerArray} strides - loop dimension strides for the input ndarray
46+
* @param {boolean} isRowMajor - boolean indicating whether the input ndarray is row-major
47+
* @param {Options} opts - function options
48+
* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function
49+
* @param {Function} clbk - callback function
50+
* @param {*} thisArg - callback exection context
51+
* @returns {void}
52+
*
53+
* @example
54+
* var Float64Array = require( '@stdlib/array/float64' );
55+
* var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
56+
* var zeros = require( '@stdlib/array/base/zeros' );
57+
* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' );
58+
*
59+
* function clbk( value ) {
60+
* return value * 2.0;
61+
* }
62+
*
63+
* // Create data buffers:
64+
* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
65+
* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] );
66+
*
67+
* // Define the array shapes:
68+
* var xsh = [ 1, 1, 3, 2, 2 ];
69+
* var ysh = [ 1, 1, 3 ];
70+
*
71+
* // Define the array strides:
72+
* var sx = [ 12, 12, 4, 2, 1 ];
73+
* var sy = [ 3, 3, 1 ];
74+
*
75+
* // Define the index offsets:
76+
* var ox = 0;
77+
* var oy = 0;
78+
*
79+
* // Create an input ndarray-like object:
80+
* var x = {
81+
* 'dtype': 'float64',
82+
* 'data': xbuf,
83+
* 'shape': xsh,
84+
* 'strides': sx,
85+
* 'offset': ox,
86+
* 'order': 'row-major'
87+
* };
88+
*
89+
* // Create an output ndarray-like object:
90+
* var y = {
91+
* 'dtype': 'float64',
92+
* 'data': ybuf,
93+
* 'shape': ysh,
94+
* 'strides': sy,
95+
* 'offset': oy,
96+
* 'order': 'row-major'
97+
* };
98+
*
99+
* // Initialize ndarray-like objects representing sub-array views:
100+
* var views = [
101+
* {
102+
* 'dtype': x.dtype,
103+
* 'data': x.data,
104+
* 'shape': [ 2, 2 ],
105+
* 'strides': [ 2, 1 ],
106+
* 'offset': x.offset,
107+
* 'order': x.order
108+
* }
109+
* ];
110+
*
111+
* // Define a reshape strategy:
112+
* function strategy( x ) {
113+
* return {
114+
* 'dtype': x.dtype,
115+
* 'data': x.data,
116+
* 'shape': [ 4 ],
117+
* 'strides': [ 1 ],
118+
* 'offset': x.offset,
119+
* 'order': x.order
120+
* };
121+
* }
122+
*
123+
* // Create a workspace array for storing iteration indices:
124+
* var ibuf = zeros( xsh.length );
125+
*
126+
* // Define the loop and core dimensions:
127+
* var ldims = [ 0, 1, 2 ];
128+
* var cdims = [ 3, 4 ];
129+
*
130+
* // Resolve the loop dimension strides for the input array:
131+
* var slx = [ 12, 12, 4 ];
132+
*
133+
* // Perform a reduction:
134+
* unary3d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, true, {}, false, clbk );
135+
*
136+
* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order );
137+
* // returns [ [ [ 8.0, 16.0, 24.0 ] ] ]
138+
*/
139+
function unary3d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, isRowMajor, opts, hasOpts, clbk, thisArg ) { // eslint-disable-line max-len
140+
var ybuf;
141+
var dv0;
142+
var dv1;
143+
var dv2;
144+
var sh;
145+
var S0;
146+
var S1;
147+
var S2;
148+
var sv;
149+
var iv;
150+
var i0;
151+
var i1;
152+
var i2;
153+
var y;
154+
var v;
155+
var i;
156+
var f;
157+
158+
// Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop...
159+
160+
// Resolve the output ndarray and associated shape:
161+
y = arrays[ 1 ];
162+
sh = y.shape;
163+
164+
// Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
165+
if ( isRowMajor ) {
166+
// For row-major ndarrays, the last dimensions have the fastest changing indices...
167+
S0 = sh[ 2 ];
168+
S1 = sh[ 1 ];
169+
S2 = sh[ 0 ];
170+
dv0 = [ strides[2] ]; // offset increment for innermost loop
171+
dv1 = [ strides[1] - ( S0*strides[2] ) ];
172+
dv2 = [ strides[0] - ( S1*strides[1] ) ]; // offset increment for outermost loop
173+
for ( i = 1; i < arrays.length; i++ ) {
174+
sv = arrays[ i ].strides;
175+
dv0.push( sv[2] );
176+
dv1.push( sv[1] - ( S0*sv[2] ) );
177+
dv2.push( sv[0] - ( S1*sv[1] ) );
178+
}
179+
} else { // order === 'column-major'
180+
// For column-major ndarrays, the first dimensions have the fastest changing indices...
181+
S0 = sh[ 0 ];
182+
S1 = sh[ 1 ];
183+
S2 = sh[ 2 ];
184+
dv0 = [ strides[0] ]; // offset increment for innermost loop
185+
dv1 = [ strides[1] - ( S0*strides[0] ) ];
186+
dv2 = [ strides[2] - ( S1*strides[1] ) ]; // offset increment for outermost loop
187+
for ( i = 1; i < arrays.length; i++ ) {
188+
sv = arrays[ i ].strides;
189+
dv0.push( sv[0] );
190+
dv1.push( sv[1] - ( S0*sv[0] ) );
191+
dv2.push( sv[2] - ( S1*sv[1] ) );
192+
}
193+
}
194+
// Resolve a list of pointers to the first indexed elements in the respective ndarrays:
195+
iv = offsets( arrays );
196+
197+
// Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views:
198+
v = copyIndexed( views );
199+
200+
// Cache a reference to the output ndarray buffer:
201+
ybuf = y.data;
202+
203+
// Iterate over the non-reduced ndarray dimensions...
204+
for ( i2 = 0; i2 < S2; i2++ ) {
205+
for ( i1 = 0; i1 < S1; i1++ ) {
206+
for ( i0 = 0; i0 < S0; i0++ ) {
207+
setViewOffsets( views, iv );
208+
v[ 0 ] = strategy( views[ 0 ] );
209+
f = wrap( arrays[ 0 ].ref, views[ 0 ], ibuf, ldims, [ i2, i1, i0 ], cdims, clbk, thisArg ); // eslint-disable-line max-len
210+
ybuf[ iv[1] ] = ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f );
211+
incrementOffsets( iv, dv0 );
212+
}
213+
incrementOffsets( iv, dv1 );
214+
}
215+
}
216+
}
217+
218+
219+
// EXPORTS //
220+
221+
module.exports = unary3d;

0 commit comments

Comments
 (0)