traversorCell.hpp 7.97 KB
Newer Older
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           *
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/                                           *
21 22 23
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/
Sylvain Thery's avatar
Sylvain Thery committed
24 25 26
#include <boost/thread.hpp>
#include <boost/thread/barrier.hpp>
#include <vector>
27 28 29 30

namespace CGoGN
{

31
template <typename MAP, unsigned int ORBIT>
Sylvain Thery's avatar
Sylvain Thery committed
32
TraversorCell<MAP, ORBIT>::TraversorCell(const MAP& map, bool forceDartMarker, unsigned int thread) :
33
	m(map), dmark(NULL), cmark(NULL), quickTraversal(NULL), current(NIL), firstTraversal(true)
34
{
Pierre Kraemer's avatar
Pierre Kraemer committed
35 36
	dimension = map.dimension();

37
	if(forceDartMarker)
38
		dmark = new DartMarker<MAP>(map, thread) ;
39
	else
Pierre Kraemer's avatar
Pierre Kraemer committed
40
	{
41 42 43
		quickTraversal = map.template getQuickTraversal<ORBIT>() ;
		if(quickTraversal != NULL)
		{
Sylvain Thery's avatar
Sylvain Thery committed
44 45
			cont = &(map.template getAttributeContainer<ORBIT>()) ;

46
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
47
		else
48 49
		{
			if(map.template isOrbitEmbedded<ORBIT>())
50
				cmark = new CellMarker<MAP, ORBIT>(map, thread) ;
51
			else
52
				dmark = new DartMarker<MAP>(map, thread) ;
53
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
54
	}
55 56
}

57 58
template <typename MAP, unsigned int ORBIT>
TraversorCell<MAP, ORBIT>::~TraversorCell()
59
{
60 61 62 63
	if(dmark)
		delete dmark ;
	else if(cmark)
		delete cmark ;
64 65
}

66
template <typename MAP, unsigned int ORBIT>
Pierre Kraemer's avatar
Pierre Kraemer committed
67
Cell<ORBIT> TraversorCell<MAP, ORBIT>::begin()
68
{
Pierre Kraemer's avatar
Pierre Kraemer committed
69
	if(quickTraversal != NULL)
70
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
71
		qCurrent = cont->begin() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
72
		current.dart = (*quickTraversal)[qCurrent] ;
73 74 75
	}
	else
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
76 77 78 79 80 81 82 83
		if(!firstTraversal)
		{
			if(dmark)
				dmark->unmarkAll() ;
			else
				cmark->unmarkAll() ;
		}

Pierre Kraemer's avatar
Pierre Kraemer committed
84 85 86
		current.dart = m.begin() ;
		while(current.dart != m.end() && (m.isBoundaryMarked(dimension, current.dart) ))
			m.next(current.dart) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
87

Pierre Kraemer's avatar
Pierre Kraemer committed
88 89
		if(current.dart == m.end())
			current.dart = NIL ;
90
		else
Pierre Kraemer's avatar
Pierre Kraemer committed
91 92
		{
			if(dmark)
Pierre Kraemer's avatar
Pierre Kraemer committed
93
				dmark->template markOrbit<ORBIT>(current.dart) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
94 95 96 97 98
			else
				cmark->mark(current) ;
		}

		firstTraversal = false ;
99 100 101 102 103
	}

	return current ;
}

104
template <typename MAP, unsigned int ORBIT>
Pierre Kraemer's avatar
Pierre Kraemer committed
105
Cell<ORBIT> TraversorCell<MAP, ORBIT>::end()
Pierre Kraemer's avatar
Pierre Kraemer committed
106
{
Pierre Kraemer's avatar
Pierre Kraemer committed
107
	return Cell<ORBIT>(NIL) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
108
}
109

110
template <typename MAP, unsigned int ORBIT>
Pierre Kraemer's avatar
Pierre Kraemer committed
111
Cell<ORBIT> TraversorCell<MAP, ORBIT>::next()
112
{
Pierre Kraemer's avatar
Pierre Kraemer committed
113 114
	assert(current.dart != NIL);
//	if(current.dart != NIL)
115 116
//	{
	if(quickTraversal != NULL)
117
	{
118 119
		cont->next(qCurrent) ;
		if (qCurrent != cont->end())
Pierre Kraemer's avatar
Pierre Kraemer committed
120 121
			current.dart = (*quickTraversal)[qCurrent] ;
		else current.dart = NIL;
122 123 124 125
	}
	else
	{
		if(dmark)
126
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
127 128
			bool ismarked = dmark->isMarked(current.dart) ;
			while(current.dart != NIL && (ismarked || m.isBoundaryMarked(dimension, current.dart)))
129
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
130 131 132
				m.next(current.dart) ;
				if(current.dart == m.end())
					current.dart = NIL ;
133
				else
Pierre Kraemer's avatar
Pierre Kraemer committed
134
					ismarked = dmark->isMarked(current.dart) ;
135
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
136 137
			if(current.dart != NIL)
				dmark->template markOrbit<ORBIT>(current.dart) ;
138 139 140 141
		}
		else
		{
			bool ismarked = cmark->isMarked(current) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
142
			while(current.dart != NIL && (ismarked || m.isBoundaryMarked(dimension, current.dart) ))
Pierre Kraemer's avatar
Pierre Kraemer committed
143
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
144 145 146
				m.next(current.dart) ;
				if(current.dart == m.end())
					current.dart = NIL ;
147 148
				else
					ismarked = cmark->isMarked(current) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
149
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
150
			if(current.dart != NIL)
151
				cmark->mark(current) ;
152 153
		}
	}
154
//	}
155 156 157
	return current ;
}

