FFmmAlgorithm.hpp 9.55 KB
Newer Older
1 2
#ifndef FFMMALGORITHM_HPP
#define FFMMALGORITHM_HPP
3
// [--License--]
4

5
#include "../Utils/FGlobal.hpp"
6 7 8 9 10 11
#include "../Utils/FAssertable.hpp"
#include "../Utils/FDebug.hpp"
#include "../Utils/FTrace.hpp"
#include "../Utils/FTic.hpp"

#include "../Containers/FOctree.hpp"
12
#include "../Containers/FVector.hpp"
13 14 15 16 17 18 19 20 21 22 23 24 25


/**
* @author Berenger Bramas (berenger.bramas@inria.fr)
* @class FFmmAlgorithm
* @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.
*/
berenger-bramas's avatar
berenger-bramas committed
26
template<class OctreeClass, class ParticleClass, class CellClass, class ContainerClass, class KernelClass, class LeafClass>
27 28
class FFmmAlgorithm : protected FAssertable{

berenger-bramas's avatar
berenger-bramas committed
29 30
    OctreeClass* const tree;       //< The octree to work on
    KernelClass* const kernels;    //< The kernels
31 32

    const int OctreeHeight;
33 34 35 36 37 38 39

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
      */
berenger-bramas's avatar
berenger-bramas committed
40
    FFmmAlgorithm(OctreeClass* const inTree, KernelClass* const inKernels)
41
                      : tree(inTree) , kernels(inKernels), OctreeHeight(tree->getHeight()) {
42

43 44
        fassert(tree, "tree cannot be null", __LINE__, __FILE__);
        fassert(kernels, "kernels cannot be null", __LINE__, __FILE__);
45 46 47 48 49 50 51 52 53 54 55 56 57

        FDEBUG(FDebug::Controller << "FFmmAlgorithm\n");
    }

    /** Default destructor */
    virtual ~FFmmAlgorithm(){
    }

    /**
      * To execute the fmm algorithm
      * Call this function to run the complete algorithm
      */
    void execute(){
58
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
59 60

        bottomPass();
berenger-bramas's avatar
berenger-bramas committed
61

62 63 64 65
        upwardPass();

        downardPass();

66
        directPass();         
67 68
    }

69
private:
berenger-bramas's avatar
berenger-bramas committed
70 71 72 73
    /////////////////////////////////////////////////////////////////////////////
    // P2M
    /////////////////////////////////////////////////////////////////////////////

74 75
    /** P2M */
    void bottomPass(){
76
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
77
        FDEBUG( FDebug::Controller.write("\tStart Bottom Pass\n").write(FDebug::Flush) );
berenger-bramas's avatar
berenger-bramas committed
78 79
        FDEBUG(FTic counterTime);
        FDEBUG(FTic computationCounter);
80

berenger-bramas's avatar
berenger-bramas committed
81
        typename OctreeClass::Iterator octreeIterator(tree);
82 83 84 85 86 87 88 89 90 91 92

        // Iterate on leafs
        octreeIterator.gotoBottomLeft();
        do{
            // We need the current cell that represent the leaf
            // and the list of particles
            FDEBUG(computationCounter.tic());
            kernels->P2M( octreeIterator.getCurrentCell() , octreeIterator.getCurrentListSrc());
            FDEBUG(computationCounter.tac());
        } while(octreeIterator.moveRight());

93
        FDEBUG( FDebug::Controller << "\tFinished (@Bottom Pass (P2M) = "  << counterTime.tacAndElapsed() << "s)\n" );
berenger-bramas's avatar
berenger-bramas committed
94
        FDEBUG( FDebug::Controller << "\t\t Computation : " << computationCounter.cumulated() << " s\n" );
95 96
    }

berenger-bramas's avatar
berenger-bramas committed
97 98 99 100
    /////////////////////////////////////////////////////////////////////////////
    // Upward
    /////////////////////////////////////////////////////////////////////////////

101 102
    /** M2M */
    void upwardPass(){
103
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
104
        FDEBUG( FDebug::Controller.write("\tStart Upward Pass\n").write(FDebug::Flush); );
berenger-bramas's avatar
berenger-bramas committed
105 106
        FDEBUG(FTic counterTime);
        FDEBUG(FTic computationCounter);
107 108

        // Start from leal level - 1
berenger-bramas's avatar
berenger-bramas committed
109
        typename OctreeClass::Iterator octreeIterator(tree);
110 111 112
        octreeIterator.gotoBottomLeft();
        octreeIterator.moveUp();

berenger-bramas's avatar
berenger-bramas committed
113
        typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

        // for each levels
        for(int idxLevel = OctreeHeight - 2 ; idxLevel > 1 ; --idxLevel ){
            // for each cells
            do{
                // We need the current cell and the child
                // child is an array (of 8 child) that may be null
                FDEBUG(computationCounter.tic());
                kernels->M2M( octreeIterator.getCurrentCell() , octreeIterator.getCurrentChild(), idxLevel);
                FDEBUG(computationCounter.tac());
            } while(octreeIterator.moveRight());

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

berenger-bramas's avatar
berenger-bramas committed
130

131
        FDEBUG( FDebug::Controller << "\tFinished (@Upward Pass (M2M) = "  << counterTime.tacAndElapsed() << "s)\n" );
berenger-bramas's avatar
berenger-bramas committed
132
        FDEBUG( FDebug::Controller << "\t\t Computation : " << computationCounter.cumulated() << " s\n" );
133 134
    }

berenger-bramas's avatar
berenger-bramas committed
135 136 137 138
    /////////////////////////////////////////////////////////////////////////////
    // Downward
    /////////////////////////////////////////////////////////////////////////////

139 140
    /** M2L L2L */
    void downardPass(){
141
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
142 143

        { // first M2L
berenger-bramas's avatar
berenger-bramas committed
144 145 146 147
            FDEBUG( FDebug::Controller.write("\tStart Downward Pass (M2L)\n").write(FDebug::Flush); );
            FDEBUG(FTic counterTime);
            FDEBUG(FTic computationCounter);

berenger-bramas's avatar
berenger-bramas committed
148
            typename OctreeClass::Iterator octreeIterator(tree);
149 150
            octreeIterator.moveDown();

berenger-bramas's avatar
berenger-bramas committed
151
            typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);
152

153
            const CellClass* neighbors[189];
154

155 156 157 158
            // for each levels
            for(int idxLevel = 2 ; idxLevel < OctreeHeight ; ++idxLevel ){
                // for each cells
                do{
159
                    const int counter = tree->getDistantNeighbors(neighbors, octreeIterator.getCurrentGlobalCoordinate(), idxLevel);
berenger-bramas's avatar
berenger-bramas committed
160
                    FDEBUG(computationCounter.tic());
161
                    if(counter) kernels->M2L( octreeIterator.getCurrentCell() , neighbors, counter, idxLevel);
162 163 164 165 166 167
                    FDEBUG(computationCounter.tac());
                } while(octreeIterator.moveRight());

                avoidGotoLeftIterator.moveDown();
                octreeIterator = avoidGotoLeftIterator;
            }
168
            FDEBUG( FDebug::Controller << "\tFinished (@Downward Pass (M2L) = "  << counterTime.tacAndElapsed() << "s)\n" );
berenger-bramas's avatar
berenger-bramas committed
169
            FDEBUG( FDebug::Controller << "\t\t Computation : " << computationCounter.cumulated() << " s\n" );
170
        }
berenger-bramas's avatar
berenger-bramas committed
171 172 173 174 175 176

        { // second L2L            
            FDEBUG( FDebug::Controller.write("\tStart Downward Pass (L2L)\n").write(FDebug::Flush); );
            FDEBUG(FTic counterTime);
            FDEBUG(FTic computationCounter );

berenger-bramas's avatar
berenger-bramas committed
177
            typename OctreeClass::Iterator octreeIterator(tree);
178 179
            octreeIterator.moveDown();

berenger-bramas's avatar
berenger-bramas committed
180
            typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);
181 182 183 184 185 186 187 188 189 190 191 192 193 194

            const int heightMinusOne = OctreeHeight - 1;
            // for each levels exepted leaf level
            for(int idxLevel = 2 ; idxLevel < heightMinusOne ; ++idxLevel ){
                // for each cells
                do{
                    FDEBUG(computationCounter.tic());
                    kernels->L2L( octreeIterator.getCurrentCell() , octreeIterator.getCurrentChild(), idxLevel);
                    FDEBUG(computationCounter.tac());
                } while(octreeIterator.moveRight());

                avoidGotoLeftIterator.moveDown();
                octreeIterator = avoidGotoLeftIterator;
            }
berenger-bramas's avatar
berenger-bramas committed
195

196
            FDEBUG( FDebug::Controller << "\tFinished (@Downward Pass (L2L) = "  << counterTime.tacAndElapsed() << "s)\n" );
berenger-bramas's avatar
berenger-bramas committed
197
            FDEBUG( FDebug::Controller << "\t\t Computation : " << computationCounter.cumulated() << " s\n" );
198 199
        }

200

201 202
    }

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

