-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose events, markers, and barriers to the user
This should be enough to organize dependency graph in a generic way, without changing the existing API. see #196
- Loading branch information
Showing
8 changed files
with
387 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#define BOOST_TEST_MODULE Let | ||
#include <boost/test/unit_test.hpp> | ||
#include <vexcl/vector.hpp> | ||
#include <vexcl/reductor.hpp> | ||
#include "context_setup.hpp" | ||
|
||
BOOST_AUTO_TEST_CASE(let_vector_expr) | ||
{ | ||
const size_t n = 1024; | ||
|
||
std::vector<vex::command_queue> q1(1, ctx.queue(0)); | ||
std::vector<vex::command_queue> q2(1, vex::backend::duplicate_queue(ctx.queue(0))); | ||
|
||
vex::vector<int> x(q1, n); | ||
vex::vector<int> y(q2, n); | ||
|
||
vex::Reductor<size_t> count(q2); | ||
|
||
x = 1; | ||
q1[0].finish(); | ||
|
||
x = 2; | ||
|
||
vex::backend::enqueue_barrier(q2[0], {vex::backend::enqueue_marker(q1[0])}); | ||
|
||
y = x; | ||
|
||
BOOST_CHECK_EQUAL(count(y != 2), 0); | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#ifndef VEXCL_BACKEND_COMPUTE_EVENT_HPP | ||
#define VEXCL_BACKEND_COMPUTE_EVENT_HPP | ||
|
||
/* | ||
The MIT License | ||
Copyright (c) 2012-2016 Denis Demidov <[email protected]> | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
*/ | ||
|
||
/** | ||
* \file vexcl/backend/compute/event.hpp | ||
* \author Denis Demidov <[email protected]> | ||
* \brief Bring Boost.Compute events into vex::backend::compute namespace. | ||
*/ | ||
|
||
#include <boost/compute/event.hpp> | ||
#include <boost/compute/wait_list.hpp> | ||
#include <vexcl/backend/compute/context.hpp> | ||
|
||
namespace vex { | ||
namespace backend { | ||
namespace compute { | ||
|
||
using boost::compute::event; | ||
using boost::compute::wait_list; | ||
|
||
/// Append event to wait list | ||
inline void wait_list_append(wait_list &dst, const event &e) { | ||
dst.insert(e); | ||
} | ||
|
||
/// Append wait list to wait list | ||
inline void wait_list_append(wait_list &dst, const wait_list &src) { | ||
for(size_t i = 0; i < src.size(); ++i) | ||
dst.insert(src[i]); | ||
} | ||
|
||
/// Get id of the context the event was submitted into | ||
inline context_id get_context_id(const event &e) { | ||
return e.get_info<CL_EVENT_CONTEXT>(); | ||
} | ||
|
||
#ifdef CL_VERSION_1_2 | ||
/// Enqueue marker (with wait list) into the queue | ||
inline event enqueue_marker(command_queue &q, | ||
const wait_list &events = wait_list()) | ||
{ | ||
context_id ctx = get_context_id(q); | ||
wait_list my_events; | ||
|
||
for(auto e = events.begin(); e != events.end(); ++e) | ||
if (ctx == get_context_id(*e)) my_events.insert(*e); | ||
|
||
return q.enqueue_marker(my_events); | ||
} | ||
|
||
/// Enqueue barrier (with wait list) into the queue | ||
inline event enqueue_barrier(command_queue &q, | ||
const wait_list &events = wait_list()) | ||
{ | ||
context_id ctx = get_context_id(q); | ||
wait_list my_events; | ||
|
||
for(auto e = events.begin(); e != events.end(); ++e) | ||
if (ctx == get_context_id(*e)) my_events.insert(*e); | ||
|
||
return q.enqueue_barrier(my_events); | ||
} | ||
#endif | ||
|
||
/// Wait for events in the list | ||
inline void wait_for_events(const wait_list &events) { | ||
std::map<context_id, wait_list> by_context; | ||
|
||
for(auto e = events.begin(); e != events.end(); ++e) { | ||
context_id ctx = get_context_id(*e); | ||
by_context[ctx].insert(*e); | ||
} | ||
|
||
for(auto c = by_context.begin(); c != by_context.end(); ++c) | ||
c->second.wait(); | ||
} | ||
|
||
} // namespace compute | ||
} // namespace backend | ||
} // namespace vex | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
#ifndef VEXCL_BACKEND_CUDA_EVENT_HPP | ||
#define VEXCL_BACKEND_CUDA_EVENT_HPP | ||
|
||
/* | ||
The MIT License | ||
Copyright (c) 2012-2016 Denis Demidov <[email protected]> | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
*/ | ||
|
||
/** | ||
* \file vexcl/backend/compute/event.hpp | ||
* \author Denis Demidov <[email protected]> | ||
* \brief Bring Boost.Compute events into vex::backend::compute namespace. | ||
*/ | ||
|
||
#include <vexcl/backend/cuda/context.hpp> | ||
|
||
namespace vex { | ||
namespace backend { | ||
namespace cuda { | ||
|
||
namespace detail { | ||
|
||
template <> | ||
struct deleter_impl<CUevent> { | ||
static void dispose(CUevent e) { | ||
cuda_check( cuEventDestroy(e) ); | ||
} | ||
}; | ||
|
||
} // namespace detail | ||
|
||
class event { | ||
public: | ||
event(const command_queue &q) | ||
: q(q), e( create(q), detail::deleter(q.context().raw()) ) { } | ||
|
||
CUevent raw() const { return e.get(); } | ||
|
||
operator CUevent() const { return e.get(); } | ||
|
||
void wait() const { | ||
cuda_check( cuStreamWaitEvent(q.raw(), e.get(), 0) ); | ||
} | ||
|
||
vex::backend::context context() const { | ||
return q.context(); | ||
} | ||
private: | ||
command_queue q; | ||
std::shared_ptr<std::remove_pointer<CUevent>::type> e; | ||
|
||
static CUevent create(const command_queue &q) { | ||
CUevent e; | ||
q.context().set_current(); | ||
|
||
cuda_check( cuEventCreate(&e, CU_EVENT_DEFAULT) ); | ||
cuda_check( cuEventRecord(e, q.raw()) ); | ||
|
||
return e; | ||
} | ||
}; | ||
|
||
typedef std::vector<event> wait_list; | ||
|
||
/// Append event to wait list | ||
inline void wait_list_append(wait_list &dst, const event &e) { | ||
dst.push_back(e); | ||
} | ||
|
||
/// Append wait list to wait list | ||
inline void wait_list_append(wait_list &dst, const wait_list &src) { | ||
dst.insert(dst.begin(), src.begin(), src.end()); | ||
} | ||
|
||
/// Get id of the context the event was submitted into | ||
inline context_id get_context_id(const event &e) { | ||
return e.context().raw(); | ||
} | ||
|
||
/// Enqueue marker (with wait list) into the queue | ||
inline event enqueue_marker(const command_queue &q, const wait_list &events = wait_list()) { | ||
context_id ctx = q.context().raw(); | ||
|
||
q.context().set_current(); | ||
|
||
for(auto e = events.begin(); e != events.end(); ++e) | ||
if (get_context_id(*e) == ctx) | ||
cuda_check( cuEventSynchronize(e->raw()) ); | ||
|
||
return event(q); | ||
} | ||
|
||
/// Enqueue barrier (with wait list) into the queue | ||
inline event enqueue_barrier(command_queue &q, | ||
const wait_list &events = wait_list()) | ||
{ | ||
return enqueue_marker(q, events); | ||
} | ||
|
||
/// Wait for events in the list | ||
inline void wait_for_events(const wait_list &events) { | ||
for(auto e = events.begin(); e != events.end(); ++e) | ||
e->wait(); | ||
} | ||
|
||
} // namespace cuda | ||
} // namespace backend | ||
} // namespace vex | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.