FFmmAlgorithmThread.hpp 15.5 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 34 35 36 37 38 39 40 41
*/
template<template< class ParticleClass, class CellClass, int OctreeHeight> class KernelClass,
        class ParticleClass, class CellClass,
        template<class ParticleClass> class LeafClass,
        int OctreeHeight, int SubtreeHeight>
class FFmmAlgorithmThread : protected FAssertable{
    // To reduce the size of variable type based on foctree in this file
    typedef FOctree<ParticleClass, CellClass, LeafClass, OctreeHeight, SubtreeHeight> Octree;
    typedef typename FOctree<ParticleClass, CellClass,LeafClass, OctreeHeight, SubtreeHeight>::Iterator OctreeIterator;
    typedef KernelClass<ParticleClass, CellClass, OctreeHeight> Kernel;

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

    OctreeIterator* iterArray;
45
    int leafsNumber;
46 47

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

    const int MaxThreads;
51 52 53 54 55 56 57 58

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)
59 60
                      : tree(inTree) , kernels(0), iterArray(0), leafsNumber(0),
                        MaxThreads(omp_get_max_threads()) {
61 62 63

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

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

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

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

    /**
      * 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;
        }
        const int LeafIndex = OctreeHeight - 1;

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

        } while(octreeIterator.moveRight());
104
        iterArray = new OctreeIterator[leafsNumber];
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        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
120 121 122 123
    /////////////////////////////////////////////////////////////////////////////
    // P2M
    /////////////////////////////////////////////////////////////////////////////

124 125 126 127
    /** 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
128
        FDEBUG(FTic counterTime);
129 130 131 132 133 134 135 136 137 138

        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
139 140
        FDEBUG(FTic computationCounter);
        #pragma omp parallel
141 142
        {
            Kernel * const myThreadkernels = kernels[omp_get_thread_num()];
berenger-bramas's avatar
berenger-bramas committed
143
            #pragma omp for nowait
144 145 146 147 148 149
            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
150
        FDEBUG(computationCounter.tac() );
151

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

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

161 162 163 164
    /** 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
165 166
        FDEBUG(FTic counterTime);
        FDEBUG(FTic computationCounter);
167 168 169 170 171 172 173 174 175

        // 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 ){
176
            int numberOfCells = 0;
177 178
            // for each cells
            do{
179 180
                iterArray[numberOfCells] = octreeIterator;
                ++numberOfCells;
181 182 183 184 185
            } while(octreeIterator.moveRight());
            avoidGotoLeftIterator.moveUp();
            octreeIterator = avoidGotoLeftIterator;// equal octreeIterator.moveUp(); octreeIterator.gotoLeft();

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

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

berenger-bramas's avatar
berenger-bramas committed
200

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

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

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

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

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

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

                FDEBUG(computationCounter.tic());
berenger-bramas's avatar
berenger-bramas committed
235
                #pragma omp parallel
236 237
                {
                    Kernel * const myThreadkernels = kernels[omp_get_thread_num()];
238
                    const CellClass* neighbors[208];
239
                    #pragma omp for
240 241 242
                    for(int idxCell = 0 ; idxCell < numberOfCells ; ++idxCell){
                        const int counter = tree->getDistantNeighbors(neighbors,  iterArray[idxCell].getCurrentGlobalIndex(),idxLevel);
                        if(counter) myThreadkernels->M2L(  iterArray[idxCell].getCurrentCell() , neighbors, counter, idxLevel);
243 244 245 246
                    }
                }
                FDEBUG(computationCounter.tac());
            }
berenger-bramas's avatar
berenger-bramas committed
247

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

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

257 258 259 260 261 262 263 264
            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 ){
265
                int numberOfCells = 0;
266 267
                // for each cells
                do{
268 269
                    iterArray[numberOfCells] = octreeIterator;
                    ++numberOfCells;
270 271 272 273 274
                } while(octreeIterator.moveRight());
                avoidGotoLeftIterator.moveDown();
                octreeIterator = avoidGotoLeftIterator;

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

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

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

berenger-bramas's avatar
berenger-bramas committed
293 294 295 296
    /////////////////////////////////////////////////////////////////////////////
    // Direct
    /////////////////////////////////////////////////////////////////////////////

297 298 299 300
    /** 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
301 302
        FDEBUG(FTic counterTime);
        FDEBUG(FTic computationCounter);
303
        FDEBUG(FTic computationCounterP2P);
304 305 306 307 308 309

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

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

318
        const int LeafIndex = OctreeHeight - 1;
319
        int leafs = 0;
320
        {
321 322 323 324 325 326
            int startPosAtShape[SizeShape];
            startPosAtShape[0] = 0;
            for(int idxShape = 1 ; idxShape < SizeShape ; ++idxShape){
                startPosAtShape[idxShape] = startPosAtShape[idxShape-1] + this->shapeLeaf[idxShape-1];
            }

327 328 329 330 331
            OctreeIterator octreeIterator(tree);
            octreeIterator.gotoBottomLeft();
            // for each leafs
            do{
                //iterArray[leafs] = octreeIterator;
332
                ++leafs;
333 334 335 336
                const MortonIndex index = octreeIterator.getCurrentGlobalIndex();
                FTreeCoordinate coord;
                coord.setPositionFromMorton(index, LeafIndex);
                const int shapePosition = (coord.getX()%3)*9 + (coord.getY()%3)*3 + (coord.getZ()%3);
337 338 339 340 341 342 343

                leafsDataArray[startPosAtShape[shapePosition]].index = octreeIterator.getCurrentGlobalIndex();
                leafsDataArray[startPosAtShape[shapePosition]].cell = octreeIterator.getCurrentCell();
                leafsDataArray[startPosAtShape[shapePosition]].targets = octreeIterator.getCurrentListTargets();
                leafsDataArray[startPosAtShape[shapePosition]].sources = octreeIterator.getCurrentListSrc();

                ++startPosAtShape[shapePosition];
344 345 346 347 348

            } while(octreeIterator.moveRight());
        }

        FDEBUG(computationCounter.tic());
349

350 351
        #pragma omp parallel
        {
352
            Kernel& myThreadkernels = (*kernels[omp_get_thread_num()]);
353
            // There is a maximum of 26 neighbors
354
            FList<ParticleClass>* neighbors[26];
berenger-bramas's avatar
berenger-bramas committed
355
            MortonIndex neighborsIndex[26];
356
            int previous = 0;
357 358

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

                #pragma omp for
362 363 364
                for(int idxLeafs = previous ; idxLeafs < endAtThisShape ; ++idxLeafs){
                    LeafData& currentIter = leafsDataArray[idxLeafs];
                    myThreadkernels.L2P(currentIter.cell, currentIter.targets);
365
                    // need the current particles and neighbors particles
366 367 368 369
                    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());
370
                }
371 372

                previous = endAtThisShape;
373 374 375 376
            }
        }
        FDEBUG(computationCounter.tac());

377
        delete [] leafsDataArray;
378

379 380
        FDEBUG( FDebug::Controller << "\tFinished (@Direct Pass (L2P + P2P) = "  << counterTime.tacAndElapsed() << "s)\n" );
        FDEBUG( FDebug::Controller << "\t\t Computation L2P + P2P : " << computationCounter.cumulated() << " s\n" );
381
        FDEBUG( FDebug::Controller << "\t\t Computation P2P : " << computationCounterP2P.cumulated() << " s\n" );
382 383 384 385 386 387 388 389 390
        FTRACE( FTrace::Controller.leaveFunction(FTrace::FMM) );
    }

};


#endif //FFMMALGORITHMTHREAD_HPP

// [--LICENSE--]