Skip to content

Commit 860d41f

Browse files
committed
Tests and fixes for new vector ctors.
1 parent d37f838 commit 860d41f

File tree

2 files changed

+119
-61
lines changed

2 files changed

+119
-61
lines changed

include/boost/compute/distributed/vector.hpp

+50-61
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ class vector
211211
}
212212

213213
/// Creates a new vector and copies the values from \p other.
214-
vector(const vector &other, bool blocking = false)
214+
explicit vector(const vector &other, bool blocking = false)
215215
: m_queue(other.m_queue),
216216
m_size(other.m_size)
217217
{
@@ -230,24 +230,38 @@ class vector
230230
copy(other, m_queue, blocking);
231231
}
232232
else {
233-
copy(other, other.get_queue(), m_queue, blocking);
233+
command_queue other_queue = other.get_queue();
234+
copy(other, other_queue, m_queue);
234235
}
235236
}
236237

238+
/// Creates a new vector and copies the values from \p other
239+
/// with \p queue.
240+
template<class OtherAlloc>
241+
vector(const vector<T, weight, OtherAlloc> &other,
242+
bool blocking = false)
243+
: m_queue(other.m_queue),
244+
m_size(other.m_size)
245+
{
246+
allocate_memory(m_size);
247+
copy(other, m_queue, blocking);
248+
}
249+
237250
/// Creates a new vector and copies the values from \p other.
238251
template<class OtherAlloc>
239252
vector(const vector<T, weight, OtherAlloc> &other,
240253
command_queue &queue,
241254
bool blocking = false)
242255
: m_queue(queue),
243-
m_size(other.m_size)
256+
m_size(other.size())
244257
{
245258
allocate_memory(m_size);
246-
if(m_queue == other.m_queue) {
259+
if(m_queue == other.get_queue()) {
247260
copy(other, m_queue, blocking);
248261
}
249262
else {
250-
copy(other, other.get_queue(), m_queue, blocking);
263+
command_queue other_queue = other.get_queue();
264+
copy(other, other_queue, m_queue);
251265
}
252266
}
253267

@@ -279,8 +293,8 @@ class vector
279293
template<class OtherAlloc>
280294
vector& operator=(const vector<T, weight, OtherAlloc> &other)
281295
{
282-
m_queue = other.m_queue;
283-
m_size = other.m_size;
296+
m_queue = other.get_queue();
297+
m_size = other.size();
284298
allocate_memory(m_size);
285299
copy(other, m_queue, false);
286300
return *this;
@@ -490,7 +504,12 @@ class vector
490504
return m_data[n].get_buffer();
491505
}
492506

493-
const command_queue& get_queue() const
507+
// const command_queue& get_queue() const
508+
// {
509+
// return m_queue;
510+
// }
511+
512+
command_queue get_queue() const
494513
{
495514
return m_queue;
496515
}
@@ -585,8 +604,9 @@ class vector
585604

586605
// device -> device (copying distributed vector)
587606
// both vectors must have the same command_queue
607+
template<class OtherAlloc>
588608
inline wait_list
589-
copy_async(vector& other, command_queue &queue)
609+
copy_async(const vector<T, weight, OtherAlloc> &other, command_queue &queue)
590610
{
591611
wait_list events;
592612
events.reserve(m_data.size());
@@ -606,8 +626,9 @@ class vector
606626

607627
// device -> device (copying distributed vector)
608628
// both vectors must have the same command_queue
629+
template<class OtherAlloc>
609630
inline void
610-
copy(vector& other, command_queue &queue, bool blocking)
631+
copy(const vector<T, weight, OtherAlloc> &other, command_queue &queue, bool blocking)
611632
{
612633
if(blocking) {
613634
copy_async(other, queue).wait();
@@ -617,62 +638,30 @@ class vector
617638
}
618639

619640
// device -> device (copying distributed vector)
620-
inline wait_list
621-
copy_async(vector& other, command_queue &other_queue, command_queue &queue)
641+
template<class OtherAlloc>
642+
inline void
643+
copy(const vector<T, weight, OtherAlloc> &other,
644+
command_queue &other_queue,
645+
command_queue &queue)
622646
{
623647
wait_list events;
624648
events.reserve(m_data.size());
625-
::boost::compute::context other_part_context;
626-
::boost::compute::context part_context;
627-
for(size_t i = 0; i < m_data.size(); i++)
649+
std::vector<T> host(other.size());
650+
typename std::vector<T>::iterator host_iter = host.begin();
651+
for(size_t i = 0; i < other.parts(); i++)
628652
{
629-
other_part_context = other.get_buffer(i).get_context();
630-
part_context = get_buffer(i).get_context();
631-
if(part_context == other_part_context) {
632-
std::vector<T> host(other.part_size(i));
633-
events.safe_insert(
634-
::boost::compute::copy_async(
635-
other.begin(i),
636-
other.end(i),
637-
host.begin(),
638-
other_queue.get(i)
639-
)
640-
);
641-
events.safe_insert(
642-
::boost::compute::copy_async(
643-
host.begin(),
644-
host.end(),
645-
begin(i),
646-
queue.get(i)
647-
)
648-
);
649-
}
650-
else {
651-
events.safe_insert(
652-
::boost::compute::copy_async(
653-
other.begin(i),
654-
other.end(i),
655-
begin(i),
656-
queue.get(i)
657-
)
658-
);
659-
}
660-
}
661-
return events;
662-
}
663-
664-
// device -> device (copying distributed vector)
665-
inline void
666-
copy(vector& other,
667-
command_queue &other_queue,
668-
command_queue &queue,
669-
bool blocking)
670-
{
671-
if(blocking) {
672-
copy_async(other, other_queue, queue).wait();
673-
} else {
674-
copy_async(other, other_queue, queue);
653+
events.safe_insert(
654+
::boost::compute::copy_async(
655+
other.begin(i),
656+
other.end(i),
657+
host_iter,
658+
other_queue.get(i)
659+
)
660+
);
661+
host_iter += other.part_size(i);
675662
}
663+
events.wait();
664+
copy_async(host.begin(), host.end(), queue).wait();
676665
}
677666

678667
private:

test/test_distributed_vector.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <boost/compute/algorithm.hpp>
1515
#include <boost/compute/system.hpp>
1616
#include <boost/compute/context.hpp>
17+
#include <boost/compute/allocator/pinned_allocator.hpp>
1718
#include <boost/compute/container/vector.hpp>
1819
#include <boost/compute/distributed/context.hpp>
1920
#include <boost/compute/distributed/command_queue.hpp>
@@ -178,4 +179,72 @@ BOOST_AUTO_TEST_CASE(host_iterator_ctor)
178179
BOOST_CHECK_EQUAL(distributed_vector.front(), 1);
179180
}
180181

182+
BOOST_AUTO_TEST_CASE(copy_ctor)
183+
{
184+
std::vector<bc::command_queue> queues;
185+
queues.push_back(queue);
186+
queues.push_back(queue);
187+
bc::distributed::command_queue distributed_queue1(queues);
188+
queues.push_back(queue);
189+
bc::distributed::command_queue distributed_queue2(queues);
190+
191+
bc::int_ value = -1;
192+
size_t size = 64;
193+
194+
bc::distributed::vector<bc::int_> distributed_vector(
195+
size, value, distributed_queue1, true
196+
);
197+
bc::distributed::vector<bc::int_> distributed_vector_copy1(
198+
distributed_vector, true
199+
);
200+
bc::distributed::vector<bc::int_> distributed_vector_copy2(
201+
distributed_vector, distributed_queue2, true
202+
);
203+
bc::distributed::vector<
204+
bc::int_,
205+
bc::distributed::default_weight_func, bc::pinned_allocator<bc::int_>
206+
> distributed_vector_copy3(
207+
distributed_vector, distributed_queue2, true
208+
);
209+
210+
for(size_t i = 0; i < distributed_vector.parts(); i++)
211+
{
212+
BOOST_CHECK(
213+
bc::equal(
214+
distributed_vector.begin(i),
215+
distributed_vector.end(i),
216+
bc::make_constant_iterator(value),
217+
distributed_queue1.get(i)
218+
)
219+
);
220+
BOOST_CHECK(
221+
bc::equal(
222+
distributed_vector_copy1.begin(i),
223+
distributed_vector_copy1.end(i),
224+
bc::make_constant_iterator(value),
225+
distributed_queue1.get(i)
226+
)
227+
);
228+
}
229+
for(size_t i = 0; i < distributed_vector_copy2.parts(); i++)
230+
{
231+
BOOST_CHECK(
232+
bc::equal(
233+
distributed_vector_copy2.begin(i),
234+
distributed_vector_copy2.end(i),
235+
bc::make_constant_iterator(value),
236+
distributed_queue2.get(i)
237+
)
238+
);
239+
BOOST_CHECK(
240+
bc::equal(
241+
distributed_vector_copy3.begin(i),
242+
distributed_vector_copy3.end(i),
243+
bc::make_constant_iterator(value),
244+
distributed_queue2.get(i)
245+
)
246+
);
247+
}
248+
}
249+
181250
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)