207 208
    /** P2P */
    void directPass(){
209
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
210
        FDEBUG( FDebug::Controller.write("\tStart Direct Pass\n").write(FDebug::Flush); );
berenger-bramas's avatar
berenger-bramas committed
211
        FDEBUG(FTic counterTime);
212 213
        FDEBUG(FTic computationCounterL2P);
        FDEBUG(FTic computationCounterP2P);
214 215 216

        const int heightMinusOne = OctreeHeight - 1;

berenger-bramas's avatar
berenger-bramas committed
217
        typename OctreeClass::Iterator octreeIterator(tree);
218 219
        octreeIterator.gotoBottomLeft();
        // There is a maximum of 26 neighbors
berenger-bramas's avatar
berenger-bramas committed
220
        ContainerClass* neighbors[26];
221
        MortonIndex neighborsIndex[26];
222 223
        // for each leafs
        do{
224
            FDEBUG(computationCounterL2P.tic());
225
            kernels->L2P(octreeIterator.getCurrentCell(), octreeIterator.getCurrentListTargets());
226
            FDEBUG(computationCounterL2P.tac());
227
            // need the current particles and neighbors particles
228
            const int counter = tree->getLeafsNeighborsWithIndex(neighbors, neighborsIndex, octreeIterator.getCurrentGlobalIndex(),heightMinusOne);
229
            FDEBUG(computationCounterP2P.tic());
230
            kernels->P2P(octreeIterator.getCurrentGlobalIndex(),octreeIterator.getCurrentListTargets(), octreeIterator.getCurrentListSrc() , neighbors, neighborsIndex, counter);
231
            FDEBUG(computationCounterP2P.tac());
232 233
        } while(octreeIterator.moveRight());

berenger-bramas's avatar
berenger-bramas committed
234

235 236 237
        FDEBUG( FDebug::Controller << "\tFinished (@Direct Pass (L2P + P2P) = "  << counterTime.tacAndElapsed() << "s)\n" );
        FDEBUG( FDebug::Controller << "\t\t Computation L2P : " << computationCounterL2P.cumulated() << " s\n" );
        FDEBUG( FDebug::Controller << "\t\t Computation P2P : " << computationCounterP2P.cumulated() << " s\n" );
238

239 240 241 242 243 244 245
    }

};


#endif //FFMMALGORITHM_HPP

246
// [--END--]