Skip to content

Commit 41986f6

Browse files
committed
Add initial PoC for strided array multiple dispatch
1 parent e60b9ca commit 41986f6

File tree

6 files changed

+683
-0
lines changed

6 files changed

+683
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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+
/* eslint-disable array-element-newline */
20+
21+
'use strict';
22+
23+
var Float64Array = require( '@stdlib/array/float64' );
24+
var Int32Array = require( '@stdlib/array/int32' );
25+
var dispatch = require( './../lib' );
26+
27+
function add2( arrays, shape, strides ) {
28+
var sx;
29+
var sy;
30+
var sz;
31+
var ix;
32+
var iy;
33+
var iz;
34+
var N;
35+
var x;
36+
var y;
37+
var z;
38+
var i;
39+
40+
N = shape[ 0 ];
41+
if ( N <= 0 ) {
42+
return;
43+
}
44+
sx = strides[ 0 ];
45+
if ( sx < 0 ) {
46+
ix = (1-N) * sx;
47+
} else {
48+
ix = 0;
49+
}
50+
sy = strides[ 1 ];
51+
if ( sy < 0 ) {
52+
iy = (1-N) * sy;
53+
} else {
54+
iy = 0;
55+
}
56+
sz = strides[ 2 ];
57+
if ( sz < 0 ) {
58+
iz = (1-N) * sz;
59+
} else {
60+
iz = 0;
61+
}
62+
x = arrays[ 0 ];
63+
y = arrays[ 1 ];
64+
z = arrays[ 2 ];
65+
for ( i = 0; i < N; i++ ) {
66+
z[ iz ] = x[ ix ] + y[ iy ];
67+
ix += sx;
68+
iy += sy;
69+
iz += sz;
70+
}
71+
}
72+
73+
var fcns = [
74+
add2,
75+
add2,
76+
add2,
77+
add2,
78+
add2,
79+
add2,
80+
add2,
81+
add2,
82+
add2
83+
];
84+
85+
var types = [
86+
'float64', 'float64', 'float64',
87+
'float32', 'float32', 'float32',
88+
'int32', 'int32', 'int32',
89+
'uint32', 'uint32', 'uint32',
90+
'int16', 'int16', 'int16',
91+
'uint16', 'uint16', 'uint16',
92+
'int8', 'int8', 'int8',
93+
'uint8', 'uint8', 'uint8',
94+
'uint8c', 'uint8c', 'uint8c'
95+
];
96+
97+
var add = dispatch( fcns, types, null, 7, 2, 1 );
98+
99+
var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
100+
var y = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
101+
var z = new Float64Array( x.length );
102+
103+
add( x.length, x, 1, y, 1, z, 1 );
104+
console.log( z );
105+
106+
x = new Int32Array( [ 10, 20, 30, 40, 50 ] );
107+
y = new Int32Array( [ 10, 20, 30, 40, 50 ] );
108+
z = new Int32Array( x.length );
109+
110+
add( x.length, x, 1, y, 1, z, 1 );
111+
console.log( z );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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+
/* eslint-disable array-element-newline */
20+
21+
'use strict';
22+
23+
var Float64Array = require( '@stdlib/array/float64' );
24+
var Float32Array = require( '@stdlib/array/float32' );
25+
var Int8Array = require( '@stdlib/array/int8' );
26+
var sin = require( '@stdlib/math/base/special/sin' );
27+
var dispatch = require( './../lib' );
28+
29+
function apply( arrays, shape, strides, fcn ) {
30+
var sx;
31+
var sy;
32+
var ix;
33+
var iy;
34+
var N;
35+
var x;
36+
var y;
37+
var i;
38+
39+
N = shape[ 0 ];
40+
if ( N <= 0 ) {
41+
return;
42+
}
43+
sx = strides[ 0 ];
44+
if ( sx < 0 ) {
45+
ix = (1-N) * sx;
46+
} else {
47+
ix = 0;
48+
}
49+
sy = strides[ 1 ];
50+
if ( sy < 0 ) {
51+
iy = (1-N) * sy;
52+
} else {
53+
iy = 0;
54+
}
55+
x = arrays[ 0 ];
56+
y = arrays[ 1 ];
57+
for ( i = 0; i < N; i++ ) {
58+
y[ iy ] = fcn( x[ ix ] );
59+
ix += sx;
60+
iy += sy;
61+
}
62+
}
63+
64+
var fcns = [
65+
apply,
66+
apply,
67+
apply,
68+
apply,
69+
apply,
70+
apply,
71+
apply,
72+
apply,
73+
apply,
74+
apply,
75+
apply,
76+
apply,
77+
apply,
78+
apply,
79+
apply
80+
];
81+
82+
var types = [
83+
'float64', 'float64',
84+
'float32', 'float64',
85+
'float32', 'float32',
86+
'int32', 'float64',
87+
'uint32', 'float64',
88+
'int16', 'float64',
89+
'int16', 'float32',
90+
'uint16', 'float64',
91+
'uint16', 'float32',
92+
'int8', 'float64',
93+
'int8', 'float32',
94+
'uint8', 'float64',
95+
'uint8', 'float32',
96+
'uint8c', 'float64',
97+
'uint8c', 'float32'
98+
];
99+
100+
var data = [
101+
sin,
102+
sin,
103+
sin,
104+
sin,
105+
sin,
106+
sin,
107+
sin,
108+
sin,
109+
sin,
110+
sin,
111+
sin,
112+
sin,
113+
sin,
114+
sin,
115+
sin
116+
];
117+
118+
var sine = dispatch( fcns, types, data, 5, 1, 1 );
119+
120+
var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
121+
var y = new Float64Array( x.length );
122+
123+
sine( x.length, x, 1, y, 1 );
124+
console.log( y );
125+
126+
x = new Int8Array( [ 1, 2, 3, 4, 5 ] );
127+
y = new Float32Array( x.length );
128+
129+
sine( x.length, x, 1, y, 1 );
130+
console.log( y );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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+
/**
22+
* Create a strided array function interface which performs multiple dispatch.
23+
*
24+
* @module @stdlib/strided/dispatch
25+
*
26+
* @example
27+
* var dispatch = require( '@stdlib/strided/dispatch' );
28+
*
29+
* TODO
30+
*/
31+
32+
// MODULES //
33+
34+
var dispatch = require( './main.js' );
35+
36+
37+
// EXPORTS //
38+
39+
module.exports = dispatch;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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+
/**
22+
* Returns the first row index at which a given one-dimensional array of types can be found in a two-dimensional reference array of types (or `-1` if not found).
23+
*
24+
* ## Notes
25+
*
26+
* - The intended use case for this function is for type dispatch (i.e., given a set of array data types, find a matching interface according the interface's accepted array data types).
27+
* - The function assumes that `x` is stored in row-major order.
28+
* - The function assumes that the number of indexed elements in `y` equals the number of columns in `x`.
29+
* - The function returns a row index. To convert to a linear index, multiply `strideX1` by the return value.
30+
*
31+
* @param {NonNegativeInteger} N - number of rows in `x` (size of first dimension)
32+
* @param {NonNegativeInteger} M - number of columns in `x` (size of second dimension)
33+
* @param {ArrayLikeObject} x - input two-dimensional reference array
34+
* @param {integer} strideX1 - `x` stride length along first dimension
35+
* @param {integer} strideX2 - `x` stride length along second dimension
36+
* @param {NonNegativeInteger} offsetX - `x` starting index
37+
* @param {ArrayLikeObject} y - search array
38+
* @param {integer} strideY - `y` stride length
39+
* @param {NonNegativeInteger} offsetY - `y` starting index
40+
* @returns {integer} row index (if found) and `-1` otherwise
41+
*
42+
* @example
43+
* // Define a reference array to search:
44+
* var types = [
45+
* 'float64', 'float64', 'float64',
46+
* 'float32', 'float32', 'float32',
47+
* 'uint32', 'uint32', 'float64',
48+
* 'int32', 'int32', 'float64',
49+
* 'uint16', 'uint16', 'float64',
50+
* 'int16', 'int16', 'float64',
51+
* 'uint8', 'uint8', 'float64',
52+
* 'int8', 'int8', 'float64'
53+
* ];
54+
*
55+
* // Define reference array dimensions:
56+
* var N = 8; // rows
57+
* var M = 3; // columns
58+
*
59+
* // Define a search array:
60+
* y1 = [
61+
* 'float32', 'float32', 'float32',
62+
* ];
63+
*
64+
* // Find the list of types:
65+
* var r1 = indexOfTypes( N, M, types, M, 1, 0, y1, 1, 0 );
66+
* // returns 1
67+
*
68+
// Define a search array:
69+
* y2 = [
70+
* 'float32', 'float32', 'float64',
71+
* ];
72+
*
73+
* // Find the list of types:
74+
* var r2 = indexOfTypes( N, M, types, M, 1, 0, y2, 1, 0 );
75+
* // returns -1
76+
*/
77+
function indexOfTypes( N, M, x, strideX1, strideX2, offsetX, y, strideY, offsetY ) { // eslint-disable-line max-len
78+
var ix;
79+
var iy;
80+
var i;
81+
var j;
82+
83+
// Search for the first row which matches `y`...
84+
ix = offsetX;
85+
for ( i = 0; i < N; i++ ) {
86+
iy = offsetY;
87+
for ( j = 0; j < M; j++ ) {
88+
if ( x[ ix+(j*strideX2) ] !== y[ iy ] ) {
89+
break;
90+
}
91+
iy += strideY;
92+
}
93+
// If we successfully iterated over all columns, then that means we've found a match...
94+
if ( j === M ) {
95+
return i;
96+
}
97+
ix += strideX1;
98+
}
99+
return -1;
100+
}
101+
102+
103+
// EXPORTS //
104+
105+
module.exports = indexOfTypes;

0 commit comments

Comments
 (0)