3d_blocked_accessors.js (5586B)
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 three-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 {Function} x.getter - callback for accessing `x` data buffer elements 42 * @param {Object} y - object containing output ndarray meta data 43 * @param {string} y.dtype - data type 44 * @param {Collection} y.data - data buffer 45 * @param {NonNegativeIntegerArray} y.shape - dimensions 46 * @param {IntegerArray} y.strides - stride lengths 47 * @param {NonNegativeInteger} y.offset - index offset 48 * @param {string} y.order - specifies whether `y` is row-major (C-style) or column-major (Fortran-style) 49 * @param {Function} y.setter - callback for setting `y` data buffer elements 50 * @param {Callback} fcn - unary callback 51 * 52 * @example 53 * var Complex64Array = require( '@stdlib/array/complex64' ); 54 * var Complex64 = require( '@stdlib/complex/float32' ); 55 * var real = require( '@stdlib/complex/real' ); 56 * var imag = require( '@stdlib/complex/imag' ); 57 * 58 * function scale( z ) { 59 * return new Complex64( real(z)*10.0, imag(z)*10.0 ); 60 * } 61 * 62 * // Create data buffers: 63 * var xbuf = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); 64 * var ybuf = new Complex64Array( 4 ); 65 * 66 * // Define the shape of the input and output arrays: 67 * var shape = [ 1, 2, 2 ]; 68 * 69 * // Define the array strides: 70 * var sx = [ 2, 2, 1 ]; 71 * var sy = [ 2, 2, 1 ]; 72 * 73 * // Define the index offsets: 74 * var ox = 0; 75 * var oy = 0; 76 * 77 * // Define getters and setters: 78 * function getter( buf, idx ) { 79 * return buf.get( idx ); 80 * } 81 * 82 * function setter( buf, idx, value ) { 83 * buf.set( value, idx ); 84 * } 85 * 86 * // Create the input and output ndarray-like objects: 87 * var x = { 88 * 'dtype': 'complex64', 89 * 'data': xbuf, 90 * 'shape': shape, 91 * 'strides': sx, 92 * 'offset': ox, 93 * 'order': 'row-major', 94 * 'getter': getter 95 * }; 96 * var y = { 97 * 'dtype': 'complex64', 98 * 'data': ybuf, 99 * 'shape': shape, 100 * 'strides': sy, 101 * 'offset': oy, 102 * 'order': 'row-major', 103 * 'setter': setter 104 * }; 105 * 106 * // Apply the unary function: 107 * blockedunary3d( x, y, scale ); 108 * 109 * var v = y.data.get( 0 ); 110 * 111 * var re = real( v ); 112 * // returns 10.0 113 * 114 * var im = imag( v ); 115 * // returns 20.0 116 */ 117 function blockedunary3d( x, y, fcn ) { 118 var bsize; 119 var xbuf; 120 var ybuf; 121 var get; 122 var set; 123 var dx0; 124 var dx1; 125 var dx2; 126 var dy0; 127 var dy1; 128 var dy2; 129 var ox1; 130 var ox2; 131 var oy1; 132 var oy2; 133 var sh; 134 var s0; 135 var s1; 136 var s2; 137 var sx; 138 var sy; 139 var ox; 140 var oy; 141 var ix; 142 var iy; 143 var i0; 144 var i1; 145 var i2; 146 var j0; 147 var j1; 148 var j2; 149 var o; 150 151 // Note on variable naming convention: s#, dx#, dy#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop... 152 153 // Initialize and unpack block data: 154 o = init( x, y ); 155 sh = o.sh; 156 sx = o.sx; 157 sy = o.sy; 158 bsize = o.bsize; 159 160 // Cache the indices of the first indexed elements in the respective ndarrays... 161 ox = x.offset; 162 oy = y.offset; 163 164 // Cache references to the input and output ndarray buffers... 165 xbuf = x.data; 166 ybuf = y.data; 167 168 // Cache offset increments for the innermost loop... 169 dx0 = sx[0]; 170 dy0 = sy[0]; 171 172 // Cache accessors: 173 get = x.getter; 174 set = y.setter; 175 176 // Iterate over blocks... 177 for ( j2 = sh[2]; j2 > 0; ) { 178 if ( j2 < bsize ) { 179 s2 = j2; 180 j2 = 0; 181 } else { 182 s2 = bsize; 183 j2 -= bsize; 184 } 185 ox2 = ox + ( j2*sx[2] ); 186 oy2 = oy + ( j2*sy[2] ); 187 for ( j1 = sh[1]; j1 > 0; ) { 188 if ( j1 < bsize ) { 189 s1 = j1; 190 j1 = 0; 191 } else { 192 s1 = bsize; 193 j1 -= bsize; 194 } 195 dx2 = sx[2] - ( s1*sx[1] ); 196 dy2 = sy[2] - ( s1*sy[1] ); 197 ox1 = ox2 + ( j1*sx[1] ); 198 oy1 = oy2 + ( j1*sy[1] ); 199 for ( j0 = sh[0]; j0 > 0; ) { 200 if ( j0 < bsize ) { 201 s0 = j0; 202 j0 = 0; 203 } else { 204 s0 = bsize; 205 j0 -= bsize; 206 } 207 // Compute index offsets for the first input and output ndarray elements in the current block... 208 ix = ox1 + ( j0*sx[0] ); 209 iy = oy1 + ( j0*sy[0] ); 210 211 // Compute loop offset increments... 212 dx1 = sx[1] - ( s0*sx[0] ); 213 dy1 = sy[1] - ( s0*sy[0] ); 214 215 // Iterate over the ndarray dimensions... 216 for ( i2 = 0; i2 < s2; i2++ ) { 217 for ( i1 = 0; i1 < s1; i1++ ) { 218 for ( i0 = 0; i0 < s0; i0++ ) { 219 set( ybuf, iy, fcn( get( xbuf, ix ) ) ); 220 ix += dx0; 221 iy += dy0; 222 } 223 ix += dx1; 224 iy += dy1; 225 } 226 ix += dx2; 227 iy += dy2; 228 } 229 } 230 } 231 } 232 } 233 234 235 // EXPORTS // 236 237 module.exports = blockedunary3d;