parallel_foreach.hpp 24.3 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
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/
#include <boost/thread.hpp>
#include <boost/thread/barrier.hpp>
sylvain thery's avatar
sylvain thery committed
26
#include <vector>
Sylvain Thery's avatar
Sylvain Thery committed
27

28

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

namespace Algo
{

namespace Parallel
{

38
/// internal functor for boost call
39
class ThreadFunctionAttrib
40 41
{
protected:
42
	std::vector<unsigned int>& m_ids;
43 44 45
	boost::barrier& m_sync1;
	boost::barrier& m_sync2;
	bool& m_finished;
46 47 48
	unsigned int m_id;
	FunctorAttribThreaded* m_functor;

49
public:
50 51
	ThreadFunctionAttrib(FunctorAttribThreaded* func, std::vector<unsigned int>& vid, boost::barrier& s1, boost::barrier& s2, bool& finished, unsigned int id):
		m_ids(vid), m_sync1(s1), m_sync2(s2), m_finished(finished), m_id(id), m_functor(func)
52 53 54
	{
	}

55 56
	ThreadFunctionAttrib(const ThreadFunctionAttrib& tf):
		m_ids(tf.m_ids), m_sync1(tf.m_sync1), m_sync2(tf.m_sync2), m_finished(tf.m_finished), m_id(tf.m_id), m_functor(tf.m_functor){}
57 58 59 60 61

	void operator()()
	{
		while (!m_finished)
		{
62
			for (std::vector<unsigned int>::const_iterator it = m_ids.begin(); it != m_ids.end(); ++it)
63
				m_functor->run(*it,m_id);
64 65 66 67
			m_sync1.wait();
			m_sync2.wait();
		}
	}
68 69
};

70

71
/// internal functor for boost call
72 73
template<typename MAP>
class ThreadFunction
Sylvain Thery's avatar
Sylvain Thery committed
74 75
{
protected:
76
	std::vector<Dart>& m_darts;
Sylvain Thery's avatar
Sylvain Thery committed
77 78 79
	boost::barrier& m_sync1;
	boost::barrier& m_sync2;
	bool& m_finished;
80 81
	unsigned int m_id;
	FunctorMapThreaded<MAP>* m_functor;
Sylvain Thery's avatar
Sylvain Thery committed
82
public:
83 84
	ThreadFunction(FunctorMapThreaded<MAP>* func, std::vector<Dart>& vd, boost::barrier& s1, boost::barrier& s2, bool& finished, unsigned int id):
		m_darts(vd), m_sync1(s1), m_sync2(s2), m_finished(finished), m_id(id), m_functor(func)
85 86 87
	{
	}

88 89
	ThreadFunction(const ThreadFunction<MAP>& tf):
		m_darts(tf.m_darts), m_sync1(tf.m_sync1), m_sync2(tf.m_sync2), m_finished(tf.m_finished), m_id(tf.m_id), m_functor(tf.m_functor){}
Sylvain Thery's avatar
Sylvain Thery committed
90 91 92 93 94 95

	void operator()()
	{
		while (!m_finished)
		{
			for (std::vector<Dart>::const_iterator it = m_darts.begin(); it != m_darts.end(); ++it)
96
				m_functor->run(*it,m_id);
Sylvain Thery's avatar
Sylvain Thery committed
97 98 99 100 101 102
			m_sync1.wait();
			m_sync2.wait();
		}
	}
};

103

104 105 106 107 108 109
inline unsigned int nbThreads()
{
	return boost::thread::hardware_concurrency();
}


110
inline unsigned int optimalNbThreads(NbParam p)
Sylvain Thery's avatar
Sylvain Thery committed
111
{
112 113 114 115 116 117 118 119
	if (p==NB_HIGHCOMPUTE)
		return nbThreads();
	if (p==NB_VERYHIGHMEMORY)
		return 2;

 // NB_HIGHMEMORY
	if (NBCORES != 0)
		return NBCORES;
120 121 122 123
	unsigned int nb = nbThreads();
	if (nb>4)
		return nb/2 ;
	return nb;
124

125
}
Sylvain Thery's avatar
Sylvain Thery committed
126 127


128

129
template <typename MAP, unsigned int ORBIT>
130
void foreach_cell(MAP& map, FunctorMapThreaded<MAP>& func, unsigned int nbth, bool needMarkers, const FunctorSelect& good, unsigned int currentThread)
131 132 133
{
	if (nbth == 0)
		nbth = optimalNbThreads();
Sylvain Thery's avatar
Sylvain Thery committed
134

135 136
	std::vector<FunctorMapThreaded<MAP>*> funcs;
	funcs.reserve(nbth);
137

138 139 140
	FunctorMapThreaded<MAP>* ptr = func.duplicate();
	bool shared = (ptr == NULL);

141
	if (shared)
142
	{
143
		for (unsigned int i = 0; i < nbth; ++i)
144
			funcs.push_back(&func);
145 146 147
	}
	else
	{
148 149
		funcs.push_back(ptr);
		for (unsigned int i = 1; i < nbth; ++i)
150
			funcs.push_back(func.duplicate());
151
	}
Sylvain Thery's avatar
Sylvain Thery committed
152

153
	foreach_cell<MAP,ORBIT>(map,funcs,needMarkers,good,currentThread);
Sylvain Thery's avatar
Sylvain Thery committed
154

155
	if (!shared)
156
		for (unsigned int i = 0; i < nbth; ++i)
157
			delete funcs[i];
Sylvain Thery's avatar
Sylvain Thery committed
158 159
}

160
template <typename MAP, unsigned int ORBIT>
161
void foreach_cell(MAP& map, std::vector<FunctorMapThreaded<MAP>*>& funcs, bool needMarkers, const FunctorSelect& good, unsigned int currentThread)
Sylvain Thery's avatar
Sylvain Thery committed
162
{
163
	unsigned int nbth = funcs.size();
164

sylvain thery's avatar
sylvain thery committed
165
	std::vector<Dart>* vd = new std::vector<Dart>[nbth];
Sylvain Thery's avatar
Sylvain Thery committed
166 167

	// nbth new functions, new thread (with good darts !)
168
	for (unsigned int i = 0; i < nbth; ++i)
169
		vd[i].reserve(SIZE_BUFFER_THREAD);
Sylvain Thery's avatar
Sylvain Thery committed
170

171 172 173 174 175 176 177 178 179 180
	AttributeContainer* cont = NULL;
	DartMarker* dmark = NULL;
	CellMarker<ORBIT>* cmark = NULL;
	AttributeMultiVector<Dart>* quickTraversal = map.template getQuickTraversal<ORBIT>() ;

	// fill each vd buffers with SIZE_BUFFER_THREAD darts
	Dart d;
	unsigned int di=0;

	if(quickTraversal != NULL)
Sylvain Thery's avatar
Sylvain Thery committed
181
	{
182 183 184 185 186
		cont = &(map.template getAttributeContainer<ORBIT>()) ;

		di = cont->begin();
		unsigned int nb = 0;
		while ((di != cont->end()) && (nb < nbth*SIZE_BUFFER_THREAD) )
Sylvain Thery's avatar
Sylvain Thery committed
187
		{
188 189 190 191 192 193 194
			d = quickTraversal->operator[](di);
			if (good(d))
			{
				vd[nb%nbth].push_back(d);
				nb++;
			}
			cont->next(di);
Sylvain Thery's avatar
Sylvain Thery committed
195 196
		}
	}
197
	else
198
	{
199 200 201 202 203 204 205 206
		if(map.template isOrbitEmbedded<ORBIT>())
		{
			cmark = new CellMarker<ORBIT>(map, currentThread) ;

			d = map.begin();
			unsigned int nb = 0;
			while ((d != map.end()) && (nb < nbth*SIZE_BUFFER_THREAD) )
			{
207
				if (good(d) && (!map.isBoundaryMarked(d)) && (!cmark->isMarked(d)))
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
				{
					cmark->mark(d);
					vd[nb%nbth].push_back(d);
					nb++;
				}
				map.next(d);
			}
		}
		else
		{
			dmark = new DartMarker(map, currentThread) ;
			d = map.begin();
			unsigned int nb = 0;
			while ((d != map.end()) && (nb < nbth*SIZE_BUFFER_THREAD) )
			{
223
				if (good(d) && (!map.isBoundaryMarked(d)) && (!dmark->isMarked(d)))
224 225 226 227 228 229 230 231
				{
					dmark->markOrbit<ORBIT>(d);
					vd[nb%nbth].push_back(d);
					nb++;
				}
				map.next(d);
			}
		}
232 233
	}

Sylvain Thery's avatar
Sylvain Thery committed
234 235 236 237
	boost::barrier sync1(nbth+1);
	boost::barrier sync2(nbth+1);
	bool finished=false;
	// lauch threads
238 239
	if (needMarkers)
	{
240 241 242
		unsigned int nbth_prec = map.getNbThreadMarkers();
		if (nbth_prec < nbth+1)
			map.addThreadMarker(nbth+1-nbth_prec);
243
	}
244

245 246 247
	boost::thread** threads = new boost::thread*[nbth];
	ThreadFunction<MAP>** tfs = new ThreadFunction<MAP>*[nbth];

248
	for (unsigned int i = 0; i < nbth; ++i)
249 250 251 252 253 254
	{
		tfs[i] = new ThreadFunction<MAP>(funcs[i], vd[i],sync1,sync2, finished,1+i);
		threads[i] = new boost::thread( boost::ref( *(tfs[i]) ) );
	}
//		threads[i] = new boost::thread(ThreadFunction<MAP>(funcs[i], vd[i],sync1,sync2, finished,1+i));

255

Sylvain Thery's avatar
Sylvain Thery committed
256
	// and continue to traverse the map
sylvain thery's avatar
sylvain thery committed
257 258
	std::vector<Dart>* tempo = new std::vector<Dart>[nbth];

259
	for (unsigned int i = 0; i < nbth; ++i)
260
		tempo[i].reserve(SIZE_BUFFER_THREAD);
Sylvain Thery's avatar
Sylvain Thery committed
261 262


263 264 265
	if (cont)
	{
		while (di != cont->end())
Sylvain Thery's avatar
Sylvain Thery committed
266
		{
267 268 269 270
			for (unsigned int i = 0; i < nbth; ++i)
				tempo[i].clear();
			unsigned int nb = 0;
			while ((di != cont->end()) && (nb < nbth*SIZE_BUFFER_THREAD) )
Sylvain Thery's avatar
Sylvain Thery committed
271
			{
272 273 274 275 276 277 278
				d = quickTraversal->operator[](di);
				if (good(d))
				{
					tempo[nb%nbth].push_back(d);
					nb++;
				}
				cont->next(di);
Sylvain Thery's avatar
Sylvain Thery committed
279
			}
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
			sync1.wait();
			for (unsigned int i = 0; i < nbth; ++i)
				vd[i].swap(tempo[i]);
			sync2.wait();
		}
	}
	else if (cmark)
	{
		while (d != map.end())
		{
			for (unsigned int i = 0; i < nbth; ++i)
				tempo[i].clear();
			unsigned int nb = 0;
			while ((d != map.end()) && (nb < nbth*SIZE_BUFFER_THREAD) )
			{
295
				if (good(d) && (!map.isBoundaryMarked(d)) && (!cmark->isMarked(d)))
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
				{
					cmark->mark(d);
					tempo[nb%nbth].push_back(d);
					nb++;
				}
				map.next(d);
			}
			sync1.wait();
			for (unsigned int i = 0; i < nbth; ++i)
				vd[i].swap(tempo[i]);
			sync2.wait();
		}
	}
	else
	{
		while (d != map.end())
		{
			for (unsigned int i = 0; i < nbth; ++i)
				tempo[i].clear();
			unsigned int nb = 0;
			while ((d != map.end()) && (nb < nbth*SIZE_BUFFER_THREAD) )
			{
318
				if (good(d) && (!map.isBoundaryMarked(d)) && (!dmark->isMarked(d)))
319 320 321 322 323 324 325 326 327 328 329
				{
					dmark->markOrbit<ORBIT>(d);
					tempo[nb%nbth].push_back(d);
					nb++;
				}
				map.next(d);
			}
			sync1.wait();
			for (unsigned int i = 0; i < nbth; ++i)
				vd[i].swap(tempo[i]);
			sync2.wait();
Sylvain Thery's avatar
Sylvain Thery committed
330 331 332 333 334 335 336 337
		}
	}

	sync1.wait();
	finished = true;
	sync2.wait();

	//wait for all theads to be finished
338
	for (unsigned int i = 0; i < nbth; ++i)
Sylvain Thery's avatar
Sylvain Thery committed
339 340 341
	{
		threads[i]->join();
		delete threads[i];
342
		delete tfs[i];
Sylvain Thery's avatar
Sylvain Thery committed
343
	}
344
	delete[] tfs;
sylvain thery's avatar
sylvain thery committed
345 346 347
	delete[] threads;
	delete[] vd;
	delete[] tempo;
Sylvain Thery's avatar
Sylvain Thery committed
348

349 350
	if (cmark != NULL)
		delete cmark;
Sylvain Thery's avatar
Sylvain Thery committed
351

352 353 354
	if (dmark != NULL)
		delete dmark;
}
Sylvain Thery's avatar
Sylvain Thery committed
355 356 357



358
template <typename MAP>
359
void foreach_dart(MAP& map, FunctorMapThreaded<MAP>& func, unsigned int nbth, bool needMarkers, const FunctorSelect& good)
360 361 362
{
	if (nbth == 0)
		nbth = optimalNbThreads();
363

364 365 366
	std::vector<FunctorMapThreaded<MAP>*> funcs;
	funcs.reserve(nbth);

367 368 369
	FunctorMapThreaded<MAP>* ptr = func.duplicate();
	bool shared = (ptr == NULL);

370
	if (shared)
371
	{
372
		for (unsigned int i = 0; i < nbth; ++i)
373
			funcs.push_back(&func);
374 375 376
	}
	else
	{
377 378
		funcs.push_back(ptr);
		for (unsigned int i = 1; i < nbth; ++i)
379
			funcs.push_back(func.duplicate());
380
	}
Sylvain Thery's avatar
Sylvain Thery committed
381

382
	foreach_dart<MAP>(map,funcs,needMarkers,good);
Sylvain Thery's avatar
Sylvain Thery committed
383

384
	if (!shared)
385
		for (unsigned int i = 0; i < nbth; ++i)
386
			delete funcs[i];
Sylvain Thery's avatar
Sylvain Thery committed
387 388
}

389 390

template <typename MAP>
391
void foreach_dart(MAP& map, std::vector<FunctorMapThreaded<MAP>*> funcs, bool needMarkers, const FunctorSelect& good)
392
{
393
	unsigned int nbth = funcs.size();
394

395 396 397
	std::vector<Dart>* vd = new std::vector<Dart>[nbth];
	boost::thread** threads = new boost::thread*[nbth];

398
	Dart d = map.begin();
399 400

	// nbth new functions, new thread (with good darts !)
401
	for (unsigned int i = 0; i < nbth; ++i)
402
		vd[i].reserve(SIZE_BUFFER_THREAD);
403 404

	// fill each vd buffers with 4096 darts
405
	unsigned int nb = 0;
406
	while ((d != map.end()) && (nb < nbth*SIZE_BUFFER_THREAD) )
407
	{
408
		if (good(d))
409 410 411 412 413 414 415
		{
			vd[nb%nbth].push_back(d);
			nb++;
		}
		map.next(d);
	}

416 417 418 419
	boost::barrier sync1(nbth+1);
	boost::barrier sync2(nbth+1);
	bool finished = false;
	// lauch threads
420 421 422 423 424 425 426
	if (needMarkers)
	{
		unsigned int nbth_prec = map.getNbThreadMarkers();
		if (nbth_prec < nbth+1)
			map.addThreadMarker(nbth+1-nbth_prec);
	}

427
	for (unsigned int i = 0; i < nbth; ++i)
428
	{
429
		threads[i] = new boost::thread(ThreadFunction<MAP>(funcs[i], vd[i],sync1,sync2, finished,1+i));
430 431 432 433
	}

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

437
	while (d != map.end())
438
	{
439
		for (unsigned int i = 0; i < nbth; ++i)
440 441
			tempo[i].clear();

442 443
		unsigned int nb =0;
		while ((d != map.end()) && (nb < nbth*SIZE_BUFFER_THREAD) )
444
		{
445
			if (good(d))
446 447 448 449 450 451 452 453
			{
				tempo[nb%nbth].push_back(d);
				nb++;
			}
			map.next(d);
		}

		sync1.wait();
454
		for (unsigned int i = 0; i < nbth; ++i)
455 456 457 458 459 460 461 462
			vd[i].swap(tempo[i]);
		sync2.wait();
	}

	sync1.wait();
	finished = true;
	sync2.wait();

463
	//wait for all theads to be finished
464
	for (unsigned int i = 0; i < nbth; ++i)
465 466 467 468
	{
		threads[i]->join();
		delete threads[i];
	}
469 470 471 472
	
	delete vd;
	delete threads;
	delete tempo;
473 474 475
}


476
// TODO same modification for transparent usage of dart marker / cell marker / quick traversal ??
477

478
template <typename MAP, unsigned int CELL>
479
void foreach_cell2Pass(MAP& map, std::vector<FunctorMapThreaded<MAP>*>& funcsFrontnBack, unsigned int nbLoops, bool needMarkers, const FunctorSelect& good)
480
{
481 482
	unsigned int nbth = funcsFrontnBack.size()/2;

483
	std::vector<Dart>* vd = new std::vector<Dart>[2*nbth];
484
	for (unsigned int i = 0; i < nbth; ++i)
485
		vd[i].reserve(SIZE_BUFFER_THREAD);
486

487
	boost::thread** threadsAB = new boost::thread*[2*nbth];
488 489 490 491 492 493 494 495 496

	if (needMarkers)
	{
		// ensure that there is enough threads
		unsigned int nbth_prec = map.getNbThreadMarkers();
		if (nbth_prec < nbth+1)
			map.addThreadMarker(nbth+1-nbth_prec);
	}

497 498 499 500
	CellMarkerNoUnmark<CELL> cm(map); // for 2 pass front mark / back unmark

	boost::barrier sync1(nbth+1);
	boost::barrier sync2(nbth+1);
501 502 503 504 505 506

	for (unsigned int loop=0; loop< nbLoops; ++loop)
	{
		// PASS FRONT (A)
		{
			Dart d = map.begin();
507
			// fill each vd buffers with SIZE_BUFFER_THREAD darts
508
			unsigned int nb = 0;
509
			while ((d != map.end()) && (nb < nbth*SIZE_BUFFER_THREAD) )
510 511 512 513 514 515 516 517 518 519 520
			{
				if (good(d) && (!cm.isMarked(d)))
				{
					cm.mark(d);
					vd[nb%nbth].push_back(d);
					nb++;
				}
				map.next(d);
			}

			bool finished=false;
521 522 523 524

			for (unsigned int i = 0; i < nbth; ++i)
				threadsAB[i] = new boost::thread(ThreadFunction<MAP>(funcsFrontnBack[i], vd[i], sync1, sync2, finished,1+i));

525 526 527
			while (d != map.end())
			{
				for (unsigned int i = 0; i < nbth; ++i)
528
					vd[nbth+i].clear();
529 530

				unsigned int nb = 0;
531
				while ((d != map.end()) && (nb < nbth*SIZE_BUFFER_THREAD) )
532 533 534 535
				{
					if (good(d) && (!cm.isMarked(d)))
					{
						cm.mark(d);
536
						vd[nbth+nb%nbth].push_back(d);
537 538 539 540 541 542 543
						nb++;
					}
					map.next(d);
				}

				sync1.wait();
				for (unsigned int i = 0; i < nbth; ++i)
544
					vd[i].swap(vd[nbth+i]);
545 546 547 548 549 550 551 552 553
				sync2.wait();
			}

			sync1.wait();
			finished = true;
			sync2.wait();

			//wait for all theads to be finished
			for (unsigned int i = 0; i < nbth; ++i)
554
				threadsAB[i]->join();
555 556 557 558 559 560
		}
		// PASS BACK (B)
		{
			for (unsigned int i = 0; i < nbth; ++i)
				vd[i].clear();
			Dart d = map.begin();
561
			// fill each vd buffers with SIZE_BUFFER_THREAD darts
562
			unsigned int nb = 0;
563
			while ((d != map.end()) && (nb < nbth*SIZE_BUFFER_THREAD) )
564 565 566 567 568 569 570 571 572 573 574
			{
				if (good(d) && (cm.isMarked(d)))
				{
					cm.unmark(d);
					vd[nb%nbth].push_back(d);
					nb++;
				}
				map.next(d);
			}

			bool finished=false;
575 576 577
			for (unsigned int i = 0; i < nbth; ++i)
				threadsAB[nbth+i] = new boost::thread(ThreadFunction<MAP>(funcsFrontnBack[nbth+i], vd[i],sync1,sync2, finished,1+i));

578 579 580 581 582
			// and continue to traverse the map

			while (d != map.end())
			{
				for (unsigned int i = 0; i < nbth; ++i)
583
					vd[nbth+i].clear();
584 585

				unsigned int nb = 0;
586
				while ((d != map.end()) && (nb < nbth*SIZE_BUFFER_THREAD) )
587 588 589 590
				{
					if (good(d) && (cm.isMarked(d)))
					{
						cm.unmark(d);
591
						vd[nbth+nb%nbth].push_back(d);
592 593 594 595 596 597 598
						nb++;
					}
					map.next(d);
				}

				sync1.wait();
				for (unsigned int i = 0; i < nbth; ++i)
599
					vd[i].swap(vd[nbth+i]);
600 601 602 603 604 605 606 607 608
				sync2.wait();
			}

			sync1.wait();
			finished = true;
			sync2.wait();

			//wait for all theads to be finished
			for (unsigned int i = 0; i < nbth; ++i)
609
				threadsAB[nbth+i]->join();
610 611 612 613
		}
	}

	// free buffers and threads
614
	for (unsigned int i = 0; i < 2*nbth; ++i)
615
	{
616
		delete threadsAB[i];
617
	}
618
	delete[] threadsAB;
619 620 621 622 623
	delete[] vd;
}



624
template <typename MAP, unsigned int CELL>
625
void foreach_cell2Pass(MAP& map, FunctorMapThreaded<MAP>& funcFront, FunctorMapThreaded<MAP>& funcBack, unsigned int nbLoops, unsigned int nbth, bool needMarkers, const FunctorSelect& good)
626 627 628
{
	if (nbth == 0)
		nbth = optimalNbThreads();
629

630 631
	std::vector<FunctorMapThreaded<MAP>*> funcs;
	funcs.reserve(nbth);
632

633 634 635
	FunctorMapThreaded<MAP>* ptr = funcFront.duplicate();
	bool shared = (ptr == NULL);

636 637 638 639 640 641 642 643 644
	if (shared)
	{
		for (unsigned int i = 0; i < nbth; ++i)
			funcs.push_back(&funcFront);
		for (unsigned int i = 0; i < nbth; ++i)
			funcs.push_back(&funcBack);
	}
	else
	{
645 646
		funcs.push_back(ptr);
		for (unsigned int i = 1; i < nbth; ++i)
647 648 649
			funcs.push_back(funcFront.duplicate());
		for (unsigned int i = 0; i < nbth; ++i)
			funcs.push_back(funcBack.duplicate());
650

651
	}
652

653
	foreach_cell2Pass<MAP,CELL>(map,funcs,nbLoops,needMarkers,good);
654

655
	if (!shared)
656
		for (unsigned int i = 0; i < 2*nbth; ++i)
657 658
			delete funcs[i];
}
659

660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
//
//
//template <typename MAP>
//void Foreach<MAP>::Foreach(MAP& map, unsigned int nbth):
//m_nbth(nbth)
//{
//	if (m_nbth == 0)
//		m_nbth = optimalNbThreads();
//
//	m_funcs.reserve(m_nbth);
//
//	m_vd = new std::vector<Dart>[2*nbth];
//	for (unsigned int i = 0; i < 2*nbth; ++i)
//		m_vd[i].reserve(SIZE_BUFFER_THREAD);
//}
//
//template <typename MAP>
//Foreach<MAP>::~Foreach(MAP& map, unsigned int nbth)
//{
//	delete[] m_vd;
//}
//
//
//template <typename MAP>
//void Foreach<MAP>:: clearFunctors()
//{
//	m_funcs.clear();
//}
//
//template <typename MAP>
//void Foreach<MAP>:: addFunctor(FunctorMapThreaded<MAP>* funcPtr)
//{
//	m_funcs.push_back(funcPtr);
//}
//
//template <typename MAP>
//template<typename T>
//T* Foreach<MAP>::getFunctor(unsigned int i)
//{
//	assert(i < m_funcs.size());
//	return dynamic_cast<T*>(m_funcs[i]);
//}
//
//
//template <typename MAP>
//template <unsigned int ORBIT>
//void Foreach<MAP>::traverseCell<ORBIT>(bool needMarkers, const FunctorSelect& good, unsigned int currentThread)
//{
//	assert(m_funcs.size() ==  m_nbth);
//
//	boost::thread** threads = new boost::thread*[m_nbth];
//
//	AttributeContainer* cont = NULL;
//	DartMarker* dmark = NULL;
//	CellMarker<ORBIT>* cmark = NULL;
//	AttributeMultiVector<Dart>* quickTraversal = m_map.template getQuickTraversal<ORBIT>() ;
//
//	// fill each vd buffers with SIZE_BUFFER_THREAD darts
//	Dart d;
//	unsigned int di=0;
//
//	if(quickTraversal != NULL)
//	{
//		cont = &(m_map.template getAttributeContainer<ORBIT>()) ;
//
//		di = cont->begin();
//		unsigned int nb = 0;
//		while ((di != cont->end()) && (nb < m_nbth*SIZE_BUFFER_THREAD) )
//		{
//			d = quickTraversal->operator[](di);
//			if (good(d))
//			{
//				m_vd[nb%m_nbth].push_back(d);
//				nb++;
//			}
//			cont->next(di);
//		}
//	}
//	else
//	{
//		if(m_map.template isOrbitEmbedded<ORBIT>())
//		{
//			cmark = new CellMarker<ORBIT>(m_map, currentThread) ;
//
//			d = m_map.begin();
//			unsigned int nb = 0;
//			while ((d != m_map.end()) && (nb < m_nbth*SIZE_BUFFER_THREAD) )
//			{
//				if (good(d) && (!m_map.isBoundaryMarked(d)) && (!cmark->isMarked(d)))
//				{
//					cmark->mark(d);
//					m_vd[nb%m_nbth].push_back(d);
//					nb++;
//				}
//				m_map.next(d);
//			}
//		}
//		else
//		{
//			dmark = new DartMarker(m_map, currentThread) ;
//			d = m_map.begin();
//			unsigned int nb = 0;
//			while ((d != m_map.end()) && (nb < m_nbth*SIZE_BUFFER_THREAD) )
//			{
//				if (good(d) && (!m_map.isBoundaryMarked(d)) && (!dmark->isMarked(d)))
//				{
//					dmark->markOrbit<ORBIT>(d);
//					m_vd[nb%m_nbth].push_back(d);
//					nb++;
//				}
//				m_map.next(d);
//			}
//		}
//	}
//
//	boost::barrier sync1(m_nbth+1);
//	boost::barrier sync2(m_nbth+1);
//	bool finished=false;
//	// lauch threads
//	if (needMarkers)
//	{
//		unsigned int nbth_prec = m_map.getNbThreadMarkers();
//		if (nbth_prec < m_nbth+1)
//			m_map.addThreadMarker(m_nbth+1-nbth_prec);
//	}
//
//	for (unsigned int i = 0; i < m_nbth; ++i)
//		threads[i] = new boost::thread(ThreadFunction<MAP>(m_funcs[i], m_vd[i],sync1,sync2, finished,1+i));
//
//
//	if (cont)
//	{
//		while (di != cont->end())
//		{
//			for (unsigned int i = 0; i < m_nbth; ++i)
//				m_vd[m_nbth+i].clear();
//			unsigned int nb = 0;
//			while ((di != cont->end()) && (nb < m_nbth*SIZE_BUFFER_THREAD) )
//			{
//				d = quickTraversal->operator[](di);
//				if (good(d))
//				{
//					m_vd[m_nbth + nb%m_nbth].push_back(d);
//					nb++;
//				}
//				cont->next(di);
//			}
//			sync1.wait();
//			for (unsigned int i = 0; i < m_nbth; ++i)
//				m_vd[i].swap(m_vd[m_nbth+i]);
//			sync2.wait();
//		}
//	}
//	else if (cmark)
//	{
//		while (d != m_map.end())
//		{
//			for (unsigned int i = 0; i < m_nbth; ++i)
//				m_vd[m_nbth+i].clear();
//			unsigned int nb = 0;
//			while ((d != m_map.end()) && (nb < m_nbth*SIZE_BUFFER_THREAD) )
//			{
//				if (good(d) && (!m_map.isBoundaryMarked(d)) && (!cmark->isMarked(d)))
//				{
//					cmark->mark(d);
//					m_vd[m_nbth+nb%m_nbth].push_back(d);
//					nb++;
//				}
//				m_map.next(d);
//			}
//			sync1.wait();
//			for (unsigned int i = 0; i < m_nbth; ++i)
//				m_vd[i].swap(m_vd[m_nbth+i]);
//			sync2.wait();
//		}
//	}
//	else
//	{
//		while (d != m_map.end())
//		{
//			for (unsigned int i = 0; i < m_nbth; ++i)
//				m_vd[m_nbth+i].clear();
//			unsigned int nb = 0;
//			while ((d != m_map.end()) && (nb < m_nbth*SIZE_BUFFER_THREAD) )
//			{
//				if (good(d) && (!m_map.isBoundaryMarked(d)) && (!dmark->isMarked(d)))
//				{
//					dmark->markOrbit<ORBIT>(d);
//					m_vd[m_nbth+nb%m_nbth].push_back(d);
//					nb++;
//				}
//				m_map.next(d);
//			}
//			sync1.wait();
//			for (unsigned int i = 0; i < m_nbth; ++i)
//				m_vd[i].swap(m_vd[m_nbth+i]);
//			sync2.wait();
//		}
//	}
//
//	sync1.wait();
//	finished = true;
//	sync2.wait();
//
//	//wait for all theads to be finished
//	for (unsigned int i = 0; i < m_nbth; ++i)
//	{
//		threads[i]->join();
//		delete threads[i];
//	}
//	delete[] threads;
//	delete[] m_vd;
//
//	if (cmark != NULL)
//		delete cmark;
//
//	if (dmark != NULL)
//		delete dmark;
//}
//
//



template <typename MAP, unsigned int ORBIT>
void foreach_cell_all_thread(MAP& map, std::vector<FunctorMapThreaded<MAP>*>& funcs, bool needMarkers, const FunctorSelect& good, unsigned int currentThread)
{
	unsigned int nbth = funcs.size();

	boost::thread** threads = new boost::thread*[nbth];

	std::vector<Dart> vd;
	vd.reserve(SIZE_BUFFER_THREAD);

	AttributeContainer* cont = NULL;
	DartMarker* dmark = NULL;
	CellMarker<ORBIT>* cmark = NULL;
	AttributeMultiVector<Dart>* quickTraversal = map.template getQuickTraversal<ORBIT>() ;

	// fill each vd buffers with SIZE_BUFFER_THREAD darts
	Dart d;
	unsigned int di=0;

	if(quickTraversal != NULL)
	{
		cont = &(map.template getAttributeContainer<ORBIT>()) ;
		di = cont->begin();
		unsigned int nb = 0;
		while ((di != cont->end()) && (nb < SIZE_BUFFER_THREAD) )
		{
			d = quickTraversal->operator[](di);
			if (good(d))
			{
				vd.push_back(d);
				nb++;
			}
			cont->next(di);
		}
	}
	else
	{
		if(map.template isOrbitEmbedded<ORBIT>())
		{
			cmark = new CellMarker<ORBIT>(map, currentThread) ;
			d = map.begin();
			unsigned int nb=0;
			while ((d != map.end()) && (nb < SIZE_BUFFER_THREAD) )
			{
				if (good(d) && (!map.isBoundaryMarked(d)) && (!cmark->isMarked(d)))
				{
					cmark->mark(d);
					vd.push_back(d);
					nb++;
				}
				map.next(d);
			}
		}
		else
		{
			dmark = new DartMarker(map, currentThread) ;
			d = map.begin();
			unsigned int nb=0;
			while ((d != map.end()) && (nb < SIZE_BUFFER_THREAD) )
			{
				if (good(d) && (!map.isBoundaryMarked(d)) && (!dmark->isMarked(d)))
				{
					dmark->markOrbit<ORBIT>(d);
					vd.push_back(d);
					nb++;
				}
				map.next(d);
			}
		}
	}

	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);
	}

