time-to-botec

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

2d_blocked.js (4415B)


      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 'use strict';
     20 
     21 // MODULES //
     22 
     23 var init = require( './init.js' );
     24 
     25 
     26 // MAIN //
     27 
     28 /**
     29 * Applies a unary callback to elements in a two-dimensional input ndarray and assigns results to elements in an equivalently shaped output ndarray via loop blocking.
     30 *
     31 * @private
     32 * @param {Object} x - object containing input ndarray meta data
     33 * @param {string} x.dtype - data type
     34 * @param {Collection} x.data - data buffer
     35 * @param {NonNegativeIntegerArray} x.shape - dimensions
     36 * @param {IntegerArray} x.strides - stride lengths
     37 * @param {NonNegativeInteger} x.offset - index offset
     38 * @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style)
     39 * @param {Object} y - object containing output ndarray meta data
     40 * @param {string} y.dtype - data type
     41 * @param {Collection} y.data - data buffer
     42 * @param {NonNegativeIntegerArray} y.shape - dimensions
     43 * @param {IntegerArray} y.strides - stride lengths
     44 * @param {NonNegativeInteger} y.offset - index offset
     45 * @param {string} y.order - specifies whether `y` is row-major (C-style) or column-major (Fortran-style)
     46 * @param {Callback} fcn - unary callback
     47 *
     48 * @example
     49 * var Float64Array = require( '@stdlib/array/float64' );
     50 *
     51 * function scale( x ) {
     52 *     return x * 10.0;
     53 * }
     54 *
     55 * // Create data buffers:
     56 * var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
     57 * var ybuf = new Float64Array( 4 );
     58 *
     59 * // Define the shape of the input and output arrays:
     60 * var shape = [ 2, 2 ];
     61 *
     62 * // Define the array strides:
     63 * var sx = [ 4, 1 ];
     64 * var sy = [ 2, 1 ];
     65 *
     66 * // Define the index offsets:
     67 * var ox = 1;
     68 * var oy = 0;
     69 *
     70 * // Create the input and output ndarray-like objects:
     71 * var x = {
     72 *     'dtype': 'float64',
     73 *     'data': xbuf,
     74 *     'shape': shape,
     75 *     'strides': sx,
     76 *     'offset': ox,
     77 *     'order': 'row-major'
     78 * };
     79 * var y = {
     80 *     'dtype': 'float64',
     81 *     'data': ybuf,
     82 *     'shape': shape,
     83 *     'strides': sy,
     84 *     'offset': oy,
     85 *     'order': 'row-major'
     86 * };
     87 *
     88 * // Apply the unary function:
     89 * blockedunary2d( x, y, scale );
     90 *
     91 * console.log( y.data );
     92 * // => <Float64Array>[ 20.0, 30.0, 60.0, 70.0 ]
     93 */
     94 function blockedunary2d( x, y, fcn ) {
     95 	var bsize;
     96 	var xbuf;
     97 	var ybuf;
     98 	var dx0;
     99 	var dx1;
    100 	var dy0;
    101 	var dy1;
    102 	var ox1;
    103 	var oy1;
    104 	var sh;
    105 	var s0;
    106 	var s1;
    107 	var sx;
    108 	var sy;
    109 	var ox;
    110 	var oy;
    111 	var ix;
    112 	var iy;
    113 	var i0;
    114 	var i1;
    115 	var j0;
    116 	var j1;
    117 	var o;
    118 
    119 	// Note on variable naming convention: s#, dx#, dy#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop...
    120 
    121 	// Initialize and unpack block data:
    122 	o = init( x, y );
    123 	sh = o.sh;
    124 	sx = o.sx;
    125 	sy = o.sy;
    126 	bsize = o.bsize;
    127 
    128 	// Cache the indices of the first indexed elements in the respective ndarrays...
    129 	ox = x.offset;
    130 	oy = y.offset;
    131 
    132 	// Cache references to the input and output ndarray buffers...
    133 	xbuf = x.data;
    134 	ybuf = y.data;
    135 
    136 	// Cache offset increments for the innermost loop...
    137 	dx0 = sx[0];
    138 	dy0 = sy[0];
    139 
    140 	// Iterate over blocks...
    141 	for ( j1 = sh[1]; j1 > 0; ) {
    142 		if ( j1 < bsize ) {
    143 			s1 = j1;
    144 			j1 = 0;
    145 		} else {
    146 			s1 = bsize;
    147 			j1 -= bsize;
    148 		}
    149 		ox1 = ox + ( j1*sx[1] );
    150 		oy1 = oy + ( j1*sy[1] );
    151 		for ( j0 = sh[0]; j0 > 0; ) {
    152 			if ( j0 < bsize ) {
    153 				s0 = j0;
    154 				j0 = 0;
    155 			} else {
    156 				s0 = bsize;
    157 				j0 -= bsize;
    158 			}
    159 			// Compute index offsets for the first input and output ndarray elements in the current block...
    160 			ix = ox1 + ( j0*sx[0] );
    161 			iy = oy1 + ( j0*sy[0] );
    162 
    163 			// Compute loop offset increments...
    164 			dx1 = sx[1] - ( s0*sx[0] );
    165 			dy1 = sy[1] - ( s0*sy[0] );
    166 
    167 			// Iterate over the ndarray dimensions...
    168 			for ( i1 = 0; i1 < s1; i1++ ) {
    169 				for ( i0 = 0; i0 < s0; i0++ ) {
    170 					ybuf[ iy ] = fcn( xbuf[ ix ] );
    171 					ix += dx0;
    172 					iy += dy0;
    173 				}
    174 				ix += dx1;
    175 				iy += dy1;
    176 			}
    177 		}
    178 	}
    179 }
    180 
    181 
    182 // EXPORTS //
    183 
    184 module.exports = blockedunary2d;