|
| 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-len, 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 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 {Array<Function>} x.accessors - data buffer accessors |
| 43 | +* @param {Object} y - object containing input ndarray meta data |
| 44 | +* @param {string} y.dtype - data type |
| 45 | +* @param {Collection} y.data - data buffer |
| 46 | +* @param {NonNegativeIntegerArray} y.shape - dimensions |
| 47 | +* @param {IntegerArray} y.strides - stride lengths |
| 48 | +* @param {NonNegativeInteger} y.offset - index offset |
| 49 | +* @param {string} y.order - specifies whether `y` is row-major (C-style) or column-major (Fortran-style) |
| 50 | +* @param {Array<Function>} y.accessors - data buffer accessors |
| 51 | +* @param {Object} z - object containing output ndarray meta data |
| 52 | +* @param {string} z.dtype - data type |
| 53 | +* @param {Collection} z.data - data buffer |
| 54 | +* @param {NonNegativeIntegerArray} z.shape - dimensions |
| 55 | +* @param {IntegerArray} z.strides - stride lengths |
| 56 | +* @param {NonNegativeInteger} z.offset - index offset |
| 57 | +* @param {string} z.order - specifies whether `z` is row-major (C-style) or column-major (Fortran-style) |
| 58 | +* @param {Array<Function>} z.accessors - data buffer accessors |
| 59 | +* @param {Callback} fcn - binary callback |
| 60 | +* |
| 61 | +* @example |
| 62 | +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); |
| 63 | +* var accessors = require( '@stdlib/array/base/accessors' ); |
| 64 | +* var copy = require( '@stdlib/array/base/copy' ); |
| 65 | +* |
| 66 | +* function fcn( x, y ) { |
| 67 | +* return x + y; |
| 68 | +* } |
| 69 | +* |
| 70 | +* // Create data buffers: |
| 71 | +* var xbuf = toAccessorArray( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); |
| 72 | +* var ybuf = toAccessorArray( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] ); |
| 73 | +* var zbuf = toAccessorArray( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); |
| 74 | +* |
| 75 | +* // Define the shape of the input and output arrays: |
| 76 | +* var shape = [ 3, 2, 2 ]; |
| 77 | +* |
| 78 | +* // Define the array strides: |
| 79 | +* var sx = [ 4, 2, 1 ]; |
| 80 | +* var sy = [ 4, 2, 1 ]; |
| 81 | +* var sz = [ 4, 2, 1 ]; |
| 82 | +* |
| 83 | +* // Define the index offsets: |
| 84 | +* var ox = 0; |
| 85 | +* var oy = 0; |
| 86 | +* var oz = 0; |
| 87 | +* |
| 88 | +* // Create the input and output ndarray-like objects: |
| 89 | +* var x = { |
| 90 | +* 'dtype': 'generic', |
| 91 | +* 'data': xbuf, |
| 92 | +* 'shape': shape, |
| 93 | +* 'strides': sx, |
| 94 | +* 'offset': ox, |
| 95 | +* 'order': 'row-major', |
| 96 | +* 'accessors': accessors( xbuf ).accessors |
| 97 | +* }; |
| 98 | +* var y = { |
| 99 | +* 'dtype': 'generic', |
| 100 | +* 'data': ybuf, |
| 101 | +* 'shape': shape, |
| 102 | +* 'strides': sy, |
| 103 | +* 'offset': oy, |
| 104 | +* 'order': 'row-major', |
| 105 | +* 'accessors': accessors( ybuf ).accessors |
| 106 | +* }; |
| 107 | +* var z = { |
| 108 | +* 'dtype': 'generic', |
| 109 | +* 'data': zbuf, |
| 110 | +* 'shape': shape, |
| 111 | +* 'strides': sz, |
| 112 | +* 'offset': oz, |
| 113 | +* 'order': 'row-major', |
| 114 | +* 'accessors': accessors( zbuf ).accessors |
| 115 | +* }; |
| 116 | +* |
| 117 | +* // Apply the binary function: |
| 118 | +* blockedbinary3d( x, y, z, fcn ); |
| 119 | +* |
| 120 | +* console.log( copy( z.data ) ); |
| 121 | +* // => [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0 ] |
| 122 | +*/ |
| 123 | +function blockedbinary3d( x, y, z, fcn ) { |
| 124 | + var bsize; |
| 125 | + var xbuf; |
| 126 | + var ybuf; |
| 127 | + var zbuf; |
| 128 | + var xget; |
| 129 | + var yget; |
| 130 | + var zset; |
| 131 | + var dx0; |
| 132 | + var dx1; |
| 133 | + var dx2; |
| 134 | + var dy0; |
| 135 | + var dy1; |
| 136 | + var dy2; |
| 137 | + var dz0; |
| 138 | + var dz1; |
| 139 | + var dz2; |
| 140 | + var ox1; |
| 141 | + var ox2; |
| 142 | + var oy1; |
| 143 | + var oy2; |
| 144 | + var oz1; |
| 145 | + var oz2; |
| 146 | + var sh; |
| 147 | + var s0; |
| 148 | + var s1; |
| 149 | + var s2; |
| 150 | + var sx; |
| 151 | + var sy; |
| 152 | + var sz; |
| 153 | + var ox; |
| 154 | + var oy; |
| 155 | + var oz; |
| 156 | + var ix; |
| 157 | + var iy; |
| 158 | + var iz; |
| 159 | + var i0; |
| 160 | + var i1; |
| 161 | + var i2; |
| 162 | + var j0; |
| 163 | + var j1; |
| 164 | + var j2; |
| 165 | + var o; |
| 166 | + |
| 167 | + // Note on variable naming convention: s#, dx#, dy#, dz#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop... |
| 168 | + |
| 169 | + // Resolve the loop interchange order: |
| 170 | + o = loopOrder( x.shape, x.strides, y.strides, z.strides ); |
| 171 | + sh = o.sh; |
| 172 | + sx = o.sx; |
| 173 | + sy = o.sy; |
| 174 | + sz = o.sz; |
| 175 | + |
| 176 | + // Determine the block size: |
| 177 | + bsize = blockSize( x.dtype, y.dtype, z.dtype ); |
| 178 | + |
| 179 | + // Cache the indices of the first indexed elements in the respective ndarrays... |
| 180 | + ox = x.offset; |
| 181 | + oy = y.offset; |
| 182 | + oz = z.offset; |
| 183 | + |
| 184 | + // Cache references to the input and output ndarray buffers... |
| 185 | + xbuf = x.data; |
| 186 | + ybuf = y.data; |
| 187 | + zbuf = z.data; |
| 188 | + |
| 189 | + // Cache offset increments for the innermost loop... |
| 190 | + dx0 = sx[0]; |
| 191 | + dy0 = sy[0]; |
| 192 | + dz0 = sz[0]; |
| 193 | + |
| 194 | + // Cache accessors: |
| 195 | + xget = x.accessors[0]; |
| 196 | + yget = y.accessors[0]; |
| 197 | + zset = z.accessors[1]; |
| 198 | + |
| 199 | + // Iterate over blocks... |
| 200 | + for ( j2 = sh[2]; j2 > 0; ) { |
| 201 | + if ( j2 < bsize ) { |
| 202 | + s2 = j2; |
| 203 | + j2 = 0; |
| 204 | + } else { |
| 205 | + s2 = bsize; |
| 206 | + j2 -= bsize; |
| 207 | + } |
| 208 | + ox2 = ox + ( j2*sx[2] ); |
| 209 | + oy2 = oy + ( j2*sy[2] ); |
| 210 | + oz2 = oz + ( j2*sz[2] ); |
| 211 | + for ( j1 = sh[1]; j1 > 0; ) { |
| 212 | + if ( j1 < bsize ) { |
| 213 | + s1 = j1; |
| 214 | + j1 = 0; |
| 215 | + } else { |
| 216 | + s1 = bsize; |
| 217 | + j1 -= bsize; |
| 218 | + } |
| 219 | + dx2 = sx[2] - ( s1*sx[1] ); |
| 220 | + dy2 = sy[2] - ( s1*sy[1] ); |
| 221 | + dz2 = sz[2] - ( s1*sz[1] ); |
| 222 | + ox1 = ox2 + ( j1*sx[1] ); |
| 223 | + oy1 = oy2 + ( j1*sy[1] ); |
| 224 | + oz1 = oz2 + ( j1*sz[1] ); |
| 225 | + for ( j0 = sh[0]; j0 > 0; ) { |
| 226 | + if ( j0 < bsize ) { |
| 227 | + s0 = j0; |
| 228 | + j0 = 0; |
| 229 | + } else { |
| 230 | + s0 = bsize; |
| 231 | + j0 -= bsize; |
| 232 | + } |
| 233 | + // Compute index offsets for the first input and output ndarray elements in the current block... |
| 234 | + ix = ox1 + ( j0*sx[0] ); |
| 235 | + iy = oy1 + ( j0*sy[0] ); |
| 236 | + iz = oz1 + ( j0*sz[0] ); |
| 237 | + |
| 238 | + // Compute loop offset increments... |
| 239 | + dx1 = sx[1] - ( s0*sx[0] ); |
| 240 | + dy1 = sy[1] - ( s0*sy[0] ); |
| 241 | + dz1 = sz[1] - ( s0*sz[0] ); |
| 242 | + |
| 243 | + // Iterate over the ndarray dimensions... |
| 244 | + for ( i2 = 0; i2 < s2; i2++ ) { |
| 245 | + for ( i1 = 0; i1 < s1; i1++ ) { |
| 246 | + for ( i0 = 0; i0 < s0; i0++ ) { |
| 247 | + zset( zbuf, iz, fcn( xget( xbuf, ix ), yget( ybuf, iy ) ) ); |
| 248 | + ix += dx0; |
| 249 | + iy += dy0; |
| 250 | + iz += dz0; |
| 251 | + } |
| 252 | + ix += dx1; |
| 253 | + iy += dy1; |
| 254 | + iz += dz1; |
| 255 | + } |
| 256 | + ix += dx2; |
| 257 | + iy += dy2; |
| 258 | + iz += dz2; |
| 259 | + } |
| 260 | + } |
| 261 | + } |
| 262 | + } |
| 263 | +} |
| 264 | + |
| 265 | + |
| 266 | +// EXPORTS // |
| 267 | + |
| 268 | +module.exports = blockedbinary3d; |
0 commit comments