	for (unsigned int i = 0; i < nbth; ++i)
		threads[i] = new boost::thread(ThreadFunction<MAP>(funcs[i], vd,sync1,sync2, finished,1+i));

	// and continue to traverse the map
	std::vector<Dart> tempo;
	tempo.reserve(SIZE_BUFFER_THREAD);

	if (cont)
	{
		while (di != cont->end())
		{
			tempo.clear();
			unsigned int nb=0;
			while ((di != cont->end()) && (nb < SIZE_BUFFER_THREAD) )
			{
				d = quickTraversal->operator[](di);
				if (good(d))
				{
					tempo.push_back(d);
					nb++;
				}
				cont->next(di);
			}
			sync1.wait();
			vd.swap(tempo);
			sync2.wait();
		}
	}
	else if (cmark)
	{
		while (d != map.end())
		{
			tempo.clear();
			unsigned int nb=0;
			while ((d != map.end()) && (nb < SIZE_BUFFER_THREAD) )
			{
				if (good(d) && (!map.isBoundaryMarked(d)) && (!cmark->isMarked(d)))
				{
					cmark->mark(d);
					tempo.push_back(d);
					nb++;
				}
				map.next(d);
			}
			sync1.wait();
			vd.swap(tempo);
			sync2.wait();
		}
	}
	else
	{
		while (d != map.end())
		{
			tempo.clear();
			unsigned int nb=0;
			while ((d != map.end()) && (nb < SIZE_BUFFER_THREAD) )
			{
				if (good(d) && (!map.isBoundaryMarked(d)) && (!dmark->isMarked(d)))
				{
					dmark->markOrbit<ORBIT>(d);
					tempo.push_back(d);
					nb++;
				}
				map.next(d);
			}
			sync1.wait();
			vd.swap(tempo);
			sync2.wait();
		}
	}

	sync1.wait();
	finished = true;
	sync2.wait();

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

	if (cmark != NULL)
		delete cmark;

	if (dmark != NULL)
		delete dmark;
}







1062

1063
} // namespace Parallel
1064

1065
} // namespace Algo
1066

1067
} // namespace CGoGN