Skip to content

Commit e6dc6be

Browse files
committed
feat: add 4d blocked 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 e7f8179 commit e6dc6be

File tree

1 file changed

+281
-0
lines changed

1 file changed

+281
-0
lines changed
Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
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-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 four-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, 3, 2, 2 ];
72+
*
73+
* // Define the array strides:
74+
* var sx = [ 12, 4, 2, 1 ];
75+
* var sy = [ 12, 4, 2, 1 ];
76+
* var sz = [ 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+
* blockedbinary4d( 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 blockedbinary4d( 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 dy0;
125+
var dy1;
126+
var dy2;
127+
var dy3;
128+
var dz0;
129+
var dz1;
130+
var dz2;
131+
var dz3;
132+
var ox1;
133+
var ox2;
134+
var ox3;
135+
var oy1;
136+
var oy2;
137+
var oy3;
138+
var oz1;
139+
var oz2;
140+
var oz3;
141+
var sh;
142+
var s0;
143+
var s1;
144+
var s2;
145+
var s3;
146+
var sx;
147+
var sy;
148+
var sz;
149+
var ox;
150+
var oy;
151+
var oz;
152+
var ix;
153+
var iy;
154+
var iz;
155+
var i0;
156+
var i1;
157+
var i2;
158+
var i3;
159+
var j0;
160+
var j1;
161+
var j2;
162+
var j3;
163+
var o;
164+
165+
// Note on variable naming convention: s#, dx#, dy#, dz#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop...
166+
167+
// Resolve the loop interchange order:
168+
o = loopOrder( x.shape, x.strides, y.strides, z.strides );
169+
sh = o.sh;
170+
sx = o.sx;
171+
sy = o.sy;
172+
sz = o.sz;
173+
174+
// Determine the block size:
175+
bsize = blockSize( x.dtype, y.dtype, z.dtype );
176+
177+
// Cache the indices of the first indexed elements in the respective ndarrays...
178+
ox = x.offset;
179+
oy = y.offset;
180+
oz = z.offset;
181+
182+
// Cache references to the input and output ndarray buffers...
183+
xbuf = x.data;
184+
ybuf = y.data;
185+
zbuf = z.data;
186+
187+
// Cache offset increments for the innermost loop...
188+
dx0 = sx[0];
189+
dy0 = sy[0];
190+
dz0 = sz[0];
191+
192+
// Iterate over blocks...
193+
for ( j3 = sh[3]; j3 > 0; ) {
194+
if ( j3 < bsize ) {
195+
s3 = j3;
196+
j3 = 0;
197+
} else {
198+
s3 = bsize;
199+
j3 -= bsize;
200+
}
201+
ox3 = ox + ( j3*sx[3] );
202+
oy3 = oy + ( j3*sy[3] );
203+
oz3 = oz + ( j3*sz[3] );
204+
for ( j2 = sh[2]; j2 > 0; ) {
205+
if ( j2 < bsize ) {
206+
s2 = j2;
207+
j2 = 0;
208+
} else {
209+
s2 = bsize;
210+
j2 -= bsize;
211+
}
212+
dx3 = sx[3] - ( s2*sx[2] );
213+
dy3 = sy[3] - ( s2*sy[2] );
214+
dz3 = sz[3] - ( s2*sz[2] );
215+
ox2 = ox3 + ( j2*sx[2] );
216+
oy2 = oy3 + ( j2*sy[2] );
217+
oz2 = oz3 + ( j2*sz[2] );
218+
for ( j1 = sh[1]; j1 > 0; ) {
219+
if ( j1 < bsize ) {
220+
s1 = j1;
221+
j1 = 0;
222+
} else {
223+
s1 = bsize;
224+
j1 -= bsize;
225+
}
226+
dx2 = sx[2] - ( s1*sx[1] );
227+
dy2 = sy[2] - ( s1*sy[1] );
228+
dz2 = sz[2] - ( s1*sz[1] );
229+
ox1 = ox2 + ( j1*sx[1] );
230+
oy1 = oy2 + ( j1*sy[1] );
231+
oz1 = oz2 + ( j1*sz[1] );
232+
for ( j0 = sh[0]; j0 > 0; ) {
233+
if ( j0 < bsize ) {
234+
s0 = j0;
235+
j0 = 0;
236+
} else {
237+
s0 = bsize;
238+
j0 -= bsize;
239+
}
240+
// Compute index offsets for the first input and output ndarray elements in the current block...
241+
ix = ox1 + ( j0*sx[0] );
242+
iy = oy1 + ( j0*sy[0] );
243+
iz = oz1 + ( j0*sz[0] );
244+
245+
// Compute loop offset increments...
246+
dx1 = sx[1] - ( s0*sx[0] );
247+
dy1 = sy[1] - ( s0*sy[0] );
248+
dz1 = sz[1] - ( s0*sz[0] );
249+
250+
// Iterate over the ndarray dimensions...
251+
for ( i3 = 0; i3 < s3; i3++ ) {
252+
for ( i2 = 0; i2 < s2; i2++ ) {
253+
for ( i1 = 0; i1 < s1; i1++ ) {
254+
for ( i0 = 0; i0 < s0; i0++ ) {
255+
zbuf[ iz ] = fcn( xbuf[ ix ], ybuf[ iy ] );
256+
ix += dx0;
257+
iy += dy0;
258+
iz += dz0;
259+
}
260+
ix += dx1;
261+
iy += dy1;
262+
iz += dz1;
263+
}
264+
ix += dx2;
265+
iy += dy2;
266+
iz += dz2;
267+
}
268+
ix += dx3;
269+
iy += dy3;
270+
iz += dz3;
271+
}
272+
}
273+
}
274+
}
275+
}
276+
}
277+
278+
279+
// EXPORTS //
280+
281+
module.exports = blockedbinary4d;

0 commit comments

Comments
 (0)