7 #ifndef NUKLEI_PARALLELIZER_H
8 #define NUKLEI_PARALLELIZER_H
16 #include <boost/filesystem.hpp>
17 #include <boost/asio.hpp>
18 #include <boost/thread.hpp>
22 template<
typename R,
typename Callable,
typename Pr
intAccessor>
23 std::vector<R> parallelizer::run_openmp(Callable callable,
24 PrintAccessor pa)
const
28 #pragma omp parallel for
30 for (
int i = 0; i < n_; ++i)
34 #pragma omp critical(nuklei_parallelizer_merge)
38 NUKLEI_INFO(
"Finished OpenMP thread " << i <<
" with value "
45 template<
typename R,
typename Callable,
typename Pr
intAccessor>
46 std::vector<R> parallelizer::run_fork(Callable callable,
47 PrintAccessor pa)
const
49 boost::filesystem::path endpoint_name = boost::filesystem::unique_path(
"/tmp/nuklei-%%%%-%%%%-%%%%-%%%%");
52 for (
int i = 0; i < n_; i++)
60 using boost::asio::local::stream_protocol;
65 stream_protocol::endpoint ep(endpoint_name.native());
66 stream_protocol::iostream stream(ep);
67 NUKLEI_SERIALIZATION_BINARY_OARCHIVE oa(stream);
68 oa & i & NUKLEI_SERIALIZATION_NVP(tmp);
79 using boost::asio::local::stream_protocol;
80 stream_protocol::endpoint ep(endpoint_name.native());
81 boost::asio::io_service io_service;
82 stream_protocol::acceptor acceptor(io_service, ep);
84 for (
int i = 0; i < n_; i++)
89 stream_protocol::iostream stream;
90 acceptor.accept(*stream.rdbuf());
91 NUKLEI_SERIALIZATION_BINARY_IARCHIVE ia(stream);
92 ia & fork_i & NUKLEI_SERIALIZATION_NVP(tmp);
96 NUKLEI_INFO(
"Finished fork " << fork_i <<
" with value "
101 boost::filesystem::remove(endpoint_name);
107 template<
typename R,
typename Callable,
typename Pr
intAccessor>
108 std::vector<R> parallelizer::run_pthread(Callable callable,
109 PrintAccessor pa)
const
111 std::vector<R> retv(n_);
112 std::vector< boost::shared_ptr<boost::thread> > threads;
113 for (
int i = 0; i < n_; ++i)
130 boost::shared_ptr<boost::thread> thread
132 (boost::bind<void>(pthread_wrapper<R, Callable>(callable),
133 boost::ref(retv.at(i)))));
134 threads.push_back(thread);
136 for (
int i = 0; i < n_; ++i)
138 threads.at(i)->join();
139 NUKLEI_INFO(
"Finished thread " << i <<
" with value "
140 << pa(retv.at(i)) <<
".");
145 template<
typename R,
typename Callable,
typename Pr
intAccessor>
146 std::vector<R> parallelizer::run_single(Callable callable,
147 PrintAccessor pa)
const
150 for (
int i = 0; i < n_; ++i)
154 NUKLEI_INFO(
"Finished slice " << i <<
" with value "