Skip to content

Commit 560897f

Browse files
committed
create array methods deasyncifyed
1 parent ed32d85 commit 560897f

File tree

5 files changed

+91
-87
lines changed

5 files changed

+91
-87
lines changed

documentation/docs/create.md

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@
1919

2020
## randu()
2121

22-
asynchronous, counterparts: `randuAsync()`, `randuSync()`
23-
2422
Create a random array sampled from uniform distribution.
2523

26-
- `randu(dim0, type, callback)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__randu.htm#gab3e2105aec551cb0bbcf104d437c0481)
27-
- `randu(dim0, dim1, type, callback)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__randu.htm#ga9a08025609da7db72c3a22493f85a171)
28-
- `randu(dim0, dim1, dim2, type, callback)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__randu.htm#gae0d335466e5f5a1cb821dc241804923b)
29-
- `randu(dim0, dim1, dim2, dim3, type, callback)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__randu.htm#gac05c5b2de1cfc2d763b7d2943e9deee3)
30-
- `randu(dims, type, callback)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__randu.htm#ga15a5110a447509cab9589b2ad56c5e55)
24+
- `randu(dim0, type)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__randu.htm#gab3e2105aec551cb0bbcf104d437c0481)
25+
- `randu(dim0, dim1, type)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__randu.htm#ga9a08025609da7db72c3a22493f85a171)
26+
- `randu(dim0, dim1, dim2, type)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__randu.htm#gae0d335466e5f5a1cb821dc241804923b)
27+
- `randu(dim0, dim1, dim2, dim3, type)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__randu.htm#gac05c5b2de1cfc2d763b7d2943e9deee3)
28+
- `randu(dims, type)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__randu.htm#ga15a5110a447509cab9589b2ad56c5e55)
3129

3230
**Arguments:**
3331

