Skip to content

Commit 55c698b

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

File tree

4 files changed

+958
-0
lines changed

4 files changed

+958
-0
lines changed
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
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, 3, 2, 2 ];
65+
* var ysh = [ 1, 1, 1, 3 ];
66+
*
67+
* // Define the array strides:
68+
* var sx = [ 12, 12, 12, 4, 2, 1 ];
69+
* var sy = [ 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+
* unary4d( base, [ x, y ], views, [ 12, 12, 12, 4 ], [ 4, 5 ], 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 unary4d( 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 sh;
123+
var S0;
124+
var S1;
125+
var S2;
126+
var S3;
127+
var sv;
128+
var iv;
129+
var i0;
130+
var i1;
131+
var i2;
132+
var i3;
133+
var y;
134+
var i;
135+
136+
// Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop...
137+
138+
// Resolve the output ndarray and associated shape:
139+
y = arrays[ 1 ];
140+
sh = y.shape;
141+
idx = zeroTo( sh.length );
142+
143+
// Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
144+
if ( isRowMajor ) {
145+
// For row-major ndarrays, the last dimensions have the fastest changing indices...
146+
S0 = sh[ 3 ];
147+
S1 = sh[ 2 ];
148+
S2 = sh[ 1 ];
149+
S3 = sh[ 0 ];
150+
dv0 = [ strides[3] ]; // offset increment for innermost loop
151+
dv1 = [ strides[2] - ( S0*strides[3] ) ];
152+
dv2 = [ strides[1] - ( S1*strides[2] ) ];
153+
dv3 = [ strides[0] - ( S2*strides[1] ) ]; // offset increment for outermost loop
154+
for ( i = 1; i < arrays.length; i++ ) {
155+
sv = arrays[ i ].strides;
156+
dv0.push( sv[3] );
157+
dv1.push( sv[2] - ( S0*sv[3] ) );
158+
dv2.push( sv[1] - ( S1*sv[2] ) );
159+
dv3.push( sv[0] - ( S2*sv[1] ) );
160+
}
161+
} else { // order === 'column-major'
162+
// For column-major ndarrays, the first dimensions have the fastest changing indices...
163+
S0 = sh[ 0 ];
164+
S1 = sh[ 1 ];
165+
S2 = sh[ 2 ];
166+
S3 = sh[ 3 ];
167+
dv0 = [ strides[0] ]; // offset increment for innermost loop
168+
dv1 = [ strides[1] - ( S0*strides[0] ) ];
169+
dv2 = [ strides[2] - ( S1*strides[1] ) ];
170+
dv3 = [ strides[3] - ( S2*strides[2] ) ]; // offset increment for outermost loop
171+
for ( i = 1; i < arrays.length; i++ ) {
172+
sv = arrays[ i ].strides;
173+
dv0.push( sv[0] );
174+
dv1.push( sv[1] - ( S0*sv[0] ) );
175+
dv2.push( sv[2] - ( S1*sv[1] ) );
176+
dv3.push( sv[3] - ( S2*sv[2] ) );
177+
}
178+
idx = reverse( idx );
179+
}
180+
// Resolve a list of pointers to the first indexed elements in the respective ndarrays:
181+
iv = offsets( arrays );
182+
183+
// Cache a reference to the output ndarray buffer:
184+
ybuf = y.data;
185+
186+
// Iterate over the non-reduced ndarray dimensions...
187+
for ( i3 = 0; i3 < S3; i3++ ) {
188+
for ( i2 = 0; i2 < S2; i2++ ) {
189+
for ( i1 = 0; i1 < S1; i1++ ) {
190+
for ( i0 = 0; i0 < S0; i0++ ) {
191+
setViewOffsets( views, iv );
192+
lidx = take( [ i3, i2, i1, i0 ], idx );
193+
wrappedClbk = wrap( lidx, d, arrays[ 0 ], clbk, thisArg );
194+
ybuf[ iv[1] ] = fcn( views, wrappedClbk );
195+
incrementOffsets( iv, dv0 );
196+
}
197+
incrementOffsets( iv, dv1 );
198+
}
199+
incrementOffsets( iv, dv2 );
200+
}
201+
incrementOffsets( iv, dv3 );
202+
}
203+
}
204+
205+
206+
// EXPORTS //
207+
208+
module.exports = unary4d;

0 commit comments

Comments
 (0)