FFmmAlgorithmThread.hpp 14.4 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 45 46

    OctreeIterator* iterArray;

    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 53 54 55 56 57

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)
berenger-bramas's avatar
berenger-bramas committed
58
                      : tree(inTree) , kernels(0), iterArray(0), MaxThreads(omp_get_max_threads()) {
59 60 61

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

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

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

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

    /**
      * 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
        int leafs = 0;
        OctreeIterator octreeIterator(tree);
        octreeIterator.gotoBottomLeft();
        do{
            ++leafs;
            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());
        iterArray = new OctreeIterator[leafs];
        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
118 119 120 121
    /////////////////////////////////////////////////////////////////////////////
    // P2M
    /////////////////////////////////////////////////////////////////////////////

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

        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
137 138
        FDEBUG(FTic computationCounter);
        #pragma omp parallel
139 140 141 142 143 144 145 146 147
        {
            Kernel * const myThreadkernels = kernels[omp_get_thread_num()];
            #pragma omp for
            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
148
        FDEBUG(computationCounter.tac() );
149

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

berenger-bramas's avatar
berenger-bramas committed
155 156 157 158
    /////////////////////////////////////////////////////////////////////////////
    // Upward
    /////////////////////////////////////////////////////////////////////////////

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

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

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

berenger-bramas's avatar
berenger-bramas committed
197

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

berenger-bramas's avatar
berenger-bramas committed
203 204 205 206
    /////////////////////////////////////////////////////////////////////////////
    // Downard
    /////////////////////////////////////////////////////////////////////////////

207 208 209 210 211
    /** M2L L2L */
    void downardPass(){
        FTRACE( FTrace::Controller.enterFunction(FTrace::FMM, __FUNCTION__ , __FILE__ , __LINE__) );

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

216 217 218 219 220 221
            OctreeIterator octreeIterator(tree);
            octreeIterator.moveDown();
            OctreeIterator avoidGotoLeftIterator(octreeIterator);

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

                FDEBUG(computationCounter.tic());
berenger-bramas's avatar
berenger-bramas committed
232
                #pragma omp parallel
233 234 235 236
                {
                    Kernel * const myThreadkernels = kernels[omp_get_thread_num()];
                    CellClass* neighbors[208];
                    #pragma omp for
237 238 239
                    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);
240 241 242 243
                    }
                }
                FDEBUG(computationCounter.tac());
            }
berenger-bramas's avatar
berenger-bramas committed
244

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

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

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

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

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

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

berenger-bramas's avatar
berenger-bramas committed
290 291 292 293
    /////////////////////////////////////////////////////////////////////////////
    // Direct
    /////////////////////////////////////////////////////////////////////////////

294 295 296 297
    /** 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
298 299
        FDEBUG(FTic counterTime);
        FDEBUG(FTic computationCounter);
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

        OctreeIterator* shapeArray[SizeShape];
        int countShape[SizeShape];
        for(int idxShape = 0 ; idxShape < SizeShape ; ++idxShape){
            shapeArray[idxShape] = new OctreeIterator[this->shapeLeaf[idxShape]];
            countShape[idxShape] = 0;
        }

        const int LeafIndex = OctreeHeight - 1;
        //int leafs = 0;
        {
            OctreeIterator octreeIterator(tree);
            octreeIterator.gotoBottomLeft();
            // for each leafs
            do{
                //iterArray[leafs] = octreeIterator;
                //++leafs;
                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);
                shapeArray[shapePosition][countShape[shapePosition]] = octreeIterator;
                ++countShape[shapePosition];

            } while(octreeIterator.moveRight());
        }

        FDEBUG(computationCounter.tic());
        for(int idxShape = 0 ; idxShape < SizeShape ; ++idxShape){
            const int leafAtThisShape = this->shapeLeaf[idxShape];
berenger-bramas's avatar
berenger-bramas committed
330
            #pragma omp parallel
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
            {
                Kernel * const myThreadkernels = kernels[omp_get_thread_num()];
                // There is a maximum of 26 neighbors
                FList<ParticleClass*>* neighbors[26];

                #pragma omp for
                for(int idxLeafs = 0 ; idxLeafs < leafAtThisShape ; ++idxLeafs){
                    myThreadkernels->L2P(shapeArray[idxShape][idxLeafs].getCurrentCell(), shapeArray[idxShape][idxLeafs].getCurrentListTargets());
                    // need the current particles and neighbors particles
                    const int counter = tree->getLeafsNeighbors(neighbors, shapeArray[idxShape][idxLeafs].getCurrentGlobalIndex(),LeafIndex);
                    myThreadkernels->P2P( shapeArray[idxShape][idxLeafs].getCurrentListTargets(), shapeArray[idxShape][idxLeafs].getCurrentListSrc() , neighbors, counter);
                }
            }
        }
        FDEBUG(computationCounter.tac());

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

351
        FDEBUG( FDebug::Controller << "\tFinished (@Direct Pass (P2P) = "  << counterTime.tacAndElapsed() << "s)\n" );
berenger-bramas's avatar
berenger-bramas committed
352
        FDEBUG( FDebug::Controller << "\t\t Computation : " << computationCounter.cumulated() << " s\n" );
353 354 355 356 357 358 359 360 361
        FTRACE( FTrace::Controller.leaveFunction(FTrace::FMM) );
    }

};


#endif //FFMMALGORITHMTHREAD_HPP

// [--LICENSE--]