@stdlib/blas-base-wasm-dnrm2 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/module.js ADDED
@@ -0,0 +1,198 @@
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
+ /* 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 dnrm2 = new Module( mem );
55
+ * // returns <Module>
56
+ *
57
+ * // Initialize the routine:
58
+ * dnrm2.initializeSync();
59
+ *
60
+ * // Define a vector data type:
61
+ * var dtype = 'float64';
62
+ *
63
+ * // Specify a vector length:
64
+ * var N = 5;
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
+ * dnrm2.write( xptr, oneTo( N, dtype ) );
71
+ *
72
+ * // Perform computation:
73
+ * var out = dnrm2.main( N, xptr, 1 );
74
+ * // returns ~7.42
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
+ * Calculates the L2-norm of a double-precision floating-point vector.
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 - `x` stride length
106
+ * @returns {number} the L2-norm
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 dnrm2 = new Module( mem );
120
+ * // returns <Module>
121
+ *
122
+ * // Initialize the routine:
123
+ * dnrm2.initializeSync();
124
+ *
125
+ * // Define a vector data type:
126
+ * var dtype = 'float64';
127
+ *
128
+ * // Specify a vector length:
129
+ * var N = 5;
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
+ * dnrm2.write( xptr, oneTo( N, dtype ) );
136
+ *
137
+ * // Perform computation:
138
+ * var out = dnrm2.main( N, xptr, 1 );
139
+ * // returns ~7.42
140
+ */
141
+ setReadOnly( Module.prototype, 'main', function dnrm2( N, xptr, strideX ) {
142
+ return this._instance.exports.c_dnrm2( N, xptr, strideX );
143
+ });
144
+
145
+ /**
146
+ * Calculates the L2-norm of a double-precision floating-point vector using 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 - `x` stride length
155
+ * @param {NonNegativeInteger} offsetX - starting `x` index
156
+ * @returns {number} the L2-norm
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 dnrm2 = new Module( mem );
170
+ * // returns <Module>
171
+ *
172
+ * // Initialize the routine:
173
+ * dnrm2.initializeSync();
174
+ *
175
+ * // Define a vector data type:
176
+ * var dtype = 'float64';
177
+ *
178
+ * // Specify a vector length:
179
+ * var N = 5;
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
+ * dnrm2.write( xptr, oneTo( N, dtype ) );
186
+ *
187
+ * // Perform computation:
188
+ * var out = dnrm2.ndarray( N, xptr, 1, 0 );
189
+ * // returns ~7.42
190
+ */
191
+ setReadOnly( Module.prototype, 'ndarray', function dnrm2( N, xptr, strideX, offsetX ) {
192
+ return this._instance.exports.c_dnrm2_ndarray( N, xptr, strideX, offsetX );
193
+ });
194
+
195
+
196
+ // EXPORTS //
197
+
198
+ module.exports = Module;
package/lib/routine.js ADDED
@@ -0,0 +1,166 @@
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
+ /* eslint-disable no-restricted-syntax, no-invalid-this */
20
+
21
+ 'use strict';
22
+
23
+ // MODULES //
24
+
25
+ var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );
26
+ var inherits = require( '@stdlib/utils-inherit' );
27
+ var stride2offset = require( '@stdlib/strided-base-stride2offset' );
28
+ var Memory = require( '@stdlib/wasm-memory' );
29
+ var arrays2ptrs = require( '@stdlib/wasm-base-arrays2ptrs' );
30
+ var strided2object = require( '@stdlib/wasm-base-strided2object' );
31
+ var Module = require( './module.js' );
32
+
33
+
34
+ // MAIN //
35
+
36
+ /**
37
+ * Routine constructor.
38
+ *
39
+ * @private
40
+ * @constructor
41
+ * @returns {Routine} routine instance
42
+ *
43
+ * @example
44
+ * var Float64Array = require( '@stdlib/array-float64' );
45
+ *
46
+ * // Create a new routine:
47
+ * var dnrm2 = new Routine();
48
+ *
49
+ * // Initialize the module:
50
+ * dnrm2.initializeSync();
51
+ *
52
+ * // Define a strided array:
53
+ * var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
54
+ *
55
+ * // Perform operation:
56
+ * var out = dnrm2.main( x.length, x, 1 );
57
+ * // returns ~7.42
58
+ *
59
+ * @example
60
+ * var Float64Array = require( '@stdlib/array-float64' );
61
+ *
62
+ * // Create a new routine:
63
+ * var dnrm2 = new Routine();
64
+ *
65
+ * // Initialize the module:
66
+ * dnrm2.initializeSync();
67
+ *
68
+ * // Define strided arrays:
69
+ * var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
70
+ *
71
+ * // Perform operation:
72
+ * var out = dnrm2.ndarray( x.length, x, 1, 0 );
73
+ * // returns ~7.42
74
+ */
75
+ function Routine() {
76
+ if ( !( this instanceof Routine ) ) {
77
+ return new Routine();
78
+ }
79
+ Module.call( this, new Memory({
80
+ 'initial': 0
81
+ }));
82
+ return this;
83
+ }
84
+
85
+ // Inherit from the parent constructor:
86
+ inherits( Routine, Module );
87
+
88
+ /**
89
+ * Calculates the L2-norm of a double-precision floating-point vector.
90
+ *
91
+ * @name main
92
+ * @memberof Routine.prototype
93
+ * @readonly
94
+ * @type {Function}
95
+ * @param {PositiveInteger} N - number of indexed elements
96
+ * @param {Float64Array} x - input array
97
+ * @param {integer} strideX - `x` stride length
98
+ * @returns {number} the L2-norm
99
+ *
100
+ * @example
101
+ * var Float64Array = require( '@stdlib/array-float64' );
102
+ *
103
+ * // Create a new routine:
104
+ * var dnrm2 = new Routine();
105
+ *
106
+ * // Initialize the module:
107
+ * dnrm2.initializeSync();
108
+ *
109
+ * // Define a strided array:
110
+ * var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
111
+ *
112
+ * // Perform operation:
113
+ * var out = dnrm2.main( x.length, x, 1 );
114
+ * // returns ~7.42
115
+ */
116
+ setReadOnly( Routine.prototype, 'main', function dnrm2( N, x, strideX ) {
117
+ return this.ndarray( N, x, strideX, stride2offset( N, strideX ) );
118
+ });
119
+
120
+ /**
121
+ * Calculates the L2-norm of a double-precision floating-point vector using alternative indexing semantics.
122
+ *
123
+ * @name ndarray
124
+ * @memberof Routine.prototype
125
+ * @readonly
126
+ * @type {Function}
127
+ * @param {PositiveInteger} N - number of indexed elements
128
+ * @param {Float64Array} x - input array
129
+ * @param {integer} strideX - `x` stride length
130
+ * @param {NonNegativeInteger} offsetX - starting `x` index
131
+ * @returns {number} the L2-norm
132
+ *
133
+ * @example
134
+ * var Float64Array = require( '@stdlib/array-float64' );
135
+ *
136
+ * // Create a new routine:
137
+ * var dnrm2 = new Routine();
138
+ *
139
+ * // Initialize the module:
140
+ * dnrm2.initializeSync();
141
+ *
142
+ * // Define a strided array:
143
+ * var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
144
+ *
145
+ * // Perform operation:
146
+ * var out = dnrm2.ndarray( x.length, x, 1, 0 );
147
+ * // returns ~7.42
148
+ */
149
+ setReadOnly( Routine.prototype, 'ndarray', function dnrm2( N, x, strideX, offsetX ) {
150
+ var ptrs;
151
+ var p0;
152
+
153
+ // Convert the input array to "pointers" in the module's memory:
154
+ ptrs = arrays2ptrs( this, [
155
+ strided2object( N, x, strideX, offsetX )
156
+ ]);
157
+ p0 = ptrs[ 0 ];
158
+
159
+ // Perform computation by calling the corresponding parent method:
160
+ return Module.prototype.ndarray.call( this, N, p0.ptr, p0.stride, p0.offset ); // eslint-disable-line max-len
161
+ });
162
+
163
+
164
+ // EXPORTS //
165
+
166
+ module.exports = Routine;
package/manifest.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "options": {},
3
+ "fields": [
4
+ {
5
+ "field": "src",
6
+ "resolve": true,
7
+ "relative": true
8
+ },
9
+ {
10
+ "field": "include",
11
+ "resolve": true,
12
+ "relative": true
13
+ },
14
+ {
15
+ "field": "libraries",
16
+ "resolve": false,
17
+ "relative": false
18
+ },
19
+ {
20
+ "field": "libpath",
21
+ "resolve": true,
22
+ "relative": false
23
+ }
24
+ ],
25
+ "confs": [
26
+ {
27
+ "src": [],
28
+ "include": [],
29
+ "libraries": [],
30
+ "libpath": [],
31
+ "dependencies": [
32
+ "@stdlib/blas-base-dnrm2"
33
+ ]
34
+ }
35
+ ]
36
+ }
package/package.json ADDED
@@ -0,0 +1,97 @@
1
+ {
2
+ "name": "@stdlib/blas-base-wasm-dnrm2",
3
+ "version": "0.1.0",
4
+ "description": "Calculate the L2-norm of a double-precision floating-point vector.",
5
+ "license": "Apache-2.0",
6
+ "author": {
7
+ "name": "The Stdlib Authors",
8
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
9
+ },
10
+ "contributors": [
11
+ {
12
+ "name": "The Stdlib Authors",
13
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
14
+ }
15
+ ],
16
+ "main": "./lib",
17
+ "browser": {
18
+ "./lib/binary.js": "./lib/binary.browser.js"
19
+ },
20
+ "directories": {
21
+ "doc": "./docs",
22
+ "lib": "./lib",
23
+ "scripts": "./scripts",
24
+ "src": "./src",
25
+ "dist": "./dist"
26
+ },
27
+ "types": "./docs/types",
28
+ "scripts": {},
29
+ "homepage": "https://stdlib.io",
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "git://github.com/stdlib-js/blas-base-wasm-dnrm2.git"
33
+ },
34
+ "bugs": {
35
+ "url": "https://github.com/stdlib-js/stdlib/issues"
36
+ },
37
+ "dependencies": {
38
+ "@stdlib/assert-is-wasm-memory": "^0.1.0",
39
+ "@stdlib/blas-base-dnrm2": "^0.4.0",
40
+ "@stdlib/fs-read-wasm": "^0.2.2",
41
+ "@stdlib/strided-base-stride2offset": "^0.1.0",
42
+ "@stdlib/string-format": "^0.2.2",
43
+ "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2",
44
+ "@stdlib/utils-inherit": "^0.2.2",
45
+ "@stdlib/wasm-base-arrays2ptrs": "github:stdlib-js/wasm-base-arrays2ptrs#main",
46
+ "@stdlib/wasm-base-strided2object": "github:stdlib-js/wasm-base-strided2object#main",
47
+ "@stdlib/wasm-memory": "^0.1.0",
48
+ "@stdlib/wasm-module-wrapper": "github:stdlib-js/wasm-module-wrapper#main",
49
+ "@stdlib/error-tools-fmtprodmsg": "^0.2.2"
50
+ },
51
+ "devDependencies": {},
52
+ "engines": {
53
+ "node": ">=0.10.0",
54
+ "npm": ">2.7.0"
55
+ },
56
+ "os": [
57
+ "aix",
58
+ "darwin",
59
+ "freebsd",
60
+ "linux",
61
+ "macos",
62
+ "openbsd",
63
+ "sunos",
64
+ "win32",
65
+ "windows"
66
+ ],
67
+ "keywords": [
68
+ "stdlib",
69
+ "stdmath",
70
+ "mathematics",
71
+ "math",
72
+ "blas",
73
+ "level 1",
74
+ "dnrm2",
75
+ "nrm2",
76
+ "euclidean",
77
+ "magnitude",
78
+ "2-norm",
79
+ "l2-norm",
80
+ "norm",
81
+ "linear",
82
+ "algebra",
83
+ "subroutines",
84
+ "vector",
85
+ "array",
86
+ "ndarray",
87
+ "float64",
88
+ "double",
89
+ "float64array",
90
+ "webassembly",
91
+ "wasm"
92
+ ],
93
+ "funding": {
94
+ "type": "opencollective",
95
+ "url": "https://opencollective.com/stdlib"
96
+ }
97
+ }
@@ -0,0 +1,4 @@
1
+ [
2
+ "_c_dnrm2",
3
+ "_c_dnrm2_ndarray"
4
+ ]
package/src/main.wasm ADDED
Binary file