FFmmAlgorithmThread.hpp 16.2 KB
Newer Older
1 2 3 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
#ifndef FFMMALGORITHMTHREAD_HPP
#define FFMMALGORITHMTHREAD_HPP
// /!\ Please, you must read the license at the bottom of this page

#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
        FAssertable::assert(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 79 80 81 82 83
    }

    /**
      * To execute the fmm algorithm
      * Call this function to run the complete algorithm
      */
    void execute(){
        FTRACE( FTrace::Controller.enterFunction(FTrace::FMM, __FUNCTION__ , __FILE__ , __LINE__) );

        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
        FAssertable::assert(iterArray, "iterArray bad alloc", __LINE__, __FILE__);
100 101 102 103 104 105 106 107 108 109 110 111 112 113

        bottomPass();
        upwardPass();

        downardPass();

        directPass();

        delete [] iterArray;
        iterArray = 0;

        FTRACE( FTrace::Controller.leaveFunction(FTrace::FMM) );
    }

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

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

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

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

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

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

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

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

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

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

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

berenger-bramas's avatar
berenger-bramas committed
194

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

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

204 205 206 207 208
    /** M2L L2L */
    void downardPass(){
        FTRACE( FTrace::Controller.enterFunction(FTrace::FMM, __FUNCTION__ , __FILE__ , __LINE__) );

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

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

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

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

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

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

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

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

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

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

        FTRACE( FTrace::Controller.leaveFunction(FTrace::FMM) );
    }

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

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

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

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

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

        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
324
        {
325 326 327 328 329

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

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

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

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

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

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

354 355
                octreeIterator.moveRight();
            }
356

357 358
            #pragma omp barrier

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

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

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

370
                #pragma omp for schedule(dynamic)
371 372 373
                for(int idxLeafs = previous ; idxLeafs < endAtThisShape ; ++idxLeafs){
                    LeafData& currentIter = leafsDataArray[idxLeafs];
                    myThreadkernels.L2P(currentIter.cell, currentIter.targets);
374
                    // need the current particles and neighbors particles
375 376 377 378
                    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());
379
                }
380 381

                previous = endAtThisShape;
382 383
            }
        }
384

385 386
        FDEBUG(computationCounter.tac());

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

392

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

};


#endif //FFMMALGORITHMTHREAD_HPP

// [--LICENSE--]