FFmmAlgorithmThread.hpp 15.7 KB
Newer Older
1 2
#ifndef FFMMALGORITHMTHREAD_HPP
#define FFMMALGORITHMTHREAD_HPP
3
// [--License--]
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

#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
29
* When using this algorithm the P2P is thread safe.
30
*/
berenger-bramas's avatar
berenger-bramas committed
31
template<class OctreeClass, class ParticleClass, class CellClass, class ContainerClass, class KernelClass, class LeafClass>
32
class FFmmAlgorithmThread : protected FAssertable{
berenger-bramas's avatar
berenger-bramas committed
33 34
    OctreeClass* const tree;                  //< The octree to work on
    KernelClass** kernels;                    //< The kernels
35

berenger-bramas's avatar
berenger-bramas committed
36
    typename OctreeClass::Iterator* iterArray;
37
    int leafsNumber;
38 39

    static const int SizeShape = 3*3*3;
berenger-bramas's avatar
berenger-bramas committed
40 41 42
    int shapeLeaf[SizeShape];    

    const int MaxThreads;
43

44 45
    const int OctreeHeight;

46 47 48 49 50 51
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
52
    FFmmAlgorithmThread(OctreeClass* const inTree, KernelClass* const inKernels)
53
                      : tree(inTree) , kernels(0), iterArray(0), leafsNumber(0),
54
                        MaxThreads(omp_get_max_threads()), OctreeHeight(tree->getHeight()) {
55

56
        fassert(tree, "tree cannot be null", __LINE__, __FILE__);
57

berenger-bramas's avatar
berenger-bramas committed
58
        this->kernels = new KernelClass*[MaxThreads];
berenger-bramas's avatar
berenger-bramas committed
59
        for(int idxThread = 0 ; idxThread < MaxThreads ; ++idxThread){
berenger-bramas's avatar
berenger-bramas committed
60
            this->kernels[idxThread] = new KernelClass(*inKernels);
61 62
        }

berenger-bramas's avatar
berenger-bramas committed
63
        FDEBUG(FDebug::Controller << "FFmmAlgorithmThread (Max Thread " << omp_get_max_threads() << ")\n");
64 65 66 67
    }

    /** Default destructor */
    virtual ~FFmmAlgorithmThread(){
berenger-bramas's avatar
berenger-bramas committed
68
        for(int idxThread = 0 ; idxThread < MaxThreads ; ++idxThread){
69 70
            delete this->kernels[idxThread];
        }
berenger-bramas's avatar
berenger-bramas committed
71
        delete [] this->kernels;
72 73 74 75 76 77 78
    }

