Skip to content

Commit

Permalink
Showing 31 changed files with 1,492 additions and 14 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/productionize.yml
Original file line number Diff line number Diff line change
@@ -100,10 +100,11 @@ jobs:
# Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency:
- name: 'Update dependencies in package.json'
run: |
PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version)
if grep -q '"@stdlib/string-format"' package.json; then
sed -i "s/\"@stdlib\/string-format\"/\"@stdlib\/error-tools-fmtprodmsg\"/g" package.json
sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json
else
node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^0.0.x'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );"
node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );"
fi
# Configure git:
60 changes: 57 additions & 3 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -46,7 +46,7 @@ jobs:
publish:

# Define display name:
name: 'Publish to npm'
name: 'Publish package to npm'

# Define the type of virtual host machine on which to run the job:
runs-on: ubuntu-latest
@@ -90,17 +90,65 @@ jobs:
NEW_VERSION=$(node -p "require('./package.json').version")
# Replace branch in README.md link definitions for badges with the new version:
find . -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/branch[=:][^ ]+/branch=v${NEW_VERSION}/g"
find . -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/branch([=:])[^ ]+/branch\1v${NEW_VERSION}/g"
# Create a new commit and tag:
git add package.json README.md
git commit -m "Release v${NEW_VERSION}"
git tag -a "v${NEW_VERSION}" -m "Release v{NEW_VERSION}"
git tag -a "v${NEW_VERSION}" -m "Release v${NEW_VERSION}"
# Push changes to GitHub:
SLUG=${{ github.repository }}
git push "https://$GITHUB_ACTOR:[email protected]/$SLUG.git" --follow-tags
# Remove CLI:
- name: 'Remove CLI'
if: ${{ github.ref == 'refs/heads/main' }}
run: |
# Exit if the package does not have a CLI:
if ! grep -q '"bin":' package.json; then
exit 0
fi
rm -rf ./bin/cli
rm test/test.cli.js
rm etc/cli_opts.json
rm docs/usage.txt
# For all dependencies, check in all *.js files if they are still used; if not, remove them:
jq -r '.dependencies | keys[]' ./package.json | while read -r dep; do
dep=$(echo "$dep" | xargs)
if ! grep -q "$dep" lib/** && ! grep -q -s "$dep" manifest.json && ! grep -q -s "$dep" include.gypi; then
jq --indent 2 "del(.dependencies[\"$dep\"])" ./package.json > ./package.json.tmp
mv ./package.json.tmp ./package.json
fi
done
jq -r '.devDependencies | keys[]' ./package.json | while read -r dep; do
if [[ "$dep" != "@stdlib"* ]]; then
continue
fi
dep=$(echo "$dep" | xargs)
if ! grep -q "$dep" lib/** && ! grep -q -s "$dep" manifest.json && ! grep -q -s "$dep" include.gypi; then
jq --indent 2 "del(.devDependencies[\"$dep\"])" ./package.json > ./package.json.tmp
mv ./package.json.tmp ./package.json
fi
done
# Remove CLI section:
find . -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?<section class=\"cli\">[\s\S]+?<\!\-\- \/.cli \-\->//"
# Remove CLI from package.json:
jq -r 'del(.bin)' package.json > package.json.tmp
mv package.json.tmp package.json
# Add entry for CLI package to See Also section of README.md:
cliPkgName=$(jq -r '.name' package.json)-cli
escapedPkg=$(echo "$cliPkgName" | sed -e 's/\//\\\//g')
escapedPkg=$(echo "$escapedPkg" | sed -e 's/\@/\\\@/g')
find . -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/<section class=\"related\">(?:\n\n\* \* \*\n\n## See Also\n\n)?/<section class=\"related\">\n\n## See Also\n\n- <span class=\"package-name\">[\`$escapedPkg\`][$escapedPkg]<\/span><span class=\"delimiter\">: <\/span><span class=\"description\">CLI package for use as a command-line utility.<\/span>\n/"
# Add link definition for CLI package to README.md:
find . -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/<section class=\"links\">/<section class=\"links\">\n\n[$escapedPkg]: https:\/\/www.npmjs.com\/package\/$escapedPkg/"
# Replace GitHub links to individual packages with npm links:
- name: 'Replace all GitHub links to individual packages with npm links'
run: |
@@ -111,6 +159,12 @@ jobs:
run: |
find . -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`\n\nAlternatively,[^<]+<\/section>/\`\`\`\n\n<\/section>/"
# Remove unnecessary files:
- name: 'Remove unnecessary files'
run: |
rm docs/repl.txt
rm docs/types/test.ts
# Replace all stdlib GitHub dependencies with the respective npm packages:
- name: 'Replace all stdlib GitHub dependencies with the respective npm packages'
run: |
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -32,6 +32,11 @@ on:
# Run workflow on each push to the main branch:
push:

# Run workflow upon completion of `publish` workflow run:
workflow_run:
workflows: ["publish"]
types: [completed]

# Workflow jobs:
jobs:

4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -182,3 +182,7 @@ jsconfig.json
################
*.sublime-workspace
*.sublime-project

# Other editor files #
######################
.idea/
2 changes: 2 additions & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
@@ -9,9 +9,11 @@ Bruno Fenzl <[email protected]>
Christopher Dambamuromo <[email protected]>
Dominik Moritz <[email protected]>
Frank Kovacs <[email protected]>
Harshita Kalani <[email protected]>
James <[email protected]>
Jithin KS <[email protected]>
Joey Reed <[email protected]>
Jordan-Gallivan <[email protected]>
Joris Labie <[email protected]>
Justin Dennison <[email protected]>
Marcus <[email protected]>
18 changes: 18 additions & 0 deletions base/lib/index.js
Original file line number Diff line number Diff line change
@@ -328,6 +328,24 @@ setReadOnly( ns, 'ndarraylike2object', require( './../../base/ndarraylike2object
*/
setReadOnly( ns, 'nonsingletonDimensions', require( './../../base/nonsingleton-dimensions' ) );

