Skip to content

Commit 83e6449

Browse files
author
Olivier Chafik
committed
JavaCL Proxy: embryonic Icd + SPI code
1 parent 28316f0 commit 83e6449

10 files changed

Lines changed: 1250 additions & 603 deletions

File tree

OpenCL4Java/src/main/java/com/nativelibs4java/opencl/library/IOpenCLLibrary.java

Lines changed: 581 additions & 8 deletions
Large diffs are not rendered by default.

OpenCL4Java/src/main/java/com/nativelibs4java/opencl/library/OpenCLLibrary.java

Lines changed: 0 additions & 580 deletions
Large diffs are not rendered by default.

OpenCL4Java/src/main/java/com/nativelibs4java/opencl/proxy/AbstractOpenCLLibrary.java

Lines changed: 450 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* To change this template, choose Tools | Templates
3+
* and open the template in the editor.
4+
*/
5+
package com.nativelibs4java.opencl.proxy;
6+
7+
import org.bridj.NativeObject;
8+
import org.bridj.Pointer;
9+
import static org.bridj.Pointer.*;
10+
11+
public class PointerUtils {
12+
13+
protected static void setSizeT(long peer, long value) {
14+
pointerToAddress(peer).setSizeT(value);
15+
}
16+
17+
protected static void setSizeTAtIndex(long peer, int index, long value) {
18+
pointerToAddress(peer).setSizeTAtIndex(index, value);
19+
}
20+
21+
protected static void setPointerAtIndex(long peer, int index, Pointer<?> value) {
22+
pointerToAddress(peer).setPointerAtIndex(index, value);
23+
}
24+
25+
protected static void setPointerAtIndex(long peer, int index, NativeObject value) {
26+
pointerToAddress(peer).setSizeTAtIndex(index, Pointer.getPeer(Pointer.pointerTo(value)));
27+
}
28+
29+
protected static long getSizeT(long peer) {
30+
return pointerToAddress(peer).getSizeT();
31+
}
32+
33+
protected static <T> Pointer<T> getPointer(long peer, Class<T> targetClass) {
34+
return pointerToAddress(peer).getPointer(targetClass);
35+
}
36+
37+
protected static void setInt(long peer, int value) {
38+
pointerToAddress(peer).setInt(value);
39+
}
40+
41+
protected static int getInt(long peer) {
42+
return pointerToAddress(peer).getInt();
43+
}
44+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* To change this template, choose Tools | Templates
3+
* and open the template in the editor.
4+
*/
5+
package com.nativelibs4java.opencl.proxy;
6+
7+
import com.nativelibs4java.opencl.library.IOpenCLLibrary;
8+
import static com.nativelibs4java.opencl.library.IOpenCLLibrary.*;
9+
import java.util.ArrayList;
10+
import java.util.Iterator;
11+
import java.util.List;
12+
import java.util.ServiceLoader;
13+
import org.bridj.BridJ;
14+
import org.bridj.Pointer;
15+
import static org.bridj.Pointer.*;
16+
import static com.nativelibs4java.opencl.proxy.PointerUtils.*;
17+
import org.bridj.SizeT;
18+
import org.bridj.StructObject;
19+
import org.bridj.ann.Field;
20+
import org.bridj.ann.Ptr;
21+
22+
/**
23+
*
24+
* @author ochafik
25+
*/
26+
public class ProxiedOpenCLLibrary extends AbstractOpenCLLibrary {
27+
private static Pointer<?> icdDispatchTable;
28+
public static void setIcdDispatchTable(long icdDispatchTablePeer) {
29+
ProxiedOpenCLLibrary.icdDispatchTable = pointerToAddress(icdDispatchTablePeer);
30+
}
31+
32+
private static ProxiedOpenCLLibrary instance;
33+
public static synchronized IOpenCLLibrary getInstance() {
34+
if (instance == null) {
35+
List<IOpenCLLibrary> platforms = new ArrayList<IOpenCLLibrary>();
36+
for (Iterator<IOpenCLLibrary> it = ServiceLoader.load(IOpenCLLibrary.class).iterator(); it.hasNext();) {
37+
IOpenCLLibrary platform = it.next();
38+
platforms.add(platform);
39+
}
40+
instance = new ProxiedOpenCLLibrary(platforms);
41+
}
42+
return instance;
43+
}
44+
45+
protected static class IcdEntity extends StructObject {
46+
@Field(0)
47+
public Pointer<?> icdDispatchTable = ProxiedOpenCLLibrary.icdDispatchTable;
48+
@Field(1)
49+
public int platformIndex;
50+
51+
public IcdEntity() {
52+
super();
53+
}
54+
public IcdEntity(Pointer<? extends IcdEntity> peer, Object... targs) {
55+
super(peer);
56+
}
57+
}
58+
protected static class PlatformId extends IcdEntity {
59+
60+
public PlatformId() {
61+
super();
62+
}
63+
public PlatformId(Pointer<? extends PlatformId> peer, Object... targs) {
64+
super(peer);
65+
}
66+
67+
}
68+
69+
private final List<IOpenCLLibrary> platforms;
70+
private final List<PlatformId> platformIds;
71+
72+
public ProxiedOpenCLLibrary(List<IOpenCLLibrary> platforms) {
73+
this.platforms = new ArrayList<IOpenCLLibrary>(platforms);
74+
75+
List<PlatformId> platformIds = new ArrayList<PlatformId>();
76+
for (IOpenCLLibrary implementation : this.platforms) {
77+
PlatformId platformId = new PlatformId();
78+
platformId.platformIndex = platforms.size();
79+
BridJ.writeToNative(platformId);
80+
81+
platforms.add(implementation);
82+
platformIds.add(platformId);
83+
}
84+
this.platformIds = platformIds;
85+
}
86+
87+
88+
protected IOpenCLLibrary getImplementation(long icdEntityPeer) {
89+
Pointer<IcdEntity> icdEntityPtr = getPointer(icdEntityPeer, IcdEntity.class);
90+
IcdEntity icdEntity = icdEntityPtr.get();
91+
if (!icdDispatchTable.equals(icdEntity.icdDispatchTable))
92+
throw new IllegalArgumentException("Not an ICD entity, or different ICD dispatch table: " + icdEntityPeer);
93+
IOpenCLLibrary implementation = platforms.get(icdEntity.platformIndex);
94+
return implementation;
95+
}
96+
97+
@Override
98+
public int clGetPlatformIDs(int num_entries, @Ptr long platforms, @Ptr long num_platforms) {
99+
System.out.println("Called clGetPlatformIDs");
100+
if ((platforms == 0 || num_entries == 0) && num_platforms == 0) {
101+
setSizeT(num_platforms, platformIds.size());
102+
} else if (platforms != 0 && num_entries != 0) {
103+
int numWrote = 0;
104+
for (int i = 0; i < num_entries; i++) {
105+
setPointerAtIndex(platforms, i, platformIds.get(i));
106+
numWrote++;
107+
}
108+
if (num_platforms != 0) {
109+
setSizeT(num_platforms, numWrote);
110+
}
111+
} else
112+
return CL_INVALID_VALUE;
113+
return CL_SUCCESS;
114+
}
115+
116+
117+
@Override
118+
public int clGetDeviceIDs(long cl_platform_id1, long cl_device_type1, int cl_uint1, long cl_device_idPtr1, long cl_uintPtr1) {
119+
IOpenCLLibrary implementation = getImplementation(cl_platform_id1);
120+
return implementation.clGetDeviceIDs(cl_platform_id1, cl_device_type1, cl_uint1, cl_device_idPtr1, cl_uintPtr1);
121+
}
122+
123+
@Override
124+
public int clGetDeviceInfo(long cl_device_id1, int cl_device_info1, long size_t1, long voidPtr1, long size_tPtr1) {
125+
IOpenCLLibrary implementation = getImplementation(cl_device_id1);
126+
return implementation.clGetDeviceInfo(cl_device_id1, cl_device_info1, size_t1, voidPtr1, size_tPtr1); //To change body of generated methods, choose Tools | Templates.
127+
}
128+
129+
@Override
130+
public int clGetPlatformInfo(long cl_platform_id1, int cl_platform_info1, long size_t1, long voidPtr1, long size_tPtr1) {
131+
IOpenCLLibrary implementation = getImplementation(cl_platform_id1);
132+
return implementation.clGetPlatformInfo(cl_platform_id1, cl_platform_info1, size_t1, voidPtr1, size_tPtr1);
133+
}
134+
135+
136+
137+
}

Proxy/src/main/cpp/proxy/API.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@ jclass gOpenCLProxyInterface = NULL;
44
jmethodID gclGetPlatformIDsMethod = NULL;
55
jobject gOpenCLProxyImplementation = NULL;
66

7+
#define PROXIED_CLASS_SIG "com/nativelibs4java/opencl/library/ProxiedOpenCLLibrary"
8+
79
void bindJavaAPI(void *instanceData) {
810
jclass proxyClass = NULL;
911
jmethodID getProxyInstanceMethod = NULL;
12+
jmethodID setIcdDispatchTableMethod = NULL;
1013
JNIEnv *env = GetEnv();
1114
gOpenCLProxyInterface = GLOBAL_REF((*env)->FindClass(env, PROXY_INTERFACE_SIG));
1215
gclGetPlatformIDsMethod = (*env)->GetMethodID(env, proxyClass, "clGetPlatformIDs", "(ILL)I");
1316

14-
proxyClass = (*env)->FindClass(env, "com/nativelibs4java/opencl/library/Proxy");
15-
getProxyInstanceMethod = (*env)->GetStaticMethodID(env, proxyClass, "getInstance", "(L)L" PROXY_INTERFACE_SIG ";");
16-
gOpenCLProxyImplementation = GLOBAL_REF((*env)->CallStaticObjectMethod(env, proxyClass, getProxyInstanceMethod, (jlong)(size_t)instanceData));
17+
proxyClass = (*env)->FindClass(env, PROXIED_CLASS_SIG);
18+
getProxyInstanceMethod = (*env)->GetStaticMethodID(env, proxyClass, "getInstance", "()L" PROXY_INTERFACE_SIG ";");
19+
setIcdDispatchTableMethod = (*env)->GetStaticMethodID(env, proxyClass, "setIcdDispatchTable", "(L)V" PROXIED_CLASS_SIG ";");
20+
21+
(*env)->CallStaticVoidMethod(env, proxyClass, setIcdDispatchTableMethod, (jlong)(size_t)instanceData);
22+
gOpenCLProxyImplementation = GLOBAL_REF((*env)->CallStaticObjectMethod(env, proxyClass, getProxyInstanceMethod));
1723
}
1824

1925
void unbindJavaAPI() {

Proxy/src/main/cpp/proxy/GNUmakefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ CPPFLAGS += -std=c99
4646
CPPFLAGS += -U_FORTIFY_SOURCE
4747

4848
ifdef BUILD_OS_darwin
49-
CPPFLAGS += -I/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers/
49+
#CPPFLAGS += -I/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home/include
50+
#CPPFLAGS += -I/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers/
51+
CPPFLAGS += -I/Library/Java/JavaVirtualMachines/jdk1.7.0_13.jdk/Contents/Home/include
52+
CPPFLAGS += -I/Library/Java/JavaVirtualMachines/jdk1.7.0_13.jdk/Contents/Home/include/darwin
53+
LDFLAGS += -L/Library/Java/JavaVirtualMachines/jdk1.7.0_13.jdk/Contents/Home/jre/lib/jli -ljli
5054
LDFLAGS += -ldl -framework Foundation -all_load
5155
endif
5256

Proxy/src/main/cpp/proxy/Library.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ JNIEnv* GetEnv() {
99
JNIEnv* env = NULL;
1010
if ((*gJVM)->GetEnv(gJVM, (void*)&env, JNI_VERSION) != JNI_OK) {
1111
if ((*gJVM)->AttachCurrentThreadAsDaemon(gJVM, (void*)&env, NULL) != JNI_OK) {
12-
printf("BridJ: Cannot attach current JVM thread !\n");
12+
printf("JavaCL Proxy: Cannot attach current JVM thread !\n");
1313
return NULL;
1414
}
1515
}
@@ -18,9 +18,21 @@ JNIEnv* GetEnv() {
1818

1919
struct _cl_icd_dispatch dispatch;
2020

21+
void createJVM() {
22+
JNIEnv *env;
23+
JavaVMInitArgs vm_args;
24+
JavaVMOption options;
25+
options.optionString = "-Djava.class.path=javacl-proxy.jar"; // TODO: change this
26+
vm_args.version = JNI_VERSION_1_6;
27+
vm_args.nOptions = 1;
28+
vm_args.options = &options;
29+
vm_args.ignoreUnrecognized = 0;
30+
JNI_CreateJavaVM(&gJVM, (void**)&env, &vm_args);
31+
// TODO: handle errors.
32+
}
2133
void initializeLibrary() {
2234
// Create JVM, based on env. OPENCL_PROXY_JAR
23-
// ...
35+
createJVM();
2436

2537
// Bind classes and methods.
2638
bindJavaAPI(&gJavaCLProxyDispatch);
@@ -31,6 +43,6 @@ void cleanupLibrary() {
3143
unbindJavaAPI();
3244

3345
// Stop JVM
34-
// ...
46+
(*gJVM)->DestroyJavaVM(gJVM);
3547
}
3648

Proxy/src/main/cpp/proxy/Proxy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ struct _cl_icd_dispatch {
55
void *funcptr[109];
66
};
77

8-
struct _cl_icd_dispatch gJavaCLProxyDispatch;
8+
extern struct _cl_icd_dispatch gJavaCLProxyDispatch;
99

1010
#endif // __JAVACL_PROXY_PROXY_H

Proxy/src/main/cpp/proxy/build.sh

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ fi
1212

1313
cd "$DYNCALL_HOME/dyncall"
1414

15-
if [[ -d /System/Library/Frameworks/ && ! -d /Applications/MobilePhone.app ]] ; then
16-
# Avoid LC_DYLD_INFO (https://discussions.apple.com/thread/3197542?start=0&tstart=0)
17-
export MACOSX_DEPLOYMENT_TARGET=10.4
18-
sh ./configure --target-universal
19-
else
20-
sh ./configure
21-
fi
15+
sh ./configure
16+
#if [[ -d /System/Library/Frameworks/ && ! -d /Applications/MobilePhone.app ]] ; then
17+
# # Avoid LC_DYLD_INFO (https://discussions.apple.com/thread/3197542?start=0&tstart=0)
18+
# export MACOSX_DEPLOYMENT_TARGET=10.4
19+
# sh ./configure --target-universal
20+
#else
21+
# sh ./configure
22+
#fi
2223

2324
cd "$CURR"
2425
$MAKE_CMD $@

0 commit comments

Comments
 (0)