piranha  0.10
parallel_vector_transform.hpp
1 /* Copyright 2009-2017 Francesco Biscani (bluescarni@gmail.com)
2 
3 This file is part of the Piranha library.
4 
5 The Piranha library is free software; you can redistribute it and/or modify
6 it under the terms of either:
7 
8  * the GNU Lesser General Public License as published by the Free
9  Software Foundation; either version 3 of the License, or (at your
10  option) any later version.
11 
12 or
13 
14  * the GNU General Public License as published by the Free Software
15  Foundation; either version 3 of the License, or (at your option) any
16  later version.
17 
18 or both in parallel, as here.
19 
20 The Piranha library is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 for more details.
24 
25 You should have received copies of the GNU General Public License and the
26 GNU Lesser General Public License along with the Piranha library. If not,
27 see https://www.gnu.org/licenses/. */
28 
29 #ifndef PIRANHA_DETAIL_PARALLEL_VECTOR_TRANSFORM_HPP
30 #define PIRANHA_DETAIL_PARALLEL_VECTOR_TRANSFORM_HPP
31 
32 #include <algorithm>
33 #include <stdexcept>
34 #include <vector>
35 
36 #include <piranha/config.hpp>
37 #include <piranha/exceptions.hpp>
38 #include <piranha/thread_pool.hpp>
39 
40 namespace piranha
41 {
42 namespace detail
43 {
44 
45 // A parallel version of std::transform that operates on vectors.
46 template <typename T, typename U, typename Op>
47 inline void parallel_vector_transform(unsigned n_threads, const std::vector<T> &ic, std::vector<U> &oc, Op op)
48 {
49  if (unlikely(n_threads == 0u)) {
50  piranha_throw(std::invalid_argument, "invalid number of threads");
51  }
52  if (unlikely(ic.size() != oc.size())) {
53  piranha_throw(std::invalid_argument, "mismatched vector sizes");
54  }
55  if (n_threads == 1u) {
56  std::transform(ic.begin(), ic.end(), oc.begin(), op);
57  return;
58  }
59  const auto block_size = ic.size() / n_threads;
60  auto local_transform = [&op](T const *b, T const *e, U *o) { std::transform(b, e, o, op); };
61  future_list<decltype(local_transform(ic.data(), ic.data(), oc.data()))> ff_list;
62  try {
63  for (unsigned i = 0u; i < n_threads; ++i) {
64  auto b = ic.data() + i * block_size;
65  auto e = (i == n_threads - 1u) ? (ic.data() + ic.size()) : (ic.data() + (i + 1u) * block_size);
66  auto o = oc.data() + i * block_size;
67  ff_list.push_back(thread_pool::enqueue(i, local_transform, b, e, o));
68  }
69  // First let's wait for everything to finish.
70  ff_list.wait_all();
71  // Then, let's handle the exceptions.
72  ff_list.get_all();
73  } catch (...) {
74  ff_list.wait_all();
75  throw;
76  }
77 }
78 }
79 }
80 
81 #endif
Exceptions.
static enqueue_t< F &&, Args &&... > enqueue(unsigned n, F &&f, Args &&... args)
Enqueue task.
#define piranha_throw(exception_type,...)
Exception-throwing macro.
Definition: exceptions.hpp:118
Root piranha namespace.
Definition: array_key.hpp:52