mtchamengo / aparapi

Automatically exported from code.google.com/p/aparapi
Other
0 stars 0 forks source link

It would be great if there were a way to request asynchronous .execute(), with a callback to a listener when execution is finished. #104

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
This is an enhancement request:

The base .execute() behavior is great for most scenarios, but sometimes one 
wishes to have .execute() operate asynchronously.

For example, in systems with multiple GPUs one may want to fire off multiple 
.execute()s in parallel on the different GPUs, and be notified as each one 
completes.

Obviously this could be done by the user with threading code, but it can be 
complex and potentially error prone, especially if the number of GPUs is not 
known ahead of time.  It would be much nicer if the library itself provided 
this feature as a simple async flag + notification mechanism with the 
implementation details under the covers.

Ultimately it would be nice to give the Aparapi programmer some visibility into 
the underlying OpenCL queues to allow for more advanced use case scenarios, but 
for now just offering async .execute() would be a great upgrade :)

Original issue reported on code.google.com by lats...@gmail.com on 31 Mar 2013 at 8:58

GoogleCodeExporter commented 9 years ago
I am sure Ryan will like to see this ;) he has been talking about something 
like this for a while. 

This may be a little tricky from the JNI side using present code base.  We 
can't hold a pinned buffer across JNI calls, we like to pin buffers so that the 
JVM does not move them around whilst we are executing or transferring between 
host and device.

We have been toying with relaxing this and possibly only pinning during the 
clEnqueueBufferRead and write Write phases, this will/might slow us down a 
little, but it would mean that the writes, execute and reads can be performed 
in separate separate JNI calls.  (Do we know how other Java OpenCL bindings 
handle this race condition?)

See 
http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/functions.html#GetPrimit
iveArrayCritical

Assuming we can get around the pinning issue. So we could create a few API's 
here

Future<Kernel> future1 = kernel1.async(range1);
Future<Kernel> future2 = kernel2.async(range2);
// do some code
while (!(future1.isDone() && future2.isDone()){
   Thread.sleep(n); // or some other work here
} 

or 

future1.get() && future2.get(); // will block for both

Or we could pass a listener SAM type to the async call
Kernel.async(range, new KernelListener(){
     public void done(Kernel _kernel){
        /* .. */
     }
});

or using lambdas ;) 

kernel.async(range, k -> {/* .. */});

Gary

Original comment by frost.g...@gmail.com on 31 Mar 2013 at 3:55

GoogleCodeExporter commented 9 years ago
Dupe: http://code.google.com/p/aparapi/issues/detail?id=62

Plus that ticket was already a year+ in the making :)

Personally, I've been envisioning the use of Annotations to mark which methods 
get called-back when a kernel.async() has completed, based on concepts such as 
a Producer/Consumer Event Bus.

Original comment by ryan.lam...@gmail.com on 1 Apr 2013 at 6:38

GoogleCodeExporter commented 9 years ago

Original comment by ryan.lam...@gmail.com on 20 Apr 2013 at 12:39