ModErn Text Analysis
META Enumerates Textual Applications
parallel_for.h
Go to the documentation of this file.
1 
10 #ifndef META_PARALLEL_FOR_H_
11 #define META_PARALLEL_FOR_H_
12 
13 #include <algorithm>
14 #include <iterator>
15 #include <thread>
16 #include <vector>
17 
18 #include "parallel/thread_pool.h"
19 
20 namespace meta
21 {
22 namespace parallel
23 {
24 
31 template <class Iterator, class Function>
32 void parallel_for(Iterator begin, Iterator end, Function func)
33 {
34  thread_pool pool;
35  parallel_for(begin, end, pool, func);
36 }
37 
45 template <class Iterator, class Function>
46 void parallel_for(Iterator begin, Iterator end, thread_pool& pool,
47  Function func)
48 {
49  auto block_size = std::distance(begin, end)
50  / std::thread::hardware_concurrency();
51 
52  Iterator last = begin;
53  if (block_size > 0)
54  {
55  std::advance(last,
56  (std::thread::hardware_concurrency() - 1) * block_size);
57  }
58  else
59  {
60  last = end;
61  block_size = 1;
62  }
63 
64  std::vector<std::future<void>> futures;
65  // first p - 1 groups
66  for (; begin != last; std::advance(begin, block_size))
67  {
68  futures.emplace_back(pool.submit_task([=]()
69  {
70  auto mylast = begin;
71  std::advance(mylast, block_size);
72  std::for_each(begin, mylast, func);
73  }));
74  }
75  // last group
76  futures.emplace_back(pool.submit_task([=]()
77  { std::for_each(begin, end, func); }));
78  for (auto& fut : futures)
79  fut.get();
80 }
81 }
82 }
83 
84 #endif
std::future< typename std::result_of< Function()>::type > submit_task(Function func)
Adds a task to the thread_pool.
Definition: thread_pool.h:70
void parallel_for(Iterator begin, Iterator end, Function func)
Runs the given function on the range denoted by begin and end in parallel.
Definition: parallel_for.h:32
The ModErn Text Analysis toolkit is a suite of natural language processing, classification, information retreival, data mining, and other applications of text processing.
Definition: analyzer.h:24
Represents a collection of a fixed number of threads, which tasks can be added to.
Definition: thread_pool.h:33