Skip to content

Commit 98600ec

Browse files
aman-095kgryte
andauthored
feat: add blas/base/dcopy-wasm
PR-URL: #2959 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 929a224 commit 98600ec

32 files changed

+5657
-0
lines changed
Lines changed: 334 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,334 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# dcopy
22+
23+
> Copy values from `x` into `y`.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var dcopy = require( '@stdlib/blas/base/dcopy-wasm' );
31+
```
32+
33+
#### dcopy.main( N, x, strideX, y, strideY )
34+
35+
Copies values from `x` into `y`.
36+
37+
```javascript
38+
var Float64Array = require( '@stdlib/array/float64' );
39+
40+
var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
41+
var y = new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0 ] );
42+
43+
dcopy.main( x.length, x, 1, y, 1 );
44+
// y => <Float64Array>[ 1.0, 2.0, 3.0, 4.0, 5.0 ]
45+
```
46+
47+
The function has the following parameters:
48+
49+
- **N**: number of indexed elements.
50+
- **x**: input [`Float64Array`][@stdlib/array/float64].
51+
- **strideX**: index increment for `x`.
52+
- **y**: input [`Float64Array`][@stdlib/array/float64].
53+
- **strideY**: index increment for `y`.
54+
55+
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to copy every other element from `x` into `y` in reverse order,
56+
57+
```javascript
58+
var Float64Array = require( '@stdlib/array/float64' );
59+
60+
var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
61+
var y = new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
62+
63+
dcopy.main( 3, x, 2, y, -1 );
64+
// y => <Float64Array>[ 5.0, 3.0, 1.0, 1.0, 1.0, 1.0 ]
65+
```
66+
67+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
68+
69+
<!-- eslint-disable stdlib/capitalized-comments -->
70+
71+
```javascript
72+
var Float64Array = require( '@stdlib/array/float64' );
73+
74+
// Initial arrays...
75+
var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
76+
var y0 = new Float64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
77+
78+
// Create offset views...
79+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
80+
var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // start at 4th element
81+
82+
dcopy.main( 3, x1, -2, y1, 1 );
83+
// y0 => <Float64Array>[ 7.0, 8.0, 9.0, 6.0, 4.0, 2.0 ]
84+
```
85+
86+
#### dcopy.ndarray( N, x, strideX, offsetX, y, strideY, offsetY )
87+
88+
Copies values from `x` into `y` using alternative indexing semantics.
89+
90+
```javascript
91+
var Float64Array = require( '@stdlib/array/float64' );
92+
93+
var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
94+
var y = new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0 ] );
95+
96+
dcopy.ndarray( x.length, x, 1, 0, y, 1, 0 );
97+
// y => <Float64Array>[ 1.0, 2.0, 3.0, 4.0, 5.0 ]
98+
```
99+
100+
The function has the following additional parameters:
101+
102+
- **offsetX**: starting index for `x`.
103+
- **offsetY**: starting index for `y`.
104+
105+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example, to copy every other value in `x` starting from the second value into the last `N` elements in `y` where `x[i] = y[n]`, `x[i+2] = y[n-1]`,...,
106+
107+
```javascript
108+
var Float64Array = require( '@stdlib/array/float64' );
109+
110+
var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
111+
var y = new Float64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
112+
113+
dcopy.ndarray( 3, x, 2, 1, y, -1, y.length-1 );
114+
// y => <Float64Array>[ 7.0, 8.0, 9.0, 6.0, 4.0, 2.0 ]
115+
```
116+
117+
* * *
118+
119+
### Module
120+
121+
#### dcopy.Module( memory )
122+
123+
Returns a new WebAssembly [module wrapper][@stdlib/wasm/module-wrapper] instance which uses the provided WebAssembly [memory][@stdlib/wasm/memory] instance as its underlying memory.
124+
125+
<!-- eslint-disable node/no-sync -->
126+
127+
```javascript
128+
var Memory = require( '@stdlib/wasm/memory' );
129+
130+
// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
131+
var mem = new Memory({
132+
'initial': 10,
133+
'maximum': 100
134+
});
135+
136+
// Create a BLAS routine:
137+
var mod = new dcopy.Module( mem );
138+
// returns <Module>
139+
140+
// Initialize the routine:
141+
mod.initializeSync();
142+
```
143+
144+
#### dcopy.Module.prototype.main( N, xp, sx, yp, sy )
145+
146+
Copies values from `x` into `y`.
147+
148+
<!-- eslint-disable node/no-sync -->
149+
150+
```javascript
151+
var Memory = require( '@stdlib/wasm/memory' );
152+
var oneTo = require( '@stdlib/array/one-to' );
153+
var ones = require( '@stdlib/array/ones' );
154+
var zeros = require( '@stdlib/array/zeros' );
155+
var bytesPerElement = require( '@stdlib/ndarray/base/bytes-per-element' );
156+
157+
// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
158+
var mem = new Memory({
159+
'initial': 10,
160+
'maximum': 100
161+
});
162+
163+
// Create a BLAS routine:
164+
var mod = new dcopy.Module( mem );
165+
// returns <Module>
166+
167+
// Initialize the routine:
168+
mod.initializeSync();
169+
170+
// Define a vector data type:
171+
var dtype = 'float64';
172+
173+
// Specify a vector length:
174+
var N = 5;
175+
176+
// Define pointers (i.e., byte offsets) for storing two vectors:
177+
var xptr = 0;
178+
var yptr = N * bytesPerElement( dtype );
179+
180+
// Write vector values to module memory:
181+
mod.write( xptr, oneTo( N, dtype ) );
182+
mod.write( yptr, ones( N, dtype ) );
183+
184+
// Perform computation:
185+
mod.main( N, xptr, 1, yptr, 1 );
186+
187+
// Read out the results:
188+
var view = zeros( N, dtype );
189+
mod.read( yptr, view );
190+
191+
console.log( view );
192+
// => <Float64Array>[ 1.0, 2.0, 3.0, 4.0, 5.0 ]
193+
```
194+
195+
The function has the following parameters:
196+
197+
- **N**: number of indexed elements.
198+
- **xp**: input [`Float64Array`][@stdlib/array/float64] pointer (i.e., byte offset).
199+
- **sx**: index increment for `x`.
200+
- **yp**: input [`Float64Array`][@stdlib/array/float64] pointer (i.e., byte offset).
201+
- **sy**: index increment for `y`.
202+
203+
#### dcopy.Module.prototype.ndarray( N, xp, sx, ox, yp, sy, oy )
204+
205+
Copies values from `x` into `y` using alternative indexing semantics.
206+
207+
<!-- eslint-disable node/no-sync -->
208+
209+
```javascript
210+
var Memory = require( '@stdlib/wasm/memory' );
211+
var oneTo = require( '@stdlib/array/one-to' );
212+
var ones = require( '@stdlib/array/ones' );
213+
var zeros = require( '@stdlib/array/zeros' );
214+
var bytesPerElement = require( '@stdlib/ndarray/base/bytes-per-element' );
215+
216+
// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
217+
var mem = new Memory({
218+
'initial': 10,
219+
'maximum': 100
220+
});
221+
222+
// Create a BLAS routine:
223+
var mod = new dcopy.Module( mem );
224+
// returns <Module>
225+
226+
// Initialize the routine:
227+
mod.initializeSync();
228+
229+
// Define a vector data type:
230+
var dtype = 'float64';
231+
232+
// Specify a vector length:
233+
var N = 5;
234+
235+
// Define pointers (i.e., byte offsets) for storing two vectors:
236+
var xptr = 0;
237+
var yptr = N * bytesPerElement( dtype );
238+
239+
// Write vector values to module memory:
240+
mod.write( xptr, oneTo( N, dtype ) );
241+
mod.write( yptr, ones( N, dtype ) );
242+
243+
// Perform computation:
244+
mod.ndarray( N, xptr, 1, 0, yptr, 1, 0 );
245+
246+
// Read out the results:
247+
var view = zeros( N, dtype );
248+
mod.read( yptr, view );
249+
250+
console.log( view );
251+
// => <Float64Array>[ 1.0, 2.0, 3.0, 4.0, 5.0 ]
252+
```
253+
254+
The function has the following additional parameters:
255+
256+
- **ox**: starting index for `x`.
257+
- **oy**: starting index for `y`.
258+
259+
</section>
260+
261+
<!-- /.usage -->
262+
263+
<section class="notes">
264+
265+
* * *
266+
267+
## Notes
268+
269+
- If `N <= 0`, `y` is left unchanged.
270+
- This package implements routines using WebAssembly. When provided arrays which are not allocated on a `dcopy` module memory instance, data must be explicitly copied to module memory prior to computation. Data movement may entail a performance cost, and, thus, if you are using arrays external to module memory, you should prefer using [`@stdlib/blas/base/dcopy`][@stdlib/blas/base/dcopy]. However, if working with arrays which are allocated and explicitly managed on module memory, you can achieve better performance when compared to the pure JavaScript implementations found in [`@stdlib/blas/base/dcopy`][@stdlib/blas/base/dcopy]. Beware that such performance gains may come at the cost of additional complexity when having to perform manual memory management. Choosing between implementations depends heavily on the particular needs and constraints of your application, with no one choice universally better than the other.
271+
- `dcopy()` corresponds to the [BLAS][blas] level 1 function [`dcopy`][dcopy].
272+
273+
</section>
274+
275+
<!-- /.notes -->
276+
277+
<section class="examples">
278+
279+
* * *
280+
281+
## Examples
282+
283+
<!-- eslint no-undef: "error" -->
284+
285+
```javascript
286+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
287+
var dcopy = require( '@stdlib/blas/base/dcopy-wasm' );
288+
289+
var opts = {
290+
'dtype': 'float64'
291+
};
292+
var x = discreteUniform( 10, 0, 100, opts );
293+
console.log( x );
294+
295+
var y = discreteUniform( x.length, 0, 10, opts );
296+
console.log( y );
297+
298+
dcopy.ndarray( x.length, x, 1, 0, y, -1, y.length-1 );
299+
console.log( y );
300+
```
301+
302+
</section>
303+
304+
<!-- /.examples -->
305+
306+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
307+
308+
<section class="related">
309+
310+
</section>
311+
312+
<!-- /.related -->
313+
314+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
315+
316+
<section class="links">
317+
318+
[blas]: http://www.netlib.org/blas
319+
320+
[dcopy]: http://www.netlib.org/lapack/explore-html/de/da4/group__double__blas__level1.html
321+
322+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
323+
324+
[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64
325+
326+
[@stdlib/wasm/memory]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/memory
327+
328+
[@stdlib/wasm/module-wrapper]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/module-wrapper
329+
330+
[@stdlib/blas/base/dcopy]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/dcopy
331+
332+
</section>
333+
334+
<!-- /.links -->

0 commit comments

Comments
 (0)