FFmmAlgorithmThread.hpp 19.9 KB
Newer Older
1
// ===================================================================================
2
// Copyright ScalFmm 2011 INRIA, Olivier Coulaud, Berenger Bramas, Matthias Messner
3 4 5 6
// 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
7 8
// abiding by the rules of distribution of free software.
//
9 10 11 12
// 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.
13
// "http://www.cecill.info".
14
// "http://www.gnu.org/licenses".
15
// ===================================================================================
16 17
#ifndef FFMMALGORITHMTHREAD_HPP
#define FFMMALGORITHMTHREAD_HPP
18

19

BRAMAS Berenger's avatar
BRAMAS Berenger committed
20
#include "../Utils/FAssert.hpp"
BRAMAS Berenger's avatar
BRAMAS Berenger committed
21
#include "../Utils/FLog.hpp"
22

23 24
#include "../Utils/FTic.hpp"
#include "../Utils/FGlobal.hpp"
25
#include "Utils/FAlgorithmTimers.hpp"
26 27 28

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

29
#include "FCoreCommon.hpp"
30 31 32 33

#include <omp.h>

/**
34 35
* \author Berenger Bramas (berenger.bramas@inria.fr)
* \brief Implements an FMM algorithm threaded using OpenMP.
36
*
37
* Please read the license
38
*
39 40 41 42
* This class runs a threaded FMM algorithm.  It just iterates on a tree and call
* the kernels with good arguments.  The inspector-executor model is used : the
* class iterates on the tree and builds an array and works in parallel on this
* array.
43
*
berenger-bramas's avatar
berenger-bramas committed
44
* When using this algorithm the P2P is thread safe.
45 46
*
* This class does not deallocate pointers given to its constructor.
47
*/
48
template<class OctreeClass, class CellClass, class ContainerClass, class KernelClass, class LeafClass>
49
class FFmmAlgorithmThread : public FAbstractAlgorithm, public FAlgorithmTimers{
50 51
    OctreeClass* const tree;                  ///< The octree to work on.
    KernelClass** kernels;                    ///< The kernels.
52

berenger-bramas's avatar
berenger-bramas committed
53
    typename OctreeClass::Iterator* iterArray;
54
    int leafsNumber;
55 56

    static const int SizeShape = 3*3*3;
57
    int shapeLeaf[SizeShape];
berenger-bramas's avatar
berenger-bramas committed
58

59
    const int MaxThreads;                     ///< The maximum number of threads.
60

61
    const int OctreeHeight;                   ///< The height of the given tree.
62

63 64
    const bool staticSchedule;

65 66
    const int leafLevelSeperationCriteria;

67
    unsigned int ompChunkSize = 0;
68

69
public:
70 71 72 73 74
    /** Class constructor
     * 
     * The constructor needs the octree and the kernels used for computation.
     * \param inTree the octree to work on.
     * \param inKernels the kernels to call.
75
     * \param inStaticSchedule Whether to use static or dynamic OpenMP scheduling. default is dynamic.
76 77 78
     *
     * \except An exception is thrown if one of the arguments is NULL.
     */
79
    FFmmAlgorithmThread(OctreeClass* const inTree, KernelClass* const inKernels,
80
                        const bool inStaticSchedule = false, const int inLeafLevelSeperationCriteria = 1)
81
        : tree(inTree) , kernels(nullptr), iterArray(nullptr), leafsNumber(0),
82
          MaxThreads(omp_get_max_threads()), OctreeHeight(tree->getHeight()),
83
          staticSchedule(inStaticSchedule), leafLevelSeperationCriteria(inLeafLevelSeperationCriteria) {
84
        
BRAMAS Berenger's avatar
BRAMAS Berenger committed
85
        FAssertLF(tree, "tree cannot be null");
86
        
berenger-bramas's avatar
berenger-bramas committed
87
        this->kernels = new KernelClass*[MaxThreads];
88
        #pragma omp parallel for schedule(static)
berenger-bramas's avatar
berenger-bramas committed
89
        for(int idxThread = 0 ; idxThread < MaxThreads ; ++idxThread){
90
            #pragma omp critical (InitFFmmAlgorithmThread) 
91 92 93
            {
                this->kernels[idxThread] = new KernelClass(*inKernels);
            }
94
        }
95
        
96
        FAbstractAlgorithm::setNbLevelsInTree(tree->getHeight());
97
        
BRAMAS Berenger's avatar
BRAMAS Berenger committed
98
        FLOG(FLog::Controller << "FFmmAlgorithmThread (Max Thread " << omp_get_max_threads() << ")\n");
99
        FLOG(FLog::Controller << "\t static schedule " << (staticSchedule?"TRUE":"FALSE") << ")\n");
100
    }
101
    
102 103
    /** Default destructor */
    virtual ~FFmmAlgorithmThread(){
berenger-bramas's avatar
berenger-bramas committed
104
        for(int idxThread = 0 ; idxThread < MaxThreads ; ++idxThread){
105 106
            delete this->kernels[idxThread];
        }
berenger-bramas's avatar
berenger-bramas committed
107
        delete [] this->kernels;
108
    }
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    
    
