FFmmAlgorithm.hpp 10.1 KB
Newer Older
1
// ===================================================================================
2 3 4 5 6 7 8 9
// Logiciel initial: ScalFmm Version 0.5
// Co-auteurs : Olivier Coulaud, Bérenger Bramas.
// Propriétaires : INRIA.
// Copyright © 2011-2012, diffusé sous les termes et conditions d’une licence propriétaire.
// Initial software: ScalFmm Version 0.5
// Co-authors: Olivier Coulaud, Bérenger Bramas.
// Owners: INRIA.
// Copyright © 2011-2012, spread under the terms and conditions of a proprietary license.
10
// ===================================================================================
11 12
#ifndef FFMMALGORITHM_HPP
#define FFMMALGORITHM_HPP
13

14

15
#include "../Utils/FGlobal.hpp"
16 17 18 19 20 21
#include "../Utils/FAssertable.hpp"
#include "../Utils/FDebug.hpp"
#include "../Utils/FTrace.hpp"
#include "../Utils/FTic.hpp"

#include "../Containers/FOctree.hpp"
22
#include "../Containers/FVector.hpp"
23 24 25 26 27 28 29 30 31 32 33 34 35


/**
* @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
36
template<class OctreeClass, class ParticleClass, class CellClass, class ContainerClass, class KernelClass, class LeafClass>
37 38
class FFmmAlgorithm : protected FAssertable{

berenger-bramas's avatar
berenger-bramas committed
39 40
    OctreeClass* const tree;       //< The octree to work on
    KernelClass* const kernels;    //< The kernels
41 42

    const int OctreeHeight;
43 44 45 46 47 48 49

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
50
    FFmmAlgorithm(OctreeClass* const inTree, KernelClass* const inKernels)
51
                      : tree(inTree) , kernels(inKernels), OctreeHeight(tree->getHeight()) {
52

53 54
        fassert(tree, "tree cannot be null", __LINE__, __FILE__);
        fassert(kernels, "kernels cannot be null", __LINE__, __FILE__);
55 56 57 58 59 60 61 62 63 64 65 66 67

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

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

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

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

72 73 74 75
        upwardPass();

        downardPass();

76
        directPass();         
77 78
    }

79
private:
berenger-bramas's avatar
berenger-bramas committed
80 81 82 83
    /////////////////////////////////////////////////////////////////////////////
    // P2M
    /////////////////////////////////////////////////////////////////////////////

84 85
    /** P2M */
    void bottomPass(){
86
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
87
        FDEBUG( FDebug::Controller.write("\tStart Bottom Pass\n").write(FDebug::Flush) );
berenger-bramas's avatar
berenger-bramas committed
88 89
        FDEBUG(FTic counterTime);
        FDEBUG(FTic computationCounter);
90

berenger-bramas's avatar
berenger-bramas committed
91
        typename OctreeClass::Iterator octreeIterator(tree);
92 93 94 95 96 97 98 99 100 101 102

        // 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());

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

berenger-bramas's avatar
berenger-bramas committed
107 108 109 110
    /////////////////////////////////////////////////////////////////////////////
    // Upward
    /////////////////////////////////////////////////////////////////////////////

111 112
    /** M2M */
    void upwardPass(){
113
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
114
        FDEBUG( FDebug::Controller.write("\tStart Upward Pass\n").write(FDebug::Flush); );
berenger-bramas's avatar
berenger-bramas committed
115 116
        FDEBUG(FTic counterTime);
        FDEBUG(FTic computationCounter);
117 118

        // Start from leal level - 1
berenger-bramas's avatar
berenger-bramas committed
119
        typename OctreeClass::Iterator octreeIterator(tree);
120 121 122
        octreeIterator.gotoBottomLeft();
        octreeIterator.moveUp();

berenger-bramas's avatar
berenger-bramas committed
123
        typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

        // 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
140

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

berenger-bramas's avatar
berenger-bramas committed
145 146 147 148
    /////////////////////////////////////////////////////////////////////////////
    // Downward
    /////////////////////////////////////////////////////////////////////////////

149 150
    /** M2L L2L */
    void downardPass(){
151
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
152 153

        { // first M2L
berenger-bramas's avatar
berenger-bramas committed
154 155 156 157
            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
158
            typename OctreeClass::Iterator octreeIterator(tree);
159 160
            octreeIterator.moveDown();

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

163
            const CellClass* neighbors[189];
164

165 166 167 168
            // for each levels
            for(int idxLevel = 2 ; idxLevel < OctreeHeight ; ++idxLevel ){
                // for each cells
                do{
169
                    const int counter = tree->getDistantNeighbors(neighbors, octreeIterator.getCurrentGlobalCoordinate(), idxLevel);
berenger-bramas's avatar
berenger-bramas committed
170
                    FDEBUG(computationCounter.tic());
171
                    if(counter) kernels->M2L( octreeIterator.getCurrentCell() , neighbors, counter, idxLevel);
172 173 174 175 176 177
                    FDEBUG(computationCounter.tac());
                } while(octreeIterator.moveRight());

                avoidGotoLeftIterator.moveDown();
                octreeIterator = avoidGotoLeftIterator;
            }
178
            FDEBUG( FDebug::Controller << "\tFinished (@Downward Pass (M2L) = "  << counterTime.tacAndElapsed() << "s)\n" );
berenger-bramas's avatar
berenger-bramas committed
179
            FDEBUG( FDebug::Controller << "\t\t Computation : " << computationCounter.cumulated() << " s\n" );
180
        }
berenger-bramas's avatar
berenger-bramas committed
181 182 183 184 185 186

        { // 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
187
            typename OctreeClass::Iterator octreeIterator(tree);
188 189
            octreeIterator.moveDown();

berenger-bramas's avatar
berenger-bramas committed
190
            typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);
191 192 193 194 195 196 197 198 199 200 201 202 203 204

            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
205

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

210

211 212
    }

berenger-bramas's avatar
berenger-bramas committed
213 214 215 216
    /////////////////////////////////////////////////////////////////////////////
    // Direct
    /////////////////////////////////////////////////////////////////////////////

217 218
    /** P2P */
    void directPass(){
219
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
220
        FDEBUG( FDebug::Controller.write("\tStart Direct Pass\n").write(FDebug::Flush); );
berenger-bramas's avatar
berenger-bramas committed
221
        FDEBUG(FTic counterTime);
222 223
        FDEBUG(FTic computationCounterL2P);
        FDEBUG(FTic computationCounterP2P);
224 225 226

        const int heightMinusOne = OctreeHeight - 1;

berenger-bramas's avatar
berenger-bramas committed
227
        typename OctreeClass::Iterator octreeIterator(tree);
228 229
        octreeIterator.gotoBottomLeft();
        // There is a maximum of 26 neighbors
berenger-bramas's avatar
berenger-bramas committed
230
        ContainerClass* neighbors[26];
231
        MortonIndex neighborsIndex[26];
232 233
        // for each leafs
        do{
234
            FDEBUG(computationCounterL2P.tic());
235
            kernels->L2P(octreeIterator.getCurrentCell(), octreeIterator.getCurrentListTargets());
236
            FDEBUG(computationCounterL2P.tac());
237
            // need the current particles and neighbors particles
238
            const int counter = tree->getLeafsNeighborsWithIndex(neighbors, neighborsIndex, octreeIterator.getCurrentGlobalIndex(),heightMinusOne);
239
            FDEBUG(computationCounterP2P.tic());
240
            kernels->P2P(octreeIterator.getCurrentGlobalIndex(),octreeIterator.getCurrentListTargets(), octreeIterator.getCurrentListSrc() , neighbors, neighborsIndex, counter);
241
            FDEBUG(computationCounterP2P.tac());
242 243
        } while(octreeIterator.moveRight());

berenger-bramas's avatar
berenger-bramas committed
244

245 246 247
        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" );
248

249 250 251 252 253 254 255
    }

};


#endif //FFMMALGORITHM_HPP

256