Skip to content

Commit be28c74

Browse files
committed
feat: add wasm/base/arrays2ptrs
1 parent 6065197 commit be28c74

File tree

12 files changed

+1344
-0
lines changed

12 files changed

+1344
-0
lines changed
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
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+
# arrays2ptrs
22+
23+
> Convert a list of arrays to "pointers" (i.e., byte offsets) in WebAssembly [module memory][@stdlib/wasm/memory].
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var arrays2ptrs = require( '@stdlib/wasm/base/arrays2ptrs' );
41+
```
42+
43+
#### arrays2ptrs( ctx, arrays )
44+
45+
Converts a list of arrays to "pointers" (i.e., byte offsets) in WebAssembly [module memory][@stdlib/wasm/memory].
46+
47+
```javascript
48+
var defineProperty = require( '@stdlib/utils/define-property' );
49+
var ArrayBuffer = require( '@stdlib/array/buffer' );
50+
var DataView = require( '@stdlib/array/dataview' );
51+
52+
var buf = new ArrayBuffer( 64*1024 ); // 64KiB
53+
54+
function isView( arr ) {
55+
return ( arr.buffer === buf );
56+
}
57+
58+
function realloc( nbytes ) {
59+
buf = new ArrayBuffer( nbytes );
60+
}
61+
62+
function view() {
63+
return new DataView( buf );
64+
}
65+
66+
var ctx = {
67+
'isView': isView,
68+
'realloc': realloc
69+
};
70+
71+
defineProperty( ctx, 'view', {
72+
'configurable': false,
73+
'enumerable': true,
74+
'get': view
75+
});
76+
77+
// ...
78+
79+
var xobj = {
80+
'dtype': 'generic',
81+
'wdtype': 'float64',
82+
'length': 2,
83+
'data': [ 1.0, 2.0 ],
84+
'stride': 1,
85+
'offset': 0
86+
};
87+
var yobj = {
88+
'dtype': 'generic',
89+
'wdtype': 'float64',
90+
'length': 2,
91+
'data': [ 3.0, 4.0 ],
92+
'stride': 1,
93+
'offset': 0
94+
};
95+
96+
// ...
97+
98+
var ptrs = arrays2ptrs( ctx, [ xobj, yobj ] );
99+
// returns [...]
100+
```
101+
102+
Each element in the list of input arrays should have the following properties:
103+
104+
- **dtype**: array [data type][@stdlib/array/dtypes].
105+
- **wdtype**: WebAssembly [array data type][@stdlib/wasm/base/array2dtype].
106+
- **length**: number of indexed elements.
107+
- **data**: original array-like object.
108+
- **stride**: index increment.
109+
- **offset**: index offset.
110+
111+
In addition to each element's existing properties, each element of the returned array has the following additional properties:
112+
113+
- **BYTES_PER_ELEMENT**: number of bytes per element.
114+
- **ptr**: byte offset.
115+
- **nbytes**: number of bytes consumed by **indexed** array elements as stored in module memory.
116+
- **copy**: boolean indicating whether an array had to be copied to module memory.
117+
118+
</section>
119+
120+
<!-- /.usage -->
121+
122+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
123+
124+
<section class="notes">
125+
126+
## Notes
127+
128+
- Beware that this function may reallocate module memory, resulting in [`ArrayBuffer`][@stdlib/array/buffer] detachment and the invalidation of any typed array views which were views of the previously allocated memory. Additionally, this function may write to module memory and does so without regard for any existing memory content. Users are thus encouraged to take suitable precautions (e.g., copying results out of module memory prior to subsequent invocation) in order to avoid unexpected results.
129+
- If an array's data is copied to module memory, the data is copied to a contiguous segment of module memory, and the respective array object in the returned array will have unit stride and an offset of zero.
130+
131+
</section>
132+
133+
<!-- /.notes -->
134+
135+
<!-- Package usage examples. -->
136+
137+
<section class="examples">
138+
139+
## Examples
140+
141+
<!-- eslint-disable no-restricted-syntax, no-invalid-this -->
142+
143+
<!-- eslint no-undef: "error" -->
144+
145+
```javascript
146+
var setReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' );
147+
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
148+
var ArrayBuffer = require( '@stdlib/array/buffer' );
149+
var DataView = require( '@stdlib/array/dataview' );
150+
var Float64Array = require( '@stdlib/array/float64' );
151+
var dtype2wasm = require( '@stdlib/wasm/base/dtype2wasm' );
152+
var arrays2ptrs = require( '@stdlib/wasm/base/arrays2ptrs' );
153+
154+
function Context() {
155+
this._buffer = new ArrayBuffer( 100 );
156+
return this;
157+
}
158+
159+
setReadOnly( Context.prototype, 'isView', function isView( arr ) {
160+
return ( arr.buffer ) ? ( arr.buffer === this._buffer ) : false;
161+
});
162+
163+
setReadOnly( Context.prototype, 'realloc', function realloc( nbytes ) {
164+
this._buffer = new ArrayBuffer( nbytes );
165+
});
166+
167+
setReadOnlyAccessor( Context.prototype, 'view', function getter() {
168+
return new DataView( this._buffer );
169+
});
170+
171+
// ...
172+
173+
var ctx = new Context();
174+
175+
// ...
176+
177+
var x = new Float64Array( 4 );
178+
var y = new Float64Array( 4 );
179+
180+
// ...
181+
182+
var xobj = {
183+
'dtype': 'float64',
184+
'wdtype': dtype2wasm( 'float64' ),
185+
'length': x.length,
186+
'data': x,
187+
'stride': 1,
188+
'offset': 0
189+
};
190+
191+
var yobj = {
192+
'dtype': 'float64',
193+
'wdtype': dtype2wasm( 'float64' ),
194+
'length': y.length,
195+
'data': y,
196+
'stride': 1,
197+
'offset': 0
198+
};
199+
200+
var out = arrays2ptrs( ctx, [ xobj, yobj ] );
201+
// returns [...]
202+
203+
console.log( out );
204+
```
205+
206+
</section>
207+
208+
<!-- /.examples -->
209+
210+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
211+
212+
<section class="references">
213+
214+
</section>
215+
216+
<!-- /.references -->
217+
218+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
219+
220+
<section class="related">
221+
222+
</section>
223+
224+
<!-- /.related -->
225+
226+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
227+
228+
<section class="links">
229+
230+
[@stdlib/wasm/memory]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/memory
231+
232+
[@stdlib/wasm/base/array2dtype]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/base/array2dtype
233+
234+
[@stdlib/array/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/dtypes
235+
236+
[@stdlib/array/buffer]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/buffer
237+
238+
</section>
239+
240+
<!-- /.links -->
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var zeros = require( '@stdlib/array/zeros' );
25+
var isArray = require( '@stdlib/assert/is-array' );
26+
var pkg = require( './../package.json' ).name;
27+
var arrays2ptrs = require( './../lib' );
28+
29+
30+
// FIXTURES //
31+
32+
var Context = require( './fixtures/context.js' );
33+
34+
35+
// MAIN //
36+
37+
bench( pkg+'::copy:num_arrays=2,len=10', function benchmark( b ) {
38+
var arrays;
39+
var ctx;
40+
var out;
41+
var i;
42+
43+
ctx = new Context();
44+
45+
arrays = [];
46+
for ( i = 0; i < 2; i++ ) {
47+
arrays.push({
48+
'dtype': 'float64',
49+
'wdtype': 'float64',
50+
'length': 10,
51+
'data': zeros( 10, 'float64' ),
52+
'stride': 1,
53+
'offset': 0
54+
});
55+
}
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
out = arrays2ptrs( ctx, arrays );
59+
if ( typeof out !== 'object' ) {
60+
b.fail( 'should return an array' );
61+
}
62+
}
63+
b.toc();
64+
if ( !isArray( out ) ) {
65+
b.fail( 'should return an array' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
/* eslint-disable no-invalid-this */
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var setReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' );
26+
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
27+
var ArrayBuffer = require( '@stdlib/array/buffer' );
28+
var DataView = require( '@stdlib/array/dataview' );
29+
30+
31+
// MAIN //
32+
33+
/**
34+
* Mock context constructor.
35+
*
36+
* @private
37+
* @constructor
38+
* @returns {Context} context instance
39+
*/
40+
function Context() {
41+
this._buffer = new ArrayBuffer( 100 );
42+
return this;
43+
}
44+
45+
/**
46+
* Returns a boolean indicating whether a provided array is a view on the context `ArrayBuffer`.
47+
*
48+
* @private
49+
* @name isView
50+
* @memberof Context.prototype
51+
* @type {Function}
52+
* @param {Collection} arr - input array
53+
* @returns {boolean} boolean indicating whether a provided array is a view
54+
*/
55+
setReadOnly( Context.prototype, 'isView', function isView( arr ) {
56+
return ( arr.buffer ) ? ( arr.buffer === this._buffer ) : false;
57+
});
58+
59+
/**
60+
* Reallocates context memory.
61+
*
62+
* @private
63+
* @name realloc
64+
* @memberof Context.prototype
65+
* @type {Function}
66+
* @param {NonNegativeInteger} nbytes - number of bytes
67+
*/
68+
setReadOnly( Context.prototype, 'realloc', function realloc( nbytes ) {
69+
this._buffer = new ArrayBuffer( nbytes );
70+
});
71+
72+
/**
73+
* Returns a `DataView` of context memory.
74+
*
75+
* @private
76+
* @name view
77+
* @memberof Context.prototype
78+
* @type {DataView}
79+
*/
80+
setReadOnlyAccessor( Context.prototype, 'view', function getter() {
81+
return new DataView( this._buffer );
82+
});
83+
84+
85+
// EXPORTS //
86+
87+
module.exports = Context;

0 commit comments

Comments
 (0)