time-to-botec

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

7d_blocked_accessors.js (7635B)


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