    /**
      * To execute the fmm algorithm
      * Call this function to run the complete algorithm
      */
    void execute(){
79
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
80 81 82 83

        for(int idxShape = 0 ; idxShape < SizeShape ; ++idxShape){
            this->shapeLeaf[idxShape] = 0;
        }
84
        //const int LeafIndex = OctreeHeight - 1;
85 86

        // Count leaf
87
        leafsNumber = 0;
berenger-bramas's avatar
berenger-bramas committed
88
        typename OctreeClass::Iterator octreeIterator(tree);
89 90
        octreeIterator.gotoBottomLeft();
        do{
91
            ++leafsNumber;
92 93 94
            const FTreeCoordinate& coord = octreeIterator.getCurrentCell()->getCoordinate();
            //const MortonIndex index = octreeIterator.getCurrentGlobalIndex();
            //coord.setPositionFromMorton(index, LeafIndex);
95 96 97
            ++this->shapeLeaf[(coord.getX()%3)*9 + (coord.getY()%3)*3 + (coord.getZ()%3)];

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

        bottomPass();
        upwardPass();

        downardPass();

        directPass();

        delete [] iterArray;
        iterArray = 0;

111
         
112 113
    }

114
private:
berenger-bramas's avatar
berenger-bramas committed
115 116 117 118
    /////////////////////////////////////////////////////////////////////////////
    // P2M
    /////////////////////////////////////////////////////////////////////////////

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

berenger-bramas's avatar
berenger-bramas committed
125
        typename OctreeClass::Iterator octreeIterator(tree);
126 127 128 129 130 131 132 133
        int leafs = 0;
        // Iterate on leafs
        octreeIterator.gotoBottomLeft();
        do{
            iterArray[leafs] = octreeIterator;
            ++leafs;
        } while(octreeIterator.moveRight());

berenger-bramas's avatar
berenger-bramas committed
134 135
        FDEBUG(FTic computationCounter);
        #pragma omp parallel
136
        {
berenger-bramas's avatar
berenger-bramas committed
137
            KernelClass * const myThreadkernels = kernels[omp_get_thread_num()];
berenger-bramas's avatar
berenger-bramas committed
138
            #pragma omp for nowait
139 140 141 142 143 144
            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
145
        FDEBUG(computationCounter.tac() );
146

147
        FDEBUG( FDebug::Controller << "\tFinished (@Bottom Pass (P2M) = "  << counterTime.tacAndElapsed() << "s)\n" );
148
        FDEBUG( FDebug::Controller << "\t\t Computation : " << computationCounter.elapsed() << " s\n" );
149
         
150 151
    }

berenger-bramas's avatar
berenger-bramas committed
152 153 154 155
    /////////////////////////////////////////////////////////////////////////////
    // Upward
    /////////////////////////////////////////////////////////////////////////////

156 157
    /** M2M */
    void upwardPass(){
158
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
159
        FDEBUG( FDebug::Controller.write("\tStart Upward Pass\n").write(FDebug::Flush); );
berenger-bramas's avatar
berenger-bramas committed
160 161
        FDEBUG(FTic counterTime);
        FDEBUG(FTic computationCounter);
162 163

        // Start from leal level - 1
berenger-bramas's avatar
berenger-bramas committed
164
        typename OctreeClass::Iterator octreeIterator(tree);
165 166
        octreeIterator.gotoBottomLeft();
        octreeIterator.moveUp();
berenger-bramas's avatar
berenger-bramas committed
167
        typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);
168 169 170

        // for each levels
        for(int idxLevel = OctreeHeight - 2 ; idxLevel > 1 ; --idxLevel ){
171
            int numberOfCells = 0;
172 173
            // for each cells
            do{
174 175
                iterArray[numberOfCells] = octreeIterator;
                ++numberOfCells;
176 177 178 179 180
            } while(octreeIterator.moveRight());
            avoidGotoLeftIterator.moveUp();
            octreeIterator = avoidGotoLeftIterator;// equal octreeIterator.moveUp(); octreeIterator.gotoLeft();

            FDEBUG(computationCounter.tic());
berenger-bramas's avatar
berenger-bramas committed
181
            #pragma omp parallel
182
            {
berenger-bramas's avatar
berenger-bramas committed
183
                KernelClass * const myThreadkernels = kernels[omp_get_thread_num()];
184
                #pragma omp for nowait
185
                for(int idxCell = 0 ; idxCell < numberOfCells ; ++idxCell){
186 187
                    // We need the current cell and the child
                    // child is an array (of 8 child) that may be null
188
                    myThreadkernels->M2M( iterArray[idxCell].getCurrentCell() , iterArray[idxCell].getCurrentChild(), idxLevel);
189 190
                }
            }
berenger-bramas's avatar
berenger-bramas committed
191

192 193 194
            FDEBUG(computationCounter.tac());
        }

berenger-bramas's avatar
berenger-bramas committed
195

196
        FDEBUG( FDebug::Controller << "\tFinished (@Upward Pass (M2M) = "  << counterTime.tacAndElapsed() << "s)\n" );
berenger-bramas's avatar
berenger-bramas committed
197
        FDEBUG( FDebug::Controller << "\t\t Computation : " << computationCounter.cumulated() << " s\n" );
198
         
199 200
    }

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

205 206
    /** M2L L2L */
    void downardPass(){
207
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
208 209

        { // first M2L
berenger-bramas's avatar
berenger-bramas committed
210 211 212 213
            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
214
            typename OctreeClass::Iterator octreeIterator(tree);
215
            octreeIterator.moveDown();
berenger-bramas's avatar
berenger-bramas committed
216
            typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);
217 218 219

            // for each levels
            for(int idxLevel = 2 ; idxLevel < OctreeHeight ; ++idxLevel ){
220
                int numberOfCells = 0;
221 222
                // for each cells
                do{
223 224
                    iterArray[numberOfCells] = octreeIterator;
                    ++numberOfCells;
225 226 227 228 229
                } while(octreeIterator.moveRight());
                avoidGotoLeftIterator.moveDown();
                octreeIterator = avoidGotoLeftIterator;

                FDEBUG(computationCounter.tic());
berenger-bramas's avatar
berenger-bramas committed
230
                #pragma omp parallel
231
                {
berenger-bramas's avatar
berenger-bramas committed
232
                    KernelClass * const myThreadkernels = kernels[omp_get_thread_num()];
233
                    const CellClass* neighbors[189];
234

235
                    #pragma omp for  schedule(dynamic) nowait
236
                    for(int idxCell = 0 ; idxCell < numberOfCells ; ++idxCell){
237 238
                        const int counter = tree->getDistantNeighbors(neighbors,  iterArray[idxCell].getCurrentGlobalCoordinate(),idxLevel);
                        if(counter) myThreadkernels->M2L( iterArray[idxCell].getCurrentCell() , neighbors, counter, idxLevel);
239 240 241 242
                    }
                }
                FDEBUG(computationCounter.tac());
            }
berenger-bramas's avatar
berenger-bramas committed
243

244
            FDEBUG( FDebug::Controller << "\tFinished (@Downward Pass (M2L) = "  << counterTime.tacAndElapsed() << "s)\n" );
berenger-bramas's avatar
berenger-bramas committed
245
            FDEBUG( FDebug::Controller << "\t\t Computation : " << computationCounter.cumulated() << " s\n" );
246 247 248
        }

