FFmmAlgorithmTask.hpp 14.4 KB
Newer Older
1
// ===================================================================================
2
// Copyright ScalFmm 2011 INRIA, Olivier Coulaud, Berenger Bramas, Matthias Messner
3 4 5 6 7 8 9 10 11 12 13 14
// olivier.coulaud@inria.fr, berenger.bramas@inria.fr
// This software is a computer program whose purpose is to compute the FMM.
//
// This software is governed by the CeCILL-C and LGPL licenses and
// abiding by the rules of distribution of free software.  
// 
// This program 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 General Public and CeCILL-C Licenses for more details.
// "http://www.cecill.info". 
// "http://www.gnu.org/licenses".
15
// ===================================================================================
16 17
#ifndef FFMMALGORITHMTASK_HPP
#define FFMMALGORITHMTASK_HPP
18

19 20

#include "../Utils/FGlobal.hpp"
BRAMAS Berenger's avatar
BRAMAS Berenger committed
21
#include "../Utils/FAssert.hpp"
BRAMAS Berenger's avatar
BRAMAS Berenger committed
22
#include "../Utils/FLog.hpp"
23

24 25 26 27 28
#include "../Utils/FTic.hpp"

#include "../Containers/FOctree.hpp"
#include "../Containers/FVector.hpp"

29
#include "FCoreCommon.hpp"
30 31

/**
32 33 34 35 36 37 38 39 40 41
 * @author Berenger Bramas (berenger.bramas@inria.fr)
 * @class FFmmAlgorithmTask
 * @brief
 * Please read the license
 *
 * This class is a basic FMM algorithm
 * It just iterates on a tree and call the kernels with good arguments.
 *
 * Of course this class does not deallocate pointer given in arguements.
 */
42
template<class OctreeClass, class CellClass, class ContainerClass, class KernelClass, class LeafClass>
43
class FFmmAlgorithmTask : public FAbstractAlgorithm, public FAlgorithmTimers {
44

45 46
	OctreeClass* const tree;       //< The octree to work on
	KernelClass** kernels;    //< The kernels
47

48
	const int MaxThreads;
49

50
	const int OctreeHeight;
51

52
	const int leafLevelSeperationCriteria;
BRAMAS Berenger's avatar
BRAMAS Berenger committed
53
public:
54 55 56 57 58 59 60 61 62 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
	/** The constructor need the octree and the kernels used for computation
	 * @param inTree the octree to work on
	 * @param inKernels the kernels to call
	 * An assert is launched if one of the arguments is null
	 */
	FFmmAlgorithmTask(OctreeClass* const inTree, KernelClass* const inKernels, const int inLeafLevelSeperationCriteria = 1)
: tree(inTree) , kernels(nullptr),
  MaxThreads(omp_get_max_threads()), OctreeHeight(tree->getHeight()), leafLevelSeperationCriteria(inLeafLevelSeperationCriteria)
{

		FAssertLF(tree, "tree cannot be null");
		FAssertLF(inKernels, "kernels cannot be null");

		this->kernels = new KernelClass*[MaxThreads];
#pragma omp parallel for schedule(static)
		for(int idxThread = 0 ; idxThread < MaxThreads ; ++idxThread){
#pragma omp critical (InitFFmmAlgorithmTask)
			{
				this->kernels[idxThread] = new KernelClass(*inKernels);
			}
		}

		FAbstractAlgorithm::setNbLevelsInTree(tree->getHeight());

		FLOG(FLog::Controller << "FFmmAlgorithmTask (Max Thread " << omp_get_max_threads() << ")\n");
}

	/** Default destructor */
	virtual ~FFmmAlgorithmTask(){
		for(int idxThread = 0 ; idxThread < MaxThreads ; ++idxThread){
			delete this->kernels[idxThread];
		}
		delete [] this->kernels;
	}
88

89
protected:
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 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
	/**
	 * To execute the fmm algorithm
	 * Call this function to run the complete algorithm
	 */
	void executeCore(const unsigned operationsToProceed) override {

		Timers[P2MTimer].tic();
		if(operationsToProceed & FFmmP2M)
			bottomPass();
		Timers[P2MTimer].tac();

		Timers[M2MTimer].tic();
		if(operationsToProceed & FFmmM2M)
			upwardPass();
		Timers[M2MTimer].tac();

		Timers[M2LTimer].tic();
		if(operationsToProceed & FFmmM2L)
			transferPass();
		Timers[M2LTimer].tac();

		Timers[L2LTimer].tic();
		if(operationsToProceed & FFmmL2L)
			downardPass();
		Timers[L2LTimer].tac();

		Timers[NearTimer].tic();
		if( (operationsToProceed & FFmmP2P) || (operationsToProceed & FFmmL2P) )
			directPass((operationsToProceed & FFmmP2P),(operationsToProceed & FFmmL2P));
		Timers[NearTimer].tac();
	}

