From 81bfec900233b19383933401faac9e887a9796b8 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Fri, 17 Aug 2018 15:41:01 -0500 Subject: [PATCH] Filter spurious cond var wakeups, instrument thread and callback in set_callback with logging --- src/wrap_cl.hpp | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/wrap_cl.hpp b/src/wrap_cl.hpp index b2d5877e..f5ab376c 100644 --- a/src/wrap_cl.hpp +++ b/src/wrap_cl.hpp @@ -1502,23 +1502,37 @@ namespace pyopencl bool m_set_callback_suceeded; + bool m_notify_thread_wakeup_is_genuine; + cl_event m_event; cl_int m_command_exec_status; event_callback_info_t(py::object py_event, py::object py_callback) - : m_py_event(py_event), m_py_callback(py_callback), m_set_callback_suceeded(true) + : m_py_event(py_event), m_py_callback(py_callback), m_set_callback_suceeded(true), + m_notify_thread_wakeup_is_genuine(false) {} }; static void evt_callback(cl_event evt, cl_int command_exec_status, void *user_data) { + // FIXME REMOVE + puts("event callback: started"); + event_callback_info_t *cb_info = reinterpret_cast(user_data); { std::lock_guard lg(cb_info->m_mutex); cb_info->m_event = evt; cb_info->m_command_exec_status = command_exec_status; + cb_info->m_notify_thread_wakeup_is_genuine = true; } + // FIXME REMOVE + puts("event callback: before cv notify"); + cb_info->m_condvar.notify_one(); + + // FIXME REMOVE + puts("event callback: done"); + } public: @@ -1535,9 +1549,16 @@ namespace pyopencl std::thread notif_thread([cb_info]() { + // FIXME REMOVE + puts("thread: started"); { std::unique_lock ulk(cb_info->m_mutex); - cb_info->m_condvar.wait(ulk); + cb_info->m_condvar.wait( + ulk, + [&](){ return cb_info->m_notify_thread_wakeup_is_genuine; }); + + // FIXME REMOVE + puts("thread: wait returned"); // ulk no longer held here, cb_info ready for deletion } @@ -1545,6 +1566,9 @@ namespace pyopencl { py::gil_scoped_acquire acquire; + // FIXME REMOVE + puts("thread: gil acquired"); + if (cb_info->m_set_callback_suceeded) { try { @@ -1561,9 +1585,15 @@ namespace pyopencl } } + // FIXME REMOVE + puts("thread: before delete"); + // Need to hold GIL to delete py::object instances in // event_callback_info_t delete cb_info; + // + // FIXME REMOVE + puts("thread: ending"); } }); // Thread is away--it is now its responsibility to free cb_info. @@ -1583,6 +1613,7 @@ namespace pyopencl { std::lock_guard lg(cb_info->m_mutex); cb_info->m_set_callback_suceeded = false; + cb_info->m_notify_thread_wakeup_is_genuine = true; } cb_info->m_condvar.notify_one(); throw; -- GitLab