import java.util.Random;
import com.arrayfire.Array;

public class MonteCarloPi {

    public static double hostCalcPi(int size) {
        Random rand = new Random();
        int count = 0;
        for (int i = 0; i < size; i++) {
            float x = rand.nextFloat();
            float y = rand.nextFloat();
            boolean lt1 = (x * x + y * y) < 1;
            if (lt1) count++;
        }

        return 4.0 * ((double)(count)) / size;
    }

    public static double deviceCalcPi(int size) throws Exception {
        int[] dims =  new int[] {size, 1};

        Array x = Array.randu(dims, Array.FloatType);
        Array y = Array.randu(dims, Array.FloatType);

        Array x2 = Array.mul(x, x);
        Array y2 = Array.mul(y, y);
        Array res = Array.lt(Array.add(x2, y2), 1);

        double count = Array.sumAll(res);
        return 4.0 * ((double)(count)) / size;
    }

    public static void main(String[] args) {

        try {
            int size = 5000000;
            int iter = 100;
            double hostPi = hostCalcPi(size);
            double devicePi = deviceCalcPi(size);

            System.out.println("Results from host: " + hostPi);
            System.out.println("Results from device: " + devicePi);

            long hostStart = System.currentTimeMillis();
            for (int i = 0; i < iter; i++) {
                hostPi = hostCalcPi(size);
            }
            double hostElapsed = (double)(System.currentTimeMillis() - hostStart)/iter;

            long deviceStart = System.currentTimeMillis();
            for (int i = 0; i < iter; i++) {
                devicePi = deviceCalcPi(size);
            }
            double deviceElapsed = (double)(System.currentTimeMillis() - deviceStart)/iter;

            System.out.println("Time taken for host (ms): " + hostElapsed);
            System.out.println("Time taken for device (ms): " + deviceElapsed);
            System.out.println("Speedup: " + Math.round((hostElapsed) / (deviceElapsed)));

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

@@ -513,16 +513,81 @@
 ption %7B%0A
+        Array x = null, y = null, res = null;%0A        try %7B%0A%0A    
         
@@ -621,25 +621,27 @@
 ze, 1%7D;%0A
-%0A
+   
         
 Array x 
@@ -628,29 +628,24 @@
 %0A           
-Array
  x = Array.r
@@ -681,21 +681,19 @@
         
-Array
+   
  y = Arr
@@ -738,16 +738,13 @@
     
-Array
+   
  x
-2
  = A
@@ -771,16 +771,13 @@
     
-Array
+   
  y
-2
  = A
@@ -792,24 +792,28 @@
 (y, y);%0A
+%0A   
         
 Array re
@@ -808,29 +808,15 @@
     
-Array
  res = 
-Array.lt(
 Arra
@@ -826,21 +826,55 @@
 dd(x
-2
+ 
 , y
-2
 )
+;%0A            res = Array.lt(res
 , 1);%0A
-%0A
+    
     
@@ -911,32 +911,36 @@
 l(res);%0A        
+    
 return 4.0 * ((d
@@ -953,32 +953,181 @@
 (count)) / size;
+%0A%0A        %7D finally %7B%0A            if (x != null) x.close();%0A            if (y != null) y.close();%0A            if (res != null) res.close();%0A        %7D
 %0A    %7D%0A%0A    publ

Use try / finally to clean up memory