@@ -17,17 +17,26 @@ public static double hostCalcPi(int size) {
1717 }
1818
1919 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 {
2122
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 );
2426
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 );
2829
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+ }
3140 }
3241
3342 public static void main (String [] args ) {
0 commit comments