time-to-botec

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

3d_blocked.js (4924B)


      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 three-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 = [ 3, 1, 2 ];
     63 *
     64 * // Define the array strides:
     65 * var sx = [ 4, 4, 1 ];
     66 * var sy = [ 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 * blockedunary3d( 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 blockedunary3d( x, y, fcn ) {
     97 	var bsize;
     98 	var xbuf;
     99 	var ybuf;
    100 	var dx0;
    101 	var dx1;
    102 	var dx2;
    103 	var dy0;
    104 	var dy1;
    105 	var dy2;
    106 	var ox1;
    107 	var ox2;
    108 	var oy1;
    109 	var oy2;
    110 	var sh;
    111 	var s0;
    112 	var s1;
    113 	var s2;
    114 	var sx;
    115 	var sy;
    116 	var ox;
    117 	var oy;
    118 	var ix;
    119 	var iy;
    120 	var i0;
    121 	var i1;
    122 	var i2;
    123 	var j0;
    124 	var j1;
    125 	var j2;
    126 	var o;
    127 
    128 	// Note on variable naming convention: s#, dx#, dy#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop...
    129 
    130 	// Initialize and unpack block data:
    131 	o = init( x, y );
    132 	sh = o.sh;
    133 	sx = o.sx;
    134 	sy = o.sy;
    135 	bsize = o.bsize;
    136 
    137 	// Cache the indices of the first indexed elements in the respective ndarrays...
    138 	ox = x.offset;
    139 	oy = y.offset;
    140 
    141 	// Cache references to the input and output ndarray buffers...
    142 	xbuf = x.data;
    143 	ybuf = y.data;
    144 
    145 	// Cache offset increments for the innermost loop...
    146 	dx0 = sx[0];
    147 	dy0 = sy[0];
    148 
    149 	// Iterate over blocks...
    150 	for ( j2 = sh[2]; j2 > 0; ) {
    151 		if ( j2 < bsize ) {
    152 			s2 = j2;
    153 			j2 = 0;
    154 		} else {
    155 			s2 = bsize;
    156 			j2 -= bsize;
    157 		}
    158 		ox2 = ox + ( j2*sx[2] );
    159 		oy2 = oy + ( j2*sy[2] );
    160 		for ( j1 = sh[1]; j1 > 0; ) {
    161 			if ( j1 < bsize ) {
    162 				s1 = j1;
    163 				j1 = 0;
    164 			} else {
    165 				s1 = bsize;
    166 				j1 -= bsize;
    167 			}
    168 			dx2 = sx[2] - ( s1*sx[1] );
    169 			dy2 = sy[2] - ( s1*sy[1] );
    170 			ox1 = ox2 + ( j1*sx[1] );
    171 			oy1 = oy2 + ( j1*sy[1] );
    172 			for ( j0 = sh[0]; j0 > 0; ) {
    173 				if ( j0 < bsize ) {
    174 					s0 = j0;
    175 					j0 = 0;
    176 				} else {
    177 					s0 = bsize;
    178 					j0 -= bsize;
    179 				}
    180 				// Compute index offsets for the first input and output ndarray elements in the current block...
    181 				ix = ox1 + ( j0*sx[0] );
    182 				iy = oy1 + ( j0*sy[0] );
    183 
    184 				// Compute loop offset increments...
    185 				dx1 = sx[1] - ( s0*sx[0] );
    186 				dy1 = sy[1] - ( s0*sy[0] );
    187 
    188 				// Iterate over the ndarray dimensions...
    189 				for ( i2 = 0; i2 < s2; i2++ ) {
    190 					for ( i1 = 0; i1 < s1; i1++ ) {
    191 						for ( i0 = 0; i0 < s0; i0++ ) {
    192 							ybuf[ iy ] = fcn( xbuf[ ix ] );
    193 							ix += dx0;
    194 							iy += dy0;
    195 						}
    196 						ix += dx1;
    197 						iy += dy1;
    198 					}
    199 					ix += dx2;
    200 					iy += dy2;
    201 				}
    202 			}
    203 		}
    204 	}
    205 }
    206 
    207 
    208 // EXPORTS //
    209 
    210 module.exports = blockedunary3d;