        { // second L2L
berenger-bramas's avatar
berenger-bramas committed
249 250 251 252
            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
253
            typename OctreeClass::Iterator octreeIterator(tree);
254 255
            octreeIterator.moveDown();

berenger-bramas's avatar
berenger-bramas committed
256
            typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);
257 258 259 260

            const int heightMinusOne = OctreeHeight - 1;
            // for each levels exepted leaf level
            for(int idxLevel = 2 ; idxLevel < heightMinusOne ; ++idxLevel ){
261
                int numberOfCells = 0;
262 263
                // for each cells
                do{
264 265
                    iterArray[numberOfCells] = octreeIterator;
                    ++numberOfCells;
266 267 268 269 270
                } while(octreeIterator.moveRight());
                avoidGotoLeftIterator.moveDown();
                octreeIterator = avoidGotoLeftIterator;

                FDEBUG(computationCounter.tic());
berenger-bramas's avatar
berenger-bramas committed
271
                #pragma omp parallel
272
                {
berenger-bramas's avatar
berenger-bramas committed
273
                    KernelClass * const myThreadkernels = kernels[omp_get_thread_num()];
274
                    #pragma omp for nowait
275 276
                    for(int idxCell = 0 ; idxCell < numberOfCells ; ++idxCell){
                        myThreadkernels->L2L( iterArray[idxCell].getCurrentCell() , iterArray[idxCell].getCurrentChild(), idxLevel);
277 278 279 280
                    }
                }
                FDEBUG(computationCounter.tac());
            }
berenger-bramas's avatar
berenger-bramas committed
281

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

286
         
287 288
    }

berenger-bramas's avatar
berenger-bramas committed
289 290 291 292
    /////////////////////////////////////////////////////////////////////////////
    // Direct
    /////////////////////////////////////////////////////////////////////////////

293 294
    /** P2P */
    void directPass(){
295
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
296
        FDEBUG( FDebug::Controller.write("\tStart Direct Pass\n").write(FDebug::Flush); );
berenger-bramas's avatar
berenger-bramas committed
297 298
        FDEBUG(FTic counterTime);
        FDEBUG(FTic computationCounter);
299
        FDEBUG(FTic computationCounterP2P);
300

301
        omp_lock_t lockShape[SizeShape];
302
        for(int idxShape = 0 ; idxShape < SizeShape ; ++idxShape){
303
            omp_init_lock(&lockShape[idxShape]);
304 305
        }

306 307 308
        struct LeafData{
            MortonIndex index;
            CellClass* cell;
berenger-bramas's avatar
berenger-bramas committed
309 310
            ContainerClass* targets;
            ContainerClass* sources;
311 312 313
        };
        LeafData* const leafsDataArray = new LeafData[this->leafsNumber];

314
        const int LeafIndex = OctreeHeight - 1;
315 316 317 318 319 320 321 322

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

        #pragma omp parallel
323
        {
324

325 326 327
            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)));
