|
| 1 | +import java.util.Random; |
| 2 | +import com.arrayfire.Array; |
| 3 | +import com.arrayfire.Util; |
| 4 | + |
| 5 | +public class HelloWorld { |
| 6 | + public static void main(String[] args) { |
| 7 | + System.out.println("Demonstrating Array Addition using ArrayFire"); |
| 8 | + |
| 9 | + int[] dims; |
| 10 | + int total = 1; |
| 11 | + float[] left, right, res; |
| 12 | + Random rand = new Random(); |
| 13 | + String str; |
| 14 | + |
| 15 | + dims = new int[2]; |
| 16 | + dims[0] = 10; |
| 17 | + dims[1] = 1; |
| 18 | + |
| 19 | + for (int i = 0; i < dims.length; i++) { |
| 20 | + total *= dims[i]; |
| 21 | + } |
| 22 | + |
| 23 | + left = new float[total]; |
| 24 | + right = new float[total]; |
| 25 | + |
| 26 | + for (int i = 0; i < total; i++) { |
| 27 | + left[i] = (float)i; |
| 28 | + double tmp = Math.ceil(rand.nextDouble() * 10) / 10; |
| 29 | + right[i] = (float)(tmp); |
| 30 | + } |
| 31 | + |
| 32 | + try { |
| 33 | + // Send data to ArrayFire |
| 34 | + Array A = new Array(dims, left); |
| 35 | + Array B = new Array(dims, right); |
| 36 | + |
| 37 | + // Create container for output |
| 38 | + Array C = new Array(); |
| 39 | + |
| 40 | + // Do vector addition on the device |
| 41 | + C = Array.add(A, B); |
| 42 | + |
| 43 | + // Get result back to host memory |
| 44 | + res = C.host(); |
| 45 | + |
| 46 | + for(int i = 0; i < total; i++) { |
| 47 | + str = Integer.toString(i) + ". "; |
| 48 | + float val = left[i] + right[i]; |
| 49 | + str = str + Float.toString(left[i] ) + " + "; |
| 50 | + str = str + Float.toString(right[i]) + " = "; |
| 51 | + str = str + Float.toString(res[i]); |
| 52 | + System.out.println(str); |
| 53 | + } |
| 54 | + |
| 55 | + } catch (Exception e) { |
| 56 | + System.out.println("Failed to use ArrayFire"); |
| 57 | + System.out.println(e.getMessage()); |
| 58 | + } |
| 59 | + |
| 60 | + } |
| 61 | +} |
0 commit comments