1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #define BOOST_TEST_MODULE TestEvent
12 #include <boost/test/unit_test.hpp>
16 #ifdef BOOST_COMPUTE_USE_CPP11
19 #endif // BOOST_COMPUTE_USE_CPP11
21 #include <boost/compute/event.hpp>
23 #include "context_setup.hpp"
25 BOOST_AUTO_TEST_CASE(null_event
)
27 boost::compute::event null
;
28 BOOST_CHECK(null
.get() == cl_event());
31 #if defined(BOOST_COMPUTE_CL_VERSION_1_1) && defined(BOOST_COMPUTE_USE_CPP11)
32 std::mutex callback_mutex
;
33 std::condition_variable callback_condition_variable
;
34 static bool callback_invoked
= false;
36 static void BOOST_COMPUTE_CL_CALLBACK
37 callback(cl_event event
, cl_int status
, void *user_data
)
39 std::lock_guard
<std::mutex
> lock(callback_mutex
);
40 callback_invoked
= true;
41 callback_condition_variable
.notify_one();
44 BOOST_AUTO_TEST_CASE(event_callback
)
46 REQUIRES_OPENCL_VERSION(1,2);
48 // ensure callback has not yet been executed
49 BOOST_CHECK_EQUAL(callback_invoked
, false);
51 // enqueue marker and set callback to be invoked
52 boost::compute::event marker
= queue
.enqueue_marker();
53 marker
.set_callback(callback
);
56 // wait up to one second for the callback to be executed
57 std::unique_lock
<std::mutex
> lock(callback_mutex
);
58 callback_condition_variable
.wait_for(
59 lock
, std::chrono::seconds(1), [&](){ return callback_invoked
; }
62 // ensure callback has been executed
63 BOOST_CHECK_EQUAL(callback_invoked
, true);
66 BOOST_AUTO_TEST_CASE(lambda_callback
)
68 REQUIRES_OPENCL_VERSION(1,2);
70 bool lambda_invoked
= false;
72 boost::compute::event marker
= queue
.enqueue_marker();
73 marker
.set_callback([&](){
74 std::lock_guard
<std::mutex
> lock(callback_mutex
);
75 lambda_invoked
= true;
76 callback_condition_variable
.notify_one();
80 // wait up to one second for the callback to be executed
81 std::unique_lock
<std::mutex
> lock(callback_mutex
);
82 callback_condition_variable
.wait_for(
83 lock
, std::chrono::seconds(1), [&](){ return lambda_invoked
; }
85 BOOST_CHECK_EQUAL(lambda_invoked
, true);
88 void BOOST_COMPUTE_CL_CALLBACK
89 event_promise_fulfiller_callback(cl_event event
, cl_int status
, void *user_data
)
91 auto *promise
= static_cast<std::promise
<void> *>(user_data
);
96 BOOST_AUTO_TEST_CASE(event_to_std_future
)
98 REQUIRES_OPENCL_VERSION(1,2);
100 // enqueue an asynchronous copy to the device
101 std::vector
<float> vector(1000, 3.14f
);
102 boost::compute::buffer
buffer(context
, 1000 * sizeof(float));
103 auto event
= queue
.enqueue_write_buffer_async(
104 buffer
, 0, 1000 * sizeof(float), vector
.data()
107 // create a promise and future to be set by the callback
108 auto *promise
= new std::promise
<void>;
109 std::future
<void> future
= promise
->get_future();
110 event
.set_callback(event_promise_fulfiller_callback
, CL_COMPLETE
, promise
);
112 // ensure commands are submitted to the device before waiting
115 // wait for future to become ready
118 #endif // BOOST_COMPUTE_CL_VERSION_1_1
120 BOOST_AUTO_TEST_SUITE_END()