4d_blocked.js (5387B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2021 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 init = require( './init.js' ); 26 27 28 // MAIN // 29 30 /** 31 * Applies a unary callback to elements in a four-dimensional input ndarray and assigns results to elements in an equivalently shaped output ndarray via loop blocking. 32 * 33 * @private 34 * @param {Object} x - object containing input ndarray meta data 35 * @param {string} x.dtype - data type 36 * @param {Collection} x.data - data buffer 37 * @param {NonNegativeIntegerArray} x.shape - dimensions 38 * @param {IntegerArray} x.strides - stride lengths 39 * @param {NonNegativeInteger} x.offset - index offset 40 * @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style) 41 * @param {Object} y - object containing output ndarray meta data 42 * @param {string} y.dtype - data type 43 * @param {Collection} y.data - data buffer 44 * @param {NonNegativeIntegerArray} y.shape - dimensions 45 * @param {IntegerArray} y.strides - stride lengths 46 * @param {NonNegativeInteger} y.offset - index offset 47 * @param {string} y.order - specifies whether `y` is row-major (C-style) or column-major (Fortran-style) 48 * @param {Callback} fcn - unary callback 49 * 50 * @example 51 * var Float64Array = require( '@stdlib/array/float64' ); 52 * 53 * function scale( x ) { 54 * return x * 10.0; 55 * } 56 * 57 * // Create data buffers: 58 * 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 ] ); 59 * var ybuf = new Float64Array( 6 ); 60 * 61 * // Define the shape of the input and output arrays: 62 * var shape = [ 1, 3, 1, 2 ]; 63 * 64 * // Define the array strides: 65 * var sx = [ 12, 4, 4, 1 ]; 66 * var sy = [ 6, 2, 2, 1 ]; 67 * 68 * // Define the index offsets: 69 * var ox = 1; 70 * var oy = 0; 71 * 72 * // Create the input and output ndarray-like objects: 73 * var x = { 74 * 'dtype': 'float64', 75 * 'data': xbuf, 76 * 'shape': shape, 77 * 'strides': sx, 78 * 'offset': ox, 79 * 'order': 'row-major' 80 * }; 81 * var y = { 82 * 'dtype': 'float64', 83 * 'data': ybuf, 84 * 'shape': shape, 85 * 'strides': sy, 86 * 'offset': oy, 87 * 'order': 'row-major' 88 * }; 89 * 90 * // Apply the unary function: 91 * blockedunary4d( x, y, scale ); 92 * 93 * console.log( y.data ); 94 * // => <Float64Array>[ 20.0, 30.0, 60.0, 70.0, 100.0, 110.0 ] 95 */ 96 function blockedunary4d( x, y, fcn ) { 97 var bsize; 98 var xbuf; 99 var ybuf; 100 var dx0; 101 var dx1; 102 var dx2; 103 var dx3; 104 var dy0; 105 var dy1; 106 var dy2; 107 var dy3; 108 var ox1; 109 var ox2; 110 var ox3; 111 var oy1; 112 var oy2; 113 var oy3; 114 var sh; 115 var s0; 116 var s1; 117 var s2; 118 var s3; 119 var sx; 120 var sy; 121 var ox; 122 var oy; 123 var ix; 124 var iy; 125 var i0; 126 var i1; 127 var i2; 128 var i3; 129 var j0; 130 var j1; 131 var j2; 132 var j3; 133 var o; 134 135 // Note on variable naming convention: s#, dx#, dy#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop... 136 137 // Initialize and unpack block data: 138 o = init( x, y ); 139 sh = o.sh; 140 sx = o.sx; 141 sy = o.sy; 142 bsize = o.bsize; 143 144 // Cache the indices of the first indexed elements in the respective ndarrays... 145 ox = x.offset; 146 oy = y.offset; 147 148 // Cache references to the input and output ndarray buffers... 149 xbuf = x.data; 150 ybuf = y.data; 151 152 // Cache offset increments for the innermost loop... 153 dx0 = sx[0]; 154 dy0 = sy[0]; 155 156 // Iterate over blocks... 157 for ( j3 = sh[3]; j3 > 0; ) { 158 if ( j3 < bsize ) { 159 s3 = j3; 160 j3 = 0; 161 } else { 162 s3 = bsize; 163 j3 -= bsize; 164 } 165 ox3 = ox + ( j3*sx[3] ); 166 oy3 = oy + ( j3*sy[3] ); 167 for ( j2 = sh[2]; j2 > 0; ) { 168 if ( j2 < bsize ) { 169 s2 = j2; 170 j2 = 0; 171 } else { 172 s2 = bsize; 173 j2 -= bsize; 174 } 175 dx3 = sx[3] - ( s2*sx[2] ); 176 dy3 = sy[3] - ( s2*sy[2] ); 177 ox2 = ox3 + ( j2*sx[2] ); 178 oy2 = oy3 + ( j2*sy[2] ); 179 for ( j1 = sh[1]; j1 > 0; ) { 180 if ( j1 < bsize ) { 181 s1 = j1; 182 j1 = 0; 183 } else { 184 s1 = bsize; 185 j1 -= bsize; 186 } 187 dx2 = sx[2] - ( s1*sx[1] ); 188 dy2 = sy[2] - ( s1*sy[1] ); 189 ox1 = ox2 + ( j1*sx[1] ); 190 oy1 = oy2 + ( j1*sy[1] ); 191 for ( j0 = sh[0]; j0 > 0; ) { 192 if ( j0 < bsize ) { 193 s0 = j0; 194 j0 = 0; 195 } else { 196 s0 = bsize; 197 j0 -= bsize; 198 } 199 // Compute index offsets for the first input and output ndarray elements in the current block... 200 ix = ox1 + ( j0*sx[0] ); 201 iy = oy1 + ( j0*sy[0] ); 202 203 // Compute loop offset increments... 204 dx1 = sx[1] - ( s0*sx[0] ); 205 dy1 = sy[1] - ( s0*sy[0] ); 206 207 // Iterate over the ndarray dimensions... 208 for ( i3 = 0; i3 < s3; i3++ ) { 209 for ( i2 = 0; i2 < s2; i2++ ) { 210 for ( i1 = 0; i1 < s1; i1++ ) { 211 for ( i0 = 0; i0 < s0; i0++ ) { 212 ybuf[ iy ] = fcn( xbuf[ ix ] ); 213 ix += dx0; 214 iy += dy0; 215 } 216 ix += dx1; 217 iy += dy1; 218 } 219 ix += dx2; 220 iy += dy2; 221 } 222 ix += dx3; 223 iy += dy3; 224 } 225 } 226 } 227 } 228 } 229 } 230 231 232 // EXPORTS // 233 234 module.exports = blockedunary4d;