|
| 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