@@ -43,15 +41,13 @@ alias of [randu()](#randu)
4341

4442
## randn()
4543

46-
asynchronous, counterparts: `randnAsync()`, `randnSync()`
47-
4844
Create a random array sampled from normal distribution.
4945

50-
- `randn(dim0, type, callback)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__randn.htm#ga5d7b55d3d0f34d71f30a70ed7a2d928d)
51-
- `randn(dim0, dim1, type, callback)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__randn.htm#ga5fe422f8cf2acc3c6f782f9148360f6c)
52-
- `randn(dim0, dim1, dim2, type, callback)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__randn.htm#ga150fb95ddda0e37e8961254ca7afc8e5)
53-
- `randn(dim0, dim1, dim2, dim3, type, callback)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__randn.htm#ga9bc154f0bf07116ad208b2da4b71c3d8)
54-
- `randn(dims, type, callback)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__randn.htm#gae8aa1f5ec310aeb9e4cbd19d63998349)
46+
- `randn(dim0, type)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__randn.htm#ga5d7b55d3d0f34d71f30a70ed7a2d928d)
47+
- `randn(dim0, dim1, type)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__randn.htm#ga5fe422f8cf2acc3c6f782f9148360f6c)
48+
- `randn(dim0, dim1, dim2, type)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__randn.htm#ga150fb95ddda0e37e8961254ca7afc8e5)
49+
- `randn(dim0, dim1, dim2, dim3, type)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__randn.htm#ga9bc154f0bf07116ad208b2da4b71c3d8)
50+
- `randn(dims, type)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__randn.htm#gae8aa1f5ec310aeb9e4cbd19d63998349)
5551

5652
**Arguments:**
5753

@@ -67,15 +63,13 @@ alias of [randn()](#randn)
6763

6864
## identity()
6965

70-
asynchronous, counterparts: `identityAsync()`, `identitySync()`
71-
7266
Create an identity matrix (array with diagonal values 1).
7367

74-
- `identity(dim0, type, callback)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__identity.htm#ga3f69c0a0ab7ba0c8c1ee3223772234b8)
75-
- `identity(dim0, dim1, type, callback)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__identity.htm#gaff9da6e4b83772359d748a4df66ec571)
76-
- `identity(dim0, dim1, dim2, type, callback)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__identity.htm#ga405b89c8f04901d58287fd6a8f5612c3)
77-
- `identity(dim0, dim1, dim2, dim3, type, callback)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__identity.htm#ga8e6605d76748c5fff365830e1a153132)
78-
- `identity(dims, type, callback)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__identity.htm#gafd8247e22fdb50218926d5d9391fa678)
68+
- `identity(dim0, type)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__identity.htm#ga3f69c0a0ab7ba0c8c1ee3223772234b8)
69+
- `identity(dim0, dim1, type)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__identity.htm#gaff9da6e4b83772359d748a4df66ec571)
70+
- `identity(dim0, dim1, dim2, type)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__identity.htm#ga405b89c8f04901d58287fd6a8f5612c3)
71+
- `identity(dim0, dim1, dim2, dim3, type)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__identity.htm#ga8e6605d76748c5fff365830e1a153132)
72+
- `identity(dims, type)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__identity.htm#gafd8247e22fdb50218926d5d9391fa678)
7973

8074
**Arguments:**
8175

@@ -87,15 +81,13 @@ Create an identity matrix (array with diagonal values 1).
8781

8882
## range()
8983

90-
asynchronous, counterparts: `rangeAsync()`, `rangeSync()`
91-
9284
Creates an array with [0, n] values along the seqDim which is tiled across other dimensions.
9385

94-
- `range(dim0, seqDim, type, callback)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__range.htm#ga1e2ae1f90d99f42854a19877261ac455)
95-
- `range(dim0, dim1, seqDim, type, callback)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__range.htm#ga1e2ae1f90d99f42854a19877261ac455)
96-
- `range(dim0, dim1, dim2, seqDim, type, callback)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__range.htm#ga1e2ae1f90d99f42854a19877261ac455)
97-
- `range(dim0, dim1, dim2, dim3, seqDim, type, callback)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__range.htm#ga1e2ae1f90d99f42854a19877261ac455)
98-
- `range(dims, seqDim, type, callback)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__range.htm#ga3789475e962b4c31e07c1c3bdab8498b)
86+
- `range(dim0, seqDim, type)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__range.htm#ga1e2ae1f90d99f42854a19877261ac455)
87+
- `range(dim0, dim1, seqDim, type)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__range.htm#ga1e2ae1f90d99f42854a19877261ac455)
88+
- `range(dim0, dim1, dim2, seqDim, type)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__range.htm#ga1e2ae1f90d99f42854a19877261ac455)
89+
- `range(dim0, dim1, dim2, dim3, seqDim, type)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__range.htm#ga1e2ae1f90d99f42854a19877261ac455)
90+
- `range(dims, seqDim, type)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__range.htm#ga3789475e962b4c31e07c1c3bdab8498b)
9991

10092
**Arguments:**
10193

@@ -108,11 +100,9 @@ Creates an array with [0, n] values along the seqDim which is tiled across other
108100

109101
## iota()
110102

111-
asynchronous, counterparts: `iotaAsync()`, `iotaSync()`
112-
113103
Create an sequence [0, dims.elements - 1] and modify to specified dimensions dims and then tile it according to tileDims.
114104

115-
- `iota(dims, tileDims, type, callback)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__iota.htm#ga8f7c55a54d1f93e55340f59b61662f29)
105+
- `iota(dims, tileDims, type)`: [-> ArrayFire Documentation](http://www.arrayfire.com/docs/group__data__func__iota.htm#ga8f7c55a54d1f93e55340f59b61662f29)
116106

117107
**Arguments:**
118108

@@ -124,8 +114,6 @@ Create an sequence [0, dims.elements - 1] and modify to specified dimensions dim
124114

125115
## diag()
126116

127-
asynchronous, counterparts: `diagAsync()`, `diagSync()`
128-
129117
Extract diagonal from a matrix when `extract` is set to `true`.
130118

131119
Create a diagonal marix from input array when `extract` is set to `false`.
@@ -140,15 +128,13 @@ Create a diagonal marix from input array when `extract` is set to `false`.
140128

141129
## constant()
142130

143-
asynchronous, counterparts: `constantAsync()`, `constantSync()`
144-
145131
Create an array filled with the specified value.
146132

147-
- `constant(value, dim0, type, callback)`
148-
- `constant(value, dim0, dim1, type, callback)`
149-
- `constant(value, dim0, dim1, dim2, type, callback)`
150-
- `constant(value, dim0, dim1, dim2, dim3, type, callback)`
151-
- `constant(value, dims, type, callback)`
133+
- `constant(value, dim0, type)`
134+
- `constant(value, dim0, dim1, type)`
135+
- `constant(value, dim0, dim1, dim2, type)`
136+
- `constant(value, dim0, dim1, dim2, dim3, type)`
137+
- `constant(value, dims, type)`
152138

153139
**Arguments:**
154140

examples/es6/bechmarks/pi.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ let pi = async(function*(fire, deviceInfo) {
1414
common.printDeviceInfo(deviceInfo);
1515
console.log("");
1616

17-
let x = yield fire.randuAsync(numberOfPoints, fire.types.dtype.f32);
18-
let y = yield fire.randuAsync(numberOfPoints, fire.types.dtype.f32);
17+
let x = fire.randu(numberOfPoints, fire.types.dtype.f32);
18+
let y = fire.randu(numberOfPoints, fire.types.dtype.f32);
1919
let dist = yield fire.sqrtAsync((x.mul(x)).add(y.mul(y)));
20-
let num_inside = yield fire.sumAsync(dist.lt(1));
21-
let piVal = (4.0 * num_inside) / numberOfPoints;
20+
let numInside = yield fire.sumAsync(dist.lt(1));
21+
let piVal = (4.0 * numInside) / numberOfPoints;
2222

2323
console.log(`PI = ${piVal}`);
2424
});

src/createarray.cpp

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ NAN_METHOD(RandU)
3333

3434
try
3535
{
36+
ARGS_LEN(2);
3637
auto dimAndType = ParseDimAndTypeArgs(args);
37-
return ArrayWrapper::NewAsync(args, [=]() { Guard(); return new af::array(move(af::randu(dimAndType.first, dimAndType.second))); });
38+
Guard();
39+
NanReturnValue(ArrayWrapper::New(af::randu(dimAndType.first, dimAndType.second)));
3840
}
3941
FIRE_CATCH
4042
}
@@ -45,10 +47,13 @@ NAN_METHOD(RandN)
4547

4648
try
4749
{
50+
ARGS_LEN(2);
4851
auto dimAndType = ParseDimAndTypeArgs(args);
4952
if (dimAndType.second == f32 || dimAndType.second == f64)
5053
{
51-
return ArrayWrapper::NewAsync(args, [=]() { Guard(); return new af::array(move(af::randn(dimAndType.first, dimAndType.second))); });
54+
Guard();
55+
NanReturnValue(ArrayWrapper::New(af::randn(dimAndType.first, dimAndType.second)));
56+
return;
5257
}
5358
return NAN_THROW_INVALID_DTYPE();
5459
}
@@ -61,8 +66,10 @@ NAN_METHOD(Identity)
6166

6267
try
6368
{
69+
ARGS_LEN(2);
6470
auto dimAndType = ParseDimAndTypeArgs(args);
65-
return ArrayWrapper::NewAsync(args, [=]() { Guard(); return new af::array(move(af::identity(dimAndType.first, dimAndType.second))); });
71+
Guard();
72+
NanReturnValue(ArrayWrapper::New(af::identity(dimAndType.first, dimAndType.second)));
6673
}
6774
FIRE_CATCH
6875
}
@@ -73,10 +80,11 @@ NAN_METHOD(Range)
7380

7481
try
7582
{
76-
ARGS_LEN(4);
83+
ARGS_LEN(3);
7784
auto dimAndType = ParseDimAndTypeArgs(args, -1, 1);
78-
af_dtype seqDim = (af_dtype)args[args.Length() - 3]->Uint32Value();
79-
return ArrayWrapper::NewAsync(args, [=]() { Guard(); return new af::array(move(af::range(dimAndType.first, seqDim, dimAndType.second))); });
85+
af_dtype seqDim = (af_dtype)args[args.Length() - 2]->Uint32Value();
86+
Guard();
87+
NanReturnValue(ArrayWrapper::New(af::range(dimAndType.first, seqDim, dimAndType.second)));
8088
}
8189
FIRE_CATCH
8290
}
@@ -87,11 +95,12 @@ NAN_METHOD(Iota)
8795

8896
try
8997
{
90-
ARGS_LEN(4);
98+
ARGS_LEN(3);
9199
auto dims = ToDim4(args[0]);
92100
auto titleDims = ToDim4(args[1]);
93101
auto type = GetDTypeInfo(args[3]->Uint32Value());
94-
return ArrayWrapper::NewAsync(args, [=]() { Guard(); return new af::array(move(af::iota(dims, titleDims, type.first))); });
102+
Guard();
103+
NanReturnValue(ArrayWrapper::New(af::iota(dims, titleDims, type.first)));
95104
}
96105
FIRE_CATCH
97106
}
@@ -114,8 +123,8 @@ NAN_METHOD(Diag)
114123
{
115124
extract = args[2]->BooleanValue();
116125
}
117-
af::array array(*pArray);
118-
return ArrayWrapper::NewAsync(args, [=]() { Guard(); return new af::array(move(af::diag(array, num, extract))); });
126+
Guard();
127+
NanReturnValue(ArrayWrapper::New(af::diag(*pArray, num, extract)));
119128
}
120129
FIRE_CATCH
121130
}
@@ -131,23 +140,30 @@ NAN_METHOD(Constant)
131140
ARGS_LEN(3);
132141
auto dimAndType = ParseDimAndTypeArgs(args, -1, 0, 1);
133142
auto value = args[0];
143+
Guard();
134144
if (value->IsNumber())
135145
{
136146
double v = value->NumberValue();
137147
switch (dimAndType.second)
138148
{
139149
case f32:
140-
return ArrayWrapper::NewAsync(args, [=]() { Guard(); return new af::array(move(af::constant<float>(v, dimAndType.first, dimAndType.second))); });
150+
NanReturnValue(ArrayWrapper::New(af::constant<float>(v, dimAndType.first, dimAndType.second)));
151+
return;
141152
case f64:
142-
return ArrayWrapper::NewAsync(args, [=]() { Guard(); return new af::array(move(af::constant<double>(v, dimAndType.first, dimAndType.second))); });
153+
NanReturnValue(ArrayWrapper::New(af::constant<double>(v, dimAndType.first, dimAndType.second)));
154+
return;
143155
case s32:
144-
return ArrayWrapper::NewAsync(args, [=]() { Guard(); return new af::array(move(af::constant<int>(v, dimAndType.first, dimAndType.second))); });
156+
NanReturnValue(ArrayWrapper::New(af::constant<int>(v, dimAndType.first, dimAndType.second)));
157+
return;
145158
case u32:
146-
return ArrayWrapper::NewAsync(args, [=]() { Guard(); return new af::array(move(af::constant<unsigned>(v, dimAndType.first, dimAndType.second))); });
159+
NanReturnValue(ArrayWrapper::New(af::constant<unsigned>(v, dimAndType.first, dimAndType.second)));
160+
return;
147161
case u8:
148-
return ArrayWrapper::NewAsync(args, [=]() { Guard(); return new af::array(move(af::constant<unsigned char>(v, dimAndType.first, dimAndType.second))); });
162+
NanReturnValue(ArrayWrapper::New(af::constant<unsigned char>(v, dimAndType.first, dimAndType.second)));
163+
return;
149164
case b8:
150-
return ArrayWrapper::NewAsync(args, [=]() { Guard(); return new af::array(move(af::constant<char>(v, dimAndType.first, dimAndType.second))); });
165+
NanReturnValue(ArrayWrapper::New(af::constant<char>(v, dimAndType.first, dimAndType.second)));
166+
return;
151167
default:
152168
break;
153169
}
@@ -159,12 +175,14 @@ NAN_METHOD(Constant)
159175
case c32:
160176
{
161177
auto cv = ToFComplex(value.As<Object>());
162-
return ArrayWrapper::NewAsync(args, [=]() { Guard(); return new af::array(move(af::constant<af::cfloat>(cv, dimAndType.first, dimAndType.second))); });
178+
NanReturnValue(ArrayWrapper::New(af::constant<af::cfloat>(cv, dimAndType.first, dimAndType.second)));
179+
return;
163180
}
164181
case c64:
165182
{
166183
auto cv = ToDComplex(value.As<Object>());
167-
return ArrayWrapper::NewAsync(args, [=]() { Guard(); return new af::array(move(af::constant<af::cdouble>(cv, dimAndType.first, dimAndType.second))); });
184+
NanReturnValue(ArrayWrapper::New(af::constant<af::cdouble>(cv, dimAndType.first, dimAndType.second)));
185+
return;
168186
}
169187
default:
170188
break;
@@ -178,22 +196,24 @@ NAN_METHOD(Constant)
178196
case s64:
179197
{
180198
long long val = strtoll(*str, nullptr, 10);
181-
return ArrayWrapper::NewAsync(args, [=]() { Guard(); return new af::array(move(af::constant<long long>(val, dimAndType.first, dimAndType.second))); });
199+
NanReturnValue(ArrayWrapper::New(af::constant<long long>(val, dimAndType.first, dimAndType.second)));
200+
return;
182201
}
183202
case u64:
184203
{
185204
unsigned long long val = strtoll(*str, nullptr, 10);
186-
return ArrayWrapper::NewAsync(args, [=]() { Guard(); return new af::array(move(af::constant<unsigned long long>(val, dimAndType.first, dimAndType.second))); });
205+
NanReturnValue(ArrayWrapper::New(af::constant<unsigned long long>(val, dimAndType.first, dimAndType.second)));
206+
return;
187207
}
188208
default:
189209
break;
190210
}
191211
}
192212
else
193213
{
194-
throw new logic_error("Argument at position 0 is not a constant.");
214+
FIRE_THROW("Argument at position 0 is not a constant.");
195215
}
196-
throw new logic_error("Type is unknown.");
216+
FIRE_THROW("Type is unknown.");
197217
}
198218
FIRE_CATCH
199219
}

src/helpers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ NAN_METHOD(F)\
253253
ARGS_LEN(2);\
254254
\
255255
auto array = *ArrayWrapper::GetArrayAt(args, 0);\
256-
if (af::isDoubleAvailable(af::getDevice()))\
256+
if (NeedsDouble(array))\
257257
{\
258258
typedef std::pair<double, unsigned> PairT;\
259259
typedef Worker<PairT> WorkerT;\

0 commit comments

Comments
 (0)