    template <class NumType>
    NumType getChunkSize(const NumType inSize) const {
        if(staticSchedule){
            return FMath::Max(NumType(1) , NumType(double(inSize)/double(omp_get_max_threads())) );
        }
        else{
            if (ompChunkSize > 0) {
                return ompChunkSize;
            }
            else {
                return FMath::Max(NumType(1) , inSize/NumType(omp_get_max_threads()*omp_get_max_threads()));
            }
        }
    }
    
    
    template <class NumType>
    void setChunkSize(const NumType size) {
        ompChunkSize = size;
    }
    
132
protected:
133
    /**
134
      * Runs the complete algorithm.
135
      */
136
    void executeCore(const unsigned operationsToProceed) override {
137 138 139 140 141 142

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

        // Count leaf
143
        leafsNumber = 0;
berenger-bramas's avatar
berenger-bramas committed
144
        typename OctreeClass::Iterator octreeIterator(tree);
145 146
        octreeIterator.gotoBottomLeft();
        do{
147
            ++leafsNumber;
148
            const FTreeCoordinate& coord = octreeIterator.getCurrentCell()->getCoordinate();
149 150 151
            ++this->shapeLeaf[(coord.getX()%3)*9 + (coord.getY()%3)*3 + (coord.getZ()%3)];

        } while(octreeIterator.moveRight());
berenger-bramas's avatar
berenger-bramas committed
152
        iterArray = new typename OctreeClass::Iterator[leafsNumber];
BRAMAS Berenger's avatar
BRAMAS Berenger committed
153
        FAssertLF(iterArray, "iterArray bad alloc");
154

155
        Timers[P2MTimer].tic();
156
        if(operationsToProceed & FFmmP2M) bottomPass();
157
        Timers[P2MTimer].tac();
158

159
        Timers[M2MTimer].tic();
160
        if(operationsToProceed & FFmmM2M) upwardPass();
161
        Timers[M2MTimer].tac();
162

163
        Timers[M2LTimer].tic();
164
        if(operationsToProceed & FFmmM2L) transferPass();
165
        Timers[M2LTimer].tac();
166

167
        Timers[L2LTimer].tic();
168
        if(operationsToProceed & FFmmL2L) downardPass();
169
        Timers[L2LTimer].tac();
170

171 172 173
        Timers[NearTimer].tic();
        if( (operationsToProceed & FFmmP2P) || (operationsToProceed & FFmmL2P) ) directPass((operationsToProceed & FFmmP2P),(operationsToProceed & FFmmL2P));
        Timers[NearTimer].tac();
174 175

        delete [] iterArray;
176
        iterArray = nullptr;
177 178
    }

berenger-bramas's avatar
berenger-bramas committed
179 180 181 182
    /////////////////////////////////////////////////////////////////////////////
    // P2M
    /////////////////////////////////////////////////////////////////////////////

183
    /** Runs the P2M kernel. */
184
    void bottomPass(){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
185
        FLOG( FLog::Controller.write("\tStart Bottom Pass\n").write(FLog::Flush) );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
186
        FLOG(FTic counterTime);
187

berenger-bramas's avatar
berenger-bramas committed
188
        typename OctreeClass::Iterator octreeIterator(tree);
189 190 191 192 193 194 195 196
        int leafs = 0;
        // Iterate on leafs
        octreeIterator.gotoBottomLeft();
        do{
            iterArray[leafs] = octreeIterator;
            ++leafs;
        } while(octreeIterator.moveRight());

197
        const int chunkSize = getChunkSize(leafs);
198

BRAMAS Berenger's avatar
BRAMAS Berenger committed
199
        FLOG(FTic computationCounter);
BRAMAS Berenger's avatar
BRAMAS Berenger committed
200
        #pragma omp parallel
201
        {
berenger-bramas's avatar
berenger-bramas committed
202
            KernelClass * const myThreadkernels = kernels[omp_get_thread_num()];
203
            #pragma omp for nowait schedule(dynamic, chunkSize)
204 205 206 207 208 209
            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());
            }
        }
BRAMAS Berenger's avatar
BRAMAS Berenger committed
210
        FLOG(computationCounter.tac() );
211

BRAMAS Berenger's avatar
BRAMAS Berenger committed
212 213
        FLOG( FLog::Controller << "\tFinished (@Bottom Pass (P2M) = "  << counterTime.tacAndElapsed() << "s)\n" );
        FLOG( FLog::Controller << "\t\t Computation : " << computationCounter.elapsed() << " s\n" );
214

215 216
    }

berenger-bramas's avatar
berenger-bramas committed
217 218 219 220
    /////////////////////////////////////////////////////////////////////////////
    // Upward
    /////////////////////////////////////////////////////////////////////////////

221
    /** Runs the M2M kernel. */
222
    void upwardPass(){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
223
        FLOG( FLog::Controller.write("\tStart Upward Pass\n").write(FLog::Flush); );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
224 225
        FLOG(FTic counterTime);
        FLOG(FTic computationCounter);
226 227

        // Start from leal level - 1
berenger-bramas's avatar
berenger-bramas committed
228
        typename OctreeClass::Iterator octreeIterator(tree);
229 230
        octreeIterator.gotoBottomLeft();
        octreeIterator.moveUp();
231

232
        for(int idxLevel = OctreeHeight - 2 ; idxLevel > FAbstractAlgorithm::lowerWorkingLevel-1 ; --idxLevel){
233 234 235
            octreeIterator.moveUp();
        }

berenger-bramas's avatar
berenger-bramas committed
236
        typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);
237 238

        // for each levels
239
        for(int idxLevel = FMath::Min(OctreeHeight - 2, FAbstractAlgorithm::lowerWorkingLevel - 1) ; idxLevel >= FAbstractAlgorithm::upperWorkingLevel ; --idxLevel ){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
240
            FLOG(FTic counterTimeLevel);
241
            int numberOfCells = 0;
242 243
            // for each cells
            do{
244 245
                iterArray[numberOfCells] = octreeIterator;
                ++numberOfCells;
246 247 248 249
            } while(octreeIterator.moveRight());
            avoidGotoLeftIterator.moveUp();
            octreeIterator = avoidGotoLeftIterator;// equal octreeIterator.moveUp(); octreeIterator.gotoLeft();

250
            const int chunkSize = getChunkSize(numberOfCells);
251

BRAMAS Berenger's avatar
BRAMAS Berenger committed
252
            FLOG(computationCounter.tic());
253
            #pragma omp parallel
254
            {
berenger-bramas's avatar
berenger-bramas committed
255
                KernelClass * const myThreadkernels = kernels[omp_get_thread_num()];
256
                #pragma omp for nowait  schedule(dynamic, chunkSize)
257
                for(int idxCell = 0 ; idxCell < numberOfCells ; ++idxCell){
258 259
                    // We need the current cell and the child
                    // child is an array (of 8 child) that may be null
260
                    myThreadkernels->M2M( iterArray[idxCell].getCurrentCell() , iterArray[idxCell].getCurrentChild(), idxLevel);
261 262
                }
            }
berenger-bramas's avatar
berenger-bramas committed
263

BRAMAS Berenger's avatar
BRAMAS Berenger committed
264
            FLOG(computationCounter.tac());
BRAMAS Berenger's avatar
BRAMAS Berenger committed
265
            FLOG( FLog::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
266 267
        }

berenger-bramas's avatar
berenger-bramas committed
268

BRAMAS Berenger's avatar
BRAMAS Berenger committed
269 270
        FLOG( FLog::Controller << "\tFinished (@Upward Pass (M2M) = "  << counterTime.tacAndElapsed() << "s)\n" );
        FLOG( FLog::Controller << "\t\t Computation : " << computationCounter.cumulated() << " s\n" );
271

272 273
    }

