Skip to content

Commit 535655b

Browse files
committed
feat: add 5d kernels
--- 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 ad39d00 commit 535655b

File tree

4 files changed

+962
-0
lines changed

4 files changed

+962
-0
lines changed
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
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 isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major-string' );
24+
var incrementOffsets = require( './increment_offsets.js' );
25+
var setViewOffsets = require( './set_view_offsets.js' );
26+
var offsets = require( './offsets.js' );
27+
28+
29+
// MAIN //
30+
31+
/**
32+
* Performs a reduction over an input ndarray and assigns results to a provided output ndarray.
33+
*
34+
* @private
35+
* @param {Function} fcn - reduction function
36+
* @param {Array<Object>} arrays - ndarrays
37+
* @param {Array<Object>} views - initialized ndarray-like objects representing sub-array views
38+
* @param {IntegerArray} strides - loop dimension strides for the input ndarray
39+
* @param {Options} opts - function options
40+
* @returns {void}
41+
*
42+
* @example
43+
* var Float64Array = require( '@stdlib/array/float64' );
44+
* var filled = require( '@stdlib/array/base/filled' );
45+
* var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
46+
* var base = require( '@stdlib/ndarray/base/every' );
47+
*
48+
* // Create data buffers:
49+
* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 0.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
50+
* var ybuf = filled( false, 3 );
51+
*
52+
* // Define the array shapes:
53+
* var xsh = [ 1, 1, 1, 1, 3, 2, 2 ];
54+
* var ysh = [ 1, 1, 1, 1, 3 ];
55+
*
56+
* // Define the array strides:
57+
* var sx = [ 12, 12, 12, 12, 4, 2, 1 ];
58+
* var sy = [ 3, 3, 3, 3, 1 ];
59+
*
60+
* // Define the index offsets:
61+
* var ox = 0;
62+
* var oy = 0;
63+
*
64+
* // Create an input ndarray-like object:
65+
* var x = {
66+
* 'dtype': 'float64',
67+
* 'data': xbuf,
68+
* 'shape': xsh,
69+
* 'strides': sx,
70+
* 'offset': ox,
71+
* 'order': 'row-major'
72+
* };
73+
*
74+
* // Create an output ndarray-like object:
75+
* var y = {
76+
* 'dtype': 'generic',
77+
* 'data': ybuf,
78+
* 'shape': ysh,
79+
* 'strides': sy,
80+
* 'offset': oy,
81+
* 'order': 'row-major'
82+
* };
83+
*
84+
* // Initialize ndarray-like objects representing sub-array views:
85+
* var views = [
86+
* {
87+
* 'dtype': x.dtype,
88+
* 'data': x.data,
89+
* 'shape': [ 2, 2 ],
90+
* 'strides': [ 2, 1 ],
91+
* 'offset': x.offset,
92+
* 'order': x.order
93+
* }
94+
* ];
95+
*
96+
* // Perform a reduction:
97+
* unary5d( base, [ x, y ], views, [ 12, 12, 12, 12, 4 ], {} );
98+
*
99+
* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order );
100+
* // returns [ [ [ [ [ true, false, true ] ] ] ] ]
101+
*/
102+
function unary5d( fcn, arrays, views, strides, opts ) {
103+
var ybuf;
104+
var dv0;
105+
var dv1;
106+
var dv2;
107+
var dv3;
108+
var dv4;
109+
var sh;
110+
var S0;
111+
var S1;
112+
var S2;
113+
var S3;
114+
var S4;
115+
var sv;
116+
var iv;
117+
var i0;
118+
var i1;
119+
var i2;
120+
var i3;
121+
var i4;
122+
var y;
123+
var i;
124+
125+
// Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop...
126+
127+
// Resolve the output ndarray and associated shape:
128+
y = arrays[ 1 ];
129+
sh = y.shape;
130+
131+
// Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
132+
if ( isRowMajor( y.order ) ) {
133+
// For row-major ndarrays, the last dimensions have the fastest changing indices...
134+
S0 = sh[ 4 ];
135+
S1 = sh[ 3 ];
136+
S2 = sh[ 2 ];
137+
S3 = sh[ 1 ];
138+
S4 = sh[ 0 ];
139+
dv0 = [ strides[4] ]; // offset increment for innermost loop
140+
dv1 = [ strides[3] - ( S0*strides[4] ) ];
141+
dv2 = [ strides[2] - ( S1*strides[3] ) ];
142+
dv3 = [ strides[1] - ( S2*strides[2] ) ];
143+
dv4 = [ strides[0] - ( S3*strides[1] ) ]; // offset increment for outermost loop
144+
for ( i = 1; i < arrays.length; i++ ) {
145+
sv = arrays[ i ].strides;
146+
dv0.push( sv[4] );
147+
dv1.push( sv[3] - ( S0*sv[4] ) );
148+
dv2.push( sv[2] - ( S1*sv[3] ) );
149+
dv3.push( sv[1] - ( S2*sv[2]) );
150+
dv4.push( sv[0] - ( S3*sv[1]) );
151+
}
152+
} else { // order === 'column-major'
153+
// For column-major ndarrays, the first dimensions have the fastest changing indices...
154+
S0 = sh[ 0 ];
155+
S1 = sh[ 1 ];
156+
S2 = sh[ 2 ];
157+
S3 = sh[ 3 ];
158+
S4 = sh[ 4 ];
159+
dv0 = [ strides[0] ]; // offset increment for innermost loop
160+
dv1 = [ strides[1] - ( S0*strides[0] ) ];
161+
dv2 = [ strides[2] - ( S1*strides[1] ) ];
162+
dv3 = [ strides[3] - ( S2*strides[2] ) ];
163+
dv4 = [ strides[4] - ( S3*strides[3] ) ]; // offset increment for outermost loop
164+
for ( i = 1; i < arrays.length; i++ ) {
165+
sv = arrays[ i ].strides;
166+
dv0.push( sv[0] );
167+
dv1.push( sv[1] - ( S0*sv[0] ) );
168+
dv2.push( sv[2] - ( S1*sv[1] ) );
169+
dv3.push( sv[3] - ( S2*sv[2] ) );
170+
dv4.push( sv[4] - ( S3*sv[3] ) );
171+
}
172+
}
173+
// Resolve a list of pointers to the first indexed elements in the respective ndarrays:
174+
iv = offsets( arrays );
175+
176+
// Cache a reference to the output ndarray buffer:
177+
ybuf = y.data;
178+
179+
// Iterate over the non-reduced ndarray dimensions...
180+
for ( i4 = 0; i4 < S4; i4++ ) {
181+
for ( i3 = 0; i3 < S3; i3++ ) {
182+
for ( i2 = 0; i2 < S2; i2++ ) {
183+
for ( i1 = 0; i1 < S1; i1++ ) {
184+
for ( i0 = 0; i0 < S0; i0++ ) {
185+
setViewOffsets( views, iv );
186+
ybuf[ iv[1] ] = fcn( views, opts );
187+
incrementOffsets( iv, dv0 );
188+
}
189+
incrementOffsets( iv, dv1 );
190+
}
191+
incrementOffsets( iv, dv2 );
192+
}
193+
incrementOffsets( iv, dv3 );
194+
}
195+
incrementOffsets( iv, dv4 );
196+
}
197+
}
198+
199+
200+
// EXPORTS //
201+
202+
module.exports = unary5d;

0 commit comments

Comments
 (0)