forked from arrayfire/arrayfire-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloWorld.java
More file actions
64 lines (50 loc) · 1.74 KB
/
HelloWorld.java
File metadata and controls
64 lines (50 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.Random;
import com.arrayfire.Array;
import com.arrayfire.Util;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Demonstrating Array Addition using ArrayFire");
int[] dims;
int total = 1;
float[] left, right, res;
Random rand = new Random();
String str;
dims = new int[2];
dims[0] = 10;
dims[1] = 1;
for (int i = 0; i < dims.length; i++) {
total *= dims[i];
}
left = new float[total];
right = new float[total];
for (int i = 0; i < total; i++) {
left[i] = (float)i;
double tmp = Math.ceil(rand.nextDouble() * 10) / 10;
right[i] = (float)(tmp);
}
try {
// Get info about arrayfire information
Array.info();
// Send data to ArrayFire
Array A = new Array(dims, left);
Array B = new Array(dims, right);
// Create container for output
Array C = new Array();
// Do vector addition on the device
C = Array.add(A, B);
// Get result back to host memory
res = C.getFloatArray();
for(int i = 0; i < total; i++) {
str = Integer.toString(i) + ". ";
float val = left[i] + right[i];
str = str + Float.toString(left[i] ) + " + ";
str = str + Float.toString(right[i]) + " = ";
str = str + Float.toString(res[i]);
System.out.println(str);
}
} catch (Exception e) {
System.out.println("Failed to use ArrayFire");
System.out.println(e.getMessage());
}
}
}