parallel_foreach.h 9.16 KB
Newer Older
Sylvain Thery's avatar
Sylvain Thery committed
1
2
3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
Sylvain Thery's avatar
Sylvain Thery committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                           *
Sylvain Thery's avatar
Sylvain Thery committed
21
22
23
24
25
26
27
28
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/


#ifndef __PARALLEL_FOREACH__
#define __PARALLEL_FOREACH__

29
30
#include "Topology/generic/functor.h"

Sylvain Thery's avatar
Sylvain Thery committed
31
32
33
34
35
36
37
38
39
namespace CGoGN
{

namespace Algo
{

namespace Parallel
{

40
41
42
43
44
static unsigned int NBCORES=0;

/// enum for optimalNbThreads parameter
enum NbParam {NB_HIGHMEMORY, NB_HIGHCOMPUTE, NB_VERYHIGHMEMORY};

45
46
/// size of buffers to store darts or indexes in each threads
const unsigned int SIZE_BUFFER_THREAD = 8192;	// seems to be the best compromise
47
48

/**
49
 * @return How much threads has you computer
50
 */
51
inline unsigned int nbThreads();
52

Sylvain Thery's avatar
Sylvain Thery committed
53
/**
54
 * @param p can be NB_HIGHMEMORY (default) or NB_HIGHCOMPUTE or NB_VERYHIGHMEMORY
55
 * @return Number of core in fact (work only with quad core with/without hyper threading)
Sylvain Thery's avatar
Sylvain Thery committed
56
 */
57
58
59
60
61
62
unsigned int optimalNbThreads( NbParam p=NB_HIGHMEMORY);

/**
 * impossible to automatically determine the number of cores so ...
 */
void setNbCore(unsigned int nb);
Sylvain Thery's avatar
Sylvain Thery committed
63

64

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
//template <typename MAP>
//class Foreach
//{
//	MAP& m_map;
//
//	std::vector<FunctorMapThreaded<MAP>*> m_funcs;
//
//	std::vector<Dart>* m_vd;
//
//	unsigned int m_nbth;
//
//public:
//	Foreach(MAP& map,unsigned int nbth);
//
//	void clearFunctors();
//
//	void addFunctor(FunctorMapThreaded<MAP>* funcPtr);
//
//	template<typename T>
//	T* getFunctor(unsigned int i);
//
//	template <unsigned int ORBIT>
//	void traverseCell(bool needMarkers = false, const FunctorSelect& good = allDarts, unsigned int currentThread = 0);
//
//	template <unsigned int ORBIT>
//	void traverseEachCell(bool needMarkers = false, const FunctorSelect& good = allDarts, unsigned int currentThread = 0);
//
//	void traverseDart(bool needMarkers = false, const FunctorSelect& good = allDarts, unsigned int currentThread = 0);
//
//	void traverseEachDart(bool needMarkers = false, const FunctorSelect& good = allDarts, unsigned int currentThread = 0);
//};



Sylvain Thery's avatar
Sylvain Thery committed
100
/**
101
102
 * Traverse cells of a map in parallel. Use quick traversal, cell markers or dart markers if available !
 * Use this version if you need to have acces to each functors after the traversal (to compute a sum or an average for example)
Sylvain Thery's avatar
Sylvain Thery committed
103
 * @param map the map
104
 * @param funcs the functors to apply (size of vector determine number of threads, and all functors must be of the same type)
105
 * @param needMarkers set to yes if you want that each thread use different markers. Warning if set to false (default) do not use algo with thread id or markers !!
Sylvain Thery's avatar
Sylvain Thery committed
106
107
 * @param good a selector
 */
108
template <typename MAP, unsigned int ORBIT>
109
void foreach_cell(MAP& map, std::vector<FunctorMapThreaded<MAP>*>& funcs, bool needMarkers = false, const FunctorSelect& good = allDarts, unsigned int currentThread = 0);
110
111

/**
112
113
 * Traverse cells of a map in parallel. Use quick traversal, cell markers or dart markers if available !
 * Use this version if you do not need to keep functors
114
115
 * @param map the map
 * @param func the functor to apply
116
117
 * @param nbth number of threads 0 for let the system choose
 * @param needMarkers set to yes if you want that each thread use different markers. Warning if set to false (default) do not use algo with thread id or markers !!
118
119
 * @param good a selector
 */
120
template <typename MAP, unsigned int ORBIT>
121
void foreach_cell(MAP& map, FunctorMapThreaded<MAP>& func, unsigned int nbth = 0, bool needMarkers = false, const FunctorSelect& good = allDarts, unsigned int currentThread = 0);
122

123
124
125
126
127
128
129
130
131
132
133
134
135
136

/**
 * Traverse cells of a map and apply differents functors in //
 * Use this version if you need to have acces to each functors after the traversal (to compute a sum or an average for example)
 * @param map the map
 * @param funcs the functors to apply ( each functors can (should!) be here of different type)
 * @param nbth number of threads
 * @param needMarkers set to yes if you want that each thread use different markers. Warning if set to false (default) do not use algo with thread id or markers !!
 * @param good a selector
 */
template <typename MAP, unsigned int ORBIT>
void foreach_cell_all_thread(MAP& map, std::vector<FunctorMapThreaded<MAP>*>& funcs, bool needMarkers = false, const FunctorSelect& good = allDarts, unsigned int currentThread = 0);


137
/**
138
139
 * Traverse darts of a map in parallel
 * Use this version if you need to have acces to each functors after the traversal (to compute a sum or an average for example)
140
 * @param map the map
141
 * @param funcs the functors to apply (size of vector determine number of threads, and all functors must be of the same type)
142
 * @param needMarkers set to yes if you want that each thread use different markers.Warning if set to false (default) do not use algo with thread id or markers !!
143
144
 * @param good a selector
 */
145
146
147
template <typename MAP>
void foreach_dart(MAP& map, std::vector<FunctorMapThreaded<MAP>*>& funcs,  unsigned int nbth, bool needMarkers = false, const FunctorSelect& good = allDarts);

148
149

/**
150
 * Traverse darts of a map in parallel
151
 * @param map the map
152
153
154
 * @param funcs the functor
 * @param nbth number of thread to use, 0 for let the system choose
 * @param needMarkers set to yes if you want that each thread use different markers. Warning if set to false (default) do not use algo with thread id or markers !!
155
156
 * @param good a selector
 */
157
template <typename MAP>
158
void foreach_dart(MAP& map, FunctorMapThreaded<MAP>& func, unsigned int nbth = 0, bool needMarkers = false, const FunctorSelect& good = allDarts);
159
160
161


/**
162
163
164
 * Traverse all elements of an attribute container (attribute handler is placed in FunctorAttribThreaded)
 * @param attr_cont the attribute container to traverse
 * @param func the fonctors to use
165
 */
166
void foreach_attrib(AttributeContainer& attr_cont, std::vector<FunctorAttribThreaded*> funcs);
167
168

/**
169
170
171
172
 * Traverse all elements of an attribute container (attribute handler is placed in FunctorAttribThreaded
 * @param attr_cont the attribute container to traverse
 * @param func the functor to use
 * @param nbth number of thread to use for computation 0 for let the system choose
173
 */
174
void foreach_attrib(AttributeContainer& attr_cont, FunctorAttribThreaded& func, unsigned int nbth = 0);
175

176
177

/**
178
179
180
181
182
183
184
 * Optimized version for // foreach with to pass (2 functors), with several loops
 * Use this version if you need to keep functors
 * @param map the map
 * @param funcsFrontnBack nbth front pass functors followed by nbth back pass functors
 * @param nbLoops number of loops to execute
 * @param needMarkers set to yes if you want that each thread use different markers (markers are allocated if necessary)
 * @param good a selector
185
 */
186
template <typename MAP, unsigned int CELL>
187
void foreach_cell2Pass(MAP& map, std::vector<FunctorMapThreaded<MAP>*>& funcsFrontnBack, unsigned int nbLoops, bool needMarkers = false, const FunctorSelect& good = allDarts);
188

189
/**
190
191
192
193
194
195
196
197
198
 * Optimized version for // foreach with to pass (2 functors), with several loops
 * Use this version if you do not need to keep functors
 * @param map the map
 * @param funcFront front pass functor
 * @param funcBack back pass functor
 * @param nbLoops number of loops to execute
 * @param nbth number of threads to use
 * @param needMarkers set to yes if you want that each thread use different markers (markers are allocated if necessary)
 * @param good a selector
199
 */
200
template <typename MAP, unsigned int CELL>
201
void foreach_cell2Pass(MAP& map, FunctorMapThreaded<MAP>& funcFront, FunctorMapThreaded<MAP>& funcBack, unsigned int nbLoops, unsigned int nbth, bool needMarkers = false, const FunctorSelect& good = allDarts);
202

203

Pierre Kraemer's avatar
Pierre Kraemer committed
204
} // namespace Parallel
Sylvain Thery's avatar
Sylvain Thery committed
205

Pierre Kraemer's avatar
Pierre Kraemer committed
206
} // namespace Algo
Sylvain Thery's avatar
Sylvain Thery committed
207

Pierre Kraemer's avatar
Pierre Kraemer committed
208
} // namespace CGoGN
Sylvain Thery's avatar
Sylvain Thery committed
209
210
211
212

#include "Algo/Parallel/parallel_foreach.hpp"

#endif