Skip to content

Commit ba6846b

Browse files
committed
feat: add 2d 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 7010ff5 commit ba6846b

File tree

1 file changed

+221
-0
lines changed

1 file changed

+221
-0
lines changed
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
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+
'use strict';
20+
21+
// MODULES //
22+
23+
var loopOrder = require( '@stdlib/ndarray/base/binary-loop-interchange-order' );
24+
var blockSize = require( '@stdlib/ndarray/base/binary-tiling-block-size' );
25+
26+
27+
// MAIN //
28+
29+
/**
30+
* Applies a binary callback to elements in two-dimensional input ndarrays and assigns results to elements in an equivalently shaped output ndarray via loop blocking.
31+
*
32+
* @private
33+
* @param {Object} x - object containing input ndarray meta data
34+
* @param {string} x.dtype - data type
35+
* @param {Collection} x.data - data buffer
36+
* @param {NonNegativeIntegerArray} x.shape - dimensions
37+
* @param {IntegerArray} x.strides - stride lengths
38+
* @param {NonNegativeInteger} x.offset - index offset
39+
* @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style)
40+
* @param {Object} y - object containing input ndarray meta data
41+
* @param {string} y.dtype - data type
42+
* @param {Collection} y.data - data buffer
43+
* @param {NonNegativeIntegerArray} y.shape - dimensions
44+
* @param {IntegerArray} y.strides - stride lengths
45+
* @param {NonNegativeInteger} y.offset - index offset
46+
* @param {string} y.order - specifies whether `y` is row-major (C-style) or column-major (Fortran-style)
47+
* @param {Object} z - object containing output ndarray meta data
48+
* @param {string} z.dtype - data type
49+
* @param {Collection} z.data - data buffer
50+
* @param {NonNegativeIntegerArray} z.shape - dimensions
51+
* @param {IntegerArray} z.strides - stride lengths
52+
* @param {NonNegativeInteger} z.offset - index offset
53+
* @param {string} z.order - specifies whether `z` is row-major (C-style) or column-major (Fortran-style)
54+
* @param {Callback} fcn - binary callback
55+
*
56+
* @example
57+
* var Float64Array = require( '@stdlib/array/float64' );
58+
*
59+
* function fcn( x, y ) {
60+
* return x + y;
61+
* }
62+
*
63+
* // Create data buffers:
64+
* 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 ] );
65+
* 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 ] );
66+
* var zbuf = new Float64Array( 12 );
67+
*
68+
* // Define the shape of the input and output arrays:
69+
* var shape = [ 6, 2 ];
70+
*
71+
* // Define the array strides:
72+
* var sx = [ 2, 1 ];
73+
* var sy = [ 2, 1 ];
74+
* var sz = [ 2, 1 ];
75+
*
76+
* // Define the index offsets:
77+
* var ox = 0;
78+
* var oy = 0;
79+
* var oz = 0;
80+
*
81+
* // Create the input and output ndarray-like objects:
82+
* var x = {
83+
* 'dtype': 'float64',
84+
* 'data': xbuf,
85+
* 'shape': shape,
86+
* 'strides': sx,
87+
* 'offset': ox,
88+
* 'order': 'row-major'
89+
* };
90+
* var y = {
91+
* 'dtype': 'float64',
92+
* 'data': ybuf,
93+
* 'shape': shape,
94+
* 'strides': sy,
95+
* 'offset': oy,
96+
* 'order': 'row-major'
97+
* };
98+
* var z = {
99+
* 'dtype': 'float64',
100+
* 'data': zbuf,
101+
* 'shape': shape,
102+
* 'strides': sz,
103+
* 'offset': oz,
104+
* 'order': 'row-major'
105+
* };
106+
*
107+
* // Apply the binary function:
108+
* blockedbinary2d( x, y, z, fcn );
109+
*
110+
* console.log( z.data );
111+
* // => <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 ]
112+
*/
113+
function blockedbinary2d( x, y, z, fcn ) {
114+
var bsize;
115+
var xbuf;
116+
var ybuf;
117+
var zbuf;
118+
var dx0;
119+
var dx1;
120+
var dy0;
121+
var dy1;
122+
var dz0;
123+
var dz1;
124+
var ox1;
125+
var oy1;
126+
var oz1;
127+
var sh;
128+
var s0;
129+
var s1;
130+
var sx;
131+
var sy;
132+
var sz;
133+
var ox;
134+
var oy;
135+
var oz;
136+
var ix;
137+
var iy;
138+
var iz;
139+
var i0;
140+
var i1;
141+
var j0;
142+
var j1;
143+
var o;
144+
145+
// Note on variable naming convention: s#, dx#, dy#, dz#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop...
146+
147+
// Resolve the loop interchange order:
148+
o = loopOrder( x.shape, x.strides, y.strides, z.strides );
149+
sh = o.sh;
150+
sx = o.sx;
151+
sy = o.sy;
152+
sz = o.sz;
153+
154+
// Determine the block size:
155+
bsize = blockSize( x.dtype, y.dtype );
156+
157+
// Cache the indices of the first indexed elements in the respective ndarrays...
158+
ox = x.offset;
159+
oy = y.offset;
160+
oz = z.offset;
161+
162+
// Cache references to the input and output ndarray buffers...
163+
xbuf = x.data;
164+
ybuf = y.data;
165+
zbuf = z.data;
166+
167+
// Cache offset increments for the innermost loop...
168+
dx0 = sx[0];
169+
dy0 = sy[0];
170+
dz0 = sz[0];
171+
172+
// Iterate over blocks...
173+
for ( j1 = sh[1]; j1 > 0; ) {
174+
if ( j1 < bsize ) {
175+
s1 = j1;
176+
j1 = 0;
177+
} else {
178+
s1 = bsize;
179+
j1 -= bsize;
180+
}
181+
ox1 = ox + ( j1*sx[1] );
182+
oy1 = oy + ( j1*sy[1] );
183+
oz1 = oz + ( j1*sz[1] );
184+
for ( j0 = sh[0]; j0 > 0; ) {
185+
if ( j0 < bsize ) {
186+
s0 = j0;
187+
j0 = 0;
188+
} else {
189+
s0 = bsize;
190+
j0 -= bsize;
191+
}
192+
// Compute index offsets for the first input and output ndarray elements in the current block...
193+
ix = ox1 + ( j0*sx[0] );
194+
iy = oy1 + ( j0*sy[0] );
195+
iz = oz1 + ( j0*sz[0] );
196+
197+
// Compute loop offset increments...
198+
dx1 = sx[1] - ( s0*sx[0] );
199+
dy1 = sy[1] - ( s0*sy[0] );
200+
dz1 = sz[1] - ( s0*sz[0] );
201+
202+
// Iterate over the ndarray dimensions...
203+
for ( i1 = 0; i1 < s1; i1++ ) {
204+
for ( i0 = 0; i0 < s0; i0++ ) {
205+
zbuf[ iz ] = fcn( xbuf[ ix ], ybuf[ iy ] );
206+
ix += dx0;
207+
iy += dy0;
208+
iz += dz0;
209+
}
210+
ix += dx1;
211+
iy += dy1;
212+
iz += dz1;
213+
}
214+
}
215+
}
216+
}
217+
218+
219+
// EXPORTS //
220+
221+
module.exports = blockedbinary2d;

0 commit comments

Comments
 (0)