6d_blocked_accessors.js (7095B)
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, max-len */ 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 six-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, 1, 1, 1, 2, 2 ]; 68 * 69 * // Define the array strides: 70 * var sx = [ 2, 2, 2, 2, 2, 1 ]; 71 * var sy = [ 2, 2, 2, 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 * blockedunary6d( 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 blockedunary6d( x, y, fcn ) { // eslint-disable-line max-statements 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 dx3; 127 var dx4; 128 var dx5; 129 var dy0; 130 var dy1; 131 var dy2; 132 var dy3; 133 var dy4; 134 var dy5; 135 var ox1; 136 var ox2; 137 var ox3; 138 var ox4; 139 var ox5; 140 var oy1; 141 var oy2; 142 var oy3; 143 var oy4; 144 var oy5; 145 var sh; 146 var s0; 147 var s1; 148 var s2; 149 var s3; 150 var s4; 151 var s5; 152 var sx; 153 var sy; 154 var ox; 155 var oy; 156 var ix; 157 var iy; 158 var i0; 159 var i1; 160 var i2; 161 var i3; 162 var i4; 163 var i5; 164 var j0; 165 var j1; 166 var j2; 167 var j3; 168 var j4; 169 var j5; 170 var o; 171 172 // Note on variable naming convention: s#, dx#, dy#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop... 173 174 // Initialize and unpack block data: 175 o = init( x, y ); 176 sh = o.sh; 177 sx = o.sx; 178 sy = o.sy; 179 bsize = o.bsize; 180 181 // Cache the indices of the first indexed elements in the respective ndarrays... 182 ox = x.offset; 183 oy = y.offset; 184 185 // Cache references to the input and output ndarray buffers... 186 xbuf = x.data; 187 ybuf = y.data; 188 189 // Cache offset increments for the innermost loop... 190 dx0 = sx[0]; 191 dy0 = sy[0]; 192 193 // Cache accessors: 194 get = x.getter; 195 set = y.setter; 196 197 // Iterate over blocks... 198 for ( j5 = sh[5]; j5 > 0; ) { 199 if ( j5 < bsize ) { 200 s5 = j5; 201 j5 = 0; 202 } else { 203 s5 = bsize; 204 j5 -= bsize; 205 } 206 ox5 = ox + ( j5*sx[5] ); 207 oy5 = oy + ( j5*sy[5] ); 208 for ( j4 = sh[4]; j4 > 0; ) { 209 if ( j4 < bsize ) { 210 s4 = j4; 211 j4 = 0; 212 } else { 213 s4 = bsize; 214 j4 -= bsize; 215 } 216 dx5 = sx[5] - ( s4*sx[4] ); 217 dy5 = sy[5] - ( s4*sy[4] ); 218 ox4 = ox5 + ( j4*sx[4] ); 219 oy4 = oy5 + ( j4*sy[4] ); 220 for ( j3 = sh[3]; j3 > 0; ) { 221 if ( j3 < bsize ) { 222 s3 = j3; 223 j3 = 0; 224 } else { 225 s3 = bsize; 226 j3 -= bsize; 227 } 228 dx4 = sx[4] - ( s3*sx[3] ); 229 dy4 = sy[4] - ( s3*sy[3] ); 230 ox3 = ox4 + ( j3*sx[3] ); 231 oy3 = oy4 + ( j3*sy[3] ); 232 for ( j2 = sh[2]; j2 > 0; ) { 233 if ( j2 < bsize ) { 234 s2 = j2; 235 j2 = 0; 236 } else { 237 s2 = bsize; 238 j2 -= bsize; 239 } 240 dx3 = sx[3] - ( s2*sx[2] ); 241 dy3 = sy[3] - ( s2*sy[2] ); 242 ox2 = ox3 + ( j2*sx[2] ); 243 oy2 = oy3 + ( j2*sy[2] ); 244 for ( j1 = sh[1]; j1 > 0; ) { 245 if ( j1 < bsize ) { 246 s1 = j1; 247 j1 = 0; 248 } else { 249 s1 = bsize; 250 j1 -= bsize; 251 } 252 dx2 = sx[2] - ( s1*sx[1] ); 253 dy2 = sy[2] - ( s1*sy[1] ); 254 ox1 = ox2 + ( j1*sx[1] ); 255 oy1 = oy2 + ( j1*sy[1] ); 256 for ( j0 = sh[0]; j0 > 0; ) { 257 if ( j0 < bsize ) { 258 s0 = j0; 259 j0 = 0; 260 } else { 261 s0 = bsize; 262 j0 -= bsize; 263 } 264 // Compute index offsets for the first input and output ndarray elements in the current block... 265 ix = ox1 + ( j0*sx[0] ); 266 iy = oy1 + ( j0*sy[0] ); 267 268 // Compute loop offset increments... 269 dx1 = sx[1] - ( s0*sx[0] ); 270 dy1 = sy[1] - ( s0*sy[0] ); 271 272 // Iterate over the ndarray dimensions... 273 for ( i5 = 0; i5 < s5; i5++ ) { 274 for ( i4 = 0; i4 < s4; i4++ ) { 275 for ( i3 = 0; i3 < s3; i3++ ) { 276 for ( i2 = 0; i2 < s2; i2++ ) { 277 for ( i1 = 0; i1 < s1; i1++ ) { 278 for ( i0 = 0; i0 < s0; i0++ ) { 279 set( ybuf, iy, fcn( get( xbuf, ix ) ) ); 280 ix += dx0; 281 iy += dy0; 282 } 283 ix += dx1; 284 iy += dy1; 285 } 286 ix += dx2; 287 iy += dy2; 288 } 289 ix += dx3; 290 iy += dy3; 291 } 292 ix += dx4; 293 iy += dy4; 294 } 295 ix += dx5; 296 iy += dy5; 297 } 298 } 299 } 300 } 301 } 302 } 303 } 304 } 305 306 307 // EXPORTS // 308 309 module.exports = blockedunary6d;