|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2023 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# binaryLoopOrder |
| 22 | + |
| 23 | +> Reorder ndarray dimensions and associated strides for loop interchange. |
| 24 | +
|
| 25 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 26 | + |
| 27 | +<section class="intro"> |
| 28 | + |
| 29 | +</section> |
| 30 | + |
| 31 | +<!-- /.intro --> |
| 32 | + |
| 33 | +<!-- Package usage documentation. --> |
| 34 | + |
| 35 | +<section class="usage"> |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +```javascript |
| 40 | +var binaryLoopOrder = require( '@stdlib/ndarray/base/binary-loop-interchange-order' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### binaryLoopOrder( shape, stridesX, stridesY, stridesZ ) |
| 44 | + |
| 45 | +Reorders [ndarray][@stdlib/ndarray/ctor] dimensions and associated strides for [loop interchange][loop-interchange]. |
| 46 | + |
| 47 | +```javascript |
| 48 | +// Define an array shape: |
| 49 | +var shape = [ 2, 2 ]; |
| 50 | + |
| 51 | +// Define the strides for the input arrays: |
| 52 | +var stridesX = [ 2, 1 ]; // row-major |
| 53 | +var stridesY = [ 4, 2 ]; // row-major |
| 54 | + |
| 55 | +// Define the strides for the output array: |
| 56 | +var stridesZ = [ 1, 2 ]; // column-major |
| 57 | + |
| 58 | +// Resolve the loop interchange order: |
| 59 | +var o = binaryLoopOrder( shape, stridesX, stridesY, stridesZ ); |
| 60 | +// returns {...} |
| 61 | +``` |
| 62 | + |
| 63 | +The function returns an object having the following properties: |
| 64 | + |
| 65 | +- **sh**: ordered dimensions. |
| 66 | +- **sx**: first input array strides sorted in loop order. |
| 67 | +- **sy**: second input array strides sorted in loop order. |
| 68 | +- **sz**: output array strides sorted in loop order. |
| 69 | + |
| 70 | +For all returned arrays, the first element corresponds to the innermost loop, and the last element corresponds to the outermost loop. |
| 71 | + |
| 72 | +</section> |
| 73 | + |
| 74 | +<!-- /.usage --> |
| 75 | + |
| 76 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 77 | + |
| 78 | +<section class="notes"> |
| 79 | + |
| 80 | +## Notes |
| 81 | + |
| 82 | +- When iterating over the elements of a multi-dimensional array, accessing elements which are closer in memory can improve performance. To this end, [loop interchange][loop-interchange] is a technique used in [loop nest optimization][loop-nest-optimization] to improve locality of reference and take advantage of CPU cache. |
| 83 | + |
| 84 | + The purpose of this function is to order [ndarray][@stdlib/ndarray/ctor] dimensions according to the magnitude of array strides. By using the ordered dimensions and associated strides, one can construct nested loops (one for each dimension) such that the innermost loop iterates over the dimension in which array elements are closest in memory and the outermost loop iterates over the dimension in which array elements are farthest apart in memory. As a consequence, element iteration is optimized to minimize cache misses and ensure locality of reference. |
| 85 | + |
| 86 | +- Cache performance may be degraded if the layout order (i.e., row-major or column-major) differs for the input and output [ndarrays][@stdlib/ndarray/ctor]. This function is intended to optimize cache performance for the most common layout order. Accordingly, if the output [ndarray][@stdlib/ndarray/ctor] has a different layout order (e.g., if the input [ndarrays][@stdlib/ndarray/ctor] are row-major and the output [ndarray][@stdlib/ndarray/ctor] is column-major), cache misses are likely for the output [ndarray][@stdlib/ndarray/ctor]. In general, to ensure best performance, input and output [ndarrays][@stdlib/ndarray/ctor] should have the same layout order. |
| 87 | + |
| 88 | +- The function assumes that the input and output [ndarrays][@stdlib/ndarray/ctor] have the same shape. Hence, loop interchange order should only be determined **after** broadcasting. |
| 89 | + |
| 90 | +</section> |
| 91 | + |
| 92 | +<!-- /.notes --> |
| 93 | + |
| 94 | +<!-- Package usage examples. --> |
| 95 | + |
| 96 | +<section class="examples"> |
| 97 | + |
| 98 | +## Examples |
| 99 | + |
| 100 | +<!-- eslint no-undef: "error" --> |
| 101 | + |
| 102 | +```javascript |
| 103 | +var array = require( '@stdlib/ndarray/array' ); |
| 104 | +var loopOrder = require( '@stdlib/ndarray/base/binary-loop-interchange-order' ); |
| 105 | + |
| 106 | +// Create ndarrays: |
| 107 | +var x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); |
| 108 | +var y = array( [ [ 5, 6 ], [ 7, 8 ] ] ); |
| 109 | +var z = array( [ [ 0, 0 ], [ 0, 0 ] ] ); |
| 110 | + |
| 111 | +// Resolve loop interchange data: |
| 112 | +var o = loopOrder( x.shape, x.strides, y.strides, z.strides ); |
| 113 | +// returns {...} |
| 114 | + |
| 115 | +console.log( o ); |
| 116 | +``` |
| 117 | + |
| 118 | +</section> |
| 119 | + |
| 120 | +<!-- /.examples --> |
| 121 | + |
| 122 | +<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 123 | + |
| 124 | +<section class="references"> |
| 125 | + |
| 126 | +</section> |
| 127 | + |
| 128 | +<!-- /.references --> |
| 129 | + |
| 130 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 131 | + |
| 132 | +<section class="related"> |
| 133 | + |
| 134 | +</section> |
| 135 | + |
| 136 | +<!-- /.related --> |
| 137 | + |
| 138 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 139 | + |
| 140 | +<section class="links"> |
| 141 | + |
| 142 | +[loop-interchange]: https://en.wikipedia.org/wiki/Loop_interchange |
| 143 | + |
| 144 | +[loop-nest-optimization]: https://en.wikipedia.org/wiki/Loop_nest_optimization |
| 145 | + |
| 146 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib |
| 147 | + |
| 148 | +</section> |
| 149 | + |
| 150 | +<!-- /.links --> |
0 commit comments