Skip to content

Commit ddb70d6

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/dindex-of-row
PR-URL: #11162 Closes: stdlib-js/metr-issue-tracker#219 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent eec976a commit ddb70d6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+5157
-0
lines changed

lib/node_modules/@stdlib/blas/ext/base/dindex-of-row/README.md

Lines changed: 450 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var zeros = require( '@stdlib/array/zeros' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var floor = require( '@stdlib/math/base/special/floor' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var dindexOfRow = require( './../lib/dindex_of_row.js' );
31+
32+
33+
// VARIABLES //
34+
35+
var LAYOUTS = [
36+
'row-major',
37+
'column-major'
38+
];
39+
40+
41+
// FUNCTIONS //
42+
43+
/**
44+
* Creates a benchmark function.
45+
*
46+
* @private
47+
* @param {string} order - storage layout
48+
* @param {PositiveInteger} N - number of elements along each dimension
49+
* @returns {Function} benchmark function
50+
*/
51+
function createBenchmark( order, N ) {
52+
var workspace = zeros( N, 'uint8' );
53+
var A = zeros( N*N, 'float64' );
54+
var x = zeros( N, 'float64' );
55+
return benchmark;
56+
57+
/**
58+
* Benchmark function.
59+
*
60+
* @private
61+
* @param {Benchmark} b - benchmark instance
62+
*/
63+
function benchmark( b ) {
64+
var z;
65+
var i;
66+
67+
b.tic();
68+
for ( i = 0; i < b.iterations; i++ ) {
69+
x[ N-1 ] += 1;
70+
z = dindexOfRow( order, N, N, A, N, x, 1, workspace, 1 );
71+
if ( isnan( z ) ) {
72+
b.fail( 'should not return NaN' );
73+
}
74+
}
75+
b.toc();
76+
if ( isnan( z ) ) {
77+
b.fail( 'should not return NaN' );
78+
}
79+
b.pass( 'benchmark finished' );
80+
b.end();
81+
}
82+
}
83+
84+
85+
// MAIN //
86+
87+
/**
88+
* Main execution sequence.
89+
*
90+
* @private
91+
*/
92+
function main() {
93+
var min;
94+
var max;
95+
var ord;
96+
var N;
97+
var f;
98+
var i;
99+
var k;
100+
101+
min = 1; // 10^min
102+
max = 6; // 10^max
103+
104+
for ( k = 0; k < LAYOUTS.length; k++ ) {
105+
ord = LAYOUTS[ k ];
106+
for ( i = min; i <= max; i++ ) {
107+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
108+
f = createBenchmark( ord, N );
109+
bench( format( '%s::square_matrix:order=%s,size=%d', pkg, ord, N*N ), f );
110+
}
111+
}
112+
}
113+
114+
main();
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var zeros = require( '@stdlib/array/zeros' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var floor = require( '@stdlib/math/base/special/floor' );
29+
var tryRequire = require( '@stdlib/utils/try-require' );
30+
var format = require( '@stdlib/string/format' );
31+
var pkg = require( './../package.json' ).name;
32+
33+
34+
// VARIABLES //
35+
36+
var dindexOfRow = tryRequire( resolve( __dirname, './../lib/dindex_of_row.native.js' ) );
37+
var opts = {
38+
'skip': ( dindexOfRow instanceof Error )
39+
};
40+
var LAYOUTS = [
41+
'row-major',
42+
'column-major'
43+
];
44+
45+
46+
// FUNCTIONS //
47+
48+
/**
49+
* Creates a benchmark function.
50+
*
51+
* @private
52+
* @param {string} order - storage layout
53+
* @param {PositiveInteger} N - number of elements along each dimension
54+
* @returns {Function} benchmark function
55+
*/
56+
function createBenchmark( order, N ) {
57+
var workspace = zeros( N, 'uint8' );
58+
var A = zeros( N*N, 'float64' );
59+
var x = zeros( N, 'float64' );
60+
return benchmark;
61+
62+
/**
63+
* Benchmark function.
64+
*
65+
* @private
66+
* @param {Benchmark} b - benchmark instance
67+
*/
68+
function benchmark( b ) {
69+
var z;
70+
var i;
71+
72+
b.tic();
73+
for ( i = 0; i < b.iterations; i++ ) {
74+
x[ N-1 ] += 1;
75+
z = dindexOfRow( order, N, N, A, N, x, 1, workspace, 1 );
76+
if ( isnan( z ) ) {
77+
b.fail( 'should not return NaN' );
78+
}
79+
}
80+
b.toc();
81+
if ( isnan( z ) ) {
82+
b.fail( 'should not return NaN' );
83+
}
84+
b.pass( 'benchmark finished' );
85+
b.end();
86+
}
87+
}
88+
89+
90+
// MAIN //
91+
92+
/**
93+
* Main execution sequence.
94+
*
95+
* @private
96+
*/
97+
function main() {
98+
var min;
99+
var max;
100+
var ord;
101+
var N;
102+
var f;
103+
var i;
104+
var k;
105+
106+
min = 1; // 10^min
107+
max = 6; // 10^max
108+
109+
for ( k = 0; k < LAYOUTS.length; k++ ) {
110+
ord = LAYOUTS[ k ];
111+
for ( i = min; i <= max; i++ ) {
112+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
113+
f = createBenchmark( ord, N );
114+
bench( format( '%s::native,square_matrix:order=%s,size=%d', pkg, ord, N*N ), opts, f );
115+
}
116+
}
117+
}
118+
119+
main();
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var floor = require( '@stdlib/math/base/special/floor' );
27+
var zeros = require( '@stdlib/array/zeros' );
28+
var isColumnMajor = require( '@stdlib/ndarray/base/assert/is-column-major-string' );
29+
var format = require( '@stdlib/string/format' );
30+
var pkg = require( './../package.json' ).name;
31+
var dindexOfRow = require( './../lib/ndarray.js' );
32+
33+
34+
// VARIABLES //
35+
36+
var LAYOUTS = [
37+
'row-major',
38+
'column-major'
39+
];
40+
41+
42+
// FUNCTIONS //
43+
44+
/**
45+
* Creates a benchmark function.
46+
*
47+
* @private
48+
* @param {string} order - storage layout
49+
* @param {PositiveInteger} N - number of elements along each dimension
50+
* @returns {Function} benchmark function
51+
*/
52+
function createBenchmark( order, N ) {
53+
var workspace = zeros( N, 'uint8' );
54+
var A = zeros( N*N, 'float64' );
55+
var x = zeros( N, 'float64' );
56+
return benchmark;
57+
58+
/**
59+
* Benchmark function.
60+
*
61+
* @private
62+
* @param {Benchmark} b - benchmark instance
63+
*/
64+
function benchmark( b ) {
65+
var sa1;
66+
var sa2;
67+
var z;
68+
var i;
69+
70+
if ( isColumnMajor( order ) ) {
71+
sa1 = 1;
72+
sa2 = N;
73+
} else { // order === 'row-major'
74+
sa1 = N;
75+
sa2 = 1;
76+
}
77+
78+
b.tic();
79+
for ( i = 0; i < b.iterations; i++ ) {
80+
x[ N-1 ] += 1;
81+
z = dindexOfRow( N, N, A, sa1, sa2, 0, x, 1, 0, workspace, 1, 0 );
82+
if ( isnan( z ) ) {
83+
b.fail( 'should not return NaN' );
84+
}
85+
}
86+
b.toc();
87+
if ( isnan( z ) ) {
88+
b.fail( 'should not return NaN' );
89+
}
90+
b.pass( 'benchmark finished' );
91+
b.end();
92+
}
93+
}
94+
95+
96+
// MAIN //
97+
98+
/**
99+
* Main execution sequence.
100+
*
101+
* @private
102+
*/
103+
function main() {
104+
var min;
105+
var max;
106+
var ord;
107+
var N;
108+
var f;
109+
var i;
110+
var k;
111+
112+
min = 1; // 10^min
113+
max = 6; // 10^max
114+
115+
for ( k = 0; k < LAYOUTS.length; k++ ) {
116+
ord = LAYOUTS[ k ];
117+
for ( i = min; i <= max; i++ ) {
118+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
119+
f = createBenchmark( ord, N );
120+
bench( format( '%s::square_matrix:ndarray:order=%s,size=%d', pkg, ord, N*N ), f );
121+
}
122+
}
123+
}
124+
125+
main();

0 commit comments

Comments
 (0)