berenger-bramas's avatar
berenger-bramas committed
274
    /////////////////////////////////////////////////////////////////////////////
275
    // Transfer
berenger-bramas's avatar
berenger-bramas committed
276 277
    /////////////////////////////////////////////////////////////////////////////

278
    /** Runs the M2L kernel. */
279
    void transferPass(){
280

BRAMAS Berenger's avatar
BRAMAS Berenger committed
281
        FLOG( FLog::Controller.write("\tStart Downward Pass (M2L)\n").write(FLog::Flush); );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
282 283
        FLOG(FTic counterTime);
        FLOG(FTic computationCounter);
berenger-bramas's avatar
berenger-bramas committed
284

285 286
        typename OctreeClass::Iterator octreeIterator(tree);
        octreeIterator.moveDown();
287

288
        for(int idxLevel = 2 ; idxLevel < FAbstractAlgorithm::upperWorkingLevel ; ++idxLevel){
289 290 291
            octreeIterator.moveDown();
        }

292 293 294
        typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);

        // for each levels
295
        for(int idxLevel = FAbstractAlgorithm::upperWorkingLevel ; idxLevel < FAbstractAlgorithm::lowerWorkingLevel ; ++idxLevel ){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
296
            FLOG(FTic counterTimeLevel);
297
            const int separationCriteria = (idxLevel != FAbstractAlgorithm::lowerWorkingLevel-1 ? 1 : leafLevelSeperationCriteria);
298 299 300 301 302 303 304 305 306
            int numberOfCells = 0;
            // for each cells
            do{
                iterArray[numberOfCells] = octreeIterator;
                ++numberOfCells;
            } while(octreeIterator.moveRight());
            avoidGotoLeftIterator.moveDown();
            octreeIterator = avoidGotoLeftIterator;

307
            const int chunkSize = getChunkSize(numberOfCells);
308

BRAMAS Berenger's avatar
BRAMAS Berenger committed
309
            FLOG(computationCounter.tic());
310
            #pragma omp parallel
311 312
            {
                KernelClass * const myThreadkernels = kernels[omp_get_thread_num()];
313
                const CellClass* neighbors[343];
314

315
                #pragma omp for  schedule(dynamic, chunkSize) nowait
316
                for(int idxCell = 0 ; idxCell < numberOfCells ; ++idxCell){
317
                    const int counter = tree->getInteractionNeighbors(neighbors, iterArray[idxCell].getCurrentGlobalCoordinate(), idxLevel, separationCriteria);
318
                    if(counter) myThreadkernels->M2L( iterArray[idxCell].getCurrentCell() , neighbors, counter, idxLevel);
319
                }
320 321

                myThreadkernels->finishedLevelM2L(idxLevel);
322
            }
BRAMAS Berenger's avatar
BRAMAS Berenger committed
323
            FLOG(computationCounter.tac());
BRAMAS Berenger's avatar
BRAMAS Berenger committed
324
            FLOG( FLog::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
325 326
        }

BRAMAS Berenger's avatar
BRAMAS Berenger committed
327 328
        FLOG( FLog::Controller << "\tFinished (@Downward Pass (M2L) = "  << counterTime.tacAndElapsed() << "s)\n" );
        FLOG( FLog::Controller << "\t\t Computation : " << computationCounter.cumulated() << " s\n" );
329
    }
berenger-bramas's avatar
berenger-bramas committed
330

331
    /////////////////////////////////////////////////////////////////////////////
COULAUD Olivier's avatar
COULAUD Olivier committed
332
    // Downward
333 334
    /////////////////////////////////////////////////////////////////////////////

