Skip to content

Instantly share code, notes, and snippets.

@vchuravy
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vchuravy/6b3d1adf270559d166f1 to your computer and use it in GitHub Desktop.
Save vchuravy/6b3d1adf270559d166f1 to your computer and use it in GitHub Desktop.
Testing OpenCL callback integration
using OpenCL; const cl = OpenCL
type Event
status :: Symbol
error_code :: Int
Event() = Event(:undefined)
Event(status :: Symbol) = new(status, 0)
end
device = first(cl.devices())
ctx = cl.Context(device)
q = cl.CmdQueue(ctx)
test = Event()
function callback(e, e_s, d)
cb_from_event_loop = (data, status) -> callback_safe(e,e_s,d)
cb_packaged = Base.SingleAsyncWork(cb_from_event_loop)
ccall(:uv_async_send, Void, (Ptr{Void},), cb_packaged.handle)
return nothing
end
function callback_safe(event, event_status, data :: Ptr{Void})
event_data = unsafe_pointer_to_objref(data) :: Event
error_code = 0
if event_status == cl.CL_COMPLETE
status = :complete
elseif event_status == cl.CL_SUBMITTED
status = :submitted
elseif event_status == cl.CL_RUNNING
status = :submitted
elseif event_status == cl.CL_QUEUED
status = :queued
elseif event_status < 0
status = :error
error_code = event_status
end
event_data.status = status
event_data.error_code = error_code
return nothing
end
const callback_c = cfunction(callback, Void, (cl.CL_event, cl.CL_int, Ptr{Void}))
usr_evt = cl.UserEvent(ctx)
cl.enqueue_wait_for_events(q, usr_evt)
mkr_evt = cl.enqueue_marker(q)
cl.api.clSetEventCallback(mkr_evt.id, cl.CL_COMPLETE, callback_c, test)
println(test)
cl.complete(usr_evt)
yield()
sleep(10.0)
println(test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment