time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

5d_blocked_accessors.js (6605B)


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