	/////////////////////////////////////////////////////////////////////////////
	// P2M
	/////////////////////////////////////////////////////////////////////////////

	/** P2M */
	void bottomPass(){
		FLOG( FLog::Controller.write("\tStart Bottom Pass\n").write(FLog::Flush) );
		FLOG(FTic counterTime);

#pragma omp parallel
		{
#pragma omp single nowait
			{
				typename OctreeClass::Iterator octreeIterator(tree);

				// Iterate on leafs
				octreeIterator.gotoBottomLeft();
				do{
					// We need the current cell that represent the leaf
					// and the list of particles
#pragma omp task firstprivate(octreeIterator)
					{
						kernels[omp_get_thread_num()]->P2M( octreeIterator.getCurrentCell() , octreeIterator.getCurrentListSrc());
					}
				} while(octreeIterator.moveRight());

#pragma omp taskwait
			}
		}

		FLOG( FLog::Controller << "\tFinished (@Bottom Pass (P2M) = "  << counterTime.tacAndElapsed() << "s)\n" );
	}

	/////////////////////////////////////////////////////////////////////////////
	// Upward
	/////////////////////////////////////////////////////////////////////////////

	/** M2M */
	void upwardPass(){
		FLOG( FLog::Controller.write("\tStart Upward Pass\n").write(FLog::Flush); );
		FLOG(FTic counterTime);

#pragma omp parallel
		{
#pragma omp single nowait
			{
				// Start from leal level - 1
				typename OctreeClass::Iterator octreeIterator(tree);
				octreeIterator.gotoBottomLeft();
				octreeIterator.moveUp();

				for(int idxLevel = OctreeHeight - 2 ; idxLevel > FAbstractAlgorithm::lowerWorkingLevel-1 ; --idxLevel){
					octreeIterator.moveUp();
				}

				typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);

				// for each levels
				for(int idxLevel = FMath::Min(OctreeHeight - 2, FAbstractAlgorithm::lowerWorkingLevel - 1) ; idxLevel >= FAbstractAlgorithm::upperWorkingLevel ; --idxLevel ){
					FLOG(FTic counterTimeLevel);
					// for each cells
					do{
						// We need the current cell and the child
						// child is an array (of 8 child) that may be null
#pragma omp task firstprivate(octreeIterator) shared(idxLevel)
						{
							kernels[omp_get_thread_num()]->M2M( octreeIterator.getCurrentCell() , octreeIterator.getCurrentChild(), idxLevel);
						}
					} while(octreeIterator.moveRight());

					avoidGotoLeftIterator.moveUp();
					octreeIterator = avoidGotoLeftIterator;// equal octreeIterator.moveUp(); octreeIterator.gotoLeft();

#pragma omp taskwait
					FLOG( FLog::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
				}
			}
		}


		FLOG( FLog::Controller << "\tFinished (@Upward Pass (M2M) = "  << counterTime.tacAndElapsed() << "s)\n" );
	}

	/////////////////////////////////////////////////////////////////////////////
	// Transfer
	/////////////////////////////////////////////////////////////////////////////

	/** M2L  */
	void transferPass(){
211 212 213 214
      #ifdef SCALFMM_USE_EZTRACE
	  
	  eztrace_start();
#endif
215
		this->transferPassWithFinalize() ;
216 217 218 219
#ifdef SCALFMM_USE_EZTRACE
	  eztrace_stop();
#endif
	    }
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 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 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435

