time-to-botec

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

4d_blocked_accessors.js (6086B)


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