init.js (3092B)
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 bytesPerElement = require( './../../../base/bytes-per-element' ); 24 var range = require( './range.js' ); 25 var sort2ins = require( './sort2ins.js' ); 26 var copy = require( './copy_array.js' ); 27 var permute = require( './permute.js' ); 28 var defaults = require( './defaults.js' ); 29 30 31 // MAIN // 32 33 /** 34 * Initialize block data (shape, strides, block size). 35 * 36 * ## Notes 37 * 38 * - The returned object has the following properties: 39 * 40 * - **sh**: dimensions sorted in loop order. 41 * - **sx**: input ndarray strides sorted in loop order. 42 * - **sy**: output ndarray strides sorted in loop order. 43 * - **bsize**: block size (in units of elements). 44 * 45 * @private 46 * @param {Object} x - object containing input ndarray meta data 47 * @param {string} x.dtype - data type 48 * @param {Collection} x.data - data buffer 49 * @param {NonNegativeIntegerArray} x.shape - dimensions 50 * @param {IntegerArray} x.strides - stride lengths 51 * @param {NonNegativeInteger} x.offset - index offset 52 * @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style) 53 * @param {Object} y - object containing output ndarray meta data 54 * @param {string} y.dtype - data type 55 * @param {Collection} y.data - data buffer 56 * @param {NonNegativeIntegerArray} y.shape - dimensions 57 * @param {IntegerArray} y.strides - stride lengths 58 * @param {NonNegativeInteger} y.offset - index offset 59 * @param {string} y.order - specifies whether `y` is row-major (C-style) or column-major (Fortran-style) 60 * @returns {Object} block data 61 */ 62 function init( x, y ) { 63 var bsize; 64 var idx; 65 var nbx; 66 var nby; 67 var sh; 68 var sx; 69 var sy; 70 71 // Initialize a loop interchange index array for generating a loop order permutation: 72 sh = x.shape; 73 idx = range( sh.length ); 74 75 // Sort the input array strides in increasing order (of magnitude): 76 sx = copy( x.strides ); 77 sort2ins( sx, idx ); 78 79 // Permute the shape and output array strides based on the sorted input array strides: 80 sh = permute( sh, idx ); 81 sy = permute( y.strides, idx ); 82 83 // Determine the block size... 84 nbx = bytesPerElement( x.dtype ); 85 nby = bytesPerElement( y.dtype ); 86 if ( nbx === null || nby === null ) { 87 bsize = defaults.BLOCK_SIZE_IN_ELEMENTS; 88 } else if ( nbx > nby ) { 89 bsize = ( defaults.BLOCK_SIZE_IN_BYTES/nbx )|0; 90 } else { 91 bsize = ( defaults.BLOCK_SIZE_IN_BYTES/nby )|0; 92 } 93 return { 94 'sh': sh, 95 'sx': sx, 96 'sy': sy, 97 'bsize': bsize 98 }; 99 } 100 101 102 // EXPORTS // 103 104 module.exports = init;