-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobject_array.js
46 lines (40 loc) · 1.21 KB
/
object_array.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function ArrayN(data, offset, stride, shape) {
this.data = data;
this.offset = offset;
this.stride = stride;
this.shape = shape;
}
ArrayN.prototype.get = function(x) {
return this.data[x[0] * this.stride[0] + x[1] * this.stride[1] + x[2] * this.stride[2] + this.offset];
};
ArrayN.prototype.set = function(x, v) {
this.data[x[0] * this.stride[0] + x[1] * this.stride[1] + x[2] * this.stride[2] + this.offset] = v;
};
function _initArray(shape) {
var stride = new Array(shape.length);
var size = 1;
for(var i=shape.length-1; i>=0; --i) {
stride[i] = size;
size *= shape[i];
}
return new ArrayN(new Float32Array(size), 0, stride, shape);
}
function initArray(nx, ny, nz) {
return _initArray([nx, ny, nz]);
}
function benchmark(A, B, nx, ny, nz, num_iter) {
var idx = [0,0,0];
for(var count=0; count<num_iter; ++count) {
for(idx[0]=0; idx[0]<nx; ++idx[0]) {
for(idx[1]=0; idx[1]<ny; ++idx[1]) {
for(idx[2]=0; idx[2]<nz; ++idx[2]) {
A.set(idx, A.get(idx) + B.get(idx) + 0.1);
B.set(idx, B.get(idx) - A.get(idx) * 0.5);
}
}
}
}
}
exports.benchmark = benchmark;
exports.initArray = initArray;
exports.prop_name = "object with array accessor";