FFmmAlgorithmTask.hpp 14.8 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 47 48 49 50 51

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

    const int MaxThreads;

    const int OctreeHeight;

52
    const int leafLevelSeperationCriteria;
BRAMAS Berenger's avatar
BRAMAS Berenger committed
53
public:
54 55 56 57 58
    /** 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
      */
59
    FFmmAlgorithmTask(OctreeClass* const inTree, KernelClass* const inKernels, const int inLeafLevelSeperationCriteria = 1)
60
        : tree(inTree) , kernels(nullptr),
61
          MaxThreads(omp_get_max_threads()), OctreeHeight(tree->getHeight()), leafLevelSeperationCriteria(inLeafLevelSeperationCriteria)
BRAMAS Berenger's avatar
BRAMAS Berenger committed
62
    {
63

BRAMAS Berenger's avatar
BRAMAS Berenger committed
64 65
        FAssertLF(tree, "tree cannot be null");
        FAssertLF(inKernels, "kernels cannot be null");
66 67

        this->kernels = new KernelClass*[MaxThreads];
68
        #pragma omp parallel for schedule(static)
69
        for(int idxThread = 0 ; idxThread < MaxThreads ; ++idxThread){
70
            #pragma omp critical (InitFFmmAlgorithmTask)
71 72 73
            {
                this->kernels[idxThread] = new KernelClass(*inKernels);
            }
74 75
        }

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

BRAMAS Berenger's avatar
BRAMAS Berenger committed
78
        FLOG(FLog::Controller << "FFmmAlgorithmTask (Max Thread " << omp_get_max_threads() << ")\n");
79 80 81 82 83 84 85 86 87 88
    }

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

89
protected:
90 91 92 93
    /**
      * To execute the fmm algorithm
      * Call this function to run the complete algorithm
      */
94
    void executeCore(const unsigned operationsToProceed) override {
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
        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();
120 121 122 123 124 125 126 127
    }

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

    /** P2M */
    void bottomPass(){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
128
        FLOG( FLog::Controller.write("\tStart Bottom Pass\n").write(FLog::Flush) );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
129
        FLOG(FTic counterTime);
130

BRAMAS Berenger's avatar
BRAMAS Berenger committed
131
        #pragma omp parallel
132
        {
BRAMAS Berenger's avatar
BRAMAS Berenger committed
133
            #pragma omp single nowait
134 135 136 137 138 139 140 141
            {
                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
142
                    #pragma omp task firstprivate(octreeIterator)
143
                    {
BRAMAS Berenger's avatar
BRAMAS Berenger committed
144
                        kernels[omp_get_thread_num()]->P2M( octreeIterator.getCurrentCell() , octreeIterator.getCurrentListSrc());
145 146 147
                    }
                } while(octreeIterator.moveRight());

BRAMAS Berenger's avatar
BRAMAS Berenger committed
148
                #pragma omp taskwait
149 150 151
            }
        }

BRAMAS Berenger's avatar
BRAMAS Berenger committed
152
        FLOG( FLog::Controller << "\tFinished (@Bottom Pass (P2M) = "  << counterTime.tacAndElapsed() << "s)\n" );
153 154 155 156 157 158 159 160
    }

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

    /** M2M */
    void upwardPass(){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
161
        FLOG( FLog::Controller.write("\tStart Upward Pass\n").write(FLog::Flush); );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
162
        FLOG(FTic counterTime);
163

BRAMAS Berenger's avatar
BRAMAS Berenger committed
164
        #pragma omp parallel
165
        {
BRAMAS Berenger's avatar
BRAMAS Berenger committed
166
            #pragma omp single nowait
167 168 169 170 171 172
            {
                // Start from leal level - 1
                typename OctreeClass::Iterator octreeIterator(tree);
                octreeIterator.gotoBottomLeft();
                octreeIterator.moveUp();

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

177 178 179
                typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);

                // for each levels
180
                for(int idxLevel = FMath::Min(OctreeHeight - 2, FAbstractAlgorithm::lowerWorkingLevel - 1) ; idxLevel >= FAbstractAlgorithm::upperWorkingLevel ; --idxLevel ){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
181
                    FLOG(FTic counterTimeLevel);
182 183 184 185
                    // for each cells
                    do{
                        // We need the current cell and the child
                        // child is an array (of 8 child) that may be null
186
                        #pragma omp task firstprivate(octreeIterator) shared(idxLevel)
187
                        {
BRAMAS Berenger's avatar
BRAMAS Berenger committed
188
                            kernels[omp_get_thread_num()]->M2M( octreeIterator.getCurrentCell() , octreeIterator.getCurrentChild(), idxLevel);
189 190 191 192 193 194
                        }
                    } while(octreeIterator.moveRight());

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

BRAMAS Berenger's avatar
BRAMAS Berenger committed
195
                    #pragma omp taskwait
BRAMAS Berenger's avatar
BRAMAS Berenger committed
196
                    FLOG( FLog::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
197 198 199 200 201
                }
            }
        }


BRAMAS Berenger's avatar
BRAMAS Berenger committed
202
        FLOG( FLog::Controller << "\tFinished (@Upward Pass (M2M) = "  << counterTime.tacAndElapsed() << "s)\n" );
203 204 205
    }

    /////////////////////////////////////////////////////////////////////////////
