Une MAJ de sécurité est nécessaire sur notre version actuelle. Elle sera effectuée lundi 02/08 entre 12h30 et 13h. L'interruption de service devrait durer quelques minutes (probablement moins de 5 minutes).

FFmmAlgorithmThread.hpp 16.7 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 FFMMALGORITHMTHREAD_HPP
#define FFMMALGORITHMTHREAD_HPP
13

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

#include "../Utils/FAssertable.hpp"
#include "../Utils/FDebug.hpp"
#include "../Utils/FTrace.hpp"
#include "../Utils/FTic.hpp"
#include "../Utils/FGlobal.hpp"

#include "../Containers/FOctree.hpp"


#include <omp.h>

/**
* @author Berenger Bramas (berenger.bramas@inria.fr)
* @class FFmmAlgorithmThread
* @brief
* Please read the license
*
* This class is a threaded FMM algorithm
* It just iterates on a tree and call the kernels with good arguments.
* It used the inspector-executor model :
* iterates on the tree and builds an array to work in parallel on this array
*
* Of course this class does not deallocate pointer given in arguements.
*
berenger-bramas's avatar
berenger-bramas committed
39
* When using this algorithm the P2P is thread safe.
40
*/
berenger-bramas's avatar
berenger-bramas committed
41
template<class OctreeClass, class ParticleClass, class CellClass, class ContainerClass, class KernelClass, class LeafClass>
42
class FFmmAlgorithmThread : protected FAssertable{
berenger-bramas's avatar
berenger-bramas committed
43 44
    OctreeClass* const tree;                  //< The octree to work on
    KernelClass** kernels;                    //< The kernels
45

berenger-bramas's avatar
berenger-bramas committed
46
    typename OctreeClass::Iterator* iterArray;
47
    int leafsNumber;
48 49

    static const int SizeShape = 3*3*3;
berenger-bramas's avatar
berenger-bramas committed
50 51 52
    int shapeLeaf[SizeShape];    

    const int MaxThreads;
53

54 55
    const int OctreeHeight;

56 57 58 59 60 61
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
62
    FFmmAlgorithmThread(OctreeClass* const inTree, KernelClass* const inKernels)
63 64
        : tree(inTree) , kernels(0), iterArray(0), leafsNumber(0),
          MaxThreads(omp_get_max_threads()), OctreeHeight(tree->getHeight()) {
65

66
        fassert(tree, "tree cannot be null", __LINE__, __FILE__);
67

berenger-bramas's avatar
berenger-bramas committed
68
        this->kernels = new KernelClass*[MaxThreads];
berenger-bramas's avatar
berenger-bramas committed
69
        for(int idxThread = 0 ; idxThread < MaxThreads ; ++idxThread){
berenger-bramas's avatar
berenger-bramas committed
70
            this->kernels[idxThread] = new KernelClass(*inKernels);
71 72
        }

berenger-bramas's avatar
berenger-bramas committed
73
        FDEBUG(FDebug::Controller << "FFmmAlgorithmThread (Max Thread " << omp_get_max_threads() << ")\n");
74 75 76 77
    }

    /** Default destructor */
    virtual ~FFmmAlgorithmThread(){
berenger-bramas's avatar
berenger-bramas committed
78
        for(int idxThread = 0 ; idxThread < MaxThreads ; ++idxThread){
79 80
            delete this->kernels[idxThread];
        }
berenger-bramas's avatar
berenger-bramas committed
81
        delete [] this->kernels;
82 83 84 85 86 87 88
    }

