Skip to content

Commit 83e4b14

Browse files
committed
feat: add 5d blocked accessors kernel
--- 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 b00bd71 commit 83e4b14

File tree

1 file changed

+326
-0
lines changed

1 file changed

+326
-0
lines changed
Lines changed: 326 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,326 @@
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-depth, max-len, max-statements */
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var loopOrder = require( '@stdlib/ndarray/base/binary-loop-interchange-order' );
26+
var blockSize = require( '@stdlib/ndarray/base/binary-tiling-block-size' );
27+
28+
29+
// MAIN //
30+
31+
/**
32+
* Applies a binary callback to elements in five-dimensional input ndarrays and assigns results to elements in an equivalently shaped output ndarray via loop blocking.
33+
*
34+
* @private
35+
* @param {Object} x - object containing input ndarray meta data
36+
* @param {string} x.dtype - data type
37+
* @param {Collection} x.data - data buffer
38+
* @param {NonNegativeIntegerArray} x.shape - dimensions
39+
* @param {IntegerArray} x.strides - stride lengths
40+
* @param {NonNegativeInteger} x.offset - index offset
41+
* @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style)
42+
* @param {Array<Function>} x.accessors - data buffer accessors
43+
* @param {Object} y - object containing input ndarray meta data
44+
* @param {string} y.dtype - data type
45+
* @param {Collection} y.data - data buffer
46+
* @param {NonNegativeIntegerArray} y.shape - dimensions
47+
* @param {IntegerArray} y.strides - stride lengths
48+
* @param {NonNegativeInteger} y.offset - index offset
49+
* @param {string} y.order - specifies whether `y` is row-major (C-style) or column-major (Fortran-style)
50+
* @param {Array<Function>} y.accessors - data buffer accessors
51+
* @param {Object} z - object containing output ndarray meta data
52+
* @param {string} z.dtype - data type
53+
* @param {Collection} z.data - data buffer
54+
* @param {NonNegativeIntegerArray} z.shape - dimensions
55+
* @param {IntegerArray} z.strides - stride lengths
56+
* @param {NonNegativeInteger} z.offset - index offset
57+
* @param {string} z.order - specifies whether `z` is row-major (C-style) or column-major (Fortran-style)
58+
* @param {Array<Function>} z.accessors - data buffer accessors
59+
* @param {Callback} fcn - binary callback
60+
*
61+
* @example
62+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
63+
* var accessors = require( '@stdlib/array/base/accessors' );
64+
* var copy = require( '@stdlib/array/base/copy' );
65+
*
66+
* function fcn( x, y ) {
67+
* return x + y;
68+
* }
69+
*
70+
* // Create data buffers:
71+
* var xbuf = toAccessorArray( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
72+
* var ybuf = toAccessorArray( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
73+
* var zbuf = toAccessorArray( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
74+
*
75+
* // Define the shape of the input and output arrays:
76+
* var shape = [ 1, 1, 3, 2, 2 ];
77+
*
78+
* // Define the array strides:
79+
* var sx = [ 12, 12, 4, 2, 1 ];
80+
* var sy = [ 12, 12, 4, 2, 1 ];
81+
* var sz = [ 12, 12, 4, 2, 1 ];
82+
*
83+
* // Define the index offsets:
84+
* var ox = 0;
85+
* var oy = 0;
86+
* var oz = 0;
87+
*
88+
* // Create the input and output ndarray-like objects:
89+
* var x = {
90+
* 'dtype': 'generic',
91+
* 'data': xbuf,
92+
* 'shape': shape,
93+
* 'strides': sx,
94+
* 'offset': ox,
95+
* 'order': 'row-major',
96+
* 'accessors': accessors( xbuf ).accessors
97+
* };
98+
* var y = {
99+
* 'dtype': 'generic',
100+
* 'data': ybuf,
101+
* 'shape': shape,
102+
* 'strides': sy,
103+
* 'offset': oy,
104+
* 'order': 'row-major',
105+
* 'accessors': accessors( ybuf ).accessors
106+
* };
107+
* var z = {
108+
* 'dtype': 'generic',
109+
* 'data': zbuf,
110+
* 'shape': shape,
111+
* 'strides': sz,
112+
* 'offset': oz,
113+
* 'order': 'row-major',
114+
* 'accessors': accessors( zbuf ).accessors
115+
* };
116+
*
117+
* // Apply the binary function:
118+
* blockedbinary5d( x, y, z, fcn );
119+
*
120+
* console.log( copy( z.data ) );
121+
* // => [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0 ]
122+
*/
123+
function blockedbinary5d( x, y, z, fcn ) {
124+
var bsize;
125+
var xbuf;
126+
var ybuf;
127+
var zbuf;
128+
var xget;
129+
var yget;
130+
var zset;
131+
var dx0;
132+
var dx1;
133+
var dx2;
134+
var dx3;
135+
var dx4;
136+
var dy0;
137+
var dy1;
138+
var dy2;
139+
var dy3;
140+
var dy4;
141+
var dz0;
142+
var dz1;
143+
var dz2;
144+
var dz3;
145+
var dz4;
146+
var ox1;
147+
var ox2;
148+
var ox3;
149+
var ox4;
150+
var oy1;
151+
var oy2;
152+
var oy3;
153+
var oy4;
154+
var oz1;
155+
var oz2;
156+
var oz3;
157+
var oz4;
158+
var sh;
159+
var s0;
160+
var s1;
161+
var s2;
162+
var s3;
163+
var s4;
164+
var sx;
165+
var sy;
166+
var sz;
167+
var ox;
168+
var oy;
169+
var oz;
170+
var ix;
171+
var iy;
172+
var iz;
173+
var i0;
174+
var i1;
175+
var i2;
176+
var i3;
177+
var i4;
178+
var j0;
179+
var j1;
180+
var j2;
181+
var j3;
182+
var j4;
183+
var o;
184+
185+
// Note on variable naming convention: s#, dx#, dy#, dz#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop...
186+
187+
// Resolve the loop interchange order:
188+
o = loopOrder( x.shape, x.strides, y.strides, z.strides );
189+
sh = o.sh;
190+
sx = o.sx;
191+
sy = o.sy;
192+
sz = o.sz;
193+
194+
// Determine the block size:
195+
bsize = blockSize( x.dtype, y.dtype, z.dtype );
196+
197+
// Cache the indices of the first indexed elements in the respective ndarrays...
198+
ox = x.offset;
199+
oy = y.offset;
200+
oz = z.offset;
201+
202+
// Cache references to the input and output ndarray buffers...
203+
xbuf = x.data;
204+
ybuf = y.data;
205+
zbuf = z.data;
206+
207+
// Cache offset increments for the innermost loop...
208+
dx0 = sx[0];
209+
dy0 = sy[0];
210+
dz0 = sz[0];
211+
212+
// Cache accessors:
213+
xget = x.accessors[0];
214+
yget = y.accessors[0];
215+
zset = z.accessors[1];
216+
217+
// Iterate over blocks...
218+
for ( j4 = sh[4]; j4 > 0; ) {
219+
if ( j4 < bsize ) {
220+
s4 = j4;
221+
j4 = 0;
222+
} else {
223+
s4 = bsize;
224+
j4 -= bsize;
225+
}
226+
ox4 = ox + ( j4*sx[4] );
227+
oy4 = oy + ( j4*sy[4] );
228+
oz4 = oz + ( j4*sz[4] );
229+
for ( j3 = sh[3]; j3 > 0; ) {
230+
if ( j3 < bsize ) {
231+
s3 = j3;
232+
j3 = 0;
233+
} else {
234+
s3 = bsize;
235+
j3 -= bsize;
236+
}
237+
dx4 = sx[4] - ( s3*sx[3] );
238+
dy4 = sy[4] - ( s3*sy[3] );
239+
dz4 = sz[4] - ( s3*sz[3] );
240+
ox3 = ox4 + ( j3*sx[3] );
241+
oy3 = oy4 + ( j3*sy[3] );
242+
oz3 = oz4 + ( j3*sz[3] );
243+
for ( j2 = sh[2]; j2 > 0; ) {
244+
if ( j2 < bsize ) {
245+
s2 = j2;
246+
j2 = 0;
247+
} else {
248+
s2 = bsize;
249+
j2 -= bsize;
250+
}
251+
dx3 = sx[3] - ( s2*sx[2] );
252+
dy3 = sy[3] - ( s2*sy[2] );
253+
dz3 = sz[3] - ( s2*sz[2] );
254+
ox2 = ox3 + ( j2*sx[2] );
255+
oy2 = oy3 + ( j2*sy[2] );
256+
oz2 = oz3 + ( j2*sz[2] );
257+
for ( j1 = sh[1]; j1 > 0; ) {
258+
if ( j1 < bsize ) {
259+
s1 = j1;
260+
j1 = 0;
261+
} else {
262+
s1 = bsize;
263+
j1 -= bsize;
264+
}
265+
dx2 = sx[2] - ( s1*sx[1] );
266+
dy2 = sy[2] - ( s1*sy[1] );
267+
dz2 = sz[2] - ( s1*sz[1] );
268+
ox1 = ox2 + ( j1*sx[1] );
269+
oy1 = oy2 + ( j1*sy[1] );
270+
oz1 = oz2 + ( j1*sz[1] );
271+
for ( j0 = sh[0]; j0 > 0; ) {
272+
if ( j0 < bsize ) {
273+
s0 = j0;
274+
j0 = 0;
275+
} else {
276+
s0 = bsize;
277+
j0 -= bsize;
278+
}
279+
// Compute index offsets for the first input and output ndarray elements in the current block...
280+
ix = ox1 + ( j0*sx[0] );
281+
iy = oy1 + ( j0*sy[0] );
282+
iz = oz1 + ( j0*sz[0] );
283+
284+
// Compute loop offset increments...
285+
dx1 = sx[1] - ( s0*sx[0] );
286+
dy1 = sy[1] - ( s0*sy[0] );
287+
dz1 = sz[1] - ( s0*sz[0] );
288+
289+
// Iterate over the ndarray dimensions...
290+
for ( i4 = 0; i4 < s4; i4++ ) {
291+
for ( i3 = 0; i3 < s3; i3++ ) {
292+
for ( i2 = 0; i2 < s2; i2++ ) {
293+
for ( i1 = 0; i1 < s1; i1++ ) {
294+
for ( i0 = 0; i0 < s0; i0++ ) {
295+
zset( zbuf, iz, fcn( xget( xbuf, ix ), yget( ybuf, iy ) ) );
296+
ix += dx0;
297+
iy += dy0;
298+
iz += dz0;
299+
}
300+
ix += dx1;
301+
iy += dy1;
302+
iz += dz1;
303+
}
304+
ix += dx2;
305+
iy += dy2;
306+
iz += dz2;
307+
}
308+
ix += dx3;
309+
iy += dy3;
310+
iz += dz3;
311+
}
312+
ix += dx4;
313+
iy += dy4;
314+
iz += dz4;
315+
}
316+
}
317+
}
318+
}
319+
}
320+
}
321+
}
322+
323+
324+
// EXPORTS //
325+
326+
module.exports = blockedbinary5d;

0 commit comments

Comments
 (0)