	void transferPassWithOutFinalize(){
		FLOG( FLog::Controller.write("\tStart Downward Pass (M2L)\n").write(FLog::Flush); );
		FLOG(FTic counterTime);

#pragma omp parallel
		{
#pragma omp single nowait
			{
				const CellClass* neighbors[343];
				typename OctreeClass::Iterator octreeIterator(tree);
				// Goto the right level
				octreeIterator.moveDown();
				for(int idxLevel = 2 ; idxLevel < FAbstractAlgorithm::upperWorkingLevel ; ++idxLevel){
					octreeIterator.moveDown();
				}
				////////////////////////////////////////////////////////////////
				typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);
				//
				// for each levels
				for(int idxLevel = FAbstractAlgorithm::upperWorkingLevel ; idxLevel < FAbstractAlgorithm::lowerWorkingLevel ; ++idxLevel ){
					FLOG(FTic counterTimeLevel);
					const int separationCriteria = (idxLevel != FAbstractAlgorithm::lowerWorkingLevel-1 ? 1 : leafLevelSeperationCriteria);
					// for each cell we apply the M2L with all cells in the implicit interaction list
					do{
#pragma omp task firstprivate(octreeIterator) private(neighbors) shared(idxLevel)
						{
							const int counter = tree->getInteractionNeighbors(neighbors, octreeIterator.getCurrentGlobalCoordinate(), idxLevel, separationCriteria);
							if(counter){
								kernels[omp_get_thread_num()]->M2L( octreeIterator.getCurrentCell() , neighbors, counter, idxLevel);
							}
						}

					} while(octreeIterator.moveRight());
					////////////////////////////////////////////////////////////////
					// move up  and goto left
					avoidGotoLeftIterator.moveDown();
					octreeIterator = avoidGotoLeftIterator;

					FLOG( FLog::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
				}
			}
		}  // end parallel region
		//
		FLOG( FLog::Controller << "\tFinished (@Downward Pass (M2L) = "  << counterTime.tacAndElapsed() << "s)\n" );

	}
	void transferPassWithFinalize(){

		FLOG( FLog::Controller.write("\tStart Downward Pass (M2L)\n").write(FLog::Flush); );
		FLOG(FTic counterTime);
#pragma omp parallel
		{
#pragma omp single nowait
			{
				const CellClass* neighbors[343]; // to move in the // section

				typename OctreeClass::Iterator octreeIterator(tree);
				octreeIterator.moveDown();

				for(int idxLevel = 2 ; idxLevel < FAbstractAlgorithm::upperWorkingLevel ; ++idxLevel){
					octreeIterator.moveDown();
				}

				typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);

				// for each levels
				for(int idxLevel = FAbstractAlgorithm::upperWorkingLevel ; idxLevel < FAbstractAlgorithm::lowerWorkingLevel ; ++idxLevel ){
					FLOG(FTic counterTimeLevel);
					const int separationCriteria = (idxLevel != FAbstractAlgorithm::lowerWorkingLevel-1 ? 1 : leafLevelSeperationCriteria);
					// for each cells
					do{
//#pragma omp task default(none) firstprivate(octreeIterator,separationCriteria)  private( neighbors) shared(idxLevel)
//					{
							const int counter = tree->getInteractionNeighbors(neighbors, octreeIterator.getCurrentGlobalCoordinate(), idxLevel, separationCriteria);
						if(counter){
#pragma omp task firstprivate(octreeIterator, neighbors, counter) shared(idxLevel)
							{
								kernels[omp_get_thread_num()]->M2L( octreeIterator.getCurrentCell() , neighbors, counter, idxLevel);
							}
						}
					//code here to update the chunk

					} while(octreeIterator.moveRight());

					avoidGotoLeftIterator.moveDown();
					octreeIterator = avoidGotoLeftIterator;

#pragma omp taskwait

					for( int idxThread = 0 ; idxThread < omp_get_num_threads() ; ++idxThread){
#pragma omp task
						{
							kernels[idxThread]->finishedLevelM2L(idxLevel);
						}
					}
#pragma omp taskwait
					FLOG( FLog::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
				}
			} // end single region
		} // end // region
		FLOG( FLog::Controller << "\tFinished (@Downward Pass (M2L) = "  << counterTime.tacAndElapsed() << "s)\n" );
	}

	/////////////////////////////////////////////////////////////////////////////
	// Downward
	/////////////////////////////////////////////////////////////////////////////