328
            const int end = (tempEnd > this->leafsNumber ? this->leafsNumber : tempEnd);
329

berenger-bramas's avatar
berenger-bramas committed
330
            typename OctreeClass::Iterator octreeIterator(tree);
331
            octreeIterator.gotoBottomLeft();
332 333 334 335 336

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

337
            // for each leafs
338
            for(int idxMyLeafs = start ; idxMyLeafs < end ; ++idxMyLeafs){
339
                //iterArray[leafs] = octreeIterator;
340
                //++leafs;
341
                const FTreeCoordinate& coord = octreeIterator.getCurrentGlobalCoordinate();
342
                const int shapePosition = (coord.getX()%3)*9 + (coord.getY()%3)*3 + (coord.getZ()%3);
343

344 345 346
                omp_set_lock(&lockShape[shapePosition]);
                const int positionToWork = startPosAtShape[shapePosition]++;
                omp_unset_lock(&lockShape[shapePosition]);
347

348 349 350 351
                leafsDataArray[positionToWork].index = octreeIterator.getCurrentGlobalIndex();
                leafsDataArray[positionToWork].cell = octreeIterator.getCurrentCell();
                leafsDataArray[positionToWork].targets = octreeIterator.getCurrentListTargets();
                leafsDataArray[positionToWork].sources = octreeIterator.getCurrentListSrc();
352

353 354
                octreeIterator.moveRight();
            }
355

356 357
            #pragma omp barrier

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

berenger-bramas's avatar
berenger-bramas committed
360
            KernelClass& myThreadkernels = (*kernels[omp_get_thread_num()]);
361
            // There is a maximum of 26 neighbors
berenger-bramas's avatar
berenger-bramas committed
362
            ContainerClass* neighbors[26];
berenger-bramas's avatar
berenger-bramas committed
363
            MortonIndex neighborsIndex[26];
364
            int previous = 0;
365 366

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

369
                #pragma omp for schedule(dynamic)
370 371 372
                for(int idxLeafs = previous ; idxLeafs < endAtThisShape ; ++idxLeafs){
                    LeafData& currentIter = leafsDataArray[idxLeafs];
                    myThreadkernels.L2P(currentIter.cell, currentIter.targets);
373
                    // need the current particles and neighbors particles
374 375 376 377
                    FDEBUG(if(!omp_get_thread_num()) computationCounterP2P.tic());
                    const int counter = tree->getLeafsNeighborsWithIndex(neighbors, neighborsIndex, currentIter.index, LeafIndex);
                    myThreadkernels.P2P(currentIter.index, currentIter.targets, currentIter.sources , neighbors, neighborsIndex, counter);
                    FDEBUG(if(!omp_get_thread_num()) computationCounterP2P.tac());
378
                }
379 380

                previous = endAtThisShape;
381 382
            }
        }
383

384 385
        FDEBUG(computationCounter.tac());

386
        delete [] leafsDataArray;
387 388 389 390
        for(int idxShape = 0 ; idxShape < SizeShape ; ++idxShape){
            omp_destroy_lock(&lockShape[idxShape]);
        }

391

392 393
        FDEBUG( FDebug::Controller << "\tFinished (@Direct Pass (L2P + P2P) = "  << counterTime.tacAndElapsed() << "s)\n" );
        FDEBUG( FDebug::Controller << "\t\t Computation L2P + P2P : " << computationCounter.cumulated() << " s\n" );
394
        FDEBUG( FDebug::Controller << "\t\t Computation P2P : " << computationCounterP2P.cumulated() << " s\n" );
395
         
396 397 398 399 400 401 402
    }

};


#endif //FFMMALGORITHMTHREAD_HPP

403
// [--END--]