206
    // Transfer
207 208 209
    /////////////////////////////////////////////////////////////////////////////

    /** M2L L2L */
210
    void transferPass(){
211

BRAMAS Berenger's avatar
BRAMAS Berenger committed
212
        FLOG( FLog::Controller.write("\tStart Downward Pass (M2L)\n").write(FLog::Flush); );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
213
        FLOG(FTic counterTime);
214
        #pragma omp parallel
215
        {
216
            #pragma omp single nowait
217
            {
218 219
                const CellClass* neighbors[343];

220 221 222
                typename OctreeClass::Iterator octreeIterator(tree);
                octreeIterator.moveDown();

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

227
                typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);
228 229

                // for each levels
230
                for(int idxLevel = FAbstractAlgorithm::upperWorkingLevel ; idxLevel < FAbstractAlgorithm::lowerWorkingLevel ; ++idxLevel ){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
231
                    FLOG(FTic counterTimeLevel);
232
                    const int separationCriteria = (idxLevel != FAbstractAlgorithm::lowerWorkingLevel-1 ? 1 : leafLevelSeperationCriteria);
233 234
                    // for each cells
                    do{
235
                        const int counter = tree->getInteractionNeighbors(neighbors, octreeIterator.getCurrentGlobalCoordinate(), idxLevel, separationCriteria);
236
                        if(counter){
237
                            #pragma omp task firstprivate(octreeIterator, neighbors, counter) shared(idxLevel)
238
                            {
BRAMAS Berenger's avatar
BRAMAS Berenger committed
239
                                kernels[omp_get_thread_num()]->M2L( octreeIterator.getCurrentCell() , neighbors, counter, idxLevel);
240
                            }
241
                        }
242

243
                    } while(octreeIterator.moveRight());
244

245 246
                    avoidGotoLeftIterator.moveDown();
                    octreeIterator = avoidGotoLeftIterator;
247

248 249 250 251 252 253 254 255 256 257
                    #pragma omp taskwait

                    for( int idxThread = 0 ; idxThread < omp_get_num_threads() ; ++idxThread){
                        #pragma omp task
                        {
                            kernels[idxThread]->finishedLevelM2L(idxLevel);
                        }
                    }

                    #pragma omp taskwait
BRAMAS Berenger's avatar
BRAMAS Berenger committed
258
                    FLOG( FLog::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
259 260 261
                }
            }
        }
BRAMAS Berenger's avatar
BRAMAS Berenger committed
262
        FLOG( FLog::Controller << "\tFinished (@Downward Pass (M2L) = "  << counterTime.tacAndElapsed() << "s)\n" );
263 264 265 266 267
    }

    /////////////////////////////////////////////////////////////////////////////
    // Downward
    /////////////////////////////////////////////////////////////////////////////
268

