|
| 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(); |
1 commit comments
stdlib-bot commentedon Feb 8, 2025
Coverage Report
The above coverage report was generated for the changes in this push.