Skip to content

Commit 8500ee3

Browse files
committed
feat: add 7d block 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 8a2f7e2 commit 8500ee3

File tree

1 file changed

+368
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)