Skip to content

Commit 333716b

Browse files
committed
Add generic BLAS ndarray interface for interchanging vectors
1 parent b6c7b68 commit 333716b

File tree

12 files changed

+1601
-0
lines changed

12 files changed

+1601
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2020 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# gswap
22+
23+
> Interchange two vectors.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var gswap = require( '@stdlib/blas/gswap' );
37+
```
38+
39+
#### gswap( x, y )
40+
41+
Interchanges two vectors `x` and `y`.
42+
43+
```javascript
44+
var Float64Array = require( '@stdlib/array/float64' );
45+
var array = require( '@stdlib/ndarray/array' );
46+
47+
var x = array( new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ) );
48+
var y = array( new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ) );
49+
50+
gswap( x, y );
51+
52+
var xbuf = x.data;
53+
// returns <Float64Array>[ 2.0, 6.0, -1.0, -4.0, 8.0 ]
54+
55+
var ybuf = y.data;
56+
// returns <Float64Array>[ 4.0, 2.0, -3.0, 5.0, -1.0 ]
57+
```
58+
59+
The function has the following parameters:
60+
61+
- **x**: a 1-dimensional [`ndarray`][@stdlib/ndarray/array] or an array-like object.
62+
- **y**: a 1-dimensional [`ndarray`][@stdlib/ndarray/array] or an array-like object.
63+
64+
</section>
65+
66+
<!-- /.usage -->
67+
68+
<section class="notes">
69+
70+
## Notes
71+
72+
- `gswap()` provides a higher-level interface to the [BLAS][blas] level 1 function [`gswap`][@stdlib/blas/base/gswap].
73+
- In general, for best performance, especially for large vectors, provide 1-dimensional [`ndarrays`][@stdlib/ndarray/array] whose underlying data type is either `float64` or `float32`.
74+
75+
</section>
76+
77+
<!-- /.notes -->
78+
79+
<section class="examples">
80+
81+
## Examples
82+
83+
<!-- eslint no-undef: "error" -->
84+
85+
```javascript
86+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
87+
var gswap = require( '@stdlib/blas/gswap' );
88+
89+
var rand1 = discreteUniform.factory( 0, 100 );
90+
var rand2 = discreteUniform.factory( 0, 10 );
91+
92+
var x = [];
93+
var y = [];
94+
var i;
95+
for ( i = 0; i < 10; i++ ) {
96+
x.push( rand1() );
97+
y.push( rand2() );
98+
}
99+
console.log( x );
100+
console.log( y );
101+
102+
gswap( x, y );
103+
console.log( x );
104+
console.log( y );
105+
```
106+
107+
</section>
108+
109+
<!-- /.examples -->
110+
111+
<section class="links">
112+
113+
[blas]: http://www.netlib.org/blas
114+
115+
[@stdlib/blas/base/gswap]: https://github.com/stdlib-js/stdlib
116+
117+
[@stdlib/ndarray/array]: https://github.com/stdlib-js/stdlib
118+
119+
</section>
120+
121+
<!-- /.links -->
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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 randu = require( '@stdlib/random/base/randu' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var Float32Array = require( '@stdlib/array/float32' );
28+
var array = require( '@stdlib/ndarray/array' );
29+
var pkg = require( './../package.json' ).name;
30+
var gswap = require( './../lib/main.js' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Creates a benchmark function.
37+
*
38+
* @private
39+
* @param {PositiveInteger} len - array length
40+
* @returns {Function} benchmark function
41+
*/
42+
function createBenchmark( len ) {
43+
var x;
44+
var y;
45+
var i;
46+
47+
x = new Float32Array( len );
48+
y = new Float32Array( len );
49+
for ( i = 0; i < len; i++ ) {
50+
x[ i ] = ( randu()*10.0 ) - 20.0;
51+
y[ i ] = ( randu()*10.0 ) - 20.0;
52+
}
53+
x = array( x );
54+
y = array( y );
55+
56+
return benchmark;
57+
58+
function benchmark( b ) {
59+
var d;
60+
var i;
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
d = gswap( x, y );
65+
if ( isnan( d[ i%x.length ] ) ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
}
69+
b.toc();
70+
if ( isnan( d ) ) {
71+
b.fail( 'should not return NaN' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
}
76+
}
77+
78+
79+
// MAIN //
80+
81+
/**
82+
* Main execution sequence.
83+
*
84+
* @private
85+
*/
86+
function main() {
87+
var len;
88+
var min;
89+
var max;
90+
var f;
91+
var i;
92+
93+
min = 1; // 10^min
94+
max = 6; // 10^max
95+
96+
for ( i = min; i <= max; i++ ) {
97+
len = pow( 10, i );
98+
f = createBenchmark( len );
99+
bench( pkg+':len='+len, f );
100+
}
101+
}
102+
103+
main();
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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 randu = require( '@stdlib/random/base/randu' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var Float64Array = require( '@stdlib/array/float64' );
28+
var array = require( '@stdlib/ndarray/array' );
29+
var pkg = require( './../package.json' ).name;
30+
var gswap = require( './../lib/main.js' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Creates a benchmark function.
37+
*
38+
* @private
39+
* @param {PositiveInteger} len - array length
40+
* @returns {Function} benchmark function
41+
*/
42+
function createBenchmark( len ) {
43+
var x;
44+
var y;
45+
var i;
46+
47+
x = new Float64Array( len );
48+
y = new Float64Array( len );
49+
for ( i = 0; i < len; i++ ) {
50+
x[ i ] = ( randu()*10.0 ) - 20.0;
51+
y[ i ] = ( randu()*10.0 ) - 20.0;
52+
}
53+
x = array( x );
54+
y = array( y );
55+
56+
return benchmark;
57+
58+
function benchmark( b ) {
59+
var d;
60+
var i;
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
d = gswap( x, y );
65+
if ( isnan( d[ i%x.length ] ) ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
}
69+
b.toc();
70+
if ( isnan( d ) ) {
71+
b.fail( 'should not return NaN' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
}
76+
}
77+
78+
79+
// MAIN //
80+
81+
/**
82+
* Main execution sequence.
83+
*
84+
* @private
85+
*/
86+
function main() {
87+
var len;
88+
var min;
89+
var max;
90+
var f;
91+
var i;
92+
93+
min = 1; // 10^min
94+
max = 6; // 10^max
95+
96+
for ( i = min; i <= max; i++ ) {
97+
len = pow( 10, i );
98+
f = createBenchmark( len );
99+
bench( pkg+':len='+len, f );
100+
}
101+
}
102+
103+
main();

0 commit comments

Comments
 (0)