269
    void downardPass(){ // second L2L
BRAMAS Berenger's avatar
BRAMAS Berenger committed
270
        FLOG( FLog::Controller.write("\tStart Downward Pass (L2L)\n").write(FLog::Flush); );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
271
        FLOG(FTic counterTime);
272

BRAMAS Berenger's avatar
BRAMAS Berenger committed
273
        #pragma omp parallel
274
        {
BRAMAS Berenger's avatar
BRAMAS Berenger committed
275
            #pragma omp single nowait
276 277 278
            {
                typename OctreeClass::Iterator octreeIterator(tree);
                octreeIterator.moveDown();
279

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

284
                typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);
285

286
                const int heightMinusOne = FAbstractAlgorithm::lowerWorkingLevel - 1;
287
                // for each levels exepted leaf level
288
                for(int idxLevel = FAbstractAlgorithm::upperWorkingLevel ; idxLevel < heightMinusOne ; ++idxLevel ){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
289
                    FLOG(FTic counterTimeLevel);
290 291
                    // for each cells
                    do{
292
                        #pragma omp task firstprivate(octreeIterator) shared(idxLevel)
293
                        {
BRAMAS Berenger's avatar
BRAMAS Berenger committed
294
                            kernels[omp_get_thread_num()]->L2L( octreeIterator.getCurrentCell() , octreeIterator.getCurrentChild(), idxLevel);
295
                        }
296

297
                    } while(octreeIterator.moveRight());
298

299 300
                    avoidGotoLeftIterator.moveDown();
                    octreeIterator = avoidGotoLeftIterator;
301

BRAMAS Berenger's avatar
BRAMAS Berenger committed
302
                    #pragma omp taskwait
BRAMAS Berenger's avatar
BRAMAS Berenger committed
303
                    FLOG( FLog::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
304 305 306 307
                }
            }
        }

BRAMAS Berenger's avatar
BRAMAS Berenger committed
308
        FLOG( FLog::Controller << "\tFinished (@Downward Pass (L2L) = "  << counterTime.tacAndElapsed() << "s)\n" );
309 310
    }

311

312 313 314 315 316
    /////////////////////////////////////////////////////////////////////////////
    // Direct
    /////////////////////////////////////////////////////////////////////////////

    /** P2P */
317
    void directPass(const bool p2pEnabled, const bool l2pEnabled){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
318
        FLOG( FLog::Controller.write("\tStart Direct Pass\n").write(FLog::Flush); );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
319 320
        FLOG(FTic counterTime);
        FLOG(FTic computationCounter);
321 322 323

        const int heightMinusOne = OctreeHeight - 1;

berenger-bramas's avatar
berenger-bramas committed
324
        #pragma omp parallel
325 326
        {

berenger-bramas's avatar
berenger-bramas committed
327
            #pragma omp single nowait
328
            {
329 330 331
                // There is a maximum of 26 neighbors
                ContainerClass* neighbors[27];

332
                const int SizeShape = 3*3*3;
berenger-bramas's avatar
berenger-bramas committed
333
                FVector<typename OctreeClass::Iterator> shapes[SizeShape];
334 335 336 337 338 339 340 341 342

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

BRAMAS Berenger's avatar
BRAMAS Berenger committed
343
                    shapes[shapePosition].push(octreeIterator);
344 345 346

                } while(octreeIterator.moveRight());

BRAMAS Berenger's avatar
BRAMAS Berenger committed
347
                FLOG( computationCounter.tic() );
348

BRAMAS Berenger's avatar
BRAMAS Berenger committed
349
                for( int idxShape = 0 ; idxShape < SizeShape ; ++idxShape){
350 351
                    const FSize nbLeaf = (shapes[idxShape].getSize());
                    for(FSize iterLeaf = 0 ; iterLeaf < nbLeaf ; ++iterLeaf ){
berenger-bramas's avatar
berenger-bramas committed
352
                        typename OctreeClass::Iterator toWork = shapes[idxShape][iterLeaf];
353
                        #pragma omp task firstprivate(neighbors, toWork, l2pEnabled, p2pEnabled)
354
                        {
355 356 357 358 359 360
                            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(),
361
                                                 toWork.getCurrentListSrc(), neighbors, counter);
362
                            }
363 364 365
                        }
                    }

berenger-bramas's avatar
berenger-bramas committed
366
                    #pragma omp taskwait
367
                }
BRAMAS Berenger's avatar
BRAMAS Berenger committed
368

BRAMAS Berenger's avatar
BRAMAS Berenger committed
369
                FLOG( computationCounter.tac() );
370 371 372 373
            }
        }


BRAMAS Berenger's avatar
BRAMAS Berenger committed
374 375
        FLOG( FLog::Controller << "\tFinished (@Direct Pass (L2P + P2P) = "  << counterTime.tacAndElapsed() << "s)\n" );
        FLOG( FLog::Controller << "\t\t Computation L2P + P2P : " << computationCounter.cumulated() << " s\n" );
376 377 378 379 380 381 382
    }

};


#endif //FFMMALGORITHMTASK_HPP

383