time-to-botec

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

5d_blocked.js (5914B)


      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 {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, 1, 3, 1, 2 ];
     63 *
     64 * // Define the array strides:
     65 * var sx = [ 12, 12, 4, 4, 1 ];
     66 * var sy = [ 6, 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 * blockedunary5d( 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 blockedunary5d( x, y, fcn ) { // eslint-disable-line max-statements
     97 	var bsize;
     98 	var xbuf;
     99 	var ybuf;
    100 	var dx0;
    101 	var dx1;
    102 	var dx2;
    103 	var dx3;
    104 	var dx4;
    105 	var dy0;
    106 	var dy1;
    107 	var dy2;
    108 	var dy3;
    109 	var dy4;
    110 	var ox1;
    111 	var ox2;
    112 	var ox3;
    113 	var ox4;
    114 	var oy1;
    115 	var oy2;
    116 	var oy3;
    117 	var oy4;
    118 	var sh;
    119 	var s0;
    120 	var s1;
    121 	var s2;
    122 	var s3;
    123 	var s4;
    124 	var sx;
    125 	var sy;
    126 	var ox;
    127 	var oy;
    128 	var ix;
    129 	var iy;
    130 	var i0;
    131 	var i1;
    132 	var i2;
    133 	var i3;
    134 	var i4;
    135 	var j0;
    136 	var j1;
    137 	var j2;
    138 	var j3;
    139 	var j4;
    140 	var o;
    141 
    142 	// Note on variable naming convention: s#, dx#, dy#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop...
    143 
    144 	// Initialize and unpack block data:
    145 	o = init( x, y );
    146 	sh = o.sh;
    147 	sx = o.sx;
    148 	sy = o.sy;
    149 	bsize = o.bsize;
    150 
    151 	// Cache the indices of the first indexed elements in the respective ndarrays...
    152 	ox = x.offset;
    153 	oy = y.offset;
    154 
    155 	// Cache references to the input and output ndarray buffers...
    156 	xbuf = x.data;
    157 	ybuf = y.data;
    158 
    159 	// Cache offset increments for the innermost loop...
    160 	dx0 = sx[0];
    161 	dy0 = sy[0];
    162 
    163 	// Iterate over blocks...
    164 	for ( j4 = sh[4]; j4 > 0; ) {
    165 		if ( j4 < bsize ) {
    166 			s4 = j4;
    167 			j4 = 0;
    168 		} else {
    169 			s4 = bsize;
    170 			j4 -= bsize;
    171 		}
    172 		ox4 = ox + ( j4*sx[4] );
    173 		oy4 = oy + ( j4*sy[4] );
    174 		for ( j3 = sh[3]; j3 > 0; ) {
    175 			if ( j3 < bsize ) {
    176 				s3 = j3;
    177 				j3 = 0;
    178 			} else {
    179 				s3 = bsize;
    180 				j3 -= bsize;
    181 			}
    182 			dx4 = sx[4] - ( s3*sx[3] );
    183 			dy4 = sy[4] - ( s3*sy[3] );
    184 			ox3 = ox4 + ( j3*sx[3] );
    185 			oy3 = oy4 + ( j3*sy[3] );
    186 			for ( j2 = sh[2]; j2 > 0; ) {
    187 				if ( j2 < bsize ) {
    188 					s2 = j2;
    189 					j2 = 0;
    190 				} else {
    191 					s2 = bsize;
    192 					j2 -= bsize;
    193 				}
    194 				dx3 = sx[3] - ( s2*sx[2] );
    195 				dy3 = sy[3] - ( s2*sy[2] );
    196 				ox2 = ox3 + ( j2*sx[2] );
    197 				oy2 = oy3 + ( j2*sy[2] );
    198 				for ( j1 = sh[1]; j1 > 0; ) {
    199 					if ( j1 < bsize ) {
    200 						s1 = j1;
    201 						j1 = 0;
    202 					} else {
    203 						s1 = bsize;
    204 						j1 -= bsize;
    205 					}
    206 					dx2 = sx[2] - ( s1*sx[1] );
    207 					dy2 = sy[2] - ( s1*sy[1] );
    208 					ox1 = ox2 + ( j1*sx[1] );
    209 					oy1 = oy2 + ( j1*sy[1] );
    210 					for ( j0 = sh[0]; j0 > 0; ) {
    211 						if ( j0 < bsize ) {
    212 							s0 = j0;
    213 							j0 = 0;
    214 						} else {
    215 							s0 = bsize;
    216 							j0 -= bsize;
    217 						}
    218 						// Compute index offsets for the first input and output ndarray elements in the current block...
    219 						ix = ox1 + ( j0*sx[0] );
    220 						iy = oy1 + ( j0*sy[0] );
    221 
    222 						// Compute loop offset increments...
    223 						dx1 = sx[1] - ( s0*sx[0] );
    224 						dy1 = sy[1] - ( s0*sy[0] );
    225 
    226 						// Iterate over the ndarray dimensions...
    227 						for ( i4 = 0; i4 < s4; i4++ ) {
    228 							for ( i3 = 0; i3 < s3; i3++ ) {
    229 								for ( i2 = 0; i2 < s2; i2++ ) {
    230 									for ( i1 = 0; i1 < s1; i1++ ) {
    231 										for ( i0 = 0; i0 < s0; i0++ ) {
    232 											ybuf[ iy ] = fcn( xbuf[ ix ] );
    233 											ix += dx0;
    234 											iy += dy0;
    235 										}
    236 										ix += dx1;
    237 										iy += dy1;
    238 									}
    239 									ix += dx2;
    240 									iy += dy2;
    241 								}
    242 								ix += dx3;
    243 								iy += dy3;
    244 							}
    245 							ix += dx4;
    246 							iy += dy4;
    247 						}
    248 					}
    249 				}
    250 			}
    251 		}
    252 	}
    253 }
    254 
    255 
    256 // EXPORTS //
    257 
    258 module.exports = blockedunary5d;