Skip to content

Commit e7f8179

Browse files
committed
feat: add 3d 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 ce28be3 commit e7f8179

File tree

1 file changed

+252
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)