-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: add ndarray/base/ternary-loop-interchange-order
#9499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
157 changes: 157 additions & 0 deletions
157
lib/node_modules/@stdlib/ndarray/base/ternary-loop-interchange-order/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| <!-- | ||
|
|
||
| @license Apache-2.0 | ||
|
|
||
| Copyright (c) 2026 The Stdlib Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| --> | ||
|
|
||
| # ternaryLoopOrder | ||
|
|
||
| > Reorder ndarray dimensions and associated strides for loop interchange. | ||
|
|
||
| <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <!-- Package usage documentation. --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var ternaryLoopOrder = require( '@stdlib/ndarray/base/binary-loop-interchange-order' ); | ||
| ``` | ||
|
|
||
| #### ternaryLoopOrder( shape, stridesW, stridesX, stridesY, stridesZ ) | ||
|
|
||
| Reorders [ndarray][@stdlib/ndarray/ctor] dimensions and associated strides for [loop interchange][loop-interchange]. | ||
|
|
||
| ```javascript | ||
| // Define an array shape: | ||
| var shape = [ 2, 2 ]; | ||
|
|
||
| // Define the strides for the input arrays: | ||
| var stridesW = [ 2, 1 ]; // row-major | ||
| var stridesX = [ 4, 2 ]; // row-major | ||
| var stridesY = [ 1, 2 ]; // column-major | ||
|
|
||
| // Define the strides for the output array: | ||
| var stridesZ = [ 1, 2 ]; // column-major | ||
|
|
||
| // Resolve the loop interchange order: | ||
| var o = ternaryLoopOrder( shape, stridesW, stridesX, stridesY, stridesZ ); | ||
| // returns {...} | ||
| ``` | ||
|
|
||
| The function returns an object having the following properties: | ||
|
|
||
| - **sh**: ordered dimensions. | ||
| - **sw**: first input array strides sorted in loop order. | ||
| - **sx**: second input array strides sorted in loop order. | ||
| - **sy**: third input array strides sorted in loop order. | ||
| - **sz**: output array strides sorted in loop order. | ||
|
|
||
| For all returned arrays, the first element corresponds to the innermost loop, and the last element corresponds to the outermost loop. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| ## Notes | ||
|
|
||
| - 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. | ||
|
|
||
| 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. | ||
|
|
||
| - 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. | ||
|
|
||
| - 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. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <!-- Package usage examples. --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint-disable max-len --> | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var array = require( '@stdlib/ndarray/array' ); | ||
| var getShape = require( '@stdlib/ndarray/shape' ); | ||
| var getStrides = require( '@stdlib/ndarray/strides' ); | ||
| var ternaryLoopOrder = require( '@stdlib/ndarray/base/binary-loop-interchange-order' ); | ||
|
|
||
| // Create ndarrays: | ||
| var w = array( [ [ 1, 2 ], [ 3, 4 ] ] ); | ||
| var x = array( [ [ 5, 6 ], [ 7, 8 ] ] ); | ||
| var y = array( [ [ 9, 10 ], [ 11, 12 ] ] ); | ||
| var z = array( [ [ 0, 0 ], [ 0, 0 ] ] ); | ||
|
|
||
| // Resolve loop interchange data: | ||
| var o = ternaryLoopOrder( getShape( w ), getStrides( w ), getStrides( x ), getStrides( y ), getStrides( z ) ); | ||
| // returns {...} | ||
|
|
||
| console.log( o ); | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| <!-- 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. --> | ||
|
|
||
| <section class="references"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.references --> | ||
|
|
||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
|
||
| <section class="related"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.related --> | ||
|
|
||
| <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="links"> | ||
|
|
||
| [loop-interchange]: https://en.wikipedia.org/wiki/Loop_interchange | ||
|
|
||
| [loop-nest-optimization]: https://en.wikipedia.org/wiki/Loop_nest_optimization | ||
|
|
||
| [@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> |
90 changes: 90 additions & 0 deletions
90
lib/node_modules/@stdlib/ndarray/base/ternary-loop-interchange-order/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // MODULES // | ||
|
|
||
| var bench = require( '@stdlib/bench' ); | ||
| var isArray = require( '@stdlib/assert/is-array' ); | ||
| var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); | ||
| var pkg = require( './../package.json' ).name; | ||
| var ternaryLoopOrder = require( './../lib' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( pkg+'::row-major', function benchmark( b ) { | ||
| var strides; | ||
| var factors; | ||
| var shape; | ||
| var out; | ||
| var i; | ||
|
|
||
| shape = [ 10, 10, 10 ]; | ||
| strides = shape2strides( shape, 'row-major' ); | ||
| factors = [ | ||
| -1, | ||
| 1 | ||
| ]; | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| strides[ i%shape.length ] *= factors[ i%factors.length ]; | ||
| out = ternaryLoopOrder( shape, strides, strides, strides, strides ); | ||
| if ( typeof out !== 'object' ) { | ||
| b.fail( 'should return an object' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( !isArray( out.sh ) || !isArray( out.sx ) || !isArray( out.sy ) || !isArray( out.sz ) ) { // eslint-disable-line max-len | ||
| b.fail( 'should return an array' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); | ||
|
|
||
| bench( pkg+'::column-major', function benchmark( b ) { | ||
| var strides; | ||
| var factors; | ||
| var shape; | ||
| var out; | ||
| var i; | ||
|
|
||
| shape = [ 10, 10, 10 ]; | ||
| strides = shape2strides( shape, 'column-major' ); | ||
| factors = [ | ||
| -1, | ||
| 1 | ||
| ]; | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| strides[ i%shape.length ] *= factors[ i%factors.length ]; | ||
| out = ternaryLoopOrder( shape, strides, strides, strides, strides ); | ||
| if ( typeof out !== 'object' ) { | ||
| b.fail( 'should return an object' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( !isArray( out.sh ) || !isArray( out.sx ) || !isArray( out.sy ) || !isArray( out.sz ) ) { // eslint-disable-line max-len | ||
| b.fail( 'should return an array' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); | ||
67 changes: 67 additions & 0 deletions
67
lib/node_modules/@stdlib/ndarray/base/ternary-loop-interchange-order/docs/repl.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
|
|
||
| {{alias}}( shape, stridesW, stridesX, stridesY, stridesZ ) | ||
| Reorders ndarray dimensions and associated strides for loop interchange. | ||
|
|
||
| The function returns an object having the following properties: | ||
|
|
||
| - sh: ordered dimensions. | ||
| - sw: first input array strides sorted in loop order. | ||
| - sx: second input array strides sorted in loop order. | ||
| - sy: third input array strides sorted in loop order. | ||
| - sz: output array strides sorted in loop order. | ||
|
|
||
| For all returned arrays, the first element corresponds to the innermost | ||
| loop, and the last element corresponds to the outermost loop. | ||
|
|
||
| The function assumes that the input and output ndarrays have the same shape. | ||
| Hence, loop interchange order should only be determined after broadcasting. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| shape: ArrayLikeObject<integer> | ||
| Array dimensions. | ||
|
|
||
| stridesW: ArrayLikeObject<integer> | ||
| First input array strides. | ||
|
|
||
| stridesX: ArrayLikeObject<integer> | ||
| Second input array strides. | ||
|
|
||
| stridesY: ArrayLikeObject<integer> | ||
| Third input array strides. | ||
|
|
||
| stridesZ: ArrayLikeObject<integer> | ||
| Output array strides. | ||
|
|
||
| Returns | ||
| ------- | ||
| out: Object | ||
| Loop interchange data. | ||
|
|
||
| out.sh: Array<integer> | ||
| Ordered dimensions. | ||
|
|
||
| out.sw: Array<integer> | ||
| First input array strides sorted in loop order. | ||
|
|
||
| out.sx: Array<integer> | ||
| Second input array strides sorted in loop order. | ||
|
|
||
| out.sy: Array<integer> | ||
| Third input array strides sorted in loop order. | ||
|
|
||
| out.sz: Array<integer> | ||
| Output array strides sorted in loop order. | ||
|
|
||
| Examples | ||
| -------- | ||
| > var w = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] ); | ||
| > var x = {{alias:@stdlib/ndarray/array}}( [ [ 5, 6 ], [ 7, 8 ] ] ); | ||
| > var y = {{alias:@stdlib/ndarray/array}}( [ [ 9, 10 ], [ 11, 12 ] ] ); | ||
| > var z = {{alias:@stdlib/ndarray/array}}( [ [ 0, 0 ], [ 0, 0 ] ] ); | ||
| > var o = {{alias}}( w.shape, w.strides, x.strides, y.strides, z.strides ) | ||
| {...} | ||
|
|
||
| See Also | ||
| -------- | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.