Skip to content

Commit 8cb5cec

Browse files
aman-095kgrytestdlib-bot
authored andcommitted
feat: add blas/base/wasm/zcopy
PR-URL: stdlib-js#3982 Ref: stdlib-js#2039 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent b90a021 commit 8cb5cec

33 files changed

+6144
-0
lines changed

lib/node_modules/@stdlib/blas/base/wasm/zcopy/README.md

+413
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 hasWebAssemblySupport = require( '@stdlib/assert/has-wasm-support' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var Complex128Array = require( '@stdlib/array/complex128' );
29+
var pkg = require( './../package.json' ).name;
30+
var zcopy = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var opts = {
36+
'skip': !hasWebAssemblySupport()
37+
};
38+
var options = {
39+
'dtype': 'float64'
40+
};
41+
42+
43+
// FUNCTIONS //
44+
45+
/**
46+
* Creates a benchmark function.
47+
*
48+
* @private
49+
* @param {PositiveInteger} len - array length
50+
* @returns {Function} benchmark function
51+
*/
52+
function createBenchmark( len ) {
53+
var xbuf;
54+
var ybuf;
55+
var x;
56+
var y;
57+
58+
xbuf = uniform( len*2, -100.0, 100.0, options );
59+
x = new Complex128Array( xbuf.buffer );
60+
61+
ybuf = uniform( len*2, -100.0, 100.0, options );
62+
y = new Complex128Array( ybuf.buffer );
63+
64+
return benchmark;
65+
66+
/**
67+
* Benchmark function.
68+
*
69+
* @private
70+
* @param {Benchmark} b - benchmark instance
71+
*/
72+
function benchmark( b ) {
73+
var i;
74+
75+
b.tic();
76+
for ( i = 0; i < b.iterations; i++ ) {
77+
zcopy.main( x.length, x, 1, y, 1 );
78+
if ( isnan( ybuf[ i%(len*2) ] ) ) {
79+
b.fail( 'should not return NaN' );
80+
}
81+
}
82+
b.toc();
83+
if ( isnan( ybuf[ i%(len*2) ] ) ) {
84+
b.fail( 'should not return NaN' );
85+
}
86+
b.pass( 'benchmark finished' );
87+
b.end();
88+
}
89+
}
90+
91+
92+
// MAIN //
93+
94+
/**
95+
* Main execution sequence.
96+
*
97+
* @private
98+
*/
99+
function main() {
100+
var len;
101+
var min;
102+
var max;
103+
var f;
104+
var i;
105+
106+
min = 1; // 10^min
107+
max = 6; // 10^max
108+
109+
for ( i = min; i <= max; i++ ) {
110+
len = pow( 10, i );
111+
f = createBenchmark( len );
112+
bench( pkg+':len='+len, opts, f );
113+
}
114+
}
115+
116+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 hasWebAssemblySupport = require( '@stdlib/assert/has-wasm-support' );
25+
var Memory = require( '@stdlib/wasm/memory' );
26+
var pkg = require( './../package.json' ).name;
27+
var zcopy = require( './../lib' );
28+
29+
30+
// VARIABLES //
31+
32+
var opts = {
33+
'skip': !hasWebAssemblySupport()
34+
};
35+
36+
37+
// MAIN //
38+
39+
bench( pkg+':Module:constructor', opts, function benchmark( b ) {
40+
var values;
41+
var o;
42+
var v;
43+
var i;
44+
45+
o = {
46+
'initial': 0
47+
};
48+
values = [
49+
new Memory( o ),
50+
new Memory( o )
51+
];
52+
53+
b.tic();
54+
for ( i = 0; i < b.iterations; i++ ) {
55+
v = new zcopy.Module( values[ i%values.length ] );
56+
if ( typeof v !== 'object' ) {
57+
b.fail( 'should return an object' );
58+
}
59+
}
60+
b.toc();
61+
if ( typeof v !== 'object' ) {
62+
b.fail( 'should return an object' );
63+
}
64+
b.pass( 'benchmark finished' );
65+
b.end();
66+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 hasWebAssemblySupport = require( '@stdlib/assert/has-wasm-support' );
25+
var Memory = require( '@stdlib/wasm/memory' );
26+
var bytesPerElement = require( '@stdlib/ndarray/base/bytes-per-element' );
27+
var uniform = require( '@stdlib/random/array/uniform' );
28+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
29+
var pow = require( '@stdlib/math/base/special/pow' );
30+
var pkg = require( './../package.json' ).name;
31+
var zcopy = require( './../lib' );
32+
33+
34+
// VARIABLES //
35+
36+
var opts = {
37+
'skip': !hasWebAssemblySupport()
38+
};
39+
var options = {
40+
'dtype': 'float64'
41+
};
42+
43+
44+
// FUNCTIONS //
45+
46+
/**
47+
* Creates a benchmark function.
48+
*
49+
* @private
50+
* @param {PositiveInteger} len - array length
51+
* @returns {Function} benchmark function
52+
*/
53+
function createBenchmark( len ) {
54+
return benchmark;
55+
56+
/**
57+
* Benchmark function.
58+
*
59+
* @private
60+
* @param {Benchmark} b - benchmark instance
61+
*/
62+
function benchmark( b ) {
63+
var byteOffset;
64+
var view;
65+
var xptr;
66+
var yptr;
67+
var mod;
68+
var mem;
69+
var nb;
70+
var N;
71+
var i;
72+
73+
N = len * 2;
74+
75+
// Create a new BLAS routine interface:
76+
mem = new Memory({
77+
'initial': 0
78+
});
79+
mod = new zcopy.Module( mem );
80+
81+
// Initialize the module:
82+
mod.initializeSync(); // eslint-disable-line node/no-sync
83+
84+
// Reallocate the underlying memory to allow storing two vectors:
85+
nb = bytesPerElement( 'complex128' );
86+
mod.realloc( 2*(N*nb) );
87+
88+
// Define pointers (i.e., byte offsets) to the first vector elements:
89+
xptr = 0;
90+
yptr = N * nb;
91+
92+
// Write random values to module memory:
93+
mod.write( xptr, uniform( N, -100.0, 100.0, options ) );
94+
mod.write( yptr, uniform( N, -100.0, 100.0, options ) );
95+
96+
// Retrieve a DataView of module memory:
97+
view = mod.view;
98+
99+
b.tic();
100+
for ( i = 0; i < b.iterations; i++ ) {
101+
mod.main( len, xptr, 1, yptr, 1 );
102+
byteOffset = yptr + ( (i%len)*nb );
103+
if ( isnan( view.getFloat64( byteOffset, true ) ) ) {
104+
b.fail( 'should not return NaN' );
105+
}
106+
}
107+
b.toc();
108+
if ( isnan( view.getFloat64( byteOffset, true ) ) ) {
109+
b.fail( 'should not return NaN' );
110+
}
111+
b.pass( 'benchmark finished' );
112+
b.end();
113+
}
114+
}
115+
116+
117+
// MAIN //
118+
119+
/**
120+
* Main execution sequence.
121+
*
122+
* @private
123+
*/
124+
function main() {
125+
var len;
126+
var min;
127+
var max;
128+
var f;
129+
var i;
130+
131+
min = 1; // 10^min
132+
max = 6; // 10^max
133+
134+
for ( i = min; i <= max; i++ ) {
135+
len = pow( 10, i );
136+
f = createBenchmark( len );
137+
bench( pkg+'::module,pointers:len='+len, opts, f );
138+
}
139+
}
140+
141+
main();

0 commit comments

Comments
 (0)