/**
* @name nullaryLoopOrder
* @memberof ns
* @readonly
* @type {Function}
* @see {@link module:@stdlib/ndarray/base/nullary-loop-interchange-order}
*/
setReadOnly( ns, 'nullaryLoopOrder', require( './../../base/nullary-loop-interchange-order' ) );

/**
* @name nullaryBlockSize
* @memberof ns
* @readonly
* @type {Function}
* @see {@link module:@stdlib/ndarray/base/nullary-tiling-block-size}
*/
setReadOnly( ns, 'nullaryBlockSize', require( './../../base/nullary-tiling-block-size' ) );

/**
* @name numel
* @memberof ns
138 changes: 138 additions & 0 deletions base/nullary-loop-interchange-order/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<!--
@license Apache-2.0
Copyright (c) 2023 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.
-->

# nullaryLoopOrder

> 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 nullaryLoopOrder = require( '@stdlib/ndarray/base/nullary-loop-interchange-order' );
```

#### nullaryLoopOrder( shape, stridesX )

Reorders [ndarray][@stdlib/ndarray/ctor] dimensions and associated strides for [loop interchange][loop-interchange].

```javascript
// Define an array shape:
var shape = [ 2, 2 ];

// Define array strides:
var stridesX = [ 2, 1 ]; // row-major

// Resolve the loop interchange order:
var o = nullaryLoopOrder( shape, stridesX );
// returns {...}
```

The function returns an object having the following properties:

- **sh**: ordered dimensions.
- **sx**: 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 minimized cache misses and ensure locality of reference.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var array = require( '@stdlib/ndarray/array' );
var loopOrder = require( '@stdlib/ndarray/base/nullary-loop-interchange-order' );

// Create an ndarray:
var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );

// Resolve loop interchange data:
var o = loopOrder( x.shape, x.strides );
// 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/ndarray/tree/main/ctor

</section>

<!-- /.links -->
81 changes: 81 additions & 0 deletions base/nullary-loop-interchange-order/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 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 randu = require( '@stdlib/random/base/randu' );
var isArray = require( '@stdlib/assert/is-array' );
var shape2strides = require( './../../../base/shape2strides' );
var pkg = require( './../package.json' ).name;
var loopOrder = require( './../lib' );


// MAIN //

bench( pkg+'::row-major', function benchmark( b ) {
var strides;
var shape;
var out;
var i;

shape = [ 10, 10, 10 ];
strides = shape2strides( shape, 'row-major' );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
strides[ i%shape.length ] *= ( randu() < 0.5 ) ? -1 : 1;
out = loopOrder( shape, strides );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isArray( out.sh ) || !isArray( out.sx ) ) {
b.fail( 'should return an array' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::column-major', function benchmark( b ) {
var strides;
var shape;
var out;
var i;

shape = [ 10, 10, 10 ];
strides = shape2strides( shape, 'column-major' );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
strides[ i%shape.length ] *= ( randu() < 0.5 ) ? -1 : 1;
out = loopOrder( shape, strides );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isArray( out.sh ) || !isArray( out.sx ) ) {
b.fail( 'should return an array' );
}
b.pass( 'benchmark finished' );
b.end();
});
Loading

0 comments on commit b89062e

Please sign in to comment.