traversorCell.hpp 10.6 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
}

Sylvain Thery's avatar
Sylvain Thery committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
template <typename MAP, unsigned int ORBIT>
unsigned int TraversorCell<MAP, ORBIT>::nbCells()
{
	return m.template getAttributeContainer<ORBIT>().size();
}



template <typename MAP, unsigned int ORBIT>
TraversorCell<MAP, ORBIT>::TraversorCell(const TraversorCell<MAP, ORBIT>& tc) :
	m(tc.m), dmark(tc.dmark), cmark(tc.cmark),
	quickTraversal(tc.quickTraversal), current(tc.current), firstTraversal(tc.firstTraversal),
	dimension(tc.dimension)
{
}




76 77
template <typename MAP, unsigned int ORBIT>
TraversorCell<MAP, ORBIT>::~TraversorCell()
78
{
79 80 81 82
	if(dmark)
		delete dmark ;
	else if(cmark)
		delete cmark ;
83 84
}

85
template <typename MAP, unsigned int ORBIT>
Pierre Kraemer's avatar
Pierre Kraemer committed
86
Cell<ORBIT> TraversorCell<MAP, ORBIT>::begin()
87
{
Pierre Kraemer's avatar
Pierre Kraemer committed
88
	if(quickTraversal != NULL)
89
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
90
		qCurrent = cont->begin() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
91
		current.dart = (*quickTraversal)[qCurrent] ;
92 93 94
	}
	else
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
95 96 97 98 99 100 101 102
		if(!firstTraversal)
		{
			if(dmark)
				dmark->unmarkAll() ;
			else
				cmark->unmarkAll() ;
		}

Pierre Kraemer's avatar
Pierre Kraemer committed
103 104 105
		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
106

Pierre Kraemer's avatar
Pierre Kraemer committed
107 108
		if(current.dart == m.end())
			current.dart = NIL ;
109
		else
Pierre Kraemer's avatar
Pierre Kraemer committed
110 111
		{
			if(dmark)
Pierre Kraemer's avatar
Pierre Kraemer committed
112
				dmark->template markOrbit<ORBIT>(current.dart) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
113 114 115 116 117
			else
				cmark->mark(current) ;
		}

		firstTraversal = false ;
118 119 120 121 122
	}

	return current ;
}

123
template <typename MAP, unsigned int ORBIT>
Pierre Kraemer's avatar
Pierre Kraemer committed
124
Cell<ORBIT> TraversorCell<MAP, ORBIT>::end()
Pierre Kraemer's avatar
Pierre Kraemer committed
125
{
Pierre Kraemer's avatar
Pierre Kraemer committed
126
	return Cell<ORBIT>(NIL) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
127
}
128

129
template <typename MAP, unsigned int ORBIT>
Pierre Kraemer's avatar
Pierre Kraemer committed
130
Cell<ORBIT> TraversorCell<MAP, ORBIT>::next()
131
{
Pierre Kraemer's avatar
Pierre Kraemer committed
132 133
	assert(current.dart != NIL);
//	if(current.dart != NIL)
134 135
//	{
	if(quickTraversal != NULL)
136
	{
137 138
		cont->next(qCurrent) ;
		if (qCurrent != cont->end())
Pierre Kraemer's avatar
Pierre Kraemer committed
139 140
			current.dart = (*quickTraversal)[qCurrent] ;
		else current.dart = NIL;
141 142 143 144
	}
	else
	{
		if(dmark)
145
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
146 147
			bool ismarked = dmark->isMarked(current.dart) ;
			while(current.dart != NIL && (ismarked || m.isBoundaryMarked(dimension, current.dart)))
148
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
149 150 151
				m.next(current.dart) ;
				if(current.dart == m.end())
					current.dart = NIL ;
152
				else
Pierre Kraemer's avatar
Pierre Kraemer committed
153
					ismarked = dmark->isMarked(current.dart) ;
154
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
155 156
			if(current.dart != NIL)
				dmark->template markOrbit<ORBIT>(current.dart) ;
157 158 159 160
		}
		else
		{
			bool ismarked = cmark->isMarked(current) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
161
			while(current.dart != NIL && (ismarked || m.isBoundaryMarked(dimension, current.dart) ))
Pierre Kraemer's avatar
Pierre Kraemer committed
162
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
163 164 165
				m.next(current.dart) ;
				if(current.dart == m.end())
					current.dart = NIL ;
166 167
				else
					ismarked = cmark->isMarked(current) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
168
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
169
			if(current.dart != NIL)
170
				cmark->mark(current) ;
171 172
		}
	}
