parallel_foreach.h 8.47 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
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#ifndef __PARALLEL_FOREACH__
#define __PARALLEL_FOREACH__

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

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

namespace Algo
{

namespace Parallel
{

Pierre Kraemer's avatar
Pierre Kraemer committed
39
static unsigned int NBCORES = 0;
40 41 42 43

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

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

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

Sylvain Thery's avatar
Sylvain Thery committed
52
/**
53
 * @param p can be NB_HIGHMEMORY (default) or NB_HIGHCOMPUTE or NB_VERYHIGHMEMORY
54
 * @return Number of core in fact (work only with quad core with/without hyper threading)
Sylvain Thery's avatar
Sylvain Thery committed
55
 */
56 57 58 59 60 61
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
62

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
//
//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>
86
//	void traverseCell(bool needMarkers = false, unsigned int currentThread = 0);
87 88
//
//	template <unsigned int ORBIT>
89
//	void traverseEachCell(bool needMarkers = false, unsigned int currentThread = 0);
90
//
91
//	void traverseDart(bool needMarkers = false, unsigned int currentThread = 0);
92
//
93
//	void traverseEachDart(bool needMarkers = false, unsigned int currentThread = 0);
94 95
//};

Sylvain Thery's avatar
Sylvain Thery committed
96
/**
97 98
 * 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
99
 * @param map the map
100
 * @param funcs the functors to apply (size of vector determine number of threads, and all functors must be of the same type)
101
 * @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
102
 */
103
template <typename MAP, unsigned int ORBIT>
104
void foreach_cell(MAP& map, std::vector<FunctorMapThreaded<MAP>*>& funcs, bool needMarkers = false);
105 106

/**
107 108
 * 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
109 110
 * @param map the map
 * @param func the functor to apply
111 112
 * @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 !!
113
 */
114
template <typename MAP, unsigned int ORBIT>
115
void foreach_cell(MAP& map, FunctorMapThreaded<MAP>& func, unsigned int nbth = 0, bool needMarkers = false);
116

117 118 119 120 121 122 123 124 125
/**
 * 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 !!
 */
template <typename MAP, unsigned int ORBIT>
126
void foreach_cell_all_thread(MAP& map, std::vector<FunctorMapThreaded<MAP>*>& funcs, bool needMarkers = false);
127

128
/**
129 130
 * 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)
131
 * @param map the map
132
 * @param funcs the functors to apply (size of vector determine number of threads, and all functors must be of the same type)
133
 * @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 !!
134
 */
135
template <typename MAP>
136
void foreach_dart(MAP& map, std::vector<FunctorMapThreaded<MAP>*>& funcs,  unsigned int nbth, bool needMarkers = false);
137

138
/**
139
 * Traverse darts of a map in parallel
140
 * @param map the map
141 142 143
 * @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 !!
144
 */
145
template <typename MAP>
146
void foreach_dart(MAP& map, FunctorMapThreaded<MAP>& func, unsigned int nbth = 0, bool needMarkers = false);
147 148

/**
149 150 151
 * 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
152
 */
153
void foreach_attrib(AttributeContainer& attr_cont, std::vector<FunctorAttribThreaded*> funcs);
154 155

/**
156 157 158 159
 * 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
160
 */
161
void foreach_attrib(AttributeContainer& attr_cont, FunctorAttribThreaded& func, unsigned int nbth = 0);
162

163
/**
164 165 166 167 168 169
 * 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)
170
 */
171
template <typename MAP, unsigned int CELL>
172
void foreach_cell2Pass(MAP& map, std::vector<FunctorMapThreaded<MAP>*>& funcsFrontnBack, unsigned int nbLoops, bool needMarkers = false);
173

174
/**
175 176 177 178 179 180 181 182
 * 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)
183
 */
184
template <typename MAP, unsigned int CELL>
185
void foreach_cell2Pass(MAP& map, FunctorMapThreaded<MAP>& funcFront, FunctorMapThreaded<MAP>& funcBack, unsigned int nbLoops, unsigned int nbth, bool needMarkers = false);
186

Pierre Kraemer's avatar
Pierre Kraemer committed
187
} // namespace Parallel
Sylvain Thery's avatar
Sylvain Thery committed
188

Pierre Kraemer's avatar
Pierre Kraemer committed
189
} // namespace Algo
Sylvain Thery's avatar
Sylvain Thery committed
190

Pierre Kraemer's avatar
Pierre Kraemer committed
191
} // namespace CGoGN
Sylvain Thery's avatar
Sylvain Thery committed
192 193 194 195

#include "Algo/Parallel/parallel_foreach.hpp"

#endif