@@ -17,17 +17,26 @@ public static double hostCalcPi(int size) {
17
17
}
18
18
19
19
public static double deviceCalcPi (int size ) throws Exception {
20
- int [] dims = new int [] {size , 1 };
20
+ Array x = null , y = null , res = null ;
21
+ try {
21
22
22
- Array x = Array .randu (dims , Array .FloatType );
23
- Array y = Array .randu (dims , Array .FloatType );
23
+ int [] dims = new int [] {size , 1 };
24
+ x = Array .randu (dims , Array .FloatType );
25
+ y = Array .randu (dims , Array .FloatType );
24
26
25
- Array x2 = Array .mul (x , x );
26
- Array y2 = Array .mul (y , y );
27
- Array res = Array .lt (Array .add (x2 , y2 ), 1 );
27
+ x = Array .mul (x , x );
28
+ y = Array .mul (y , y );
28
29
29
- double count = Array .sumAll (res );
30
- return 4.0 * ((double )(count )) / size ;
30
+ res = Array .add (x , y );
31
+ res = Array .lt (res , 1 );
32
+ double count = Array .sumAll (res );
33
+ return 4.0 * ((double )(count )) / size ;
34
+
35
+ } finally {
36
+ if (x != null ) x .close ();
37
+ if (y != null ) y .close ();
38
+ if (res != null ) res .close ();
39
+ }
31
40
}
32
41
33
42
public static void main (String [] args ) {
0 commit comments