Skip to content

Commit 5ff457b

Browse files
ShadyBoukharyumar456
authored andcommitted
Fixed MonteCarloExample
1 parent 81289bf commit 5ff457b

File tree

3 files changed

+36
-44
lines changed

3 files changed

+36
-44
lines changed

com/arrayfire/Array.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ public Array(int[] dims, float[] elems) throws Exception {
5050
int[] adims = Array.dim4(dims);
5151

5252
int total_size = 1;
53-
for (int i = 0; i < adims.length; i++)
54-
total_size *= adims[i];
53+
for (int dim : adims) {
54+
total_size *= dim;
55+
}
5556

5657
if (elems == null) {
5758
throw new Exception("Null elems object provided");
@@ -68,8 +69,9 @@ public Array(int[] dims, double[] elems) throws Exception {
6869
int[] adims = Array.dim4(dims);
6970

7071
int total_size = 1;
71-
for (int i = 0; i < adims.length; i++)
72-
total_size *= adims[i];
72+
for (int dim : adims) {
73+
total_size *= dim;
74+
}
7375

7476
if (elems == null) {
7577
throw new Exception("Null elems object provided");
@@ -86,7 +88,9 @@ public Array(int[] dims, int[] elems) throws Exception {
8688
int[] adims = Array.dim4(dims);
8789

8890
int total_size = 1;
89-
for (int dim : adims) { total_size *= dim; }
91+
for (int dim : adims) {
92+
total_size *= dim;
93+
}
9094
if (elems == null) {
9195
throw new Exception("Null elems object provided");
9296
}
@@ -102,8 +106,9 @@ public Array(int[] dims, FloatComplex[] elems) throws Exception {
102106
int[] adims = Array.dim4(dims);
103107

104108
int total_size = 1;
105-
for (int i = 0; i < adims.length; i++)
106-
total_size *= adims[i];
109+
for (int dim : adims) {
110+
total_size *= dim;
111+
}
107112

108113
if (elems == null) {
109114
throw new Exception("Null elems object provided");
@@ -120,8 +125,9 @@ public Array(int[] dims, DoubleComplex[] elems) throws Exception {
120125
int[] adims = Array.dim4(dims);
121126

122127
int total_size = 1;
123-
for (int i = 0; i < adims.length; i++)
124-
total_size *= adims[i];
128+
for (int dim : adims) {
129+
total_size *= dim;
130+
}
125131

126132
if (elems == null) {
127133
throw new Exception("Null elems object provided");

com/arrayfire/Data.java

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public class Data extends ArrayFire {
2222

2323
private native static long createIdentityArray(int[] dims, int type);
2424

25-
2625
public static float[] getFloatArray(Array A) throws Exception {
2726
A.assertType(Array.FloatType);
2827
return getFloatFromArray(A.ref);
@@ -53,44 +52,29 @@ public static boolean[] getBooleanArray(Array A) throws Exception {
5352
return getBooleanFromArray(A.ref);
5453
}
5554

56-
// Binary operations
57-
public static Array randu(int[] dims, int type) throws Exception {
58-
int[] adims = Array.dim4(dims);
59-
long ref = createRanduArray(adims, type);
60-
if (ref == 0)
61-
throw new Exception("Failed to create Array");
62-
return new Array(ref);
55+
// Binary operations
56+
public static void randu(Array res, int[] dims, int type) throws Exception {
57+
int[] adims = Array.dim4(dims);
58+
res.set(createRanduArray(adims, type));
6359
}
6460

65-
public static Array randn(int[] dims, int type) throws Exception {
66-
int[] adims = Array.dim4(dims);
67-
long ref = createRandnArray(adims, type);
68-
if (ref == 0)
69-
throw new Exception("Failed to create Array");
70-
return new Array(ref);
61+
public static void randn(Array res, int[] dims, int type) throws Exception {
62+
int[] adims = Array.dim4(dims);
63+
res.set(createRandnArray(adims, type));
7164
}
7265

73-
public static Array constant(double val, int[] dims, int type) throws Exception {
74-
int[] adims = Array.dim4(dims);
75-
long ref = createConstantsArray(val, adims, type);
76-
if (ref == 0)
77-
throw new Exception("Failed to create Array");
78-
return new Array(ref);
66+
public static void constant(Array res, double val, int[] dims, int type) throws Exception {
67+
int[] adims = Array.dim4(dims);
68+
res.set(createConstantsArray(val, adims, type));
7969
}
8070

81-
public static Array identity(int[] dims, int type) throws Exception {
82-
int[] adims = Array.dim4(dims);
83-
long ref = createIdentityArray(adims, type);
84-
if (ref == 0) {
85-
throw new Exception("Failed to create Array");
86-
}
87-
return new Array(ref);
71+
public static void identity(Array res, int[] dims, int type) throws Exception {
72+
int[] adims = Array.dim4(dims);
73+
res.set(createIdentityArray(adims, type));
8874
}
8975

90-
public static Array identity(int[] dims) throws Exception {
91-
int[] adims = Array.dim4(dims);
92-
long ref = createIdentityArray(adims, Array.FloatType);
93-
return new Array(ref);
76+
public static void identity(Array res, int[] dims) throws Exception {
77+
identity(res, dims, Array.FloatType);
9478
}
9579

9680
}

examples/MonteCarloPi.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ public static double hostCalcPi(int size) {
1818
}
1919

2020
public static double deviceCalcPi(int size) throws Exception {
21-
Array x = null, y = null, res = new Array();
21+
Array x = new Array(), y = new Array(), res = new Array();
2222
try {
2323

2424
int[] dims = new int[] { size, 1 };
25-
x = Data.randu(dims, Array.FloatType);
26-
y = Data.randu(dims, Array.FloatType);
25+
Data.randu(x, dims, Array.FloatType);
26+
Data.randu(y, dims, Array.FloatType);
2727

2828
Arith.mul(x, x, x);
2929
Arith.mul(y, y, y);
@@ -34,6 +34,8 @@ public static double deviceCalcPi(int size) throws Exception {
3434
double count = Algorithm.sumAll(res);
3535
return 4.0 * ((double) (count)) / size;
3636

37+
} catch (Exception e) {
38+
throw e;
3739
} finally {
3840
x.close();
3941
y.close();
@@ -70,7 +72,7 @@ public static void main(String[] args) {
7072
System.out.println("Speedup: " + Math.round((hostElapsed) / (deviceElapsed)));
7173

7274
} catch (Exception e) {
73-
System.out.println(e.getMessage());
75+
e.printStackTrace();
7476
}
7577
}
7678
}

0 commit comments

Comments
 (0)