Skip to content

Commit b00bd71

Browse files
committed
feat: add 6d 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 2e4f0bc commit b00bd71

File tree

1 file changed

+355
-0
lines changed

1 file changed

+355
-0
lines changed
Lines changed: 355 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,355 @@
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, max-lines-per-function */
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 six-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, 1, 3, 2, 2 ];
77+
*
78+
* // Define the array strides:
79+
* var sx = [ 12, 12, 12, 4, 2, 1 ];
80+
* var sy = [ 12, 12, 12, 4, 2, 1 ];
81+
* var sz = [ 12, 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+
* blockedbinary6d( 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 blockedbinary6d( 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 dx5;
137+
var dy0;
138+
var dy1;
139+
var dy2;
140+
var dy3;
141+
var dy4;
142+
var dy5;
143+
var dz0;
144+
var dz1;
145+
var dz2;
146+
var dz3;
147+
var dz4;
148+
var dz5;
149+
var ox1;
150+
var ox2;
151+
var ox3;
152+
var ox4;
153+
var ox5;
154+
var oy1;
155+
var oy2;
156+
var oy3;
157+
var oy4;
158+
var oy5;
159+
var oz1;
160+
var oz2;
161+
var oz3;
162+
var oz4;
163+
var oz5;
164+
var sh;
165+
var s0;
166+
var s1;
167+
var s2;
168+
var s3;
169+
var s4;
170+
var s5;
171+
var sx;
172+
var sy;
173+
var sz;
174+
var ox;
175+
var oy;
176+
var oz;
177+
var ix;
178+
var iy;
179+
var iz;
180+
var i0;
181+
var i1;
182+
var i2;
183+
var i3;
184+
var i4;
185+
var i5;
186+
var j0;
187+
var j1;
188+
var j2;
189+
var j3;
190+
var j4;
191+
var j5;
192+
var o;
193+
194+
// Note on variable naming convention: s#, dx#, dy#, dz#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop...
195+
196+
// Resolve the loop interchange order:
197+
o = loopOrder( x.shape, x.strides, y.strides, z.strides );
198+
sh = o.sh;
199+
sx = o.sx;
200+
sy = o.sy;
201+
sz = o.sz;
202+
203+
// Determine the block size:
204+
bsize = blockSize( x.dtype, y.dtype, z.dtype );
205+
206+
// Cache the indices of the first indexed elements in the respective ndarrays...
207+
ox = x.offset;
208+
oy = y.offset;
209+
oz = z.offset;
210+
211+
// Cache references to the input and output ndarray buffers...
212+
xbuf = x.data;
213+
ybuf = y.data;
214+
zbuf = z.data;
215+
216+
// Cache offset increments for the innermost loop...
217+
dx0 = sx[0];
218+
dy0 = sy[0];
219+
dz0 = sz[0];
220+
221+
// Cache accessors:
222+
xget = x.accessors[0];
223+
yget = y.accessors[0];
224+
zset = z.accessors[1];
225+
226+
// Iterate over blocks...
227+
for ( j5 = sh[5]; j5 > 0; ) {
228+
if ( j5 < bsize ) {
229+
s5 = j5;
230+
j5 = 0;
231+
} else {
232+
s5 = bsize;
233+
j5 -= bsize;
234+
}
235+
ox5 = ox + ( j5*sx[5] );
236+
oy5 = oy + ( j5*sy[5] );
237+
oz5 = oz + ( j5*sz[5] );
238+
for ( j4 = sh[4]; j4 > 0; ) {
239+
if ( j4 < bsize ) {
240+
s4 = j4;
241+
j4 = 0;
242+
} else {
243+
s4 = bsize;
244+
j4 -= bsize;
245+
}
246+
dx5 = sx[5] - ( s4*sx[4] );
247+
dy5 = sy[5] - ( s4*sy[4] );
248+
dz5 = sz[5] - ( s4*sz[4] );
249+
ox4 = ox5 + ( j4*sx[4] );
250+
oy4 = oy5 + ( j4*sy[4] );
251+
oz4 = oz5 + ( j4*sz[4] );
252+
for ( j3 = sh[3]; j3 > 0; ) {
253+
if ( j3 < bsize ) {
254+
s3 = j3;
255+
j3 = 0;
256+
} else {
257+
s3 = bsize;
258+
j3 -= bsize;
259+
}
260+
dx4 = sx[4] - ( s3*sx[3] );
261+
dy4 = sy[4] - ( s3*sy[3] );
262+
dz4 = sz[4] - ( s3*sz[3] );
263+
ox3 = ox4 + ( j3*sx[3] );
264+
oy3 = oy4 + ( j3*sy[3] );
265+
oz3 = oz4 + ( j3*sz[3] );
266+
for ( j2 = sh[2]; j2 > 0; ) {
267+
if ( j2 < bsize ) {
268+
s2 = j2;
269+
j2 = 0;
270+
} else {
271+
s2 = bsize;
272+
j2 -= bsize;
273+
}
274+
dx3 = sx[3] - ( s2*sx[2] );
275+
dy3 = sy[3] - ( s2*sy[2] );
276+
dz3 = sz[3] - ( s2*sz[2] );
277+
ox2 = ox3 + ( j2*sx[2] );
278+
oy2 = oy3 + ( j2*sy[2] );
279+
oz2 = oz3 + ( j2*sz[2] );
280+
for ( j1 = sh[1]; j1 > 0; ) {
281+
if ( j1 < bsize ) {
282+
s1 = j1;
283+
j1 = 0;
284+
} else {
285+
s1 = bsize;
286+
j1 -= bsize;
287+
}
288+
dx2 = sx[2] - ( s1*sx[1] );
289+
dy2 = sy[2] - ( s1*sy[1] );
290+
dz2 = sz[2] - ( s1*sz[1] );
291+
ox1 = ox2 + ( j1*sx[1] );
292+
oy1 = oy2 + ( j1*sy[1] );
293+
oz1 = oz2 + ( j1*sz[1] );
294+
for ( j0 = sh[0]; j0 > 0; ) {
295+
if ( j0 < bsize ) {
296+
s0 = j0;
297+
j0 = 0;
298+
} else {
299+
s0 = bsize;
300+
j0 -= bsize;
301+
}
302+
// Compute index offsets for the first input and output ndarray elements in the current block...
303+
ix = ox1 + ( j0*sx[0] );
304+
iy = oy1 + ( j0*sy[0] );
305+
iz = oz1 + ( j0*sz[0] );
306+
307+
// Compute loop offset increments...
308+
dx1 = sx[1] - ( s0*sx[0] );
309+
dy1 = sy[1] - ( s0*sy[0] );
310+
dz1 = sz[1] - ( s0*sz[0] );
311+
312+
// Iterate over the ndarray dimensions...
313+
for ( i5 = 0; i5 < s5; i5++ ) {
314+
for ( i4 = 0; i4 < s4; i4++ ) {
315+
for ( i3 = 0; i3 < s3; i3++ ) {
316+
for ( i2 = 0; i2 < s2; i2++ ) {
317+
for ( i1 = 0; i1 < s1; i1++ ) {
318+
for ( i0 = 0; i0 < s0; i0++ ) {
319+
zset( zbuf, iz, fcn( xget( xbuf, ix ), yget( ybuf, iy ) ) );
320+
ix += dx0;
321+
iy += dy0;
322+
iz += dz0;
323+
}
324+
ix += dx1;
325+
iy += dy1;
326+
iz += dz1;
327+
}
328+
ix += dx2;
329+
iy += dy2;
330+
iz += dz2;
331+
}
332+
ix += dx3;
333+
iy += dy3;
334+
iz += dz3;
335+
}
336+
ix += dx4;
337+
iy += dy4;
338+
iz += dz4;
339+
}
340+
ix += dx5;
341+
iy += dy5;
342+
iz += dz5;
343+
}
344+
}
345+
}
346+
}
347+
}
348+
}
349+
}
350+
}
351+
352+
353+
// EXPORTS //
354+
355+
module.exports = blockedbinary6d;

0 commit comments

Comments
 (0)