Skip to content

Commit 8f880ce

Browse files
committed
feat: add initial wasm implementation for dasumpw
--- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na ---
1 parent 4efada9 commit 8f880ce

File tree

11 files changed

+1024
-0
lines changed

11 files changed

+1024
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 resolve = require( 'path' ).resolve;
24+
var readWASM = require( '@stdlib/fs/read-wasm' ).sync;
25+
26+
27+
// MAIN //
28+
29+
var wasm = readWASM( resolve( __dirname, '..', 'src', 'main.wasm' ) );
30+
31+
32+
// EXPORTS //
33+
34+
module.exports = wasm;
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
* WebAssembly routine to calculate the sum of absolute values (L1 norm) of double-precision floating-point strided array elements using pairwise summation.
23+
*
24+
* @module @stdlib/blas/ext/base/wasm/dasumpw
25+
*
26+
* @example
27+
* var Float64Array = require( '@stdlib/array/float64' );
28+
* var dasumpw = require( '@stdlib/blas/ext/base/wasm/dasumpw' );
29+
*
30+
* // Define a strided array:
31+
* var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
32+
*
33+
* // Perform operation:
34+
* var v = dasumpw.main( x.length, x, 1 );
35+
* // returns 5.0
36+
*
37+
* @example
38+
* var Float64Array = require( '@stdlib/array/float64' );
39+
* var dasumpw = require( '@stdlib/blas/ext/base/wasm/dasumpw' );
40+
*
41+
* // Define a strided array:
42+
* var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
43+
*
44+
* // Perform operation:
45+
* var v = dasumpw.ndarray( 4, x, 2, 1 );
46+
* // returns 9.0
47+
*
48+
* @example
49+
* var Memory = require( '@stdlib/wasm/memory' );
50+
* var oneTo = require( '@stdlib/array/one-to' );
51+
* var zeros = require( '@stdlib/array/zeros' );
52+
* var dasumpw = require( '@stdlib/blas/ext/base/wasm/dasumpw' );
53+
*
54+
* // Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
55+
* var mem = new Memory({
56+
* 'initial': 10,
57+
* 'maximum': 100
58+
* });
59+
*
60+
* // Create a BLAS routine:
61+
* var mod = new dasumpw.Module( mem );
62+
* // returns <Module>
63+
*
64+
* // Initialize the routine:
65+
* mod.initializeSync();
66+
*
67+
* // Define a vector data type:
68+
* var dtype = 'float64';
69+
*
70+
* // Specify a vector length:
71+
* var N = 3;
72+
*
73+
* // Define a pointer (i.e., byte offset) for storing the input vector:
74+
* var xptr = 0;
75+
*
76+
* // Write vector values to module memory:
77+
* mod.write( xptr, oneTo( N, dtype ) );
78+
*
79+
* // Perform computation:
80+
* var sum = mod.main( N, xptr, 1 );
81+
* // returns 6.0
82+
*/
83+
84+
// MODULES //
85+
86+
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
87+
var main = require( './main.js' );
88+
var Module = require( './module.js' );
89+
90+
91+
// MAIN //
92+
93+
setReadOnly( main, 'Module', Module );
94+
95+
96+
// EXPORTS //
97+
98+
module.exports = main;
99+
100+
// exports: { "Module": "main.Module" }
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 Routine = require( './routine.js' );
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* WebAssembly routine to calculate the sum of absolute values (L1 norm) of double-precision floating-point strided array elements using pairwise summation.
30+
*
31+
* @name dasumpw
32+
* @type {Routine}
33+
*
34+
* @example
35+
* var Float64Array = require( '@stdlib/array/float64' );
36+
* var dasumpw = require( '@stdlib/blas/ext/base/wasm/dasumpw' );
37+
*
38+
* // Define a strided array:
39+
* var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
40+
*
41+
* // Perform operation:
42+
* var v = dasumpw.main( 3, x, 1 );
43+
* // returns 5.0
44+
*
45+
* @example
46+
* var Float64Array = require( '@stdlib/array/float64' );
47+
* var dasumpw = require( '@stdlib/blas/ext/base/wasm/dasumpw' );
48+
*
49+
* // Define a strided array:
50+
* var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
51+
*
52+
* // Perform operation:
53+
* var v = dasumpw.ndarray( 4, x, 2, 1 );
54+
* // returns 9.0
55+
*/
56+
var dasumpw = new Routine();
57+
dasumpw.initializeSync(); // eslint-disable-line node/no-sync
58+
59+
60+
// EXPORTS //
61+
62+
module.exports = dasumpw;
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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+
/* eslint-disable no-restricted-syntax, no-invalid-this */
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var isWebAssemblyMemory = require( '@stdlib/assert/is-wasm-memory' );
26+
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
27+
var inherits = require( '@stdlib/utils/inherit' );
28+
var WasmModule = require( '@stdlib/wasm/module-wrapper' );
29+
var format = require( '@stdlib/string/format' );
30+
var wasmBinary = require( './binary.js' );
31+
32+
33+
// MAIN //
34+
35+
/**
36+
* BLAS routine WebAssembly module wrapper constructor.
37+
*
38+
* @constructor
39+
* @param {Object} memory - WebAssembly memory instance
40+
* @throws {TypeError} must provide a WebAssembly memory instance
41+
* @returns {Module} module instance
42+
*
43+
* @example
44+
* var Memory = require( '@stdlib/wasm/memory' );
45+
* var oneTo = require( '@stdlib/array/one-to' );
46+
*
47+
* // Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
48+
* var mem = new Memory({
49+
* 'initial': 10,
50+
* 'maximum': 100
51+
* });
52+
*
53+
* // Create a BLAS routine:
54+
* var dasumpw = new Module( mem );
55+
* // returns <Module>
56+
*
57+
* // Initialize the routine:
58+
* dasumpw.initializeSync();
59+
*
60+
* // Define a vector data type:
61+
* var dtype = 'float64';
62+
*
63+
* // Specify a vector length:
64+
* var N = 3;
65+
*
66+
* // Define a pointer (i.e., byte offset) for storing the input vector:
67+
* var xptr = 0;
68+
*
69+
* // Write vector values to module memory:
70+
* dasumpw.write( xptr, oneTo( N, dtype ) );
71+
*
72+
* // Perform computation:
73+
* var sum = dasumpw.main( N, xptr, 1 );
74+
* // returns 6.0
75+
*/
76+
function Module( memory ) {
77+
if ( !( this instanceof Module ) ) {
78+
return new Module( memory );
79+
}
80+
if ( !isWebAssemblyMemory( memory ) ) {
81+
throw new TypeError( format( 'invalid argument. Must provide a WebAssembly memory instance. Value: `%s`.', memory ) );
82+
}
83+
// Call the parent constructor:
84+
WasmModule.call( this, wasmBinary, memory, {
85+
'env': {
86+
'memory': memory
87+
}
88+
});
89+
90+
return this;
91+
}
92+
93+
// Inherit from the parent constructor:
94+
inherits( Module, WasmModule );
95+
96+
/**
97+
* Computes the sum of absolute values (L1 norm) of double-precision floating-point strided array elements using pairwise summation.
98+
*
99+
* @name main
100+
* @memberof Module.prototype
101+
* @readonly
102+
* @type {Function}
103+
* @param {PositiveInteger} N - number of indexed elements
104+
* @param {NonNegativeInteger} xptr - input array pointer (i.e., byte offset)
105+
* @param {integer} strideX - stride length
106+
* @returns {number} sum
107+
*
108+
* @example
109+
* var Memory = require( '@stdlib/wasm/memory' );
110+
* var oneTo = require( '@stdlib/array/one-to' );
111+
*
112+
* // Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
113+
* var mem = new Memory({
114+
* 'initial': 10,
115+
* 'maximum': 100
116+
* });
117+
*
118+
* // Create a BLAS routine:
119+
* var dasumpw = new Module( mem );
120+
* // returns <Module>
121+
*
122+
* // Initialize the routine:
123+
* dasumpw.initializeSync();
124+
*
125+
* // Define a vector data type:
126+
* var dtype = 'float64';
127+
*
128+
* // Specify a vector length:
129+
* var N = 3;
130+
*
131+
* // Define a pointer (i.e., byte offset) for storing the input vector:
132+
* var xptr = 0;
133+
*
134+
* // Write vector values to module memory:
135+
* dasumpw.write( xptr, oneTo( N, dtype ) );
136+
*
137+
* // Perform computation:
138+
* var sum = dasumpw.main( N, xptr, 1 );
139+
* // returns 6.0
140+
*/
141+
setReadOnly( Module.prototype, 'main', function dasumpw( N, xptr, strideX ) {
142+
return this._instance.exports.stdlib_strided_dasumpw( N, xptr, strideX );
143+
});
144+
145+
/**
146+
* Computes the sum of absolute values (L1 norm) of double-precision floating-point strided array elements using pairwise summation and alternative indexing semantics.
147+
*
148+
* @name ndarray
149+
* @memberof Module.prototype
150+
* @readonly
151+
* @type {Function}
152+
* @param {PositiveInteger} N - number of indexed elements
153+
* @param {NonNegativeInteger} xptr - input array pointer (i.e., byte offset)
154+
* @param {integer} strideX - stride length
155+
* @param {NonNegativeInteger} offsetX - starting index
156+
* @returns {number} sum
157+
*
158+
* @example
159+
* var Memory = require( '@stdlib/wasm/memory' );
160+
* var oneTo = require( '@stdlib/array/one-to' );
161+
*
162+
* // Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
163+
* var mem = new Memory({
164+
* 'initial': 10,
165+
* 'maximum': 100
166+
* });
167+
*
168+
* // Create a BLAS routine:
169+
* var dasumpw = new Module( mem );
170+
* // returns <Module>
171+
*
172+
* // Initialize the routine:
173+
* dasumpw.initializeSync();
174+
*
175+
* // Define a vector data type:
176+
* var dtype = 'float64';
177+
*
178+
* // Specify a vector length:
179+
* var N = 3;
180+
*
181+
* // Define a pointer (i.e., byte offset) for storing the input vector:
182+
* var xptr = 0;
183+
*
184+
* // Write vector values to module memory:
185+
* dasumpw.write( xptr, oneTo( N, dtype ) );
186+
*
187+
* // Perform computation:
188+
* var sum = dasumpw.ndarray( N, xptr, 1, 0 );
189+
* // returns 6.0
190+
*/
191+
setReadOnly( Module.prototype, 'ndarray', function dasumpw( N, xptr, strideX, offsetX ) {
192+
return this._instance.exports.stdlib_strided_dasumpw_ndarray( N, xptr, strideX, offsetX ); // eslint-disable-line max-len
193+
});
194+
195+
196+
// EXPORTS //
197+
198+
module.exports = Module;

0 commit comments

Comments
 (0)