    /**
      * To execute the fmm algorithm
      * Call this function to run the complete algorithm
      */
    void execute(){
89
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
90 91 92 93 94 95

        for(int idxShape = 0 ; idxShape < SizeShape ; ++idxShape){
            this->shapeLeaf[idxShape] = 0;
        }

        // Count leaf
96
        leafsNumber = 0;
berenger-bramas's avatar
berenger-bramas committed
97
        typename OctreeClass::Iterator octreeIterator(tree);
98 99
        octreeIterator.gotoBottomLeft();
        do{
100
            ++leafsNumber;
101
            const FTreeCoordinate& coord = octreeIterator.getCurrentCell()->getCoordinate();
102 103 104
            ++this->shapeLeaf[(coord.getX()%3)*9 + (coord.getY()%3)*3 + (coord.getZ()%3)];

        } while(octreeIterator.moveRight());
berenger-bramas's avatar
berenger-bramas committed
105
        iterArray = new typename OctreeClass::Iterator[leafsNumber];
106
        fassert(iterArray, "iterArray bad alloc", __LINE__, __FILE__);
107 108

        bottomPass();
109

110 111
        upwardPass();

112 113
        transferPass();

114 115 116 117 118
        downardPass();

        directPass();

        delete [] iterArray;
119
        iterArray = 0;         
120 121
    }

122
private:
berenger-bramas's avatar
berenger-bramas committed
123 124 125 126
    /////////////////////////////////////////////////////////////////////////////
    // P2M
    /////////////////////////////////////////////////////////////////////////////

127 128
    /** P2M */
    void bottomPass(){
129
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
130
        FDEBUG( FDebug::Controller.write("\tStart Bottom Pass\n").write(FDebug::Flush) );
berenger-bramas's avatar
berenger-bramas committed
131
        FDEBUG(FTic counterTime);
132

berenger-bramas's avatar
berenger-bramas committed
133
        typename OctreeClass::Iterator octreeIterator(tree);
134 135 136 137 138 139 140 141
        int leafs = 0;
        // Iterate on leafs
        octreeIterator.gotoBottomLeft();
        do{
            iterArray[leafs] = octreeIterator;
            ++leafs;
        } while(octreeIterator.moveRight());

berenger-bramas's avatar
berenger-bramas committed
142
        FDEBUG(FTic computationCounter);
143
#pragma omp parallel
144
        {
berenger-bramas's avatar
berenger-bramas committed
145
            KernelClass * const myThreadkernels = kernels[omp_get_thread_num()];
146
#pragma omp for nowait
147 148 149 150 151 152
            for(int idxLeafs = 0 ; idxLeafs < leafs ; ++idxLeafs){
                // We need the current cell that represent the leaf
                // and the list of particles
                myThreadkernels->P2M( iterArray[idxLeafs].getCurrentCell() , iterArray[idxLeafs].getCurrentListSrc());
            }
        }
berenger-bramas's avatar
berenger-bramas committed
153
        FDEBUG(computationCounter.tac() );
154

155
        FDEBUG( FDebug::Controller << "\tFinished (@Bottom Pass (P2M) = "  << counterTime.tacAndElapsed() << "s)\n" );
156
        FDEBUG( FDebug::Controller << "\t\t Computation : " << computationCounter.elapsed() << " s\n" );
157

158 159
    }

berenger-bramas's avatar
berenger-bramas committed
160 161 162 163
    /////////////////////////////////////////////////////////////////////////////
    // Upward
    /////////////////////////////////////////////////////////////////////////////

164 165
    /** M2M */
    void upwardPass(){
166
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
167
        FDEBUG( FDebug::Controller.write("\tStart Upward Pass\n").write(FDebug::Flush); );
berenger-bramas's avatar
berenger-bramas committed
168 169
        FDEBUG(FTic counterTime);
        FDEBUG(FTic computationCounter);
170 171

        // Start from leal level - 1
berenger-bramas's avatar
berenger-bramas committed
172
        typename OctreeClass::Iterator octreeIterator(tree);
173 174
        octreeIterator.gotoBottomLeft();
        octreeIterator.moveUp();
berenger-bramas's avatar
berenger-bramas committed
175
        typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);
176 177 178

