SkePU  1.2
 All Classes Namespaces Files Functions Variables Enumerations Friends Macros Groups Pages
mapreduce.h
Go to the documentation of this file.
1 
5 #ifndef MAPREDUCE_H
6 #define MAPREDUCE_H
7 
8 #ifdef SKEPU_OPENCL
9 #include <string>
10 #include <vector>
11 #ifdef USE_MAC_OPENCL
12 #include <OpenCL/opencl.h>
13 #else
14 #include <CL/cl.h>
15 #endif
16 #include "src/device_cl.h"
17 #endif
18 
19 #include "src/environment.h"
20 
21 #include "skepu/vector.h"
22 #include "skepu/matrix.h"
23 #include "skepu/sparse_matrix.h"
24 
25 #include "src/operator_macros.h"
26 #include "src/exec_plan.h"
27 
28 namespace skepu
29 {
30 
53 template <typename MapFunc, typename ReduceFunc>
54 class MapReduce
55 {
56 
57 public:
58 
59  MapReduce(MapFunc* mapFunc, ReduceFunc* reduceFunc);
60 
61  ~MapReduce();
62 
63  template <typename T>
64  void setConstant(T constant1)
65  {
66  m_mapFunc->setConstant(constant1);
67  }
68 
69  void finishAll()
70  {
71  m_environment->finishAll();
72  }
73 
74  void setExecPlan(ExecPlan& plan)
75  {
76  m_execPlan = &plan;
77  }
78  void setExecPlan(ExecPlan *plan)
79  {
80  m_execPlanMulti = plan;
81  }
82 
83 private:
84  Environment<int>* m_environment;
85  MapFunc* m_mapFunc;
86  ReduceFunc* m_reduceFunc;
87 
89  ExecPlan *m_execPlanMulti;
90 
92  ExecPlan *m_execPlan;
93 
95  ExecPlan m_defPlan;
96 
97 public:
98  template <typename T>
99  T operator()(Vector<T>& input);
100 
101  template <typename T>
102  T operator()(Matrix<T>& input);
103 
104  template <typename InputIterator>
105  typename InputIterator::value_type operator()(InputIterator inputBegin, InputIterator inputEnd);
106 
107  template <typename T>
108  T operator()(Vector<T>& input1, Vector<T>& input2);
109 
110  template <typename T>
111  T operator()(Matrix<T>& input1, Matrix<T>& input2);
112 
113  template <typename Input1Iterator, typename Input2Iterator>
114  typename Input1Iterator::value_type operator()(Input1Iterator input1Begin, Input1Iterator input1End, Input2Iterator input2Begin, Input2Iterator input2End);
115 
116  template <typename T>
117  T operator()(Vector<T>& input1, Vector<T>& input2, Vector<T>& input3);
118 
119  template <typename T>
120  T operator()(Matrix<T>& input1, Matrix<T>& input2, Matrix<T>& input3);
121 
122  template <typename Input1Iterator, typename Input2Iterator, typename Input3Iterator>
123  typename Input1Iterator::value_type operator()(Input1Iterator input1Begin, Input1Iterator input1End, Input2Iterator input2Begin, Input2Iterator input2End, Input3Iterator input3Begin, Input3Iterator input3End);
124 
125 public:
126  template <typename T>
127  T CPU(Vector<T>& input);
128 
129  template <typename T>
130  T CPU(Matrix<T>& input);
131 
132  template <typename InputIterator>
133  typename InputIterator::value_type CPU(InputIterator inputBegin, InputIterator inputEnd);
134 
135  template <typename T>
136  T CPU(Vector<T>& input1, Vector<T>& input2);
137 
138  template <typename T>
139  T CPU(Matrix<T>& input1, Matrix<T>& input2);
140 
141  template <typename Input1Iterator, typename Input2Iterator>
142  typename Input1Iterator::value_type CPU(Input1Iterator input1Begin, Input1Iterator input1End, Input2Iterator input2Begin, Input2Iterator input2End);
143 
144  template <typename T>
145  T CPU(Vector<T>& input1, Vector<T>& input2, Vector<T>& input3);
146 
147  template <typename T>
148  T CPU(Matrix<T>& input1, Matrix<T>& input2, Matrix<T>& input3);
149 
150  template <typename Input1Iterator, typename Input2Iterator, typename Input3Iterator>
151  typename Input1Iterator::value_type CPU(Input1Iterator input1Begin, Input1Iterator input1End, Input2Iterator input2Begin, Input2Iterator input2End, Input3Iterator input3Begin, Input3Iterator input3End);
152 
153 #ifdef SKEPU_OPENMP
154 public:
155  template <typename T>
156  T OMP(Vector<T>& input);
157 
158  template <typename T>
159  T OMP(Matrix<T>& input);
160 
161  template <typename InputIterator>
162  typename InputIterator::value_type OMP(InputIterator inputBegin, InputIterator inputEnd);
163 
164  template <typename T>
165  T OMP(Vector<T>& input1, Vector<T>& input2);
166 
167  template <typename T>
168  T OMP(Matrix<T>& input1, Matrix<T>& input2);
169 
170  template <typename Input1Iterator, typename Input2Iterator>
171  typename Input1Iterator::value_type OMP(Input1Iterator input1Begin, Input1Iterator input1End, Input2Iterator input2Begin, Input2Iterator input2End);
172 
173  template <typename T>
174  T OMP(Vector<T>& input1, Vector<T>& input2, Vector<T>& input3);
175 
176  template <typename T>
177  T OMP(Matrix<T>& input1, Matrix<T>& input2, Matrix<T>& input3);
178 
179  template <typename Input1Iterator, typename Input2Iterator, typename Input3Iterator>
180  typename Input1Iterator::value_type OMP(Input1Iterator input1Begin, Input1Iterator input1End, Input2Iterator input2Begin, Input2Iterator input2End, Input3Iterator input3Begin, Input3Iterator input3End);
181 #endif
182 
183 #ifdef SKEPU_CUDA
184 public:
185  template <typename T>
186  T CU(Vector<T>& input, int useNumGPU = 1);
187 
188  template <typename T>
189  T CU(Matrix<T>& input, int useNumGPU = 1);
190 
191  template <typename InputIterator>
192  typename InputIterator::value_type CU(InputIterator inputBegin, InputIterator inputEnd, int useNumGPU = 1);
193 
194  template <typename T>
195  T CU(Vector<T>& input1, Vector<T>& input2, int useNumGPU = 1);
196 
197  template <typename T>
198  T CU(Matrix<T>& input1, Matrix<T>& input2, int useNumGPU = 1);
199 
200  template <typename Input1Iterator, typename Input2Iterator>
201  typename Input1Iterator::value_type CU(Input1Iterator input1Begin, Input1Iterator input1End, Input2Iterator input2Begin, Input2Iterator input2End, int useNumGPU = 1);
202 
203  template <typename T>
204  T CU(Vector<T>& input1, Vector<T>& input2, Vector<T>& input3, int useNumGPU = 1);
205 
206  template <typename T>
207  T CU(Matrix<T>& input1, Matrix<T>& input2, Matrix<T>& input3, int useNumGPU = 1);
208 
209  template <typename Input1Iterator, typename Input2Iterator, typename Input3Iterator>
210  typename Input1Iterator::value_type CU(Input1Iterator input1Begin, Input1Iterator input1End, Input2Iterator input2Begin, Input2Iterator input2End, Input3Iterator input3Begin, Input3Iterator input3End, int useNumGPU = 1);
211 
212 private:
213  unsigned int cudaDeviceID;
214 
215  template <typename InputIterator>
216  typename InputIterator::value_type mapReduceSingleThread_CU(InputIterator inputBegin, InputIterator inputEnd, unsigned int deviceID);
217 
218  template <typename Input1Iterator, typename Input2Iterator>
219  typename Input1Iterator::value_type mapReduceSingleThread_CU(Input1Iterator input1Begin, Input1Iterator input1End, Input2Iterator input2Begin, Input2Iterator input2End, unsigned int deviceID);
220 
221  template <typename Input1Iterator, typename Input2Iterator, typename Input3Iterator>
222  typename Input1Iterator::value_type mapReduceSingleThread_CU(Input1Iterator input1Begin, Input1Iterator input1End, Input2Iterator input2Begin, Input2Iterator input2End, Input3Iterator input3Begin, Input3Iterator input3End, unsigned int deviceID);
223 
224  //MultiStream Skeleton implementation headers
225  template <typename InputIterator>
226  typename InputIterator::value_type mapReduceMultiStream_CU(InputIterator inputBegin, InputIterator inputEnd, unsigned int deviceID);
227 
228  template <typename Input1Iterator, typename Input2Iterator>
229  typename Input1Iterator::value_type mapReduceMultiStream_CU(Input1Iterator input1Begin, Input1Iterator input1End, Input2Iterator input2Begin, Input2Iterator input2End, unsigned int deviceID);
230 
231  template <typename Input1Iterator, typename Input2Iterator, typename Input3Iterator>
232  typename Input1Iterator::value_type mapReduceMultiStream_CU(Input1Iterator input1Begin, Input1Iterator input1End, Input2Iterator input2Begin, Input2Iterator input2End, Input3Iterator input3Begin, Input3Iterator input3End, unsigned int deviceID);
233 
234  template <typename InputIterator>
235  typename InputIterator::value_type mapReduceMultiStreamMultiGPU_CU(InputIterator inputBegin, InputIterator inputEnd, int useNumGPU);
236 
237  template <typename Input1Iterator, typename Input2Iterator>
238  typename Input1Iterator::value_type mapReduceMultiStreamMultiGPU_CU(Input1Iterator input1Begin, Input1Iterator input1End, Input2Iterator input2Begin, Input2Iterator input2End, int useNumGPU);
239 
240  template <typename Input1Iterator, typename Input2Iterator, typename Input3Iterator>
241  typename Input1Iterator::value_type mapReduceMultiStreamMultiGPU_CU(Input1Iterator input1Begin, Input1Iterator input1End, Input2Iterator input2Begin, Input2Iterator input2End, Input3Iterator input3Begin, Input3Iterator input3End, int useNumGPU);
242 
243 #endif
244 
245 #ifdef SKEPU_OPENCL
246 public:
247  template <typename T>
248  T CL(Vector<T>& input, int useNumGPU = 1);
249 
250  template <typename T>
251  T CL(Matrix<T>& input, int useNumGPU = 1);
252 
253  template <typename InputIterator>
254  typename InputIterator::value_type CL(InputIterator inputBegin, InputIterator inputEnd, int useNumGPU = 1);
255 
256  template <typename T>
257  T CL(Vector<T>& input1, Vector<T>& input2, int useNumGPU = 1);
258 
259  template <typename T>
260  T CL(Matrix<T>& input1, Matrix<T>& input2, int useNumGPU = 1);
261 
262  template <typename Input1Iterator, typename Input2Iterator>
263  typename Input1Iterator::value_type CL(Input1Iterator input1Begin, Input1Iterator input1End, Input2Iterator input2Begin, Input2Iterator input2End, int useNumGPU = 1);
264 
265  template <typename T>
266  T CL(Vector<T>& input1, Vector<T>& input2, Vector<T>& input3, int useNumGPU = 1);
267 
268  template <typename T>
269  T CL(Matrix<T>& input1, Matrix<T>& input2, Matrix<T>& input3, int useNumGPU = 1);
270 
271  template <typename Input1Iterator, typename Input2Iterator, typename Input3Iterator>
272  typename Input1Iterator::value_type CL(Input1Iterator input1Begin, Input1Iterator input1End, Input2Iterator input2Begin, Input2Iterator input2End, Input3Iterator input3Begin, Input3Iterator input3End, int useNumGPU = 1);
273 
274 private:
275  template <typename InputIterator>
276  typename InputIterator::value_type mapReduceSingle_CL(InputIterator inputBegin, InputIterator inputEnd, unsigned int deviceID);
277 
278  template <typename InputIterator>
279  typename InputIterator::value_type mapReduceNumDevices_CL(InputIterator inputBegin, InputIterator inputEnd, size_t numDevices);
280 
281  template <typename Input1Iterator, typename Input2Iterator>
282  typename Input1Iterator::value_type mapReduceSingle_CL(Input1Iterator input1Begin, Input1Iterator input1End, Input2Iterator input2Begin, Input2Iterator input2End, unsigned int deviceID);
283 
284  template <typename Input1Iterator, typename Input2Iterator>
285  typename Input1Iterator::value_type mapReduceNumDevices_CL(Input1Iterator input1Begin, Input1Iterator input1End, Input2Iterator input2Begin, Input2Iterator input2End, size_t numDevices);
286 
287  template <typename Input1Iterator, typename Input2Iterator, typename Input3Iterator>
288  typename Input1Iterator::value_type mapReduceSingle_CL(Input1Iterator input1Begin, Input1Iterator input1End, Input2Iterator input2Begin, Input2Iterator input2End, Input3Iterator input3Begin, Input3Iterator input3End, unsigned int deviceID);
289 
290  template <typename Input1Iterator, typename Input2Iterator, typename Input3Iterator>
291  typename Input1Iterator::value_type mapReduceNumDevices_CL(Input1Iterator input1Begin, Input1Iterator input1End, Input2Iterator input2Begin, Input2Iterator input2End, Input3Iterator input3Begin, Input3Iterator input3End, size_t numDevices);
292 
293 private:
294  std::vector<std::pair<cl_kernel, Device_CL*> > m_mapReduceKernels_CL;
295  std::vector<std::pair<cl_kernel, Device_CL*> > m_reduceKernels_CL;
296 
297  void createOpenCLProgram();
298 #endif
299 
300 };
301 
302 
303 
304 
305 }
306 
307 #include "src/mapreduce.inl"
308 
309 #include "src/mapreduce_cpu.inl"
310 
311 #ifdef SKEPU_OPENMP
312 #include "src/mapreduce_omp.inl"
313 #endif
314 
315 #ifdef SKEPU_OPENCL
316 #include "src/mapreduce_cl.inl"
317 #endif
318 
319 #ifdef SKEPU_CUDA
320 #include "src/mapreduce_cu.inl"
321 #endif
322 
323 #endif
324 
325 
Contains a class declaration for the SparseMatrix container.
~MapReduce()
Definition: mapreduce.inl:86
Includes the macro files needed for the defined backends.
void finishAll()
Definition: environment.inl:575
T operator()(Vector< T > &input)
Definition: mapreduce.inl:105
MapReduce(MapFunc *mapFunc, ReduceFunc *reduceFunc)
Definition: mapreduce.inl:23
Contains the definitions of non-backend specific member functions for the MapReduce skeleton...
Contains a class declaration for the object that represents an OpenCL device.
T CPU(Vector< T > &input)
Definition: mapreduce_cpu.inl:23
T CU(Vector< T > &input, int useNumGPU=1)
Definition: mapreduce_cu.inl:349
Contains a class declaration for the Matrix container.
A vector container class, implemented as a wrapper for std::vector.
Definition: vector.h:61
A class representing the MapReduce skeleton.
Definition: mapreduce.h:54
A class that describes an execution plan.
Definition: exec_plan.h:47
T CL(Vector< T > &input, int useNumGPU=1)
Definition: mapreduce_cl.inl:385
Contains the definitions of OpenMP specific member functions for the MapReduce skeleton.
Contains a class declaration for the Vector container.
Contains the definitions of OpenCL specific member functions for the MapReduce skeleton.
Contains a class declaration for Environment class.
Contains the definitions of CUDA specific member functions for the MapReduce skeleton.
Contains the definitions of CPU specific member functions for the MapReduce skeleton.
A class representing the column-wise iterator for the Matrix data-type.
T OMP(Vector< T > &input)
Definition: mapreduce_omp.inl:27
Contains a class that stores information about which back ends to use when executing.