FFmmAlgorithmThread.hpp 16.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#ifndef FFMMALGORITHMTHREAD_HPP
#define FFMMALGORITHMTHREAD_HPP
// /!\ Please, you must read the license at the bottom of this page

#include "../Utils/FAssertable.hpp"
#include "../Utils/FDebug.hpp"
#include "../Utils/FTrace.hpp"
#include "../Utils/FTic.hpp"
#include "../Utils/FGlobal.hpp"

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


#include <omp.h>

/**
* @author Berenger Bramas (berenger.bramas@inria.fr)
* @class FFmmAlgorithmThread
* @brief
* Please read the license
*
* This class is a threaded FMM algorithm
* It just iterates on a tree and call the kernels with good arguments.
* It used the inspector-executor model :
* iterates on the tree and builds an array to work in parallel on this array
*
* Of course this class does not deallocate pointer given in arguements.
*
berenger-bramas's avatar
berenger-bramas committed
29
* When using this algorithm the P2P is thread safe.
30
*/
31 32 33
template<template< class ParticleClass, class CellClass, template <class ParticleClass> class ContainerClass> class KernelClass,
        class ParticleClass, class CellClass,template <class ParticleClass> class ContainerClass,
        template<class ParticleClass, template <class ParticleClass> class ContainerClass> class LeafClass>
34 35
class FFmmAlgorithmThread : protected FAssertable{
    // To reduce the size of variable type based on foctree in this file
36 37 38
    typedef FOctree<ParticleClass, CellClass, ContainerClass, LeafClass> Octree;
    typedef typename FOctree<ParticleClass, CellClass,ContainerClass,LeafClass>::Iterator OctreeIterator;
    typedef KernelClass<ParticleClass, CellClass,ContainerClass> Kernel;
39 40

    Octree* const tree;                  //< The octree to work on
berenger-bramas's avatar
berenger-bramas committed
41
    Kernel** kernels;                    //< The kernels
42 43

    OctreeIterator* iterArray;
44
    int leafsNumber;
45 46

    static const int SizeShape = 3*3*3;
berenger-bramas's avatar
berenger-bramas committed
47 48 49
    int shapeLeaf[SizeShape];    

    const int MaxThreads;
50

51 52
    const int OctreeHeight;

53 54 55 56 57 58 59
public:
    /** 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
      */
    FFmmAlgorithmThread(Octree* const inTree, Kernel* const inKernels)
60
                      : tree(inTree) , kernels(0), iterArray(0), leafsNumber(0),
61
                        MaxThreads(omp_get_max_threads()), OctreeHeight(tree->getHeight()) {
62 63 64

        assert(tree, "tree cannot be null", __LINE__, __FILE__);

berenger-bramas's avatar
berenger-bramas committed
65 66
        this->kernels = new Kernel*[MaxThreads];
        for(int idxThread = 0 ; idxThread < MaxThreads ; ++idxThread){
67
            this->kernels[idxThread] = new Kernel(*inKernels);
68 69
        }

berenger-bramas's avatar
berenger-bramas committed
70
        FDEBUG(FDebug::Controller << "FFmmAlgorithmThread (Max Thread " << omp_get_max_threads() << ")\n");
71 72 73 74
    }

    /** Default destructor */
    virtual ~FFmmAlgorithmThread(){
berenger-bramas's avatar
berenger-bramas committed
75
        for(int idxThread = 0 ; idxThread < MaxThreads ; ++idxThread){
76 77
            delete this->kernels[idxThread];
        }
berenger-bramas's avatar
berenger-bramas committed
78
        delete [] this->kernels;
79 80 81 82 83 84 85 86 87 88 89 90
    }

