FFmmAlgorithmThread.hpp 15.6 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
template<template< class ParticleClass, class CellClass> class KernelClass,
32
        class ParticleClass, class CellClass,
33
        template<class ParticleClass> 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, LeafClass> Octree;
    typedef typename FOctree<ParticleClass, CellClass,LeafClass>::Iterator OctreeIterator;
    typedef KernelClass<ParticleClass, CellClass> 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 KernelClass<ParticleClass, CellClass>(*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 91 92 93
    }

    /**
      * 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
94
        leafsNumber = 0;
95 96 97
        OctreeIterator octreeIterator(tree);
        octreeIterator.gotoBottomLeft();
        do{
98
            ++leafsNumber;
99 100 101 102 103 104
            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());
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 190
            {
                Kernel * const myThreadkernels = kernels[omp_get_thread_num()];
                #pragma omp for
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 242
                    FTreeCoordinate currentPosition;
                    FTreeCoordinate neighborsPosition[208];

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

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

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

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

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

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

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

berenger-bramas's avatar
berenger-bramas committed
297 298 299 300
    /////////////////////////////////////////////////////////////////////////////
    // Direct
    /////////////////////////////////////////////////////////////////////////////

301 302 303 304
    /** 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
305 306
        FDEBUG(FTic counterTime);
        FDEBUG(FTic computationCounter);
307
        FDEBUG(FTic computationCounterP2P);
308 309 310 311 312 313

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

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

322
        const int LeafIndex = OctreeHeight - 1;
323
        int leafs = 0;
324
        {
325 326 327 328 329 330
            int startPosAtShape[SizeShape];
            startPosAtShape[0] = 0;
            for(int idxShape = 1 ; idxShape < SizeShape ; ++idxShape){
                startPosAtShape[idxShape] = startPosAtShape[idxShape-1] + this->shapeLeaf[idxShape-1];
            }

331 332 333 334 335
            OctreeIterator octreeIterator(tree);
            octreeIterator.gotoBottomLeft();
            // for each leafs
            do{
                //iterArray[leafs] = octreeIterator;
336
                ++leafs;
337 338 339 340
                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);
341 342 343 344 345 346 347

                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];
348 349 350 351 352

            } while(octreeIterator.moveRight());
        }

        FDEBUG(computationCounter.tic());
353

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

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

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

                previous = endAtThisShape;
377 378 379 380
            }
        }
        FDEBUG(computationCounter.tac());

381
        delete [] leafsDataArray;
382

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

};


#endif //FFMMALGORITHMTHREAD_HPP

// [--LICENSE--]