	void downardPass(){ // second L2L
		FLOG( FLog::Controller.write("\tStart Downward Pass (L2L)\n").write(FLog::Flush); );
		FLOG(FTic counterTime);

#pragma omp parallel
		{
#pragma omp single nowait
			{
				typename OctreeClass::Iterator octreeIterator(tree);
				octreeIterator.moveDown();

				for(int idxLevel = 2 ; idxLevel < FAbstractAlgorithm::upperWorkingLevel ; ++idxLevel){
					octreeIterator.moveDown();
				}

				typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);

				const int heightMinusOne = FAbstractAlgorithm::lowerWorkingLevel - 1;
				// for each levels exepted leaf level
				for(int idxLevel = FAbstractAlgorithm::upperWorkingLevel ; idxLevel < heightMinusOne ; ++idxLevel ){
					FLOG(FTic counterTimeLevel);
					// for each cells
					do{
#pragma omp task firstprivate(octreeIterator) shared(idxLevel)
						{
							kernels[omp_get_thread_num()]->L2L( octreeIterator.getCurrentCell() , octreeIterator.getCurrentChild(), idxLevel);
						}

					} while(octreeIterator.moveRight());

					avoidGotoLeftIterator.moveDown();
					octreeIterator = avoidGotoLeftIterator;

#pragma omp taskwait
					FLOG( FLog::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
				}
			}
		}

		FLOG( FLog::Controller << "\tFinished (@Downward Pass (L2L) = "  << counterTime.tacAndElapsed() << "s)\n" );
	}


	/////////////////////////////////////////////////////////////////////////////
	// Direct
	/////////////////////////////////////////////////////////////////////////////

	/** P2P */
	void directPass(const bool p2pEnabled, const bool l2pEnabled){
		FLOG( FLog::Controller.write("\tStart Direct Pass\n").write(FLog::Flush); );
		FLOG(FTic counterTime);
		FLOG(FTic computationCounter);

		const int heightMinusOne = OctreeHeight - 1;

#pragma omp parallel
		{

#pragma omp single nowait
			{
				// There is a maximum of 26 neighbors
				ContainerClass* neighbors[27];

				const int SizeShape = 3*3*3;
				FVector<typename OctreeClass::Iterator> shapes[SizeShape];

				typename OctreeClass::Iterator octreeIterator(tree);
				octreeIterator.gotoBottomLeft();

				// for each leafs
				do{
					const FTreeCoordinate& coord = octreeIterator.getCurrentGlobalCoordinate();
					const int shapePosition = (coord.getX()%3)*9 + (coord.getY()%3)*3 + (coord.getZ()%3);

					shapes[shapePosition].push(octreeIterator);

				} while(octreeIterator.moveRight());

				FLOG( computationCounter.tic() );

				for( int idxShape = 0 ; idxShape < SizeShape ; ++idxShape){
					const FSize nbLeaf = (shapes[idxShape].getSize());
					for(FSize iterLeaf = 0 ; iterLeaf < nbLeaf ; ++iterLeaf ){
						typename OctreeClass::Iterator toWork = shapes[idxShape][iterLeaf];
#pragma omp task firstprivate(neighbors, toWork, l2pEnabled, p2pEnabled)
						{
							if(l2pEnabled){
								kernels[omp_get_thread_num()]->L2P(toWork.getCurrentCell(), toWork.getCurrentListTargets());
							}
							if(p2pEnabled){
								const int counter = tree->getLeafsNeighbors(neighbors, toWork.getCurrentGlobalCoordinate(),heightMinusOne);
								kernels[omp_get_thread_num()]->P2P(toWork.getCurrentGlobalCoordinate(), toWork.getCurrentListTargets(),
										toWork.getCurrentListSrc(), neighbors, counter);
							}
						}
					}

#pragma omp taskwait
				}

				FLOG( computationCounter.tac() );
			}
		}


		FLOG( FLog::Controller << "\tFinished (@Direct Pass (L2P + P2P) = "  << counterTime.tacAndElapsed() << "s)\n" );
		FLOG( FLog::Controller << "\t\t Computation L2P + P2P : " << computationCounter.cumulated() << " s\n" );
	}
436 437 438 439 440 441

};


#endif //FFMMALGORITHMTASK_HPP

442