Skip to content

Commit

Permalink
Expose events, markers, and barriers to the user
Browse files Browse the repository at this point in the history
This should be enough to organize dependency graph in a generic way,
without changing the existing API.

see #196
  • Loading branch information
ddemidov committed Mar 18, 2016
1 parent b6e7852 commit 9084216
Show file tree
Hide file tree
Showing 8 changed files with 387 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ add_vexcl_test(reduce_by_key reduce_by_key.cpp)
add_vexcl_test(logical logical.cpp)
add_vexcl_test(threads threads.cpp)
add_vexcl_test(svm svm.cpp)
add_vexcl_test(events events.cpp)
add_vexcl_test(multiple_objects "dummy1.cpp;dummy2.cpp")

if (NOT DEFINED ENV{APPVEYOR})
Expand Down
32 changes: 32 additions & 0 deletions tests/events.cpp
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()

1 change: 1 addition & 0 deletions vexcl/backend/compute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ namespace vex {

#include <vexcl/backend/compute/compiler.hpp>
#include <vexcl/backend/compute/kernel.hpp>
#include <vexcl/backend/compute/event.hpp>

#endif
106 changes: 106 additions & 0 deletions vexcl/backend/compute/event.hpp
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
1 change: 1 addition & 0 deletions vexcl/backend/cuda.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ THE SOFTWARE.
#include <vexcl/backend/cuda/source.hpp>
#include <vexcl/backend/cuda/compiler.hpp>
#include <vexcl/backend/cuda/kernel.hpp>
#include <vexcl/backend/cuda/event.hpp>

#endif
130 changes: 130 additions & 0 deletions vexcl/backend/cuda/event.hpp
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
1 change: 1 addition & 0 deletions vexcl/backend/opencl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ THE SOFTWARE.
#include <vexcl/backend/opencl/source.hpp>
#include <vexcl/backend/opencl/compiler.hpp>
#include <vexcl/backend/opencl/kernel.hpp>
#include <vexcl/backend/opencl/event.hpp>

#endif
Loading

0 comments on commit 9084216

Please sign in to comment.