Skip to content

Commit 23250e6

Browse files
committed
Adding a small example to the Java wrapper
1 parent ff81228 commit 23250e6

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,17 @@ else
3333
AF_JAVA_LIB_EXT=$(patsubst %.so, %_cuda.so, $(AF_JAVA_LIB))
3434
endif
3535

36+
run: all
37+
AF_JAVA_PATH=$(AF_JAVA_PATH) make -C examples run
38+
3639
cuda: all
3740

3841
opencl: all
3942

4043
all: $(AF_JAVA_JAR)
4144

4245
$(AF_JAVA_JAR): $(AF_JAVA_LIB) $(AF_JAVA_CLASSES)
43-
jar cfm $@ $(AF_JAVA_MANIFEST) $(AF_JAVA_CLASSES) $(AF_JAVA_LIB)
46+
jar cfm $@ $(AF_JAVA_MANIFEST) $(AF_JAVA_CLASSES)
4447

4548
%.class: %.java
4649
javac $<

examples/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
*.jar
2+
*.class

examples/HelloWorld.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
}

examples/Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
ifeq ($(shell uname -m), x86_64)
2+
LIB:=lib64
3+
else
4+
LIB:=lib
5+
endif
6+
7+
AF_JAVA_PATH?=../
8+
AF_JAVA_JAR?=$(AF_JAVA_PATH)/ArrayFire.jar
9+
AF_JAVA_LIB_PATH?=$(AF_JAVA_PATH)/$(LIB)
10+
11+
CLASSES=$(patsubst %.java, %.class, $(shell ls *.java))
12+
BINS=$(patsubst %.class, %, $(CLASSES))
13+
14+
run: $(BINS)
15+
16+
%: %.class
17+
LD_LIBRARY_PATH=$(AF_JAVA_LIB_PATH) java -cp .:$(AF_JAVA_JAR) $@
18+
19+
%.class: %.java
20+
javac -cp $(AF_JAVA_PATH)/ArrayFire.jar $<

0 commit comments

Comments
 (0)