parallelizer_decl.h
Go to the documentation of this file.
1 // (C) Copyright Renaud Detry 2007-2015.
2 // Distributed under the GNU General Public License and under the
3 // BSD 3-Clause License (See accompanying file LICENSE.txt).
4 
5 /** @file */
6 
7 #ifndef NUKLEI_PARALLELIZER_DECL_H
8 #define NUKLEI_PARALLELIZER_DECL_H
9 
10 #include <nuklei/Random.h>
11 #include <nuklei/Common.h>
13 
14 #include <cstdlib>
15 #include <boost/filesystem.hpp>
16 #include <boost/asio.hpp>
17 #include <boost/thread.hpp>
18 
19 namespace nuklei {
20 
21  struct parallelizer
22  {
23  typedef enum { OPENMP = 0, FORK, PTHREAD, SINGLE, UNKNOWN } Type;
24  static const Type defaultType = OPENMP;
25  static const std::string TypeNames[];
26 
27  /**
28  * If chosing the fork()-based implementation, make sure that your program
29  * consists of a single thread at the time run() is called, or you will run
30  * into problems (Google "forking a multithreaded program" to see why).
31  */
32  parallelizer(const int n,
33  const Type& type = OPENMP,
34  const unsigned long seed = 0) :
35  n_(n), type_(type), seed_(seed) {}
36 
38  {
39  template<typename T>
40  std::string operator()(const T& t)
41  {
42  return "n/a";
43  }
44  };
45 
47  {
48  template<typename T>
49  const T& operator()(const T& t)
50  {
51  return t;
52  }
53  };
54 
55  template
56  <typename R, typename Callable>
57  std::vector<R> run(Callable callable) const
58  {
59  return run<R>(callable, na_print_accessor());
60  }
61 
62  template
63  <typename R, typename Callable, typename PrintAccessor>
64  std::vector<R> run(Callable callable,
65  PrintAccessor pa) const
66  {
67  switch (type_)
68  {
69  case OPENMP:
70  return run_openmp<R>(callable, pa);
71  break;
72  case FORK:
73  return run_fork<R>(callable, pa);
74  break;
75  case PTHREAD:
76  return run_pthread<R>(callable, pa);
77  break;
78  case SINGLE:
79  return run_single<R>(callable, pa);
80  break;
81  default:
82  NUKLEI_THROW("Unknown parallelization method.");
83  }
84  return std::vector<R>();
85  }
86 
87  private:
88 
89  template<typename R, typename Callable, typename PrintAccessor>
90  std::vector<R> run_openmp(Callable callable,
91  PrintAccessor pa) const;
92 
93  template<typename R, typename Callable, typename PrintAccessor>
94  std::vector<R> run_fork(Callable callable,
95  PrintAccessor pa) const;
96 
97  template<typename R, typename Callable>
98  struct pthread_wrapper
99  {
100  pthread_wrapper(Callable callable) : callable_(callable) {}
101  void operator()(R& ret)
102  {
103  ret = callable_();
104  }
105  private:
106  Callable callable_;
107  };
108 
109  template<typename R, typename Callable, typename PrintAccessor>
110  std::vector<R> run_pthread(Callable callable,
111  PrintAccessor pa) const;
112 
113  template<typename R, typename Callable, typename PrintAccessor>
114  std::vector<R> run_single(Callable callable,
115  PrintAccessor pa) const;
116 
117  static void reap();
118 
119  int n_;
120  Type type_;
121  unsigned long seed_;
122  };
123 
124 }
125 
126 #endif
Public namespace.
Definition: Color.cpp:9
parallelizer(const int n, const Type &type=OPENMP, const unsigned long seed=0)
#define NUKLEI_THROW(x)
Throws an Error.
Definition: Common.h:94
© Copyright 2007-2013 Renaud Detry.
Distributed under the terms of the GNU General Public License (GPL).
(See accompanying file LICENSE.txt or copy at http://www.gnu.org/copyleft/gpl.html.)
Revised Sun Sep 13 2020 19:10:06.