    /**
      * To execute the fmm algorithm
      * Call this function to run the complete algorithm
      */
    void execute(){
        FTRACE( FTrace::Controller.enterFunction(FTrace::FMM, __FUNCTION__ , __FILE__ , __LINE__) );

        for(int idxShape = 0 ; idxShape < SizeShape ; ++idxShape){
            this->shapeLeaf[idxShape] = 0;
        }
91
        //const int LeafIndex = OctreeHeight - 1;
92 93

        // Count leaf
94
        leafsNumber = 0;
95 96 97
        OctreeIterator octreeIterator(tree);
        octreeIterator.gotoBottomLeft();
        do{
98
            ++leafsNumber;
99 100 101
            const FTreeCoordinate& coord = octreeIterator.getCurrentCell()->getCoordinate();
            //const MortonIndex index = octreeIterator.getCurrentGlobalIndex();
            //coord.setPositionFromMorton(index, LeafIndex);
102 103 104
            ++this->shapeLeaf[(coord.getX()%3)*9 + (coord.getY()%3)*3 + (coord.getZ()%3)];

        } while(octreeIterator.moveRight());
105
        iterArray = new OctreeIterator[leafsNumber];
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
        assert(iterArray, "iterArray bad alloc", __LINE__, __FILE__);

        bottomPass();
        upwardPass();

        downardPass();

        directPass();

        delete [] iterArray;
        iterArray = 0;

        FTRACE( FTrace::Controller.leaveFunction(FTrace::FMM) );
    }

berenger-bramas's avatar
berenger-bramas committed
121 122 123 124
    /////////////////////////////////////////////////////////////////////////////
    // P2M
    /////////////////////////////////////////////////////////////////////////////

125 126 127 128
    /** P2M */
    void bottomPass(){
        FTRACE( FTrace::Controller.enterFunction(FTrace::FMM, __FUNCTION__ , __FILE__ , __LINE__) );
        FDEBUG( FDebug::Controller.write("\tStart Bottom Pass\n").write(FDebug::Flush) );
berenger-bramas's avatar
berenger-bramas committed
129
        FDEBUG(FTic counterTime);
130 131 132 133 134 135 136 137 138 139

        OctreeIterator octreeIterator(tree);
        int leafs = 0;
        // Iterate on leafs
        octreeIterator.gotoBottomLeft();
        do{
            iterArray[leafs] = octreeIterator;
            ++leafs;
        } while(octreeIterator.moveRight());

berenger-bramas's avatar
berenger-bramas committed
140 141
        FDEBUG(FTic computationCounter);
        #pragma omp parallel
142 143
        {
            Kernel * const myThreadkernels = kernels[omp_get_thread_num()];
berenger-bramas's avatar
berenger-bramas committed
144
            #pragma omp for nowait
145 146 147 148 149 150
            for(int idxLeafs = 0 ; idxLeafs < leafs ; ++idxLeafs){
                // We need the current cell that represent the leaf
                // and the list of particles
                myThreadkernels->P2M( iterArray[idxLeafs].getCurrentCell() , iterArray[idxLeafs].getCurrentListSrc());
            }
        }
berenger-bramas's avatar
berenger-bramas committed
151
        FDEBUG(computationCounter.tac() );
152

153
        FDEBUG( FDebug::Controller << "\tFinished (@Bottom Pass (P2M) = "  << counterTime.tacAndElapsed() << "s)\n" );
154 155 156 157
        FDEBUG( FDebug::Controller << "\t\t Computation : " << computationCounter.elapsed() << " s\n" );
        FTRACE( FTrace::Controller.leaveFunction(FTrace::FMM) );
    }

berenger-bramas's avatar
berenger-bramas committed
158 159 160 161
    /////////////////////////////////////////////////////////////////////////////
    // Upward
    /////////////////////////////////////////////////////////////////////////////

162 163 164 165
    /** M2M */
    void upwardPass(){
        FTRACE( FTrace::Controller.enterFunction(FTrace::FMM, __FUNCTION__ , __FILE__ , __LINE__) );
        FDEBUG( FDebug::Controller.write("\tStart Upward Pass\n").write(FDebug::Flush); );
berenger-bramas's avatar
berenger-bramas committed
166 167
        FDEBUG(FTic counterTime);
        FDEBUG(FTic computationCounter);
168 169 170 171 172 173 174 175 176

        // Start from leal level - 1
        OctreeIterator octreeIterator(tree);
        octreeIterator.gotoBottomLeft();
        octreeIterator.moveUp();
        OctreeIterator avoidGotoLeftIterator(octreeIterator);

        // for each levels
        for(int idxLevel = OctreeHeight - 2 ; idxLevel > 1 ; --idxLevel ){
177
            int numberOfCells = 0;
178 179
            // for each cells
            do{
180 181
                iterArray[numberOfCells] = octreeIterator;
                ++numberOfCells;
182 183 184 185 186
            } while(octreeIterator.moveRight());
            avoidGotoLeftIterator.moveUp();
            octreeIterator = avoidGotoLeftIterator;// equal octreeIterator.moveUp(); octreeIterator.gotoLeft();

            FDEBUG(computationCounter.tic());
berenger-bramas's avatar
berenger-bramas committed
187
            #pragma omp parallel
188 189
            {
                Kernel * const myThreadkernels = kernels[omp_get_thread_num()];
190
                #pragma omp for nowait
191
                for(int idxCell = 0 ; idxCell < numberOfCells ; ++idxCell){
192 193
                    // We need the current cell and the child
                    // child is an array (of 8 child) that may be null
194
                    myThreadkernels->M2M( iterArray[idxCell].getCurrentCell() , iterArray[idxCell].getCurrentChild(), idxLevel);
195 196
                }
            }
berenger-bramas's avatar
berenger-bramas committed
197

198 199 200
            FDEBUG(computationCounter.tac());
        }

berenger-bramas's avatar
berenger-bramas committed
201

202
        FDEBUG( FDebug::Controller << "\tFinished (@Upward Pass (M2M) = "  << counterTime.tacAndElapsed() << "s)\n" );
berenger-bramas's avatar
berenger-bramas committed
203
        FDEBUG( FDebug::Controller << "\t\t Computation : " << computationCounter.cumulated() << " s\n" );
204 205 206
        FTRACE( FTrace::Controller.leaveFunction(FTrace::FMM) );
    }

berenger-bramas's avatar
berenger-bramas committed
207 208 209 210
    /////////////////////////////////////////////////////////////////////////////
    // Downard
    /////////////////////////////////////////////////////////////////////////////

211 212 213 214 215
    /** M2L L2L */
    void downardPass(){
        FTRACE( FTrace::Controller.enterFunction(FTrace::FMM, __FUNCTION__ , __FILE__ , __LINE__) );

        { // first M2L
berenger-bramas's avatar
berenger-bramas committed
216 217 218 219
            FDEBUG( FDebug::Controller.write("\tStart Downward Pass (M2L)\n").write(FDebug::Flush); );
            FDEBUG(FTic counterTime);
            FDEBUG(FTic computationCounter);

220 221 222 223 224 225
            OctreeIterator octreeIterator(tree);
            octreeIterator.moveDown();
            OctreeIterator avoidGotoLeftIterator(octreeIterator);

            // for each levels
            for(int idxLevel = 2 ; idxLevel < OctreeHeight ; ++idxLevel ){
226
                int numberOfCells = 0;
227 228
                // for each cells
                do{
229 230
                    iterArray[numberOfCells] = octreeIterator;
                    ++numberOfCells;
231 232 233 234 235
                } while(octreeIterator.moveRight());
                avoidGotoLeftIterator.moveDown();
                octreeIterator = avoidGotoLeftIterator;

                FDEBUG(computationCounter.tic());
berenger-bramas's avatar
berenger-bramas committed
236
                #pragma omp parallel
237 238
                {
                    Kernel * const myThreadkernels = kernels[omp_get_thread_num()];
239
                    const CellClass* neighbors[208];
240

241
                    #pragma omp for nowait
242
                    for(int idxCell = 0 ; idxCell < numberOfCells ; ++idxCell){
243 244
                        const int counter = tree->getDistantNeighbors(neighbors,  iterArray[idxCell].getCurrentGlobalCoordinate(),idxLevel);
                        if(counter) myThreadkernels->M2L( iterArray[idxCell].getCurrentCell() , neighbors, counter, idxLevel);
245 246 247 248
                    }
                }
                FDEBUG(computationCounter.tac());
            }
berenger-bramas's avatar
berenger-bramas committed
249

250
            FDEBUG( FDebug::Controller << "\tFinished (@Downward Pass (M2L) = "  << counterTime.tacAndElapsed() << "s)\n" );
berenger-bramas's avatar
berenger-bramas committed
251
            FDEBUG( FDebug::Controller << "\t\t Computation : " << computationCounter.cumulated() << " s\n" );
252 253 254
        }

        { // second L2L
berenger-bramas's avatar
berenger-bramas committed
255 256 257 258
            FDEBUG( FDebug::Controller.write("\tStart Downward Pass (L2L)\n").write(FDebug::Flush); );
            FDEBUG(FTic counterTime);
            FDEBUG(FTic computationCounter);

259 260 261 262 263 264 265 266
            OctreeIterator octreeIterator(tree);
            octreeIterator.moveDown();

            OctreeIterator avoidGotoLeftIterator(octreeIterator);

            const int heightMinusOne = OctreeHeight - 1;
            // for each levels exepted leaf level
            for(int idxLevel = 2 ; idxLevel < heightMinusOne ; ++idxLevel ){
267
                int numberOfCells = 0;
268 269
                // for each cells
                do{
270 271
                    iterArray[numberOfCells] = octreeIterator;
                    ++numberOfCells;
272 273 274 275 276
                } while(octreeIterator.moveRight());
                avoidGotoLeftIterator.moveDown();
                octreeIterator = avoidGotoLeftIterator;

                FDEBUG(computationCounter.tic());
berenger-bramas's avatar
berenger-bramas committed
277
                #pragma omp parallel
278 279
                {
                    Kernel * const myThreadkernels = kernels[omp_get_thread_num()];
280
                    #pragma omp for nowait
281 282
                    for(int idxCell = 0 ; idxCell < numberOfCells ; ++idxCell){
                        myThreadkernels->L2L( iterArray[idxCell].getCurrentCell() , iterArray[idxCell].getCurrentChild(), idxLevel);
283 284 285 286
                    }
                }
                FDEBUG(computationCounter.tac());
            }
berenger-bramas's avatar
berenger-bramas committed
287

288
            FDEBUG( FDebug::Controller << "\tFinished (@Downward Pass (L2L) = "  << counterTime.tacAndElapsed() << "s)\n" );
berenger-bramas's avatar
berenger-bramas committed
289
            FDEBUG( FDebug::Controller << "\t\t Computation : " << computationCounter.cumulated() << " s\n" );
290 291 292 293 294
        }

        FTRACE( FTrace::Controller.leaveFunction(FTrace::FMM) );
    }

berenger-bramas's avatar
berenger-bramas committed
295 296 297 298
    /////////////////////////////////////////////////////////////////////////////
    // Direct
    /////////////////////////////////////////////////////////////////////////////

299 300 301 302
    /** P2P */
    void directPass(){
        FTRACE( FTrace::Controller.enterFunction(FTrace::FMM, __FUNCTION__ , __FILE__ , __LINE__) );
        FDEBUG( FDebug::Controller.write("\tStart Direct Pass\n").write(FDebug::Flush); );
berenger-bramas's avatar
berenger-bramas committed
303 304
        FDEBUG(FTic counterTime);
        FDEBUG(FTic computationCounter);
305
        FDEBUG(FTic computationCounterP2P);
306 307 308 309 310 311

        int countShape[SizeShape];
        for(int idxShape = 0 ; idxShape < SizeShape ; ++idxShape){
            countShape[idxShape] = 0;
        }

312 313 314
        struct LeafData{
            MortonIndex index;
            CellClass* cell;
315 316
            ContainerClass<ParticleClass>* targets;
            ContainerClass<ParticleClass>* sources;
317 318 319
        };
        LeafData* const leafsDataArray = new LeafData[this->leafsNumber];

320
        const int LeafIndex = OctreeHeight - 1;
321 322 323 324 325 326 327 328

        int startPosAtShape[SizeShape];
        startPosAtShape[0] = 0;
        for(int idxShape = 1 ; idxShape < SizeShape ; ++idxShape){
            startPosAtShape[idxShape] = startPosAtShape[idxShape-1] + this->shapeLeaf[idxShape-1];
        }

        #pragma omp parallel
329
        {
330 331 332 333 334

            const float step = (float(this->leafsNumber) / omp_get_num_threads());
            const int start = int(FMath::Ceil(step * omp_get_thread_num()));
            const int tempEnd = int(FMath::Ceil(step * (omp_get_thread_num()+1)));
            const int end = (tempEnd > this->leafsNumber ? this->leafsNumber : tempEnd);
335

336 337
            OctreeIterator octreeIterator(tree);
            octreeIterator.gotoBottomLeft();
338 339 340 341 342

            for(int idxPreLeaf = 0 ; idxPreLeaf < start ; ++idxPreLeaf){
                octreeIterator.moveRight();
            }

343
            // for each leafs
344
            for(int idxMyLeafs = start ; idxMyLeafs < end ; ++idxMyLeafs){
345
                //iterArray[leafs] = octreeIterator;
346
                //++leafs;
347
                const FTreeCoordinate& coord = octreeIterator.getCurrentGlobalCoordinate();
348
                const int shapePosition = (coord.getX()%3)*9 + (coord.getY()%3)*3 + (coord.getZ()%3);
349

350 351 352 353 354
                int positionToWork = 0;
                #pragma omp critical
                {
                    positionToWork = startPosAtShape[shapePosition]++;
                }
355

356 357 358 359
                leafsDataArray[positionToWork].index = octreeIterator.getCurrentGlobalIndex();
                leafsDataArray[positionToWork].cell = octreeIterator.getCurrentCell();
                leafsDataArray[positionToWork].targets = octreeIterator.getCurrentListTargets();
                leafsDataArray[positionToWork].sources = octreeIterator.getCurrentListSrc();
360

361 362
                octreeIterator.moveRight();
            }
363

364 365
            #pragma omp barrier

berenger-bramas's avatar
berenger-bramas committed
366
            FDEBUG(if(!omp_get_thread_num()) computationCounter.tic());
367 368

            Kernel& myThreadkernels = (*kernels[omp_get_thread_num()]);
369
            // There is a maximum of 26 neighbors
370
            ContainerClass<ParticleClass>* neighbors[26];
berenger-bramas's avatar
berenger-bramas committed
371
            MortonIndex neighborsIndex[26];
372
            int previous = 0;
373 374

            for(int idxShape = 0 ; idxShape < SizeShape ; ++idxShape){
375
                const int endAtThisShape = this->shapeLeaf[idxShape] + previous;
376

377
                #pragma omp for schedule(dynamic)
378 379 380
                for(int idxLeafs = previous ; idxLeafs < endAtThisShape ; ++idxLeafs){
                    LeafData& currentIter = leafsDataArray[idxLeafs];
                    myThreadkernels.L2P(currentIter.cell, currentIter.targets);
381
                    // need the current particles and neighbors particles
382 383 384 385
                    FDEBUG(if(!omp_get_thread_num()) computationCounterP2P.tic());
                    const int counter = tree->getLeafsNeighborsWithIndex(neighbors, neighborsIndex, currentIter.index, LeafIndex);
                    myThreadkernels.P2P(currentIter.index, currentIter.targets, currentIter.sources , neighbors, neighborsIndex, counter);
                    FDEBUG(if(!omp_get_thread_num()) computationCounterP2P.tac());
386
                }
387 388

                previous = endAtThisShape;
389 390
            }
        }
391

392 393
        FDEBUG(computationCounter.tac());

394
        delete [] leafsDataArray;
395

396 397
        FDEBUG( FDebug::Controller << "\tFinished (@Direct Pass (L2P + P2P) = "  << counterTime.tacAndElapsed() << "s)\n" );
        FDEBUG( FDebug::Controller << "\t\t Computation L2P + P2P : " << computationCounter.cumulated() << " s\n" );
398
        FDEBUG( FDebug::Controller << "\t\t Computation P2P : " << computationCounterP2P.cumulated() << " s\n" );
399 400 401 402 403 404 405 406 407
        FTRACE( FTrace::Controller.leaveFunction(FTrace::FMM) );
    }

};


#endif //FFMMALGORITHMTHREAD_HPP

// [--LICENSE--]