335 336
    /** Runs the L2L kernel. */
    void downardPass(){
337

BRAMAS Berenger's avatar
BRAMAS Berenger committed
338
        FLOG( FLog::Controller.write("\tStart Downward Pass (L2L)\n").write(FLog::Flush); );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
339 340
        FLOG(FTic counterTime);
        FLOG(FTic computationCounter);
341 342 343 344

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

345
        for(int idxLevel = 2 ; idxLevel < FAbstractAlgorithm::upperWorkingLevel ; ++idxLevel){
346 347 348
            octreeIterator.moveDown();
        }

349 350
        typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);

351
        const int heightMinusOne = FAbstractAlgorithm::lowerWorkingLevel - 1;
COULAUD Olivier's avatar
COULAUD Olivier committed
352
        // for each levels excepted leaf level
353
        for(int idxLevel = FAbstractAlgorithm::upperWorkingLevel ; idxLevel < heightMinusOne ; ++idxLevel ){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
354
            FLOG(FTic counterTimeLevel);
355 356 357 358 359 360 361 362 363
            int numberOfCells = 0;
            // for each cells
            do{
                iterArray[numberOfCells] = octreeIterator;
                ++numberOfCells;
            } while(octreeIterator.moveRight());
            avoidGotoLeftIterator.moveDown();
            octreeIterator = avoidGotoLeftIterator;

BRAMAS Berenger's avatar
BRAMAS Berenger committed
364
            FLOG(computationCounter.tic());
365
            const int chunkSize = getChunkSize(numberOfCells);
366
            #pragma omp parallel
367 368
            {
                KernelClass * const myThreadkernels = kernels[omp_get_thread_num()];
369
                #pragma omp for nowait schedule(dynamic, chunkSize)
370 371
                for(int idxCell = 0 ; idxCell < numberOfCells ; ++idxCell){
                    myThreadkernels->L2L( iterArray[idxCell].getCurrentCell() , iterArray[idxCell].getCurrentChild(), idxLevel);
372 373
                }
            }
BRAMAS Berenger's avatar
BRAMAS Berenger committed
374
            FLOG(computationCounter.tac());
BRAMAS Berenger's avatar
BRAMAS Berenger committed
375
            FLOG( FLog::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
376 377
        }

BRAMAS Berenger's avatar
BRAMAS Berenger committed
378 379
        FLOG( FLog::Controller << "\tFinished (@Downward Pass (L2L) = "  << counterTime.tacAndElapsed() << "s)\n" );
        FLOG( FLog::Controller << "\t\t Computation : " << computationCounter.cumulated() << " s\n" );
380 381
    }

382 383


berenger-bramas's avatar
berenger-bramas committed
384 385 386 387
    /////////////////////////////////////////////////////////////////////////////
    // Direct
    /////////////////////////////////////////////////////////////////////////////

388 389 390 391 392
    /** Runs the P2P & L2P kernels.
      *
     * \param p2pEnabled Run the P2P kernel.
     * \param l2pEnabled Run the L2P kernel.
     */
393
    void directPass(const bool p2pEnabled, const bool l2pEnabled){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
394
        FLOG( FLog::Controller.write("\tStart Direct Pass\n").write(FLog::Flush); );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
395 396 397
        FLOG(FTic counterTime);
        FLOG(FTic computationCounter);
        FLOG(FTic computationCounterP2P);
398

399
        omp_lock_t lockShape[SizeShape];
400
        for(int idxShape = 0 ; idxShape < SizeShape ; ++idxShape){
401
            omp_init_lock(&lockShape[idxShape]);
402 403
        }

404 405 406
        struct LeafData{
            MortonIndex index;
            CellClass* cell;
berenger-bramas's avatar
berenger-bramas committed
407 408
            ContainerClass* targets;
            ContainerClass* sources;
409 410 411
        };
        LeafData* const leafsDataArray = new LeafData[this->leafsNumber];

412
        const int LeafIndex = OctreeHeight - 1;
413 414 415 416 417 418 419

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

420
        #pragma omp parallel
421
        {
422

423 424 425
            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)));
426
            const int end = (tempEnd > this->leafsNumber ? this->leafsNumber : tempEnd);
427

