FFmmAlgorithm.hpp 11.2 KB
Newer Older
1
// ===================================================================================
2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright ScalFmm 2011 INRIA, Olivier Coulaud, Bérenger Bramas, Matthias Messner
// olivier.coulaud@inria.fr, berenger.bramas@inria.fr
// This software is a computer program whose purpose is to compute the FMM.
//
// This software is governed by the CeCILL-C and LGPL licenses and
// abiding by the rules of distribution of free software.  
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public and CeCILL-C Licenses for more details.
// "http://www.cecill.info". 
// "http://www.gnu.org/licenses".
15
// ===================================================================================
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
#include "FCoreCommon.hpp"
30 31 32 33 34 35 36 37 38 39 40 41

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

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

    const int OctreeHeight;
49 50 51 52 53 54 55

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

59 60
        fassert(tree, "tree cannot be null", __LINE__, __FILE__);
        fassert(kernels, "kernels cannot be null", __LINE__, __FILE__);
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
      */
73
    void execute(const unsigned operationsToProceed = FFmmNearAndFarFields){
74
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
75

76
        if(operationsToProceed & FFmmP2M) bottomPass();
berenger-bramas's avatar
berenger-bramas committed
77

78
        if(operationsToProceed & FFmmM2M) upwardPass();
79

80
        if(operationsToProceed & FFmmM2L) transferPass();
81

82
        if(operationsToProceed & FFmmL2L) downardPass();
83

COULAUD Olivier's avatar
COULAUD Olivier committed
84
        if( (operationsToProceed & FFmmP2P) || (operationsToProceed & FFmmL2P) ) directPass();
85 86
    }

87
private:
berenger-bramas's avatar
berenger-bramas committed
88 89 90 91
    /////////////////////////////////////////////////////////////////////////////
    // P2M
    /////////////////////////////////////////////////////////////////////////////

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

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

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

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

berenger-bramas's avatar
berenger-bramas committed
115 116 117 118
    /////////////////////////////////////////////////////////////////////////////
    // Upward
    /////////////////////////////////////////////////////////////////////////////

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

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

berenger-bramas's avatar
berenger-bramas committed
131
        typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);
132 133 134

        // for each levels
        for(int idxLevel = OctreeHeight - 2 ; idxLevel > 1 ; --idxLevel ){
135 136
            FDEBUG(FTic counterTimeLevel);

137 138 139 140 141 142 143 144 145 146
            // 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();
147 148 149
            octreeIterator = avoidGotoLeftIterator;

            FDEBUG( FDebug::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
150 151
        }

berenger-bramas's avatar
berenger-bramas committed
152

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

berenger-bramas's avatar
berenger-bramas committed
157
    /////////////////////////////////////////////////////////////////////////////
158
    // Transfer
berenger-bramas's avatar
berenger-bramas committed
159 160
    /////////////////////////////////////////////////////////////////////////////

161 162
    /** M2L */
    void transferPass(){
163
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
164

165 166 167 168 169 170 171 172 173
        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);

174
        const CellClass* neighbors[343];
175 176 177

        // for each levels
        for(int idxLevel = 2 ; idxLevel < OctreeHeight ; ++idxLevel ){
178 179
            FDEBUG(FTic counterTimeLevel);

180 181
            // for each cells
            do{
182
                const int counter = tree->getInteractionNeighbors(neighbors, octreeIterator.getCurrentGlobalCoordinate(), idxLevel);
183 184 185 186 187
                FDEBUG(computationCounter.tic());
                if(counter) kernels->M2L( octreeIterator.getCurrentCell() , neighbors, counter, idxLevel);
                FDEBUG(computationCounter.tac());
            } while(octreeIterator.moveRight());

berenger-bramas's avatar
berenger-bramas committed
188 189 190 191
            FDEBUG(computationCounter.tic());
            kernels->finishedLevelM2L(idxLevel);
            FDEBUG(computationCounter.tac());

192 193
            avoidGotoLeftIterator.moveDown();
            octreeIterator = avoidGotoLeftIterator;
194 195

            FDEBUG( FDebug::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
196
        }
197 198 199
        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
200

201 202 203
    /////////////////////////////////////////////////////////////////////////////
    // Downward
    /////////////////////////////////////////////////////////////////////////////
berenger-bramas's avatar
berenger-bramas committed
204

205 206 207 208 209 210
    /** 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 );
211

212 213
        typename OctreeClass::Iterator octreeIterator(tree);
        octreeIterator.moveDown();
214

215
        typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);
216

217 218 219
        const int heightMinusOne = OctreeHeight - 1;
        // for each levels exepted leaf level
        for(int idxLevel = 2 ; idxLevel < heightMinusOne ; ++idxLevel ){
220 221
            FDEBUG(FTic counterTimeLevel);

222 223 224 225 226 227
            // 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
228

229 230
            avoidGotoLeftIterator.moveDown();
            octreeIterator = avoidGotoLeftIterator;
231 232

            FDEBUG( FDebug::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
233 234
        }

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

238

239 240
    }

berenger-bramas's avatar
berenger-bramas committed
241 242 243 244
    /////////////////////////////////////////////////////////////////////////////
    // Direct
    /////////////////////////////////////////////////////////////////////////////

245 246
    /** P2P */
    void directPass(){
247
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
248
        FDEBUG( FDebug::Controller.write("\tStart Direct Pass\n").write(FDebug::Flush); );
berenger-bramas's avatar
berenger-bramas committed
249
        FDEBUG(FTic counterTime);
250 251
        FDEBUG(FTic computationCounterL2P);
        FDEBUG(FTic computationCounterP2P);
252 253 254

        const int heightMinusOne = OctreeHeight - 1;

berenger-bramas's avatar
berenger-bramas committed
255
        typename OctreeClass::Iterator octreeIterator(tree);
256 257
        octreeIterator.gotoBottomLeft();
        // There is a maximum of 26 neighbors
berenger-bramas's avatar
berenger-bramas committed
258
        ContainerClass* neighbors[27];
259 260
        // for each leafs
        do{
261
            FDEBUG(computationCounterL2P.tic());
262
            kernels->L2P(octreeIterator.getCurrentCell(), octreeIterator.getCurrentListTargets());
263
            FDEBUG(computationCounterL2P.tac());
264
            // need the current particles and neighbors particles
265
            const int counter = tree->getLeafsNeighbors(neighbors, octreeIterator.getCurrentGlobalCoordinate(),heightMinusOne);
266
            FDEBUG(computationCounterP2P.tic());
267 268
            kernels->P2P(octreeIterator.getCurrentGlobalCoordinate(),octreeIterator.getCurrentListTargets(),
                         octreeIterator.getCurrentListSrc(), neighbors, counter);
269
            FDEBUG(computationCounterP2P.tac());
270 271
        } while(octreeIterator.moveRight());

berenger-bramas's avatar
berenger-bramas committed
272

273 274 275
        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" );
276

277 278 279 280 281 282 283
    }

};


#endif //FFMMALGORITHM_HPP

284