Skip to content

Commit 1c9fd33

Browse files
committed
Use distributed::copy() algorithm in distributed::vector
1 parent 854acc5 commit 1c9fd33

File tree

4 files changed

+163
-229
lines changed

4 files changed

+163
-229
lines changed

include/boost/compute/distributed/copy.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,20 @@
2727

2828
#include <boost/compute/distributed/context.hpp>
2929
#include <boost/compute/distributed/command_queue.hpp>
30-
#include <boost/compute/distributed/vector.hpp>
30+
#include <boost/compute/distributed/detail/weight_func.hpp>
3131

3232
namespace boost {
3333
namespace compute {
3434
namespace distributed {
3535

36+
// forward declaration for distributed::vector
37+
template<
38+
class T,
39+
weight_func weight,
40+
class Alloc
41+
>
42+
class vector;
43+
3644
namespace detail {
3745

3846
template<class T>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//---------------------------------------------------------------------------//
2+
// Copyright (c) 2016 Jakub Szuppe <j.szuppe@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+
#ifndef BOOST_COMPUTE_DETAIL_WEIGHT_FUNC_HPP
12+
#define BOOST_COMPUTE_DETAIL_WEIGHT_FUNC_HPP
13+
14+
#include <vector>
15+
16+
#include <boost/compute/distributed/context.hpp>
17+
#include <boost/compute/distributed/command_queue.hpp>
18+
19+
namespace boost {
20+
namespace compute {
21+
namespace distributed {
22+
23+
typedef std::vector<double> (*weight_func)(const command_queue&);
24+
25+
namespace detail {
26+
27+
/// \internal_
28+
/// Rounds up \p n to the nearest multiple of \p m.
29+
/// Note: \p m must be a multiple of 2.
30+
size_t round_up(size_t n, size_t m)
31+
{
32+
assert(m && ((m & (m -1)) == 0));
33+
return (n + m - 1) & ~(m - 1);
34+
}
35+
36+
/// \internal_
37+
///
38+
std::vector<size_t> partition(const command_queue& queue,
39+
weight_func weight_func,
40+
const size_t size,
41+
const size_t align)
42+
{
43+
std::vector<double> weights = weight_func(queue);
44+
std::vector<size_t> partition;
45+
partition.reserve(queue.size() + 1);
46+
partition.push_back(0);
47+
48+
if(queue.size() > 1)
49+
{
50+
double acc = 0;
51+
for(size_t i = 0; i < queue.size(); i++)
52+
{
53+
acc += weights[i];
54+
partition.push_back(
55+
std::min(
56+
size,
57+
round_up(size * acc, align)
58+
)
59+
);
60+
}
61+
return partition;
62+
}
63+
partition.push_back(size);
64+
return partition;
65+
}
66+
67+
} // end distributed detail
68+
69+
std::vector<double> default_weight_func(const command_queue& queue)
70+
{
71+
return std::vector<double>(queue.size(), 1.0/queue.size());
72+
}
73+
74+
} // end distributed namespace
75+
} // end compute namespace
76+
} // end boost namespace
77+
78+
79+
#endif /* INCLUDE_BOOST_COMPUTE_DETAIL_WEIGHT_FUNC_HPP_ */

0 commit comments

Comments
 (0)