Skip to content

Commit bc7d3d3

Browse files
committed
feat: add 5d 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 55c698b commit bc7d3d3

File tree

4 files changed

+1028
-0
lines changed

4 files changed

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

0 commit comments

Comments
 (0)