FFmmAlgorithm.hpp 10.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// ===================================================================================
// Ce LOGICIEL "ScalFmm" est couvert par le copyright Inria 20xx-2012.
// Inria détient tous les droits de propriété sur le LOGICIEL, et souhaite que
// la communauté scientifique l'utilise afin de le tester et de l'évaluer.
// Inria donne gracieusement le droit d'utiliser ce LOGICIEL. Toute utilisation
// dans un but lucratif ou à des fins commerciales est interdite sauf autorisation
// expresse et préalable d'Inria.
// Toute utilisation hors des limites précisées ci-dessus et réalisée sans l'accord
// expresse préalable d'Inria constituerait donc le délit de contrefaçon.
// Le LOGICIEL étant un produit en cours de développement, Inria ne saurait assurer
// aucune responsabilité et notamment en aucune manière et en aucun cas, être tenu
// de répondre d'éventuels dommages directs ou indirects subits par l'utilisateur.
// Tout utilisateur du LOGICIEL s'engage à communiquer à Inria ses remarques
// relatives à l'usage du LOGICIEL
// ===================================================================================
16 17
#ifndef FFMMALGORITHM_HPP
#define FFMMALGORITHM_HPP
18

19

20
#include "../Utils/FGlobal.hpp"
21 22 23 24 25 26
#include "../Utils/FAssertable.hpp"
#include "../Utils/FDebug.hpp"
#include "../Utils/FTrace.hpp"
#include "../Utils/FTic.hpp"

#include "../Containers/FOctree.hpp"
27
#include "../Containers/FVector.hpp"
28 29 30 31 32 33 34 35 36 37 38 39 40


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

berenger-bramas's avatar
berenger-bramas committed
44 45
    OctreeClass* const tree;       //< The octree to work on
    KernelClass* const kernels;    //< The kernels
46 47

    const int OctreeHeight;
48 49 50 51 52 53 54

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

58 59
        fassert(tree, "tree cannot be null", __LINE__, __FILE__);
        fassert(kernels, "kernels cannot be null", __LINE__, __FILE__);
60 61 62 63 64 65 66 67 68 69 70 71 72

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

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

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

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

77 78 79 80
        upwardPass();

        downardPass();

81
        directPass();         
82 83
    }

84
private:
berenger-bramas's avatar
berenger-bramas committed
85 86 87 88
    /////////////////////////////////////////////////////////////////////////////
    // P2M
    /////////////////////////////////////////////////////////////////////////////

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

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

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

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

berenger-bramas's avatar
berenger-bramas committed
112 113 114 115
    /////////////////////////////////////////////////////////////////////////////
    // Upward
    /////////////////////////////////////////////////////////////////////////////

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

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

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

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

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

berenger-bramas's avatar
berenger-bramas committed
150 151 152 153
    /////////////////////////////////////////////////////////////////////////////
    // Downward
    /////////////////////////////////////////////////////////////////////////////

154 155
    /** M2L L2L */
    void downardPass(){
156
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
157 158

        { // first M2L
berenger-bramas's avatar
berenger-bramas committed
159 160 161 162
            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
163
            typename OctreeClass::Iterator octreeIterator(tree);
164 165
            octreeIterator.moveDown();

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

168
            const CellClass* neighbors[189];
169

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

                avoidGotoLeftIterator.moveDown();
                octreeIterator = avoidGotoLeftIterator;
            }
183
            FDEBUG( FDebug::Controller << "\tFinished (@Downward Pass (M2L) = "  << counterTime.tacAndElapsed() << "s)\n" );
berenger-bramas's avatar
berenger-bramas committed
184
            FDEBUG( FDebug::Controller << "\t\t Computation : " << computationCounter.cumulated() << " s\n" );
185
        }
berenger-bramas's avatar
berenger-bramas committed
186 187 188 189 190 191

        { // 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
192
            typename OctreeClass::Iterator octreeIterator(tree);
193 194
            octreeIterator.moveDown();

berenger-bramas's avatar
berenger-bramas committed
195
            typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);
196 197 198 199 200 201 202 203 204 205 206 207 208 209

            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
210

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

215

216 217
    }

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

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

        const int heightMinusOne = OctreeHeight - 1;

berenger-bramas's avatar
berenger-bramas committed
232
        typename OctreeClass::Iterator octreeIterator(tree);
233 234
        octreeIterator.gotoBottomLeft();
        // There is a maximum of 26 neighbors
berenger-bramas's avatar
berenger-bramas committed
235
        ContainerClass* neighbors[26];
236
        MortonIndex neighborsIndex[26];
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->getLeafsNeighborsWithIndex(neighbors, neighborsIndex, octreeIterator.getCurrentGlobalIndex(),heightMinusOne);
244
            FDEBUG(computationCounterP2P.tic());
245
            kernels->P2P(octreeIterator.getCurrentGlobalIndex(),octreeIterator.getCurrentListTargets(), octreeIterator.getCurrentListSrc() , neighbors, neighborsIndex, counter);
246
            FDEBUG(computationCounterP2P.tac());
247 248
        } while(octreeIterator.moveRight());

berenger-bramas's avatar
berenger-bramas committed
249

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

254 255 256 257 258 259 260
    }

};


#endif //FFMMALGORITHM_HPP

261