Skip to content

Commit e297a8e

Browse files
committed
Using the C API for implementing the JNI
1 parent 80fd922 commit e297a8e

File tree

14 files changed

+510
-591
lines changed

14 files changed

+510
-591
lines changed

com/arrayfire/Arith.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class Arith extends Array {
1212
private native static long ge (long a, long b);
1313
private native static long gt (long a, long b);
1414
private native static long eq (long a, long b);
15-
private native static long ne (long a, long b);
15+
private native static long neq(long a, long b);
1616
//private native static long pow(long a, float b);
1717

1818
// Scalar Binary operations
@@ -25,7 +25,7 @@ public class Arith extends Array {
2525
private native static long gef (long a, float b);
2626
private native static long gtf (long a, float b);
2727
private native static long eqf (long a, float b);
28-
private native static long nef (long a, float b);
28+
private native static long neqf(long a, float b);
2929
private native static long powf(long a, float b);
3030

3131
private native static long fsub(float a, long b);
@@ -100,8 +100,8 @@ public static void eq(Array c, Array a, Array b) throws Exception {
100100
c.set(ref);
101101
}
102102

103-
public static void ne(Array c, Array a, Array b) throws Exception {
104-
long ref = ne(a.ref,b.ref);
103+
public static void neq(Array c, Array a, Array b) throws Exception {
104+
long ref = neq(a.ref,b.ref);
105105
c.set(ref);
106106
}
107107

@@ -232,8 +232,8 @@ public static void eq(Array c, Array a, float b) throws Exception {
232232
c.set(ref);
233233
}
234234

235-
public static void ne(Array c, Array a, float b) throws Exception {
236-
long ref = nef(a.ref,b);
235+
public static void neq(Array c, Array a, float b) throws Exception {
236+
long ref = neqf(a.ref,b);
237237
c.set(ref);
238238
}
239239

com/arrayfire/Array.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ public class Array implements AutoCloseable {
1313
System.loadLibrary("af_java");
1414
}
1515

16-
public native static void info();
17-
1816
private native static void destroyArray(long ref);
1917
private native static int[] getDims(long ref);
2018
private native static int getType(long ref);

com/arrayfire/Image.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ public class Image extends Array {
99
private native static long meanshift(long a, float space, float color, int iter);
1010
private native static long histogram(long a, int nbins);
1111
private native static long hist_mnmx(long a, int nbins, float min, float max);
12-
private native static long rotate (long a, float theta, boolean crop);
13-
private native static long resize1 (long a, float scale, char method);
14-
private native static long resize2 (long a, float scalex, float scaley, char method);
15-
private native static long resize3 (long a, int height, int width, char method);
12+
private native static long rotate (long a, float theta, boolean crop, int method);
13+
private native static long resize1 (long a, float scale, int method);
14+
private native static long resize2 (long a, float scalex, float scaley, int method);
15+
private native static long resize3 (long a, int height, int width, int method);
1616

1717
public static void erode(Array res, Array a, Array b) throws Exception {
1818
long ref = erode(a.ref,b.ref);
@@ -50,23 +50,41 @@ public static void histogram(Array res, Array a, int nbins, float min, float max
5050
}
5151

5252
public static void rotate(Array res, Array a, float theta, boolean crop) throws Exception {
53-
long ref = rotate(a.ref,theta,crop);
53+
long ref = rotate(a.ref,theta,crop,0);
5454
res.set(ref);
5555
}
5656

57-
// method ='L' - Linear interpolation
58-
// or 'N' - Nearest neighbor
59-
public static void resize(Array res, Array a, float scale, char method) throws Exception {
57+
public static void rotate(Array res, Array a, float theta, boolean crop, int method) throws Exception {
58+
long ref = rotate(a.ref,theta,crop,method);
59+
res.set(ref);
60+
}
61+
62+
public static void resize(Array res, Array a, float scale) throws Exception {
63+
long ref = resize1(a.ref,scale,0);
64+
res.set(ref);
65+
}
66+
67+
public static void resize(Array res, Array a, float scale, int method) throws Exception {
6068
long ref = resize1(a.ref,scale,method);
6169
res.set(ref);
6270
}
6371

64-
public static void resize(Array res, Array a, float scalex, float scaley, char method) throws Exception {
72+
public static void resize(Array res, Array a, float scalex, float scaley) throws Exception {
73+
long ref = resize2(a.ref,scalex,scaley,0);
74+
res.set(ref);
75+
}
76+
77+
public static void resize(Array res, Array a, float scalex, float scaley, int method) throws Exception {
6578
long ref = resize2(a.ref,scalex,scaley,method);
6679
res.set(ref);
6780
}
6881

69-
public static void resize(Array res, Array a, int height, int width, char method) throws Exception {
82+
public static void resize(Array res, Array a, int height, int width) throws Exception {
83+
long ref = resize3(a.ref,height,width,0);
84+
res.set(ref);
85+
}
86+
87+
public static void resize(Array res, Array a, int height, int width, int method) throws Exception {
7088
long ref = resize3(a.ref,height,width,method);
7189
res.set(ref);
7290
}

com/arrayfire/Signal.java

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,90 @@
22

33
public class Signal extends Array {
44

5-
private native static long fft (long a);
6-
private native static long fft2 (long a);
7-
private native static long fft3 (long a);
5+
private native static long fft (long a, int dim0);
6+
private native static long fft2 (long a, int dim0, int dim1);
7+
private native static long fft3 (long a, int dim0, int dim1, int dim2);
88

9-
private native static long ifft (long a);
10-
private native static long ifft2(long a);
11-
private native static long ifft3(long a);
9+
private native static long ifft (long a, int dim0);
10+
private native static long ifft2(long a, int dim0, int dim1);
11+
private native static long ifft3(long a, int dim0, int dim1, int dim2);
1212

13-
private native static long convolve (long a, long b);
13+
private native static long convolve1(long a, long b, int type);
14+
private native static long convolve2(long a, long b, int type);
15+
private native static long convolve3(long a, long b, int type);
1416

1517
public static void fft(Array res, Array a) throws Exception {
16-
long ref = fft(a.ref);
18+
long ref = fft(a.ref, 0);
19+
res.set(ref);
20+
}
21+
22+
public static void fft(Array res, Array a, int dim0) throws Exception {
23+
long ref = fft(a.ref, dim0);
1724
res.set(ref);
1825
}
1926

2027
public static void fft2(Array res, Array a) throws Exception {
21-
long ref = fft2(a.ref);
28+
long ref = fft2(a.ref, 0, 0);
29+
res.set(ref);
30+
}
31+
32+
public static void fft2(Array res, Array a, int dim0, int dim1) throws Exception {
33+
long ref = fft2(a.ref, dim0, dim1);
2234
res.set(ref);
2335
}
2436

2537
public static void fft3(Array res, Array a) throws Exception {
26-
long ref = fft3(a.ref);
38+
long ref = fft3(a.ref, 0, 0, 0);
39+
res.set(ref);
40+
}
41+
42+
public static void fft3(Array res, Array a, int dim0, int dim1, int dim2) throws Exception {
43+
long ref = fft3(a.ref, dim0, dim1, dim2);
2744
res.set(ref);
2845
}
2946

3047
public static void ifft(Array res, Array a) throws Exception {
31-
long ref = ifft(a.ref);
48+
long ref = ifft(a.ref, 0);
49+
res.set(ref);
50+
}
51+
52+
public static void ifft(Array res, Array a, int dim0) throws Exception {
53+
long ref = ifft(a.ref, dim0);
3254
res.set(ref);
3355
}
3456

3557
public static void ifft2(Array res, Array a) throws Exception {
36-
long ref = ifft2(a.ref);
58+
long ref = ifft2(a.ref, 0, 0);
59+
res.set(ref);
60+
}
61+
62+
public static void ifft2(Array res, Array a, int dim0, int dim1) throws Exception {
63+
long ref = ifft2(a.ref, dim0, dim1);
3764
res.set(ref);
3865
}
3966

4067
public static void ifft3(Array res, Array a) throws Exception {
41-
long ref = ifft3(a.ref);
68+
long ref = ifft3(a.ref, 0, 0, 0);
69+
res.set(ref);
70+
}
71+
72+
public static void ifft3(Array res, Array a, int dim0, int dim1, int dim2) throws Exception {
73+
long ref = ifft3(a.ref, dim0, dim1, dim2);
74+
res.set(ref);
75+
}
76+
77+
public static void convolve1(Array res, Array a, Array b) throws Exception {
78+
long ref = convolve1(a.ref, b.ref, 0);
79+
res.set(ref);
80+
}
81+
82+
public static void convolve2(Array res, Array a, Array b) throws Exception {
83+
long ref = convolve2(a.ref, b.ref, 0);
4284
res.set(ref);
4385
}
4486

45-
public static void convolve(Array res, Array a, Array b) throws Exception {
46-
long ref = convolve(a.ref, b.ref);
87+
public static void convolve3(Array res, Array a, Array b) throws Exception {
88+
long ref = convolve3(a.ref, b.ref, 0);
4789
res.set(ref);
4890
}
4991
}

com/arrayfire/Util.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ public static float[] toFloatArray(String text, String delim) {
2626
return ret_ary;
2727
}
2828

29+
public native static void info();
2930
}

examples/HelloWorld.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ public static void main(String[] args) {
3030

3131
try {
3232
Array A = new Array(), B = new Array(), C = new Array();
33+
3334
// Get info about arrayfire information
34-
Array.info();
35+
Util.info();
3536

3637
// Send data to ArrayFire
3738
Data.createArray(A, dims, left);

src/algorithm.cpp

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,10 @@ BEGIN_EXTERN_C
88
JNIEXPORT jdouble JNICALL ALGO_FUNC(func##All) \
99
(JNIEnv *env, jclass clazz, jlong a) \
1010
{ \
11-
try { \
12-
af::array *A = (af::array*)(a); \
13-
if (A->type() == f32) \
14-
return af::func<float>( (*A) ); \
15-
if (A->type() == s32) \
16-
return af::func<int>( (*A) ); \
17-
if (A->type() == f64) \
18-
return af::func<double>( (*A) ); \
19-
if (A->type() == b8) \
20-
return af::func<float>( (*A) ); \
21-
return af::NaN; \
22-
} catch(af::exception& e) { \
23-
return af::NaN; \
24-
} catch(std::exception& e) { \
25-
return af::NaN; \
26-
} \
11+
double real, imag; \
12+
(af_##func##_all(&real, &imag, \
13+
ARRAY(a))); \
14+
return real; \
2715
}
2816

2917
SCALAR_RET_OP_DEF(sum)
@@ -34,18 +22,9 @@ SCALAR_RET_OP_DEF(min)
3422
JNIEXPORT jlong JNICALL ALGO_FUNC(func) \
3523
(JNIEnv *env, jclass clazz, jlong a, jint dim) \
3624
{ \
37-
jlong ret = 0; \
38-
try { \
39-
af::array *A = (af::array*)(a); \
40-
af::array *res = new af::array(); \
41-
*res = af::func((*A), dim); \
42-
ret = (jlong)res; \
43-
} catch(af::exception& e) { \
44-
return 0; \
45-
} catch(std::exception& e) { \
46-
return 0; \
47-
} \
48-
return ret; \
25+
af_array ret = 0; \
26+
(af_##func(&ret, ARRAY(a), dim)); \
27+
return JLONG(ret); \
4928
}
5029

5130
ARRAY_RET_OP_DEF(sum)

0 commit comments

Comments
 (0)