Skip to content

Commit 191295b

Browse files
committed
JavaCL: fixes / optimized event callbacks (but broke API: CLEvent.EventCallback now only takes the completion status as argument, not the event anymore)
1 parent 68290c0 commit 191295b

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

Core/src/main/velocity/com/nativelibs4java/opencl/CLEvent.java

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.nativelibs4java.util.EnumValue;
1212
import com.nativelibs4java.util.EnumValues;
1313
import org.bridj.*;
14+
import org.bridj.ann.Ptr;
1415
import static org.bridj.Pointer.*;
1516

1617
/**
@@ -33,18 +34,25 @@ public class CLEvent extends CLAbstractEntity {
3334
/**
3435
* Pass this to special value to any method that expects a variable number of events to wait for and that returns an event, to completely avoid returning the completion event (will return null instead of the event).
3536
*/
36-
public static final CLEvent FIRE_AND_FORGET = new CLEvent(-1);
37+
public static final CLEvent FIRE_AND_FORGET = new CLEvent(null, -1);
3738

3839
#declareInfosGetter("infos", "CL.clGetEventInfo")
3940

4041
#declareInfosGetter("profilingInfos", "CL.clGetEventProfilingInfo")
4142
42-
CLEvent(long evt) {
43+
private final CLQueue queue;
44+
45+
CLEvent(CLQueue queue, long evt) {
4346
super(evt, false);
47+
this.queue = queue;
48+
}
49+
50+
public CLQueue getQueue() {
51+
return queue;
4452
}
4553
4654
public interface EventCallback {
47-
public void callback(CLEvent event, int executionStatus);
55+
public void callback(int executionStatus);
4856
}
4957
5058
/**
@@ -56,6 +64,19 @@ public interface EventCallback {
5664
public void setCompletionCallback(final EventCallback callback) {
5765
setCallback(CL_COMPLETE, callback);
5866
}
67+
68+
private static final clSetEventCallback_arg1_callback eventCallback = new clSetEventCallback_arg1_callback() {
69+
public void apply(@Ptr long evt, int executionStatus, @Ptr long callbackPeer) {
70+
EventCallback callback = (EventCallback)JNI.refToObject(callbackPeer);
71+
try {
72+
callback.callback(executionStatus);
73+
} finally {
74+
JNI.deleteGlobalRef(callbackPeer);
75+
}
76+
}
77+
};
78+
private static final long eventCallbackPeer = getPeer(pointerTo(eventCallback));
79+
5980
/**
6081
#documentCallsFunction("clSetEventCallback")
6182
* Registers a user callback function for a specific command execution status. <br/>
@@ -67,30 +88,23 @@ public void setCompletionCallback(final EventCallback callback) {
6788
*/
6889
public void setCallback(int commandExecStatus, final EventCallback callback) {
6990
try {
70-
clSetEventCallback_arg1_callback cb = new clSetEventCallback_arg1_callback() {
71-
public void apply(OpenCLLibrary.cl_event evt, int executionStatus, Pointer voidPtr1) {
72-
callback.callback(CLEvent.this, executionStatus);
73-
}
74-
};
75-
// TODO manage lifespan of cb
76-
BridJ.protectFromGC(cb);
77-
error(CL.clSetEventCallback(getEntity(), commandExecStatus, getPeer(pointerTo(cb)), 0));
78-
} catch (Throwable th) {
79-
// TODO check if supposed to handle OpenCL 1.1
80-
throw new UnsupportedOperationException("Cannot set event callback (OpenCL 1.1 feature).", th);
91+
error(CL.clSetEventCallback(getEntity(), commandExecStatus, eventCallbackPeer, JNI.newGlobalRef(callback)));
92+
} catch (UnsatisfiedLinkError th) {
93+
throw new UnsupportedOperationException("Cannot set event callback (OpenCL 1.1 feature): " + th, th);
8194
}
8295
}
8396
8497
static CLEvent createEvent(final CLQueue queue, long evt) {
8598
return createEvent(queue, evt, false);
8699
}
100+
87101
static CLEvent createEvent(final CLQueue queue, long evt, boolean isUserEvent) {
88102
if (evt == 0)
89103
return null;
90104
91105
return isUserEvent ?
92-
new CLUserEvent(evt) :
93-
new CLEvent(evt);
106+
new CLUserEvent(queue, evt) :
107+
new CLEvent(queue, evt);
94108
}
95109
96110
static CLEvent createEventFromPointer(CLQueue queue, Pointer<cl_event> evt1) {
@@ -101,7 +115,7 @@ static CLEvent createEventFromPointer(CLQueue queue, Pointer<cl_event> evt1) {
101115
if (peer == 0)
102116
return null;
103117
104-
return new CLEvent(peer);
118+
return new CLEvent(queue, peer);
105119
}
106120
107121

Core/src/main/velocity/com/nativelibs4java/opencl/CLUserEvent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import com.nativelibs4java.opencl.library.OpenCLLibrary.cl_event;
99

1010
public class CLUserEvent extends CLEvent {
11-
CLUserEvent(long evt) {
12-
super(evt);
11+
CLUserEvent(CLQueue queue, long evt) {
12+
super(queue, evt);
1313
}
1414
/**
1515
#documentCallsFunction("clSetUserEventStatus")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ public static abstract class clLinkProgram_arg1_callback extends Callback<clLink
525525
public abstract void apply(OpenCLLibrary.cl_program cl_program1, Pointer<? > voidPtr1);
526526
};
527527
public static abstract class clSetEventCallback_arg1_callback extends Callback<clSetEventCallback_arg1_callback > {
528-
public abstract void apply(OpenCLLibrary.cl_event cl_event1, int cl_int1, Pointer<? > voidPtr1);
528+
public abstract void apply(@Ptr long cl_event1, int cl_int1, @Ptr long voidPtr1);
529529
};
530530
public static abstract class clEnqueueNativeKernel_arg1_callback extends Callback<clEnqueueNativeKernel_arg1_callback > {
531531
public abstract void apply(Pointer<? > voidPtr1);

0 commit comments

Comments
 (0)