Skip to content

Commit 1f79854

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/base/ternary-tiling-block-size
PR-URL: #9495 Closes: stdlib-js/metr-issue-tracker#149 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 8107a5a commit 1f79854

File tree

11 files changed

+662
-0
lines changed

11 files changed

+662
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
# ternaryBlockSize
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 ternaryBlockSize = require( '@stdlib/ndarray/base/ternary-tiling-block-size' );
41+
```
42+
43+
#### ternaryBlockSize( dtypeW, dtypeX, dtypeY, dtypeZ )
44+
45+
Resolves a loop block size according to provided ndarray [dtypes][@stdlib/ndarray/dtypes] for multi-dimensional array tiled loops applying a ternary function.
46+
47+
```javascript
48+
var bsize = ternaryBlockSize( 'float64', 'float64', 'float64', 'float64' );
49+
// returns <number>
50+
```
51+
52+
</section>
53+
54+
<!-- /.usage -->
55+
56+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
57+
58+
<section class="notes">
59+
60+
## Notes
61+
62+
- The returned loop tiling block size is in units of elements.
63+
64+
</section>
65+
66+
<!-- /.notes -->
67+
68+
<!-- Package usage examples. -->
69+
70+
<section class="examples">
71+
72+
## Examples
73+
74+
<!-- eslint no-undef: "error" -->
75+
76+
```javascript
77+
var dtypes = require( '@stdlib/ndarray/dtypes' );
78+
var cartesianPower = require( '@stdlib/array/base/cartesian-power' );
79+
var promotionRules = require( '@stdlib/ndarray/promotion-rules' );
80+
var ternaryBlockSize = require( '@stdlib/ndarray/base/ternary-tiling-block-size' );
81+
82+
// Generate a list of input ndarray dtype triplets:
83+
var dt = cartesianPower( dtypes(), 3 );
84+
85+
// Resolve the block size for each dtype triplet and its promoted dtype...
86+
var t;
87+
var b;
88+
var i;
89+
console.log( 'block_size, wdtype, xdtype, ydtype, zdtype' );
90+
for ( i = 0; i < dt.length; i++ ) {
91+
t = promotionRules.apply( null, dt[ i ] );
92+
dt[ i ].push( ( t === -1 ) ? 'generic' : t );
93+
b = ternaryBlockSize.apply( null, dt[ i ] );
94+
console.log( '%d, %s, %s, %s, %s', b, dt[i][0], dt[i][1], dt[i][2], dt[i][3] );
95+
}
96+
```
97+
98+
</section>
99+
100+
<!-- /.examples -->
101+
102+
<!-- 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. -->
103+
104+
<section class="references">
105+
106+
</section>
107+
108+
<!-- /.references -->
109+
110+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
111+
112+
<section class="related">
113+
114+
</section>
115+
116+
<!-- /.related -->
117+
118+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
119+
120+
<section class="links">
121+
122+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
123+
124+
</section>
125+
126+
<!-- /.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 dw;
33+
var dx;
34+
var dy;
35+
var dz;
36+
var s;
37+
var i;
38+
39+
dw = [
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+
dx = [
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+
dy = [
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+
dz = [
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( dw[ i%dw.length ], dx[ i%dx.length ], dy[ i%dy.length ], dz[ i%dz.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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
{{alias}}( dtypeW, dtypeX, dtypeY, dtypeZ )
3+
Returns a loop block size for multi-dimensional array tiled loops.
4+
5+
Parameters
6+
----------
7+
dtypeW: string|DataType
8+
First input array data type.
9+
10+
dtypeX: string|DataType
11+
Second input array data type.
12+
13+
dtypeY: string|DataType
14+
Third input array data type.
15+
16+
dtypeZ: string|DataType
17+
Output array data type.
18+
19+
Returns
20+
-------
21+
out: integer
22+
Block size.
23+
24+
Examples
25+
--------
26+
> var out = {{alias}}( 'float64', 'float64', 'float64', 'float64' )
27+
<number>
28+
> out = {{alias}}( 'float64', 'int32', 'float64', 'float64' )
29+
<number>
30+
31+
See Also
32+
--------
33+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 { DataType } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Returns a loop block size for multi-dimensional array tiled loops.
27+
*
28+
* @param dtypeW - first input array data type
29+
* @param dtypeX - second input array data type
30+
* @param dtypeY - third input array data type
31+
* @param dtypeZ - output array data type
32+
* @returns block size (in units of elements)
33+
*
34+
* @example
35+
* var bsize = ternaryBlockSize( 'float64', 'float64', 'float64', 'float64' );
36+
* // returns <number>
37+
*/
38+
declare function ternaryBlockSize( dtypeW: DataType, dtypeX: DataType, dtypeY: DataType, dtypeZ: DataType ): number;
39+
40+
41+
// EXPORTS //
42+
43+
export = ternaryBlockSize;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 ternaryBlockSize = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
ternaryBlockSize( 'float64', 'float64', 'float64', 'float64' ); // $ExpectType number
27+
ternaryBlockSize( 'float32', 'int8', 'uint16', 'complex128' ); // $ExpectType number
28+
ternaryBlockSize( 'generic', 'generic', 'generic', 'generic' ); // $ExpectType number
29+
}
30+
31+
// The compiler throws an error if the function is provided insufficient arguments...
32+
{
33+
ternaryBlockSize(); // $ExpectError
34+
ternaryBlockSize( 'float64' ); // $ExpectError
35+
ternaryBlockSize( 'float64', 'float64' ); // $ExpectError
36+
ternaryBlockSize( 'float64', 'float64', 'float64' ); // $ExpectError
37+
}
38+
39+
// The compiler throws an error if the function is provided too many arguments...
40+
{
41+
ternaryBlockSize( 'float64', 'float64', 'float64', 'float64', {} ); // $ExpectError
42+
}
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 promotionRules = require( '@stdlib/ndarray/promotion-rules' );
24+
var ternaryBlockSize = 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, wdtype, xdtype, ydtype, zdtype' );
34+
for ( i = 0; i < dt.length; i++ ) {
35+
t = promotionRules.apply( null, dt[ i ] );
36+
dt[ i ].push( ( t === -1 ) ? 'generic' : t );
37+
b = ternaryBlockSize.apply( null, 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)