]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | //---------------------------------------------------------------------------// |
2 | // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com> | |
3 | // | |
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 | |
7 | // | |
8 | // See http://boostorg.github.com/compute for more information. | |
9 | //---------------------------------------------------------------------------// | |
10 | ||
11 | #define BOOST_TEST_MODULE TestEvent | |
12 | #include <boost/test/unit_test.hpp> | |
13 | ||
14 | #include <vector> | |
15 | ||
16 | #ifdef BOOST_COMPUTE_USE_CPP11 | |
17 | #include <mutex> | |
18 | #include <future> | |
19 | #endif // BOOST_COMPUTE_USE_CPP11 | |
20 | ||
21 | #include <boost/compute/event.hpp> | |
22 | ||
23 | #include "context_setup.hpp" | |
24 | ||
25 | BOOST_AUTO_TEST_CASE(null_event) | |
26 | { | |
27 | boost::compute::event null; | |
28 | BOOST_CHECK(null.get() == cl_event()); | |
29 | } | |
30 | ||
b32b8144 | 31 | #if defined(BOOST_COMPUTE_CL_VERSION_1_1) && defined(BOOST_COMPUTE_USE_CPP11) |
7c673cae FG |
32 | std::mutex callback_mutex; |
33 | std::condition_variable callback_condition_variable; | |
34 | static bool callback_invoked = false; | |
35 | ||
36 | static void BOOST_COMPUTE_CL_CALLBACK | |
37 | callback(cl_event event, cl_int status, void *user_data) | |
38 | { | |
39 | std::lock_guard<std::mutex> lock(callback_mutex); | |
40 | callback_invoked = true; | |
41 | callback_condition_variable.notify_one(); | |
42 | } | |
43 | ||
44 | BOOST_AUTO_TEST_CASE(event_callback) | |
45 | { | |
46 | REQUIRES_OPENCL_VERSION(1,2); | |
47 | ||
48 | // ensure callback has not yet been executed | |
49 | BOOST_CHECK_EQUAL(callback_invoked, false); | |
50 | ||
51 | // enqueue marker and set callback to be invoked | |
52 | boost::compute::event marker = queue.enqueue_marker(); | |
53 | marker.set_callback(callback); | |
54 | marker.wait(); | |
55 | ||
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; } | |
60 | ); | |
61 | ||
62 | // ensure callback has been executed | |
63 | BOOST_CHECK_EQUAL(callback_invoked, true); | |
64 | } | |
65 | ||
66 | BOOST_AUTO_TEST_CASE(lambda_callback) | |
67 | { | |
68 | REQUIRES_OPENCL_VERSION(1,2); | |
69 | ||
70 | bool lambda_invoked = false; | |
71 | ||
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(); | |
77 | }); | |
78 | marker.wait(); | |
79 | ||
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; } | |
84 | ); | |
85 | BOOST_CHECK_EQUAL(lambda_invoked, true); | |
86 | } | |
87 | ||
88 | void BOOST_COMPUTE_CL_CALLBACK | |
89 | event_promise_fulfiller_callback(cl_event event, cl_int status, void *user_data) | |
90 | { | |
91 | auto *promise = static_cast<std::promise<void> *>(user_data); | |
92 | promise->set_value(); | |
93 | delete promise; | |
94 | } | |
95 | ||
96 | BOOST_AUTO_TEST_CASE(event_to_std_future) | |
97 | { | |
98 | REQUIRES_OPENCL_VERSION(1,2); | |
99 | ||
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() | |
105 | ); | |
106 | ||
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); | |
111 | ||
112 | // ensure commands are submitted to the device before waiting | |
113 | queue.flush(); | |
114 | ||
115 | // wait for future to become ready | |
116 | future.wait(); | |
117 | } | |
b32b8144 | 118 | #endif // BOOST_COMPUTE_CL_VERSION_1_1 |
7c673cae FG |
119 | |
120 | BOOST_AUTO_TEST_SUITE_END() |