173
//	}
174 175 176
	return current ;
}

177
template <typename MAP, unsigned int ORBIT>
Pierre Kraemer's avatar
Pierre Kraemer committed
178
void TraversorCell<MAP, ORBIT>::skip(Cell<ORBIT> c)
179 180
{
	if(dmark)
Pierre Kraemer's avatar
Pierre Kraemer committed
181
		dmark->template markOrbit<ORBIT>(c.dart) ;
182
	else
Pierre Kraemer's avatar
Pierre Kraemer committed
183
		cmark->mark(c) ;
184 185
}

Sylvain Thery's avatar
Sylvain Thery committed
186 187


Sylvain Thery's avatar
Sylvain Thery committed
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

template <typename MAP, unsigned int ORBIT>
Cell<ORBIT> TraversorCellEven<MAP, ORBIT>::begin()
{
	Cell<ORBIT> c = TraversorCell<MAP, ORBIT>::begin();
	this->firstTraversal = true;
	return c;
}



template <typename MAP, unsigned int ORBIT>
Cell<ORBIT> TraversorCellOdd<MAP, ORBIT>::begin()
{
	if(this->quickTraversal != NULL)
	{
		this->qCurrent = this->cont->begin() ;
		this->current.dart = this->quickTraversal->operator[](this->qCurrent);
	}
	else
	{
		this->current.dart = this->m.begin() ;
		while(this->current.dart != this->m.end() && (this->m.isBoundaryMarked(this->dimension, this->current.dart) ))
			this->m.next(this->current.dart) ;

		if(this->current.dart == this->m.end())
			this->current.dart = NIL ;
		else
		{
			if(this->dmark)
				this->dmark->template unmarkOrbit<ORBIT>(this->current.dart) ;
			else
				this->cmark->unmark(this->current) ;
		}
	}
	return this->current ;
}


template <typename MAP, unsigned int ORBIT>
Cell<ORBIT> TraversorCellOdd<MAP, ORBIT>::next()
{
	assert(this->current.dart != NIL);

	if(this->quickTraversal != NULL)
	{
		this->cont->next(this->qCurrent) ;
		if (this->qCurrent != this->cont->end())
			this->current.dart = this->quickTraversal->operator[](this->qCurrent) ;
		else this->current.dart = NIL;
	}
	else
	{
		if(this->dmark)
		{
			bool ismarked = this->dmark->isMarked(this->current.dart) ;
			while(this->current.dart != NIL && (!ismarked || this->m.isBoundaryMarked(this->dimension,this->current.dart)))
			{
				this->m.next(this->current.dart) ;
				if(this->current.dart == this->m.end())
					this->current.dart = NIL ;
				else
					ismarked = this->dmark->isMarked(this->current.dart) ;
			}
			if(this->current.dart != NIL)
				this->dmark->template unmarkOrbit<ORBIT>(this->current.dart) ;
		}
		else
		{
			bool ismarked = this->cmark->isMarked(this->current) ;
			while(this->current.dart != NIL && (!ismarked || this->m.isBoundaryMarked(this->dimension, this->current.dart) ))
			{
				this->m.next(this->current.dart) ;
				if(this->current.dart == this->m.end())
					this->current.dart = NIL ;
				else
					ismarked = this->cmark->isMarked(this->current) ;
			}
			if(this->current.dart != NIL)
				this->cmark->unmark(this->current) ;
		}
	}
	return this->current ;
}




Sylvain Thery's avatar
Sylvain Thery committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
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
399
} // namespace CGoGN