Skip to content

Commit 3095c9a

Browse files
committed
docs: add example using little-endian arrays
1 parent bffda37 commit 3095c9a

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
var hasWebAssemblySupport = require( '@stdlib/assert/has-wasm-support' );
22+
var Memory = require( '@stdlib/wasm/memory' );
23+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
24+
var gfill = require( '@stdlib/blas/ext/base/gfill' );
25+
var gfillBy = require( '@stdlib/blas/ext/base/gfill-by' );
26+
var bytesPerElement = require( '@stdlib/ndarray/base/bytes-per-element' );
27+
var Float64ArrayLE = require( '@stdlib/array/little-endian-float64' );
28+
var daxpy = require( './../lib' );
29+
30+
function main() {
31+
if ( !hasWebAssemblySupport() ) {
32+
console.error( 'Environment does not support WebAssembly.' );
33+
return;
34+
}
35+
// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
36+
var mem = new Memory({
37+
'initial': 10,
38+
'maximum': 100
39+
});
40+
41+
// Create a BLAS routine:
42+
var mod = new daxpy.Module( mem );
43+
// returns <Module>
44+
45+
// Initialize the routine:
46+
mod.initializeSync(); // eslint-disable-line node/no-sync
47+
48+
// Define a vector data type:
49+
var dtype = 'float64';
50+
51+
// Specify a vector length:
52+
var N = 5;
53+
54+
// Define pointers (i.e., byte offsets) for storing two vectors:
55+
var xptr = 0;
56+
var yptr = N * bytesPerElement( dtype );
57+
58+
// Create typed array views over module memory:
59+
var x = new Float64ArrayLE( mod.memory.buffer, xptr, N );
60+
var y = new Float64ArrayLE( mod.memory.buffer, yptr, N );
61+
62+
// Write values to module memory:
63+
gfillBy( N, x, 1, discreteUniform( -10.0, 10.0 ) );
64+
gfill( N, 1.0, y, 1 );
65+
66+
// Perform computation:
67+
mod.ndarray( N, 5.0, xptr, 1, 0, yptr, 1, 0 );
68+
69+
// Print the results:
70+
console.log( 'x[:] = [%s]', x.toString() );
71+
console.log( 'y[:] = [%s]', y.toString() );
72+
}
73+
74+
main();

0 commit comments

Comments
 (0)