        // for each levels
        for(int idxLevel = OctreeHeight - 2 ; idxLevel > 1 ; --idxLevel ){
179
            FDEBUG(FTic counterTimeLevel);
180
            int numberOfCells = 0;
181 182
            // for each cells
            do{
183 184
                iterArray[numberOfCells] = octreeIterator;
                ++numberOfCells;
185 186 187 188 189
            } while(octreeIterator.moveRight());
            avoidGotoLeftIterator.moveUp();
            octreeIterator = avoidGotoLeftIterator;// equal octreeIterator.moveUp(); octreeIterator.gotoLeft();

            FDEBUG(computationCounter.tic());
190
            #pragma omp parallel
191
            {
berenger-bramas's avatar
berenger-bramas committed
192
                KernelClass * const myThreadkernels = kernels[omp_get_thread_num()];
193
                #pragma omp for nowait
194
                for(int idxCell = 0 ; idxCell < numberOfCells ; ++idxCell){
195 196
                    // We need the current cell and the child
                    // child is an array (of 8 child) that may be null
197
                    myThreadkernels->M2M( iterArray[idxCell].getCurrentCell() , iterArray[idxCell].getCurrentChild(), idxLevel);
198 199
                }
            }
berenger-bramas's avatar
berenger-bramas committed
200

201
            FDEBUG(computationCounter.tac());
202
            FDEBUG( FDebug::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
203 204
        }

berenger-bramas's avatar
berenger-bramas committed
205

206
        FDEBUG( FDebug::Controller << "\tFinished (@Upward Pass (M2M) = "  << 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
    }

berenger-bramas's avatar
berenger-bramas committed
211
    /////////////////////////////////////////////////////////////////////////////
212
    // Transfer
berenger-bramas's avatar
berenger-bramas committed
213 214
    /////////////////////////////////////////////////////////////////////////////

215
    /** M2L L2L */
216
    void transferPass(){
217
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
218

219 220 221
        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
222

223 224 225 226 227 228
        typename OctreeClass::Iterator octreeIterator(tree);
        octreeIterator.moveDown();
        typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);

        // for each levels
        for(int idxLevel = 2 ; idxLevel < OctreeHeight ; ++idxLevel ){
229
            FDEBUG(FTic counterTimeLevel);
230 231 232 233 234 235 236 237 238 239
            int numberOfCells = 0;
            // for each cells
            do{
                iterArray[numberOfCells] = octreeIterator;
                ++numberOfCells;
            } while(octreeIterator.moveRight());
            avoidGotoLeftIterator.moveDown();
            octreeIterator = avoidGotoLeftIterator;

            FDEBUG(computationCounter.tic());
240
            #pragma omp parallel
241 242
            {
                KernelClass * const myThreadkernels = kernels[omp_get_thread_num()];
243
                const CellClass* neighbors[343];
244

245
                #pragma omp for  schedule(dynamic) nowait
246
                for(int idxCell = 0 ; idxCell < numberOfCells ; ++idxCell){
247
                    const int counter = tree->getInteractionNeighbors(neighbors,  iterArray[idxCell].getCurrentGlobalCoordinate(),idxLevel);
248
                    if(counter) myThreadkernels->M2L( iterArray[idxCell].getCurrentCell() , neighbors, counter, idxLevel);
249
                }
250 251 252 253

                FDEBUG(computationCounter.tic());
                myThreadkernels->finishedLevelM2L(idxLevel);
                FDEBUG(computationCounter.tac());
254
            }
255
            FDEBUG(computationCounter.tac());
256
            FDEBUG( FDebug::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
257 258
        }

259 260 261
        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
262

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    /////////////////////////////////////////////////////////////////////////////
    // Downard
    /////////////////////////////////////////////////////////////////////////////

    void downardPass(){ // second L2L
        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);

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

        typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);

        const int heightMinusOne = OctreeHeight - 1;
        // for each levels exepted leaf level
        for(int idxLevel = 2 ; idxLevel < heightMinusOne ; ++idxLevel ){
282
            FDEBUG(FTic counterTimeLevel);
283 284 285 286 287 288 289 290 291 292
            int numberOfCells = 0;
            // for each cells
            do{
                iterArray[numberOfCells] = octreeIterator;
                ++numberOfCells;
            } while(octreeIterator.moveRight());
            avoidGotoLeftIterator.moveDown();
            octreeIterator = avoidGotoLeftIterator;

            FDEBUG(computationCounter.tic());
293
            #pragma omp parallel
294 295
            {
                KernelClass * const myThreadkernels = kernels[omp_get_thread_num()];
296
                #pragma omp for nowait
297 298
                for(int idxCell = 0 ; idxCell < numberOfCells ; ++idxCell){
                    myThreadkernels->L2L( iterArray[idxCell].getCurrentCell() , iterArray[idxCell].getCurrentChild(), idxLevel);
299 300
                }
            }
301
            FDEBUG(computationCounter.tac());
302
            FDEBUG( FDebug::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
303 304
        }

305 306
        FDEBUG( FDebug::Controller << "\tFinished (@Downward Pass (L2L) = "  << counterTime.tacAndElapsed() << "s)\n" );
        FDEBUG( FDebug::Controller << "\t\t Computation : " << computationCounter.cumulated() << " s\n" );
307 308
    }

309 310


berenger-bramas's avatar
berenger-bramas committed
311 312 313 314
    /////////////////////////////////////////////////////////////////////////////
    // Direct
    /////////////////////////////////////////////////////////////////////////////

315 316
    /** P2P */
    void directPass(){
317
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
318
        FDEBUG( FDebug::Controller.write("\tStart Direct Pass\n").write(FDebug::Flush); );
berenger-bramas's avatar
berenger-bramas committed
319 320
        FDEBUG(FTic counterTime);
        FDEBUG(FTic computationCounter);
321
        FDEBUG(FTic computationCounterP2P);
322

323
        omp_lock_t lockShape[SizeShape];
324
        for(int idxShape = 0 ; idxShape < SizeShape ; ++idxShape){
325
            omp_init_lock(&lockShape[idxShape]);
326 327
        }

328 329 330
        struct LeafData{
            MortonIndex index;
            CellClass* cell;
berenger-bramas's avatar
berenger-bramas committed
331 332
            ContainerClass* targets;
            ContainerClass* sources;
333 334 335
        };
        LeafData* const leafsDataArray = new LeafData[this->leafsNumber];

336
        const int LeafIndex = OctreeHeight - 1;
337 338 339 340 341 342 343

        int startPosAtShape[SizeShape];
        startPosAtShape[0] = 0;
        for(int idxShape = 1 ; idxShape < SizeShape ; ++idxShape){
            startPosAtShape[idxShape] = startPosAtShape[idxShape-1] + this->shapeLeaf[idxShape-1];
        }

344
        #pragma omp parallel
345
        {
346

347 348 349
            const float step = float(this->leafsNumber) / float(omp_get_num_threads());
            const int start = int(FMath::Ceil(step * float(omp_get_thread_num())));
            const int tempEnd = int(FMath::Ceil(step * float(omp_get_thread_num()+1)));
350
            const int end = (tempEnd > this->leafsNumber ? this->leafsNumber : tempEnd);
351

berenger-bramas's avatar
berenger-bramas committed
352
            typename OctreeClass::Iterator octreeIterator(tree);
353
            octreeIterator.gotoBottomLeft();
354 355 356 357 358

            for(int idxPreLeaf = 0 ; idxPreLeaf < start ; ++idxPreLeaf){
                octreeIterator.moveRight();
            }

359
            // for each leafs
360
            for(int idxMyLeafs = start ; idxMyLeafs < end ; ++idxMyLeafs){
361
                //iterArray[leafs] = octreeIterator;
362
                //++leafs;
363
                const FTreeCoordinate& coord = octreeIterator.getCurrentGlobalCoordinate();
364
                const int shapePosition = (coord.getX()%3)*9 + (coord.getY()%3)*3 + (coord.getZ()%3);
365

366 367 368
                omp_set_lock(&lockShape[shapePosition]);
                const int positionToWork = startPosAtShape[shapePosition]++;
                omp_unset_lock(&lockShape[shapePosition]);
369

370 371 372 373
                leafsDataArray[positionToWork].index = octreeIterator.getCurrentGlobalIndex();
                leafsDataArray[positionToWork].cell = octreeIterator.getCurrentCell();
                leafsDataArray[positionToWork].targets = octreeIterator.getCurrentListTargets();
                leafsDataArray[positionToWork].sources = octreeIterator.getCurrentListSrc();
374

375 376
                octreeIterator.moveRight();
            }
377

378
            #pragma omp barrier
379

berenger-bramas's avatar
berenger-bramas committed
380
            FDEBUG(if(!omp_get_thread_num()) computationCounter.tic());
381

berenger-bramas's avatar
berenger-bramas committed
382
            KernelClass& myThreadkernels = (*kernels[omp_get_thread_num()]);
383
            // There is a maximum of 26 neighbors
berenger-bramas's avatar
berenger-bramas committed
384
            ContainerClass* neighbors[27];
385
            int previous = 0;
386 387

            for(int idxShape = 0 ; idxShape < SizeShape ; ++idxShape){
388
                const int endAtThisShape = this->shapeLeaf[idxShape] + previous;
389

390
                #pragma omp for schedule(dynamic)
391 392 393
                for(int idxLeafs = previous ; idxLeafs < endAtThisShape ; ++idxLeafs){
                    LeafData& currentIter = leafsDataArray[idxLeafs];
                    myThreadkernels.L2P(currentIter.cell, currentIter.targets);
394
                    // need the current particles and neighbors particles
395
                    FDEBUG(if(!omp_get_thread_num()) computationCounterP2P.tic());
396
                    const int counter = tree->getLeafsNeighbors(neighbors, currentIter.cell->getCoordinate(), LeafIndex);
397 398
                    myThreadkernels.P2P(currentIter.cell->getCoordinate(), currentIter.targets,
                                        currentIter.sources, neighbors, counter);
399
                    FDEBUG(if(!omp_get_thread_num()) computationCounterP2P.tac());
400
                }
401 402

                previous = endAtThisShape;
403 404
            }
        }
405

406 407
        FDEBUG(computationCounter.tac());

408
        delete [] leafsDataArray;
409 410 411 412
        for(int idxShape = 0 ; idxShape < SizeShape ; ++idxShape){
            omp_destroy_lock(&lockShape[idxShape]);
        }

413

414 415
        FDEBUG( FDebug::Controller << "\tFinished (@Direct Pass (L2P + P2P) = "  << counterTime.tacAndElapsed() << "s)\n" );
        FDEBUG( FDebug::Controller << "\t\t Computation L2P + P2P : " << computationCounter.cumulated() << " s\n" );
416
        FDEBUG( FDebug::Controller << "\t\t Computation P2P : " << computationCounterP2P.cumulated() << " s\n" );
417

418 419 420 421 422 423 424
    }

};


#endif //FFMMALGORITHMTHREAD_HPP

425