GrPPI  0.2
Generic and Reusable Parallel Pattern Interface
omp/map.h
Go to the documentation of this file.
1 /*
2 * @version GrPPI v0.2
3 * @copyright Copyright (C) 2017 Universidad Carlos III de Madrid. All rights reserved.
4 * @license GNU/GPL, see LICENSE.txt
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You have received a copy of the GNU General Public License in LICENSE.txt
16 * also available in <http://www.gnu.org/licenses/gpl.html>.
17 *
18 * See COPYRIGHT.txt for copyright notices and details.
19 */
20 
21 #ifndef GRPPI_OMP_MAP_H
22 #define GRPPI_OMP_MAP_H
23 
24 #ifdef GRPPI_OMP
25 
26 #include "parallel_execution_omp.h"
27 
28 namespace grppi {
29 
30 template <typename InputIt, typename OutputIt, typename Transformer,
31  typename ... OtherInputIts>
33  InputIt first, InputIt last,
34  OutputIt first_out,
35  Transformer && transf_op,
36  int i,
37  int elemperthr, OtherInputIts ... more_firsts)
38 {
39  //Calculate local input and output iterator
40  auto begin = first + (elemperthr * i);
41  auto end = first + (elemperthr * (i+1));
42  if( i == ex.concurrency_degree()-1) end = last;
43  auto out = first_out + (elemperthr * i);
44  advance_iterators(elemperthr*i, more_firsts ...);
45  while(begin!=end){
46  *out = transf_op(*begin, *more_firsts ...);
47  advance_iterators(more_firsts ...);
48  begin++;
49  out++;
50  }
51 }
52 
73 template <typename InputIt, typename OutputIt, typename Transformer>
75  InputIt first, InputIt last,
76  OutputIt first_out,
77  Transformer && transf_op)
78 {
79  int numElements = last - first;
80 
81  int elemperthr = numElements/ex.concurrency_degree();
82 
83  #pragma omp parallel
84  {
85  #pragma omp single nowait
86  {
87  for(int i=1;i<ex.concurrency_degree();i++){
88 
89 
90 
91  #pragma omp task firstprivate(i)
92  {
93  auto begin = first + (elemperthr * i);
94  auto end = first + (elemperthr * (i+1));
95  if(i == ex.concurrency_degree() -1 ) end = last;
96  auto out = first_out + (elemperthr * i);
97  while(begin!=end){
98  *out = transf_op(*begin);
99  begin++;
100  out++;
101  }
102  }
103  }
104  //Map main threads
105  auto beg =first;
106  auto out = first_out;
107  auto end = first+elemperthr;
108  while(beg!=end){
109  *out = transf_op(*beg);
110  beg++;
111  out++;
112  }
113  #pragma omp taskwait
114  }
115  }
116 }
117 
132 template <typename InputIt, typename OutputIt, typename Transformer,
133  typename ... OtherInputIts>
135  InputIt first, InputIt last,
136  OutputIt first_out,
137  Transformer && transf_op,
138  OtherInputIts ... more_firsts)
139 {
140  //Calculate number of elements per thread
141  int numElements = last - first;
142  int elemperthr = numElements/ex.concurrency_degree();
143 
144  //Create tasks
145  #pragma omp parallel
146  {
147  #pragma omp single nowait
148  {
149  for(int i=1;i<ex.concurrency_degree();i++){
150 
151  #pragma omp task firstprivate(i)
152  {
153  internal_map(ex, first, last, first_out,
154  std::forward<Transformer>(transf_op) , i, elemperthr,
155  more_firsts ...);
156  }
157  //End task
158  }
159 
160  //Map main thread
161  internal_map(ex, first,last, first_out,
162  std::forward<Transformer>(transf_op), 0, elemperthr,
163  more_firsts ...);
164 
165  //Join threads
166  #pragma omp taskwait
167  }
168  }
169 }
170 
175 }
176 
177 #endif
178 
179 #endif
Definition: callable_traits.h:24
void internal_map(parallel_execution_omp &ex, InputIt first, InputIt last, OutputIt first_out, Transformer &&transf_op, int i, int elemperthr, OtherInputIts...more_firsts)
Definition: omp/map.h:32
OpenMP parallel execution policy.
Definition: parallel_execution_omp.h:40
void advance_iterators(size_t delta, InputIt &...in)
Definition: iterator.h:29
int concurrency_degree() const noexcept
Get number of grppi trheads.
Definition: parallel_execution_omp.h:85
void map(parallel_execution_native &ex, InputIt first, InputIt last, OutputIt first_out, Transformer &&transf_op)
Invoke Map pattern on a data sequence with native paralell execution.
Definition: native/map.h:50