time-to-botec

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

2d_blocked_accessors.js (5114B)


      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 {Function} x.getter - callback for accessing `x` data buffer elements
     40 * @param {Object} y - object containing output ndarray meta data
     41 * @param {string} y.dtype - data type
     42 * @param {Collection} y.data - data buffer
     43 * @param {NonNegativeIntegerArray} y.shape - dimensions
     44 * @param {IntegerArray} y.strides - stride lengths
     45 * @param {NonNegativeInteger} y.offset - index offset
     46 * @param {string} y.order - specifies whether `y` is row-major (C-style) or column-major (Fortran-style)
     47 * @param {Function} y.setter - callback for setting `y` data buffer elements
     48 * @param {Callback} fcn - unary callback
     49 *
     50 * @example
     51 * var Complex64Array = require( '@stdlib/array/complex64' );
     52 * var Complex64 = require( '@stdlib/complex/float32' );
     53 * var real = require( '@stdlib/complex/real' );
     54 * var imag = require( '@stdlib/complex/imag' );
     55 *
     56 * function scale( z ) {
     57 *     return new Complex64( real(z)*10.0, imag(z)*10.0 );
     58 * }
     59 *
     60 * // Create data buffers:
     61 * var xbuf = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
     62 * var ybuf = new Complex64Array( 4 );
     63 *
     64 * // Define the shape of the input and output arrays:
     65 * var shape = [ 2, 2 ];
     66 *
     67 * // Define the array strides:
     68 * var sx = [ 2, 1 ];
     69 * var sy = [ 2, 1 ];
     70 *
     71 * // Define the index offsets:
     72 * var ox = 0;
     73 * var oy = 0;
     74 *
     75 * // Define getters and setters:
     76 * function getter( buf, idx ) {
     77 *     return buf.get( idx );
     78 * }
     79 *
     80 * function setter( buf, idx, value ) {
     81 *     buf.set( value, idx );
     82 * }
     83 *
     84 * // Create the input and output ndarray-like objects:
     85 * var x = {
     86 *     'dtype': 'complex64',
     87 *     'data': xbuf,
     88 *     'shape': shape,
     89 *     'strides': sx,
     90 *     'offset': ox,
     91 *     'order': 'row-major',
     92 *     'getter': getter
     93 * };
     94 * var y = {
     95 *     'dtype': 'complex64',
     96 *     'data': ybuf,
     97 *     'shape': shape,
     98 *     'strides': sy,
     99 *     'offset': oy,
    100 *     'order': 'row-major',
    101 *     'setter': setter
    102 * };
    103 *
    104 * // Apply the unary function:
    105 * blockedunary2d( x, y, scale );
    106 *
    107 * var v = y.data.get( 0 );
    108 *
    109 * var re = real( v );
    110 * // returns 10.0
    111 *
    112 * var im = imag( v );
    113 * // returns 20.0
    114 */
    115 function blockedunary2d( x, y, fcn ) {
    116 	var bsize;
    117 	var xbuf;
    118 	var ybuf;
    119 	var get;
    120 	var set;
    121 	var dx0;
    122 	var dx1;
    123 	var dy0;
    124 	var dy1;
    125 	var ox1;
    126 	var oy1;
    127 	var sh;
    128 	var s0;
    129 	var s1;
    130 	var sx;
    131 	var sy;
    132 	var ox;
    133 	var oy;
    134 	var ix;
    135 	var iy;
    136 	var i0;
    137 	var i1;
    138 	var j0;
    139 	var j1;
    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 	// Cache accessors:
    164 	get = x.getter;
    165 	set = y.setter;
    166 
    167 	// Iterate over blocks...
    168 	for ( j1 = sh[1]; j1 > 0; ) {
    169 		if ( j1 < bsize ) {
    170 			s1 = j1;
    171 			j1 = 0;
    172 		} else {
    173 			s1 = bsize;
    174 			j1 -= bsize;
    175 		}
    176 		ox1 = ox + ( j1*sx[1] );
    177 		oy1 = oy + ( j1*sy[1] );
    178 		for ( j0 = sh[0]; j0 > 0; ) {
    179 			if ( j0 < bsize ) {
    180 				s0 = j0;
    181 				j0 = 0;
    182 			} else {
    183 				s0 = bsize;
    184 				j0 -= bsize;
    185 			}
    186 			// Compute index offsets for the first input and output ndarray elements in the current block...
    187 			ix = ox1 + ( j0*sx[0] );
    188 			iy = oy1 + ( j0*sy[0] );
    189 
    190 			// Compute loop offset increments...
    191 			dx1 = sx[1] - ( s0*sx[0] );
    192 			dy1 = sy[1] - ( s0*sy[0] );
    193 
    194 			// Iterate over the ndarray dimensions...
    195 			for ( i1 = 0; i1 < s1; i1++ ) {
    196 				for ( i0 = 0; i0 < s0; i0++ ) {
    197 					set( ybuf, iy, fcn( get( xbuf, ix ) ) );
    198 					ix += dx0;
    199 					iy += dy0;
    200 				}
    201 				ix += dx1;
    202 				iy += dy1;
    203 			}
    204 		}
    205 	}
    206 }
    207 
    208 
    209 // EXPORTS //
    210 
    211 module.exports = blockedunary2d;