berenger-bramas's avatar
berenger-bramas committed
428
            typename OctreeClass::Iterator octreeIterator(tree);
429
            octreeIterator.gotoBottomLeft();
430 431 432 433 434

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

435
            // for each leafs
436
            for(int idxMyLeafs = start ; idxMyLeafs < end ; ++idxMyLeafs){
437
                //iterArray[leafs] = octreeIterator;
438
                //++leafs;
439
                const FTreeCoordinate& coord = octreeIterator.getCurrentGlobalCoordinate();
440
                const int shapePosition = (coord.getX()%3)*9 + (coord.getY()%3)*3 + (coord.getZ()%3);
441

442 443 444
                omp_set_lock(&lockShape[shapePosition]);
                const int positionToWork = startPosAtShape[shapePosition]++;
                omp_unset_lock(&lockShape[shapePosition]);
445

COULAUD Olivier's avatar
COULAUD Olivier committed
446 447
                leafsDataArray[positionToWork].index   = octreeIterator.getCurrentGlobalIndex();
                leafsDataArray[positionToWork].cell    = octreeIterator.getCurrentCell();
448 449
                leafsDataArray[positionToWork].targets = octreeIterator.getCurrentListTargets();
                leafsDataArray[positionToWork].sources = octreeIterator.getCurrentListSrc();
450

451 452
                octreeIterator.moveRight();
            }
453

454
            #pragma omp barrier
455

BRAMAS Berenger's avatar
BRAMAS Berenger committed
456
            FLOG(if(!omp_get_thread_num()) computationCounter.tic());
457

berenger-bramas's avatar
berenger-bramas committed
458
            KernelClass& myThreadkernels = (*kernels[omp_get_thread_num()]);
459
            // There is a maximum of 26 neighbors
berenger-bramas's avatar
berenger-bramas committed
460
            ContainerClass* neighbors[27];
461
            int previous = 0;
462 463

            for(int idxShape = 0 ; idxShape < SizeShape ; ++idxShape){
464
                const int endAtThisShape = this->shapeLeaf[idxShape] + previous;
465
                const int chunkSize = getChunkSize(endAtThisShape-previous);
466
                #pragma omp for schedule(dynamic, chunkSize)
467 468
                for(int idxLeafs = previous ; idxLeafs < endAtThisShape ; ++idxLeafs){
                    LeafData& currentIter = leafsDataArray[idxLeafs];
469 470 471 472 473 474 475 476 477 478 479
                    if(l2pEnabled){
                        myThreadkernels.L2P(currentIter.cell, currentIter.targets);
                    }
                    if(p2pEnabled){
                        // need the current particles and neighbors particles
                        FLOG(if(!omp_get_thread_num()) computationCounterP2P.tic());
                        const int counter = tree->getLeafsNeighbors(neighbors, currentIter.cell->getCoordinate(), LeafIndex);
                        myThreadkernels.P2P(currentIter.cell->getCoordinate(), currentIter.targets,
                                            currentIter.sources, neighbors, counter);
                        FLOG(if(!omp_get_thread_num()) computationCounterP2P.tac());
                    }
480
                }
481 482

                previous = endAtThisShape;
483 484
            }
        }
485

BRAMAS Berenger's avatar
BRAMAS Berenger committed
486
        FLOG(computationCounter.tac());
487

488
        delete [] leafsDataArray;
489 490 491 492
        for(int idxShape = 0 ; idxShape < SizeShape ; ++idxShape){
            omp_destroy_lock(&lockShape[idxShape]);
        }

493

BRAMAS Berenger's avatar
BRAMAS Berenger committed
494
        FLOG( FLog::Controller << "\tFinished (@Direct Pass (L2P + P2P) = "  << counterTime.tacAndElapsed() << "s)\n" );
COULAUD Olivier's avatar
COULAUD Olivier committed
495 496
        FLOG( FLog::Controller << "\t\t Computation L2P + P2P : " << computationCounter.cumulated()    << " s\n" );
        FLOG( FLog::Controller << "\t\t Computation P2P :       " << computationCounterP2P.cumulated() << " s\n" );
497

498 499 500 501 502 503
    }

};


#endif //FFMMALGORITHMTHREAD_HPP