Skip to content

Commit 2d51ff6

Browse files
committed
feat: add ndarray/base/tiling-block-size
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 9c75b30 commit 2d51ff6

File tree

11 files changed

+785
-0
lines changed

11 files changed

+785
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# blockSize
22+
23+
> Resolve a loop block size for multi-dimensional array tiled loops.
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 blockSize = require( '@stdlib/ndarray/base/tiling-block-size' );
41+
```
42+
43+
#### blockSize( dtypes )
44+
45+
Resolves a loop block size for multi-dimensional array tiled loops according to provided ndarray [data types][@stdlib/ndarray/dtypes].
46+
47+
```javascript
48+
var bsize = blockSize( [ 'float64', 'float64', 'float64', 'float64' ] );
49+
// returns <number>
50+
```
51+
52+
The function supports the following arguments:
53+
54+
- **dtypes**: list of input and output ndarray [data types][@stdlib/ndarray/dtypes].
55+
56+
</section>
57+
58+
<!-- /.usage -->
59+
60+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
61+
62+
<section class="notes">
63+
64+
## Notes
65+
66+
- The returned loop tiling block size is in units of elements.
67+
68+
</section>
69+
70+
<!-- /.notes -->
71+
72+
<!-- Package usage examples. -->
73+
74+
<section class="examples">
75+
76+
## Examples
77+
78+
<!-- eslint no-undef: "error" -->
79+
80+
```javascript
81+
var dtypes = require( '@stdlib/ndarray/dtypes' );
82+
var cartesianPower = require( '@stdlib/array/base/cartesian-power' );
83+
var promoteDataTypes = require( '@stdlib/ndarray/base/promote-dtypes' );
84+
var blockSize = require( '@stdlib/ndarray/base/tiling-block-size' );
85+
86+
// Generate a list of input ndarray dtype triplets:
87+
var dt = cartesianPower( dtypes(), 3 );
88+
89+
// Resolve the block size for each dtype triplet and its promoted dtype...
90+
var t;
91+
var b;
92+
var i;
93+
console.log( 'block_size, xdtype, ydtype, zdtype, wdtype' );
94+
for ( i = 0; i < dt.length; i++ ) {
95+
t = promoteDataTypes( dt[ i ] );
96+
dt[ i ].push( ( t === null ) ? 'generic' : t );
97+
b = blockSize( dt[ i ] );
98+
console.log( '%d, %s, %s, %s, %s', b, dt[i][0], dt[i][1], dt[i][2], dt[i][3] );
99+
}
100+
```
101+
102+
</section>
103+
104+
<!-- /.examples -->
105+
106+
<!-- 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. -->
107+
108+
<section class="references">
109+
110+
</section>
111+
112+
<!-- /.references -->
113+
114+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
115+
116+
<section class="related">
117+
118+
</section>
119+
120+
<!-- /.related -->
121+
122+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
123+
124+
<section class="links">
125+
126+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
127+
128+
</section>
129+
130+
<!-- /.links -->
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 bench = require( '@stdlib/bench' );
24+
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var blockSize = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var dx;
33+
var dy;
34+
var dz;
35+
var dw;
36+
var s;
37+
var i;
38+
39+
dx = [
40+
'float64',
41+
'float32',
42+
'int8',
43+
'uint8',
44+
'uint8c',
45+
'int16',
46+
'uint16',
47+
'int32',
48+
'uint32',
49+
'binary',
50+
'generic',
51+
'foobar'
52+
];
53+
dy = [
54+
'float64',
55+
'float32',
56+
'int8',
57+
'uint8',
58+
'uint8c',
59+
'int16',
60+
'uint16',
61+
'int32',
62+
'uint32',
63+
'binary',
64+
'generic',
65+
'foobar'
66+
];
67+
dz = [
68+
'float64',
69+
'float32',
70+
'int8',
71+
'uint8',
72+
'uint8c',
73+
'int16',
74+
'uint16',
75+
'int32',
76+
'uint32',
77+
'binary',
78+
'generic',
79+
'foobar'
80+
];
81+
dw = [
82+
'float64',
83+
'generic',
84+
'int32',
85+
'int16',
86+
'int8'
87+
];
88+
89+
b.tic();
90+
for ( i = 0; i < b.iterations; i++ ) {
91+
s = blockSize( [ dx[ i%dx.length ], dy[ i%dy.length ], dz[ i%dz.length ], dw[ i%dw.length ] ] ); // eslint-disable-line max-len
92+
if ( typeof s !== 'number' ) {
93+
b.fail( 'should return a number' );
94+
}
95+
}
96+
b.toc();
97+
if ( !isPositiveInteger( s ) ) {
98+
b.fail( 'should return a positive integer' );
99+
}
100+
b.pass( 'benchmark finished' );
101+
b.end();
102+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
{{alias}}( dtypes )
3+
Returns a loop block size for multi-dimensional array tiled loops.
4+
5+
Parameters
6+
----------
7+
dtypes: Array<string|DataType>
8+
List of input and output ndarray data types.
9+
10+
Returns
11+
-------
12+
out: integer
13+
Block size.
14+
15+
Examples
16+
--------
17+
> var out = {{alias}}( [ 'float64', 'float64', 'float64', 'float64' ] )
18+
<number>
19+
> out = {{alias}}( [ 'float64', 'int32', 'float64', 'float64' ] )
20+
<number>
21+
22+
See Also
23+
--------
24+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { ArrayLike } from '@stdlib/types/array';
24+
import { DataType } from '@stdlib/types/ndarray';
25+
26+
/**
27+
* Returns a loop block size for multi-dimensional array tiled loops.
28+
*
29+
* @param dtypes - list of input and output ndarray data types
30+
* @returns block size (in units of elements)
31+
*
32+
* @example
33+
* var bsize = blockSize( [ 'float64', 'float64', 'float64', 'float64' ] );
34+
* // returns <number>
35+
*/
36+
declare function blockSize( dtypes: ArrayLike<DataType> ): number;
37+
38+
39+
// EXPORTS //
40+
41+
export = blockSize;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
import blockSize = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
blockSize( [ 'float64' ] ); // $ExpectType number
27+
blockSize( [ 'float32', 'int8' ] ); // $ExpectType number
28+
blockSize( [ 'generic', 'generic', 'generic' ] ); // $ExpectType number
29+
}
30+
31+
// The compiler throws an error if the function is provided an argument which is not an array-like object of data types...
32+
{
33+
blockSize( true ); // $ExpectError
34+
blockSize( false ); // $ExpectError
35+
blockSize( '5' ); // $ExpectError
36+
blockSize( 123 ); // $ExpectError
37+
blockSize( null ); // $ExpectError
38+
blockSize( void 0 ); // $ExpectError
39+
blockSize( {} ); // $ExpectError
40+
blockSize( ( x: number ): number => x ); // $ExpectError
41+
}
42+
43+
// The compiler throws an error if the function is provided an unsupported number of arguments...
44+
{
45+
blockSize(); // $ExpectError
46+
blockSize( [ 'float64' ], {} ); // $ExpectError
47+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
var dtypes = require( '@stdlib/ndarray/dtypes' );
22+
var cartesianPower = require( '@stdlib/array/base/cartesian-power' );
23+
var promoteDataTypes = require( '@stdlib/ndarray/base/promote-dtypes' );
24+
var blockSize = require( './../lib' );
25+
26+
// Generate a list of input ndarray dtype triplets:
27+
var dt = cartesianPower( dtypes(), 3 );
28+
29+
// Resolve the block size for each dtype triplet and its promoted dtype...
30+
var t;
31+
var b;
32+
var i;
33+
console.log( 'block_size, xdtype, ydtype, zdtype, wdtype' );
34+
for ( i = 0; i < dt.length; i++ ) {
35+
t = promoteDataTypes( dt[ i ] );
36+
dt[ i ].push( ( t === null ) ? 'generic' : t );
37+
b = blockSize( dt[ i ] );
38+
console.log( '%d, %s, %s, %s, %s', b, dt[i][0], dt[i][1], dt[i][2], dt[i][3] );
39+
}

0 commit comments

Comments
 (0)