Skip to content

Commit

Permalink
docs: add example using little-endian arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
kgryte committed Sep 25, 2024
1 parent bffda37 commit 3095c9a
Showing 1 changed file with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

var hasWebAssemblySupport = require( '@stdlib/assert/has-wasm-support' );
var Memory = require( '@stdlib/wasm/memory' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
var gfill = require( '@stdlib/blas/ext/base/gfill' );
var gfillBy = require( '@stdlib/blas/ext/base/gfill-by' );
var bytesPerElement = require( '@stdlib/ndarray/base/bytes-per-element' );
var Float64ArrayLE = require( '@stdlib/array/little-endian-float64' );
var daxpy = require( './../lib' );

function main() {
if ( !hasWebAssemblySupport() ) {
console.error( 'Environment does not support WebAssembly.' );
return;
}
// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
var mem = new Memory({
'initial': 10,
'maximum': 100
});

// Create a BLAS routine:
var mod = new daxpy.Module( mem );
// returns <Module>

// Initialize the routine:
mod.initializeSync(); // eslint-disable-line node/no-sync

// Define a vector data type:
var dtype = 'float64';

// Specify a vector length:
var N = 5;

// Define pointers (i.e., byte offsets) for storing two vectors:
var xptr = 0;
var yptr = N * bytesPerElement( dtype );

// Create typed array views over module memory:
var x = new Float64ArrayLE( mod.memory.buffer, xptr, N );
var y = new Float64ArrayLE( mod.memory.buffer, yptr, N );

// Write values to module memory:
gfillBy( N, x, 1, discreteUniform( -10.0, 10.0 ) );
gfill( N, 1.0, y, 1 );

// Perform computation:
mod.ndarray( N, 5.0, xptr, 1, 0, yptr, 1, 0 );

// Print the results:
console.log( 'x[:] = [%s]', x.toString() );
console.log( 'y[:] = [%s]', y.toString() );
}

main();

0 comments on commit 3095c9a

Please sign in to comment.