158
template <typename MAP, unsigned int ORBIT>
Pierre Kraemer's avatar
Pierre Kraemer committed
159
void TraversorCell<MAP, ORBIT>::skip(Cell<ORBIT> c)
160 161
{
	if(dmark)
Pierre Kraemer's avatar
Pierre Kraemer committed
162
		dmark->template markOrbit<ORBIT>(c.dart) ;
163
	else
Pierre Kraemer's avatar
Pierre Kraemer committed
164
		cmark->mark(c) ;
165 166
}

Sylvain Thery's avatar
Sylvain Thery committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291


namespace Parallel
{

/// internal functor for boost call
template <unsigned int ORBIT, typename FUNC>
class ThreadFunction
{
protected:
	typedef Cell<ORBIT> CELL;
	std::vector<CELL>& m_cells;
	boost::barrier& m_sync1;
	boost::barrier& m_sync2;
	bool& m_finished;
	unsigned int m_id;
	FUNC m_lambda;
public:
	ThreadFunction(FUNC func, std::vector<CELL>& vd, boost::barrier& s1, boost::barrier& s2, bool& finished, unsigned int id):
		m_cells(vd), m_sync1(s1), m_sync2(s2), m_finished(finished), m_id(id), m_lambda(func)
	{
	}

	ThreadFunction(const ThreadFunction<ORBIT, FUNC>& tf):
		m_cells(tf.m_cells), m_sync1(tf.m_sync1), m_sync2(tf.m_sync2), m_finished(tf.m_finished), m_id(tf.m_id), m_lambda(tf.m_lambda){}

	void operator()()
	{
		while (!m_finished)
		{
			for (typename std::vector<CELL>::const_iterator it = m_cells.begin(); it != m_cells.end(); ++it)
				m_lambda(*it,m_id);
			m_sync1.wait(); // wait every body has finished
			m_sync2.wait(); // wait vectors has been refilled
		}
	}
};


template <unsigned int ORBIT, typename MAP, typename FUNC>
void foreach_cell(MAP& map, FUNC func, unsigned int nbth, bool needMarkers)
{
	if (nbth==0)
		nbth = boost::thread::hardware_concurrency();

	std::vector< Cell<ORBIT> >* vd = new std::vector< Cell<ORBIT> >[nbth];

	for (unsigned int i = 0; i < nbth; ++i)
		vd[i].reserve(SIZE_BUFFER_THREAD);

	//    TraversorCell<MAP, ORBIT> trav(map,false,th_orig);
	unsigned int nb = 0;
	TraversorCell<MAP, ORBIT> trav(map);
	Cell<ORBIT> cell = trav.begin();
	Cell<ORBIT> c_end = trav.end();
	while ((cell.dart != c_end.dart) && (nb < nbth*SIZE_BUFFER_THREAD) )
	{
		vd[nb%nbth].push_back(cell);
		nb++;
		cell = trav.next();
	}
	boost::barrier sync1(nbth+1);
	boost::barrier sync2(nbth+1);
	bool finished=false;
	// lauch threads
	if (needMarkers)
	{
		unsigned int nbth_prec = map.getNbThreadMarkers();
		if (nbth_prec < nbth+1)
			map.addThreadMarker(nbth+1-nbth_prec);
	}

	boost::thread** threads = new boost::thread*[nbth];
	ThreadFunction<ORBIT,FUNC>** tfs = new ThreadFunction<ORBIT,FUNC>*[nbth];

	for (unsigned int i = 0; i < nbth; ++i)
	{
		tfs[i] = new ThreadFunction<ORBIT,FUNC>(func, vd[i],sync1,sync2, finished,1+i);
		threads[i] = new boost::thread( boost::ref( *(tfs[i]) ) );
	}

	// and continue to traverse the map
	std::vector< Cell<ORBIT> >* tempo = new std::vector< Cell<ORBIT> >[nbth];
	for (unsigned int i = 0; i < nbth; ++i)
		tempo[i].reserve(SIZE_BUFFER_THREAD);

	while (cell.dart != c_end.dart)
	{
		for (unsigned int i = 0; i < nbth; ++i)
			tempo[i].clear();
		unsigned int nb = 0;

		while ((cell.dart != c_end.dart) && (nb < nbth*SIZE_BUFFER_THREAD) )
		{
			tempo[nb%nbth].push_back(cell);
			nb++;
			cell = trav.next();
		}
		sync1.wait();// wait for all thread to finish its vector
		for (unsigned int i = 0; i < nbth; ++i)
			vd[i].swap(tempo[i]);
		sync2.wait();// everybody refilled then go
	}

	sync1.wait();// wait for all thread to finish its vector
	finished = true;// say finsih to everyone
	sync2.wait(); // just wait for last barrier wait !


	//wait for all theads to be finished
	for (unsigned int i = 0; i < nbth; ++i)
	{
		threads[i]->join();
		delete threads[i];
		delete tfs[i];
	}
	delete[] tfs;
	delete[] threads;
	delete[] vd;
	delete[] tempo;
}

}


Pierre Kraemer's avatar
Pierre Kraemer committed
292
} // namespace CGoGN