Skip to content

Commit

Permalink
Tests and fixes for new vector ctors.
Browse files Browse the repository at this point in the history
  • Loading branch information
jszuppe committed Aug 17, 2016
1 parent d37f838 commit 860d41f
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 61 deletions.
111 changes: 50 additions & 61 deletions include/boost/compute/distributed/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ class vector
}

/// Creates a new vector and copies the values from \p other.
vector(const vector &other, bool blocking = false)
explicit vector(const vector &other, bool blocking = false)
: m_queue(other.m_queue),
m_size(other.m_size)
{
Expand All @@ -230,24 +230,38 @@ class vector
copy(other, m_queue, blocking);
}
else {
copy(other, other.get_queue(), m_queue, blocking);
command_queue other_queue = other.get_queue();
copy(other, other_queue, m_queue);
}
}

/// Creates a new vector and copies the values from \p other
/// with \p queue.
template<class OtherAlloc>
vector(const vector<T, weight, OtherAlloc> &other,
bool blocking = false)
: m_queue(other.m_queue),
m_size(other.m_size)
{
allocate_memory(m_size);
copy(other, m_queue, blocking);
}

/// Creates a new vector and copies the values from \p other.
template<class OtherAlloc>
vector(const vector<T, weight, OtherAlloc> &other,
command_queue &queue,
bool blocking = false)
: m_queue(queue),
m_size(other.m_size)
m_size(other.size())
{
allocate_memory(m_size);
if(m_queue == other.m_queue) {
if(m_queue == other.get_queue()) {
copy(other, m_queue, blocking);
}
else {
copy(other, other.get_queue(), m_queue, blocking);
command_queue other_queue = other.get_queue();
copy(other, other_queue, m_queue);
}
}

Expand Down Expand Up @@ -279,8 +293,8 @@ class vector
template<class OtherAlloc>
vector& operator=(const vector<T, weight, OtherAlloc> &other)
{
m_queue = other.m_queue;
m_size = other.m_size;
m_queue = other.get_queue();
m_size = other.size();
allocate_memory(m_size);
copy(other, m_queue, false);
return *this;
Expand Down Expand Up @@ -490,7 +504,12 @@ class vector
return m_data[n].get_buffer();
}

const command_queue& get_queue() const
// const command_queue& get_queue() const
// {
// return m_queue;
// }

command_queue get_queue() const
{
return m_queue;
}
Expand Down Expand Up @@ -585,8 +604,9 @@ class vector

// device -> device (copying distributed vector)
// both vectors must have the same command_queue
template<class OtherAlloc>
inline wait_list
copy_async(vector& other, command_queue &queue)
copy_async(const vector<T, weight, OtherAlloc> &other, command_queue &queue)
{
wait_list events;
events.reserve(m_data.size());
Expand All @@ -606,8 +626,9 @@ class vector

// device -> device (copying distributed vector)
// both vectors must have the same command_queue
template<class OtherAlloc>
inline void
copy(vector& other, command_queue &queue, bool blocking)
copy(const vector<T, weight, OtherAlloc> &other, command_queue &queue, bool blocking)
{
if(blocking) {
copy_async(other, queue).wait();
Expand All @@ -617,62 +638,30 @@ class vector
}

// device -> device (copying distributed vector)
inline wait_list
copy_async(vector& other, command_queue &other_queue, command_queue &queue)
template<class OtherAlloc>
inline void
copy(const vector<T, weight, OtherAlloc> &other,
command_queue &other_queue,
command_queue &queue)
{
wait_list events;
events.reserve(m_data.size());
::boost::compute::context other_part_context;
::boost::compute::context part_context;
for(size_t i = 0; i < m_data.size(); i++)
std::vector<T> host(other.size());
typename std::vector<T>::iterator host_iter = host.begin();
for(size_t i = 0; i < other.parts(); i++)
{
other_part_context = other.get_buffer(i).get_context();
part_context = get_buffer(i).get_context();
if(part_context == other_part_context) {
std::vector<T> host(other.part_size(i));
events.safe_insert(
::boost::compute::copy_async(
other.begin(i),
other.end(i),
host.begin(),
other_queue.get(i)
)
);
events.safe_insert(
::boost::compute::copy_async(
host.begin(),
host.end(),
begin(i),
queue.get(i)
)
);
}
else {
events.safe_insert(
::boost::compute::copy_async(
other.begin(i),
other.end(i),
begin(i),
queue.get(i)
)
);
}
}
return events;
}

// device -> device (copying distributed vector)
inline void
copy(vector& other,
command_queue &other_queue,
command_queue &queue,
bool blocking)
{
if(blocking) {
copy_async(other, other_queue, queue).wait();
} else {
copy_async(other, other_queue, queue);
events.safe_insert(
::boost::compute::copy_async(
other.begin(i),
other.end(i),
host_iter,
other_queue.get(i)
)
);
host_iter += other.part_size(i);
}
events.wait();
copy_async(host.begin(), host.end(), queue).wait();
}

private:
Expand Down
69 changes: 69 additions & 0 deletions test/test_distributed_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <boost/compute/algorithm.hpp>
#include <boost/compute/system.hpp>
#include <boost/compute/context.hpp>
#include <boost/compute/allocator/pinned_allocator.hpp>
#include <boost/compute/container/vector.hpp>
#include <boost/compute/distributed/context.hpp>
#include <boost/compute/distributed/command_queue.hpp>
Expand Down Expand Up @@ -178,4 +179,72 @@ BOOST_AUTO_TEST_CASE(host_iterator_ctor)
BOOST_CHECK_EQUAL(distributed_vector.front(), 1);
}

BOOST_AUTO_TEST_CASE(copy_ctor)
{
std::vector<bc::command_queue> queues;
queues.push_back(queue);
queues.push_back(queue);
bc::distributed::command_queue distributed_queue1(queues);
queues.push_back(queue);
bc::distributed::command_queue distributed_queue2(queues);

bc::int_ value = -1;
size_t size = 64;

bc::distributed::vector<bc::int_> distributed_vector(
size, value, distributed_queue1, true
);
bc::distributed::vector<bc::int_> distributed_vector_copy1(
distributed_vector, true
);
bc::distributed::vector<bc::int_> distributed_vector_copy2(
distributed_vector, distributed_queue2, true
);
bc::distributed::vector<
bc::int_,
bc::distributed::default_weight_func, bc::pinned_allocator<bc::int_>
> distributed_vector_copy3(
distributed_vector, distributed_queue2, true
);

for(size_t i = 0; i < distributed_vector.parts(); i++)
{
BOOST_CHECK(
bc::equal(
distributed_vector.begin(i),
distributed_vector.end(i),
bc::make_constant_iterator(value),
distributed_queue1.get(i)
)
);
BOOST_CHECK(
bc::equal(
distributed_vector_copy1.begin(i),
distributed_vector_copy1.end(i),
bc::make_constant_iterator(value),
distributed_queue1.get(i)
)
);
}
for(size_t i = 0; i < distributed_vector_copy2.parts(); i++)
{
BOOST_CHECK(
bc::equal(
distributed_vector_copy2.begin(i),
distributed_vector_copy2.end(i),
bc::make_constant_iterator(value),
distributed_queue2.get(i)
)
);
BOOST_CHECK(
bc::equal(
distributed_vector_copy3.begin(i),
distributed_vector_copy3.end(i),
bc::make_constant_iterator(value),
distributed_queue2.get(i)
)
);
}
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 860d41f

Please sign in to comment.