GrPPI  0.2
Generic and Reusable Parallel Pattern Interface
native/stencil.h
Go to the documentation of this file.
1 
21 #ifndef GRPPI_NATIVE_STENCIL_H
22 #define GRPPI_NATIVE_STENCIL_H
23 
25 #include "../common/iterator.h"
26 
27 namespace grppi {
28 
51 template <typename InputIt, typename OutputIt, typename StencilTransformer,
52  typename Neighbourhood>
54  InputIt first, InputIt last, OutputIt first_out,
55  StencilTransformer transform_op, Neighbourhood neighbour_op)
56 {
57  using namespace std;
58 
59  vector<thread> tasks;
60  int size = distance(first,last);
61  int elements_per_thread = size/ex.concurrency_degree();
62 
63  for (int i=1; i<ex.concurrency_degree(); ++i) {
64  auto begin = first + (elements_per_thread * i);
65  auto end = (i==ex.concurrency_degree()-1)?
66  last :
67  next(first, elements_per_thread * (i+1));
68 
69  auto out = first_out + (elements_per_thread * i);
70 
71  tasks.emplace_back([&](InputIt begin, InputIt end, OutputIt out) {
72  auto manager = ex.thread_manager();
73 
74  while (begin!=end) {
75  *out = transform_op(begin, neighbour_op(begin));
76  begin++;
77  out++;
78  }
79  },
80  begin, end, out);
81  }
82 
83  auto end = first + elements_per_thread;
84  while(first!=end){
85  *first_out = transform_op(first, neighbour_op(first));
86  first++;
87  first_out++;
88  }
89 
90  for (auto && t : tasks) { t.join(); }
91 }
92 
109 template <typename InputIt, typename OutputIt, typename StencilTransformer,
110  typename Neighbourhood, typename ... OtherInputIts>
112  InputIt first, InputIt last, OutputIt first_out,
113  StencilTransformer transform_op, Neighbourhood neighbour_op,
114  OtherInputIts ... other_firsts )
115 {
116  using namespace std;
117 
118  vector<thread> tasks;
119  int size = last - first;
120  int elements_per_thread = size/ex.concurrency_degree();
121 
122  for (int i=1; i<ex.concurrency_degree(); i++){
123  auto begin = first + (elements_per_thread * i);
124  auto end = (i==ex.concurrency_degree()-1)?
125  last :
126  first + elements_per_thread * (i+1);
127 
128  auto out = first_out + (elements_per_thread * i);
129 
130  tasks.emplace_back(
131  [&](InputIt begin, InputIt end, OutputIt out, int i, int n, OtherInputIts ... other_firsts){
132  auto manager = ex.thread_manager();
133 
134  advance_iterators(n*i, other_firsts ...);
135  while (begin!=end) {
136  *out = transform_op(begin, neighbour_op(begin,other_firsts...));
137  begin++;
138  advance_iterators(other_firsts ... );
139  out++;
140  }
141  },
142  begin, end, out, i, elements_per_thread,other_firsts ...);
143  }
144 
145  auto end = first + elements_per_thread;
146  while (first!=end) {
147  *first_out = transform_op(*first, neighbour_op(first,other_firsts...));
148  first++;
149  advance_iterators( other_firsts ... );
150  first_out++;
151  }
152 
153 
154  for (auto && t : tasks) { t.join(); }
155 }
156 
162 }
163 #endif
Definition: callable_traits.h:24
void stencil(parallel_execution_native &ex, InputIt first, InputIt last, OutputIt first_out, StencilTransformer transform_op, Neighbourhood neighbour_op)
Invoke Stencil pattern on a data sequence with native parallel execution.
Definition: native/stencil.h:53
int concurrency_degree() const noexcept
Get number of grppi trheads.
Definition: parallel_execution_native.h:178
STL namespace.
Native parallel execution policy. This policy uses ISO C++ threads as implementation building block a...
Definition: parallel_execution_native.h:136
native_thread_manager thread_manager()
Get a manager object for registration/deregistration in the thread index table for current thread...
Definition: parallel_execution_native.h:199
void advance_iterators(size_t delta, InputIt &...in)
Definition: iterator.h:29