Skip to content

Commit e152a98

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 bd392dc commit e152a98

File tree

1 file changed

+292
-0
lines changed
  • lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-to-struct/lib

1 file changed

+292
-0
lines changed
Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
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/unary-loop-interchange-order' );
24+
var blockSize = require( '@stdlib/ndarray/base/unary-tiling-block-size' );
25+
var takeIndexed = require( '@stdlib/array/base/take-indexed' );
26+
var copyIndexed = require( '@stdlib/array/base/copy-indexed' );
27+
var zeros = require( '@stdlib/array/base/zeros' );
28+
var incrementOffsets = require( './increment_offsets.js' );
29+
var setViewOffsets = require( './set_view_offsets.js' );
30+
var offsets = require( './offsets.js' );
31+
32+
33+
// MAIN //
34+
35+
/**
36+
* Performs a reduction over an input ndarray and assigns results to a provided output ndarray via loop blocking.
37+
*
38+
* @private
39+
* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function
40+
* @param {Array<Object>} arrays - ndarrays
41+
* @param {Array<Object>} views - initialized ndarray-like objects representing sub-array views
42+
* @param {IntegerArray} strides - loop dimension strides for the input ndarray
43+
* @param {Function} strategy - input ndarray reshape strategy
44+
* @param {Options} opts - function options
45+
* @returns {void}
46+
*
47+
* @example
48+
* var Float64Array = require( '@stdlib/array/float64' );
49+
* var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
50+
* var Float64Results = require( '@stdlib/stats/base/ztest/one-sample/results/float64' );
51+
* var structFactory = require( '@stdlib/array/struct-factory' );
52+
* var ztest = require( '@stdlib/stats/base/ndarray/ztest' );
53+
*
54+
* var ResultsArray = structFactory( Float64Results );
55+
*
56+
* // Create data buffers:
57+
* 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 ] );
58+
* var ybuf = new ResultsArray( 3 );
59+
*
60+
* // Define the array shapes:
61+
* var xsh = [ 1, 3, 2, 2 ];
62+
* var ysh = [ 1, 3 ];
63+
*
64+
* // Define the array strides:
65+
* var sx = [ 12, 4, 2, 1 ];
66+
* var sy = [ 3, 1 ];
67+
*
68+
* // Define the index offsets:
69+
* var ox = 0;
70+
* var oy = 0;
71+
*
72+
* // Create an input ndarray-like object:
73+
* var x = {
74+
* 'dtype': 'float64',
75+
* 'data': xbuf,
76+
* 'shape': xsh,
77+
* 'strides': sx,
78+
* 'offset': ox,
79+
* 'order': 'row-major'
80+
* };
81+
*
82+
* // Create an output ndarray-like object:
83+
* var y = {
84+
* 'dtype': Float64Results,
85+
* 'data': ybuf,
86+
* 'shape': ysh,
87+
* 'strides': sy,
88+
* 'offset': oy,
89+
* 'order': 'row-major'
90+
* };
91+
*
92+
* // Create additional parameter ndarray-like objects:
93+
* var alternative = {
94+
* 'dtype': 'generic',
95+
* 'data': [ 'two-sided' ],
96+
* 'shape': ysh,
97+
* 'strides': [ 0, 0 ],
98+
* 'offset': 0,
99+
* 'order': 'row-major'
100+
};
101+
* var alpha = {
102+
* 'dtype': 'float64',
103+
* 'data': [ 0.05 ],
104+
* 'shape': ysh,
105+
* 'strides': [ 0, 0 ],
106+
* 'offset': 0,
107+
* 'order': 'row-major'
108+
};
109+
* var mu = {
110+
* 'dtype': 'float64',
111+
* 'data': [ 0.0 ],
112+
* 'shape': ysh,
113+
* 'strides': [ 0, 0 ],
114+
* 'offset': 0,
115+
* 'order': 'row-major'
116+
};
117+
* var sigma = {
118+
* 'dtype': 'float64',
119+
* 'data': [ 1.0 ],
120+
* 'shape': ysh,
121+
* 'strides': [ 0, 0 ],
122+
* 'offset': 0,
123+
* 'order': 'row-major'
124+
* };
125+
*
126+
* // Initialize ndarray-like objects representing sub-array views:
127+
* var views = [
128+
* {
129+
* 'dtype': x.dtype,
130+
* 'data': x.data,
131+
* 'shape': [ 2, 2 ],
132+
* 'strides': [ 2, 1 ],
133+
* 'offset': x.offset,
134+
* 'order': x.order
135+
* },
136+
* {
137+
* 'dtype': y.dtype,
138+
* 'data': y.data,
139+
* 'shape': [],
140+
* 'strides': [ 0 ],
141+
* 'offset': y.offset,
142+
* 'order': y.order
143+
* },
144+
* {
145+
* 'dtype': alternative.dtype,
146+
* 'data': alternative.data,
147+
* 'shape': [],
148+
* 'strides': [ 0 ],
149+
* 'offset': alternative.offset,
150+
* 'order': alternative.order
151+
* },
152+
* {
153+
* 'dtype': alpha.dtype,
154+
* 'data': alpha.data,
155+
* 'shape': [],
156+
* 'strides': [ 0 ],
157+
* 'offset': alpha.offset,
158+
* 'order': alpha.order
159+
* },
160+
* {
161+
* 'dtype': mu.dtype,
162+
* 'data': mu.data,
163+
* 'shape': [],
164+
* 'strides': [ 0 ],
165+
* 'offset': mu.offset,
166+
* 'order': mu.order
167+
* },
168+
* {
169+
* 'dtype': sigma.dtype,
170+
* 'data': sigma.data,
171+
* 'shape': [],
172+
* 'strides': [ 0 ],
173+
* 'offset': sigma.offset,
174+
* 'order': sigma.order
175+
* }
176+
* ];
177+
*
178+
* // Define a reshape strategy:
179+
* function strategy( x ) {
180+
* return {
181+
* 'dtype': x.dtype,
182+
* 'data': x.data,
183+
* 'shape': [ 4 ],
184+
* 'strides': [ 1 ],
185+
* 'offset': x.offset,
186+
* 'order': x.order
187+
* };
188+
* }
189+
*
190+
* // Perform a reduction:
191+
* blockedunary2d( ztest, [ x, y, alternative, alpha, mu, sigma ], views, [ 12, 4 ], strategy, {} );
192+
*
193+
* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order );
194+
* // returns [ [ <Float64Results>, <Float64Results>, <Float64Results> ] ]
195+
*/
196+
function blockedunary2d( fcn, arrays, views, strides, strategy, opts ) {
197+
var bsize;
198+
var dv0;
199+
var dv1;
200+
var ov1;
201+
var sh;
202+
var s0;
203+
var s1;
204+
var sv;
205+
var ov;
206+
var iv;
207+
var i0;
208+
var i1;
209+
var j0;
210+
var j1;
211+
var N;
212+
var x;
213+
var y;
214+
var v;
215+
var o;
216+
var k;
217+
218+
// Note on variable naming convention: S#, dv#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop...
219+
220+
N = arrays.length;
221+
x = arrays[ 0 ];
222+
y = arrays[ 1 ];
223+
224+
// Resolve the loop interchange order:
225+
o = loopOrder( y.shape, strides, y.strides );
226+
sh = o.sh;
227+
sv = [ o.sx, o.sy ];
228+
for ( k = 2; k < N; k++ ) {
229+
sv.push( takeIndexed( arrays[k].strides, o.idx ) );
230+
}
231+
// Determine the block size:
232+
bsize = blockSize( x.dtype, y.dtype );
233+
234+
// Resolve a list of pointers to the first indexed elements in the respective ndarrays:
235+
ov = offsets( arrays );
236+
237+
// Cache offset increments for the innermost loop...
238+
dv0 = [];
239+
for ( k = 0; k < N; k++ ) {
240+
dv0.push( sv[k][0] );
241+
}
242+
// Initialize loop variables...
243+
ov1 = zeros( N );
244+
dv1 = zeros( N );
245+
iv = zeros( N );
246+
247+
// Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views:
248+
v = copyIndexed( views );
249+
250+
// Iterate over blocks...
251+
for ( j1 = sh[1]; j1 > 0; ) {
252+
if ( j1 < bsize ) {
253+
s1 = j1;
254+
j1 = 0;
255+
} else {
256+
s1 = bsize;
257+
j1 -= bsize;
258+
}
259+
for ( k = 0; k < N; k++ ) {
260+
ov1[ k ] = ov[k] + ( j1*sv[k][1] );
261+
}
262+
for ( j0 = sh[0]; j0 > 0; ) {
263+
if ( j0 < bsize ) {
264+
s0 = j0;
265+
j0 = 0;
266+
} else {
267+
s0 = bsize;
268+
j0 -= bsize;
269+
}
270+
// Compute index offsets and loop offset increments for the first ndarray elements in the current block...
271+
for ( k = 0; k < N; k++ ) {
272+
iv[ k ] = ov1[k] + ( j0*sv[k][0] );
273+
dv1[ k ] = sv[k][1] - ( s0*sv[k][0] );
274+
}
275+
// Iterate over the non-reduced ndarray dimensions...
276+
for ( i1 = 0; i1 < s1; i1++ ) {
277+
for ( i0 = 0; i0 < s0; i0++ ) {
278+
setViewOffsets( views, iv );
279+
v[ 0 ] = strategy( views[ 0 ] );
280+
fcn( v, opts );
281+
incrementOffsets( iv, dv0 );
282+
}
283+
incrementOffsets( iv, dv1 );
284+
}
285+
}
286+
}
287+
}
288+
289+
290+
// EXPORTS //
291+
292+
module.exports = blockedunary2d;

0 commit comments

Comments
 (0)