Skip to content

Commit 27ddd92

Browse files
committed
feat: add 7d 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 c17a77b commit 27ddd92

File tree

4 files changed

+1274
-0
lines changed

4 files changed

+1274
-0
lines changed
Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
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-len, max-params, max-depth */
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, 1, 1, 1, 1, 3, 2, 2 ];
69+
* var ysh = [ 1, 1, 1, 1, 1, 1, 3 ];
70+
*
71+
* // Define the array strides:
72+
* var sx = [ 12, 12, 12, 12, 12, 12, 4, 2, 1 ];
73+
* var sy = [ 3, 3, 3, 3, 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, 3, 4, 5, 6 ];
128+
* var cdims = [ 7, 8 ];
129+
*
130+
* // Resolve the loop dimension strides for the input array:
131+
* var slx = [ 12, 12, 12, 12, 12, 12, 4 ];
132+
*
133+
* // Perform a reduction:
134+
* unary7d( 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 unary7d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, isRowMajor, opts, hasOpts, clbk, thisArg ) {
140+
var ybuf;
141+
var dv0;
142+
var dv1;
143+
var dv2;
144+
var dv3;
145+
var dv4;
146+
var dv5;
147+
var dv6;
148+
var sh;
149+
var S0;
150+
var S1;
151+
var S2;
152+
var S3;
153+
var S4;
154+
var S5;
155+
var S6;
156+
var sv;
157+
var iv;
158+
var i0;
159+
var i1;
160+
var i2;
161+
var i3;
162+
var i4;
163+
var i5;
164+
var i6;
165+
var y;
166+
var v;
167+
var i;
168+
var f;
169+
170+
// Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop...
171+
172+
// Resolve the output ndarray and associated shape:
173+
y = arrays[ 1 ];
174+
sh = y.shape;
175+
176+
// Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
177+
if ( isRowMajor ) {
178+
// For row-major ndarrays, the last dimensions have the fastest changing indices...
179+
S0 = sh[ 6 ];
180+
S1 = sh[ 5 ];
181+
S2 = sh[ 4 ];
182+
S3 = sh[ 3 ];
183+
S4 = sh[ 2 ];
184+
S5 = sh[ 1 ];
185+
S6 = sh[ 0 ];
186+
dv0 = [ strides[6] ]; // offset increment for innermost loop
187+
dv1 = [ strides[5] - ( S0*strides[6] ) ];
188+
dv2 = [ strides[4] - ( S1*strides[5] ) ];
189+
dv3 = [ strides[3] - ( S2*strides[4] ) ];
190+
dv4 = [ strides[2] - ( S3*strides[3] ) ];
191+
dv5 = [ strides[1] - ( S4*strides[2] ) ];
192+
dv6 = [ strides[0] - ( S5*strides[1] ) ]; // offset increment for outermost loop
193+
for ( i = 1; i < arrays.length; i++ ) {
194+
sv = arrays[ i ].strides;
195+
dv0.push( sv[6] );
196+
dv1.push( sv[5] - ( S0*sv[6] ) );
197+
dv2.push( sv[4] - ( S1*sv[5] ) );
198+
dv3.push( sv[3] - ( S2*sv[4] ) );
199+
dv4.push( sv[2] - ( S3*sv[3] ) );
200+
dv5.push( sv[1] - ( S4*sv[2] ) );
201+
dv6.push( sv[0] - ( S5*sv[1] ) );
202+
}
203+
} else { // order === 'column-major'
204+
// For column-major ndarrays, the first dimensions have the fastest changing indices...
205+
S0 = sh[ 0 ];
206+
S1 = sh[ 1 ];
207+
S2 = sh[ 2 ];
208+
S3 = sh[ 3 ];
209+
S4 = sh[ 4 ];
210+
S5 = sh[ 5 ];
211+
S6 = sh[ 6 ];
212+
dv0 = [ strides[0] ]; // offset increment for innermost loop
213+
dv1 = [ strides[1] - ( S0*strides[0] ) ];
214+
dv2 = [ strides[2] - ( S1*strides[1] ) ];
215+
dv3 = [ strides[3] - ( S2*strides[2] ) ];
216+
dv4 = [ strides[4] - ( S3*strides[3] ) ];
217+
dv5 = [ strides[5] - ( S4*strides[4] ) ];
218+
dv6 = [ strides[6] - ( S5*strides[5] ) ]; // offset increment for outermost loop
219+
for ( i = 1; i < arrays.length; i++ ) {
220+
sv = arrays[ i ].strides;
221+
dv0.push( sv[0] );
222+
dv1.push( sv[1] - ( S0*sv[0] ) );
223+
dv2.push( sv[2] - ( S1*sv[1] ) );
224+
dv3.push( sv[3] - ( S2*sv[2] ) );
225+
dv4.push( sv[4] - ( S3*sv[3] ) );
226+
dv5.push( sv[5] - ( S4*sv[4] ) );
227+
dv6.push( sv[6] - ( S5*sv[5] ) );
228+
}
229+
}
230+
// Resolve a list of pointers to the first indexed elements in the respective ndarrays:
231+
iv = offsets( arrays );
232+
233+
// 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:
234+
v = copyIndexed( views );
235+
236+
// Cache a reference to the output ndarray buffer:
237+
ybuf = y.data;
238+
239+
// Iterate over the non-reduced ndarray dimensions...
240+
for ( i6 = 0; i6 < S6; i6++ ) {
241+
for ( i5 = 0; i5 < S5; i5++ ) {
242+
for ( i4 = 0; i4 < S4; i4++ ) {
243+
for ( i3 = 0; i3 < S3; i3++ ) {
244+
for ( i2 = 0; i2 < S2; i2++ ) {
245+
for ( i1 = 0; i1 < S1; i1++ ) {
246+
for ( i0 = 0; i0 < S0; i0++ ) {
247+
setViewOffsets( views, iv );
248+
v[ 0 ] = strategy( views[ 0 ] );
249+
f = wrap( arrays[ 0 ].ref, views[ 0 ], ibuf, ldims, [ i6, i5, i4, i3, i2, i1, i0 ], cdims, clbk, thisArg );
250+
ybuf[ iv[1] ] = ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f );
251+
incrementOffsets( iv, dv0 );
252+
}
253+
incrementOffsets( iv, dv1 );
254+
}
255+
incrementOffsets( iv, dv2 );
256+
}
257+
incrementOffsets( iv, dv3 );
258+
}
259+
incrementOffsets( iv, dv4 );
260+
}
261+
incrementOffsets( iv, dv5 );
262+
}
263+
incrementOffsets( iv, dv6 );
264+
}
265+
}
266+
267+
268+
// EXPORTS //
269+
270+
module.exports = unary7d;

0 commit comments

Comments
 (0)