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

74 75
        transferPass();

76 77
        downardPass();

78
        directPass();         
79 80
    }

81
private:
berenger-bramas's avatar
berenger-bramas committed
82 83 84 85
    /////////////////////////////////////////////////////////////////////////////
    // P2M
    /////////////////////////////////////////////////////////////////////////////

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

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

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

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

berenger-bramas's avatar
berenger-bramas committed
109 110 111 112
    /////////////////////////////////////////////////////////////////////////////
    // Upward
    /////////////////////////////////////////////////////////////////////////////

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

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

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

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

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

berenger-bramas's avatar
berenger-bramas committed
147
    /////////////////////////////////////////////////////////////////////////////
148
    // Transfer
berenger-bramas's avatar
berenger-bramas committed
149 150
    /////////////////////////////////////////////////////////////////////////////

151 152
    /** M2L */
    void transferPass(){
153
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
154

155 156 157 158 159 160 161 162 163
        FDEBUG( FDebug::Controller.write("\tStart Downward Pass (M2L)\n").write(FDebug::Flush); );
        FDEBUG(FTic counterTime);
        FDEBUG(FTic computationCounter);

        typename OctreeClass::Iterator octreeIterator(tree);
        octreeIterator.moveDown();

        typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);

164
        const CellClass* neighbors[343];
165 166 167 168 169

        // for each levels
        for(int idxLevel = 2 ; idxLevel < OctreeHeight ; ++idxLevel ){
            // for each cells
            do{
170
                const int counter = tree->getInteractionNeighbors(neighbors, octreeIterator.getCurrentGlobalCoordinate(), idxLevel);
171 172 173 174 175 176 177
                FDEBUG(computationCounter.tic());
                if(counter) kernels->M2L( octreeIterator.getCurrentCell() , neighbors, counter, idxLevel);
                FDEBUG(computationCounter.tac());
            } while(octreeIterator.moveRight());

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

183 184 185
    /////////////////////////////////////////////////////////////////////////////
    // Downward
    /////////////////////////////////////////////////////////////////////////////
berenger-bramas's avatar
berenger-bramas committed
186

187 188 189 190 191 192
    /** L2L */
    void downardPass(){
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
        FDEBUG( FDebug::Controller.write("\tStart Downward Pass (L2L)\n").write(FDebug::Flush); );
        FDEBUG(FTic counterTime);
        FDEBUG(FTic computationCounter );
193

194 195
        typename OctreeClass::Iterator octreeIterator(tree);
        octreeIterator.moveDown();
196

197
        typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);
198

199 200 201 202 203 204 205 206 207
        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());
berenger-bramas's avatar
berenger-bramas committed
208

209 210
            avoidGotoLeftIterator.moveDown();
            octreeIterator = avoidGotoLeftIterator;
211 212
        }

213 214 215
        FDEBUG( FDebug::Controller << "\tFinished (@Downward Pass (L2L) = "  << counterTime.tacAndElapsed() << "s)\n" );
        FDEBUG( FDebug::Controller << "\t\t Computation : " << computationCounter.cumulated() << " s\n" );

216

217 218
    }

berenger-bramas's avatar
berenger-bramas committed
219 220 221 222
    /////////////////////////////////////////////////////////////////////////////
    // Direct
    /////////////////////////////////////////////////////////////////////////////

223 224
    /** P2P */
    void directPass(){
225
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
226
        FDEBUG( FDebug::Controller.write("\tStart Direct Pass\n").write(FDebug::Flush); );
berenger-bramas's avatar
berenger-bramas committed
227
        FDEBUG(FTic counterTime);
228 229
        FDEBUG(FTic computationCounterL2P);
        FDEBUG(FTic computationCounterP2P);
230 231 232

        const int heightMinusOne = OctreeHeight - 1;

berenger-bramas's avatar
berenger-bramas committed
233
        typename OctreeClass::Iterator octreeIterator(tree);
234 235
        octreeIterator.gotoBottomLeft();
        // There is a maximum of 26 neighbors
berenger-bramas's avatar
berenger-bramas committed
236
        ContainerClass* neighbors[27];
237 238
        // for each leafs
        do{
239
            FDEBUG(computationCounterL2P.tic());
240
            kernels->L2P(octreeIterator.getCurrentCell(), octreeIterator.getCurrentListTargets());
241
            FDEBUG(computationCounterL2P.tac());
242
            // need the current particles and neighbors particles
243
            const int counter = tree->getLeafsNeighbors(neighbors, octreeIterator.getCurrentGlobalCoordinate(),heightMinusOne);
244
            FDEBUG(computationCounterP2P.tic());
245 246
            kernels->P2P(octreeIterator.getCurrentGlobalCoordinate(),octreeIterator.getCurrentListTargets(),
                         octreeIterator.getCurrentListSrc(), neighbors, counter);
247
            FDEBUG(computationCounterP2P.tac());
248 249
        } while(octreeIterator.moveRight());

berenger-bramas's avatar
berenger-bramas committed
250

251 252 253
        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" );
254

255 256 257 258 259 260 261
    }

};


#endif //FFMMALGORITHM_HPP

262