Skip to content

Commit

Permalink
Use distributed::copy() algorithm in distributed::vector
Browse files Browse the repository at this point in the history
  • Loading branch information
jszuppe committed Aug 22, 2016
1 parent 854acc5 commit 1c9fd33
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 229 deletions.
10 changes: 9 additions & 1 deletion include/boost/compute/distributed/copy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,20 @@

#include <boost/compute/distributed/context.hpp>
#include <boost/compute/distributed/command_queue.hpp>
#include <boost/compute/distributed/vector.hpp>
#include <boost/compute/distributed/detail/weight_func.hpp>

namespace boost {
namespace compute {
namespace distributed {

// forward declaration for distributed::vector
template<
class T,
weight_func weight,
class Alloc
>
class vector;

namespace detail {

template<class T>
Expand Down
79 changes: 79 additions & 0 deletions include/boost/compute/distributed/detail/weight_func.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2016 Jakub Szuppe <[email protected]>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
// See http://boostorg.github.com/compute for more information.
//---------------------------------------------------------------------------//

#ifndef BOOST_COMPUTE_DETAIL_WEIGHT_FUNC_HPP
#define BOOST_COMPUTE_DETAIL_WEIGHT_FUNC_HPP

#include <vector>

#include <boost/compute/distributed/context.hpp>
#include <boost/compute/distributed/command_queue.hpp>

namespace boost {
namespace compute {
namespace distributed {

typedef std::vector<double> (*weight_func)(const command_queue&);

namespace detail {

/// \internal_
/// Rounds up \p n to the nearest multiple of \p m.
/// Note: \p m must be a multiple of 2.
size_t round_up(size_t n, size_t m)
{
assert(m && ((m & (m -1)) == 0));
return (n + m - 1) & ~(m - 1);
}

/// \internal_
///
std::vector<size_t> partition(const command_queue& queue,
weight_func weight_func,
const size_t size,
const size_t align)
{
std::vector<double> weights = weight_func(queue);
std::vector<size_t> partition;
partition.reserve(queue.size() + 1);
partition.push_back(0);

if(queue.size() > 1)
{
double acc = 0;
for(size_t i = 0; i < queue.size(); i++)
{
acc += weights[i];
partition.push_back(
std::min(
size,
round_up(size * acc, align)
)
);
}
return partition;
}
partition.push_back(size);
return partition;
}

} // end distributed detail

std::vector<double> default_weight_func(const command_queue& queue)
{
return std::vector<double>(queue.size(), 1.0/queue.size());
}

} // end distributed namespace
} // end compute namespace
} // end boost namespace


#endif /* INCLUDE_BOOST_COMPUTE_DETAIL_WEIGHT_FUNC_HPP_ */
Loading

0 comments on commit 1c9fd33

Please sign in to comment.