FFmmAlgorithmThread.hpp 20.3 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
#include "FP2PExclusion.hpp"
31 32 33 34

#include <omp.h>

/**
35 36
* \author Berenger Bramas (berenger.bramas@inria.fr)
* \brief Implements an FMM algorithm threaded using OpenMP.
37
*
38
* Please read the license
39
*
40 41 42 43
* 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.
44
*
berenger-bramas's avatar
berenger-bramas committed
45
* When using this algorithm the P2P is thread safe.
46 47
*
* This class does not deallocate pointers given to its constructor.
48
*/
49
template<class OctreeClass, class CellClass, class ContainerClass, class KernelClass, class LeafClass, class P2PExclusionClass = FP2PMiddleExclusion>
50
class FFmmAlgorithmThread : public FAbstractAlgorithm, public FAlgorithmTimers{
51 52
    OctreeClass* const tree;                  ///< The octree to work on.
    KernelClass** kernels;                    ///< The kernels.
53

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

57
    static const int SizeShape = P2PExclusionClass::SizeShape;
58
    int shapeLeaf[SizeShape];
berenger-bramas's avatar
berenger-bramas committed
59

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

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

64
    int userChunkSize;
65

66
    const int leafLevelSeparationCriteria;
67

68
public:
69 70 71 72 73
    /** 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.
74 75
     * \param inUserChunckSize To specify the chunck size in the loops (-1 is static, 0 is N/p^2, otherwise it
     * directly used as the number of item to proceed together), default is 10
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 int inUserChunkSize = 10, const int inLeafLevelSeperationCriteria = 1)
81
        : tree(inTree) , kernels(nullptr), iterArray(nullptr), leafsNumber(0),
82
          MaxThreads(omp_get_max_threads()), OctreeHeight(tree->getHeight()),
83
          userChunkSize(inUserChunkSize), leafLevelSeparationCriteria(inLeafLevelSeperationCriteria) {
BRAMAS Berenger's avatar
BRAMAS Berenger committed
84
        FAssertLF(tree, "tree cannot be null");
85 86
        FAssertLF(leafLevelSeparationCriteria < 3, "Separation criteria should be < 3");
        FAssertLF(0 < userChunkSize, "Chunk size should be > 0");
87
        
berenger-bramas's avatar
berenger-bramas committed
88
        this->kernels = new KernelClass*[MaxThreads];
89 90 91
        #pragma omp parallel num_threads(MaxThreads)
        {
            #pragma omp critical (InitFFmmAlgorithmThread)
92
            {
93
                this->kernels[omp_get_thread_num()] = new KernelClass(*inKernels);
94
            }
95
        }
96
        
97
        FAbstractAlgorithm::setNbLevelsInTree(tree->getHeight());
98
        
BRAMAS Berenger's avatar
BRAMAS Berenger committed
99
        FLOG(FLog::Controller << "FFmmAlgorithmThread (Max Thread " << omp_get_max_threads() << ")\n");
100
        FLOG(FLog::Controller << "\t static schedule " << (userChunkSize == -1 ? "static" : (userChunkSize == 0 ? "N/p^2" : std::to_string(userChunkSize))) << ")\n");
101
    }
102
    
103 104
    /** Default destructor */
    virtual ~FFmmAlgorithmThread(){
berenger-bramas's avatar
berenger-bramas committed
105
        for(int idxThread = 0 ; idxThread < MaxThreads ; ++idxThread){
106 107
            delete this->kernels[idxThread];
        }
berenger-bramas's avatar
berenger-bramas committed
108
        delete [] this->kernels;
109
    }
110 111 112
    
    template <class NumType>
    NumType getChunkSize(const NumType inSize) const {
113
        if(userChunkSize <= -1){
114
            return FMath::Max(NumType(1) , NumType(double(inSize)/double(omp_get_max_threads())) );
115 116 117 118
        } else if(userChunkSize == 0){
            return FMath::Max(NumType(1) , inSize/NumType(omp_get_max_threads()*omp_get_max_threads()));
        } else {
            return userChunkSize;
119
        }
120
    }    
121 122 123
    
    template <class NumType>
    void setChunkSize(const NumType size) {
124
            userChunkSize = size;
125 126
    }
    
127
protected:
128
    /**
129
      * Runs the complete algorithm.
130
      */
131
    void executeCore(const unsigned operationsToProceed) override {
132 133 134 135 136 137

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

        // Count leaf
138
        leafsNumber = 0;
berenger-bramas's avatar
berenger-bramas committed
139
        typename OctreeClass::Iterator octreeIterator(tree);
140 141
        octreeIterator.gotoBottomLeft();
        do{
142
            ++leafsNumber;
143
            const FTreeCoordinate& coord = octreeIterator.getCurrentCell()->getCoordinate();
144
            ++this->shapeLeaf[P2PExclusionClass::GetShapeIdx(coord)];
145 146

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

150
        Timers[P2MTimer].tic();
151
        if(operationsToProceed & FFmmP2M) bottomPass();
152
        Timers[P2MTimer].tac();
153

154
        Timers[M2MTimer].tic();
155
        if(operationsToProceed & FFmmM2M) upwardPass();
156
        Timers[M2MTimer].tac();
157

158
        Timers[M2LTimer].tic();
159
        if(operationsToProceed & FFmmM2L) transferPass();
160
        Timers[M2LTimer].tac();
161

162
        Timers[L2LTimer].tic();
163
        if(operationsToProceed & FFmmL2L) downardPass();
164
        Timers[L2LTimer].tac();
165

166 167 168
        Timers[NearTimer].tic();
        if( (operationsToProceed & FFmmP2P) || (operationsToProceed & FFmmL2P) ) directPass((operationsToProceed & FFmmP2P),(operationsToProceed & FFmmL2P));
        Timers[NearTimer].tac();
169 170

        delete [] iterArray;
171
        iterArray = nullptr;
172 173
    }

berenger-bramas's avatar
berenger-bramas committed
174 175 176 177
    /////////////////////////////////////////////////////////////////////////////
    // P2M
    /////////////////////////////////////////////////////////////////////////////

178
    /** Runs the P2M kernel. */
179
    void bottomPass(){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
180
        FLOG( FLog::Controller.write("\tStart Bottom Pass\n").write(FLog::Flush) );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
181
        FLOG(FTic counterTime);
182

berenger-bramas's avatar
berenger-bramas committed
183
        typename OctreeClass::Iterator octreeIterator(tree);
184 185 186 187 188 189 190 191
        int leafs = 0;
        // Iterate on leafs
        octreeIterator.gotoBottomLeft();
        do{
            iterArray[leafs] = octreeIterator;
            ++leafs;
        } while(octreeIterator.moveRight());

192
        const int chunkSize = getChunkSize(leafs);
193

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

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

210 211
    }

berenger-bramas's avatar
berenger-bramas committed
212 213 214 215
    /////////////////////////////////////////////////////////////////////////////
    // Upward
    /////////////////////////////////////////////////////////////////////////////

216
    /** Runs the M2M kernel. */
217
    void upwardPass(){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
218
        FLOG( FLog::Controller.write("\tStart Upward Pass\n").write(FLog::Flush); );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
219 220
        FLOG(FTic counterTime);
        FLOG(FTic computationCounter);
221 222

        // Start from leal level - 1
berenger-bramas's avatar
berenger-bramas committed
223
        typename OctreeClass::Iterator octreeIterator(tree);
224 225
        octreeIterator.gotoBottomLeft();
        octreeIterator.moveUp();
226

227
        for(int idxLevel = OctreeHeight - 2 ; idxLevel > FAbstractAlgorithm::lowerWorkingLevel-1 ; --idxLevel){
228 229 230
            octreeIterator.moveUp();
        }

berenger-bramas's avatar
berenger-bramas committed
231
        typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);
232 233

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

245
            const int chunkSize = getChunkSize(numberOfCells);
246

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

BRAMAS Berenger's avatar
BRAMAS Berenger committed
259
            FLOG(computationCounter.tac());
BRAMAS Berenger's avatar
BRAMAS Berenger committed
260
            FLOG( FLog::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
261 262
        }

berenger-bramas's avatar
berenger-bramas committed
263

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

267 268
    }

berenger-bramas's avatar
berenger-bramas committed
269
    /////////////////////////////////////////////////////////////////////////////
270
    // Transfer
berenger-bramas's avatar
berenger-bramas committed
271
    /////////////////////////////////////////////////////////////////////////////
272 273 274 275 276 277 278 279 280 281
	/** M2L  */
  void transferPass(){
#ifdef SCALFMM_USE_EZTRACE
    eztrace_start();
#endif
    this->transferPassWithFinalize() ;
#ifdef SCALFMM_USE_EZTRACE
    eztrace_stop();
#endif
  }
berenger-bramas's avatar
berenger-bramas committed
282

283
    /** Runs the M2L kernel. */
284
    void transferPassWithFinalize(){
285

BRAMAS Berenger's avatar
BRAMAS Berenger committed
286
        FLOG( FLog::Controller.write("\tStart Downward Pass (M2L)\n").write(FLog::Flush); );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
287 288
        FLOG(FTic counterTime);
        FLOG(FTic computationCounter);
berenger-bramas's avatar
berenger-bramas committed
289

290 291
        typename OctreeClass::Iterator octreeIterator(tree);
        octreeIterator.moveDown();
292

293
        for(int idxLevel = 2 ; idxLevel < FAbstractAlgorithm::upperWorkingLevel ; ++idxLevel){
294 295 296
            octreeIterator.moveDown();
        }

297 298 299
        typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);

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

312
            const int chunkSize = getChunkSize(numberOfCells);
313

BRAMAS Berenger's avatar
BRAMAS Berenger committed
314
            FLOG(computationCounter.tic());
315
            #pragma omp parallel
316 317
            {
                KernelClass * const myThreadkernels = kernels[omp_get_thread_num()];
318
                const CellClass* neighbors[343];
319

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

                myThreadkernels->finishedLevelM2L(idxLevel);
327
            }  //Synchro end of parallel section
BRAMAS Berenger's avatar
BRAMAS Berenger committed
328
            FLOG(computationCounter.tac());
BRAMAS Berenger's avatar
BRAMAS Berenger committed
329
            FLOG( FLog::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
330 331
        }

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

336
    /////////////////////////////////////////////////////////////////////////////
COULAUD Olivier's avatar
COULAUD Olivier committed
337
    // Downward
338 339
    /////////////////////////////////////////////////////////////////////////////

340 341
    /** Runs the L2L kernel. */
    void downardPass(){
342

BRAMAS Berenger's avatar
BRAMAS Berenger committed
343
        FLOG( FLog::Controller.write("\tStart Downward Pass (L2L)\n").write(FLog::Flush); );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
344 345
        FLOG(FTic counterTime);
        FLOG(FTic computationCounter);
346 347 348 349

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

350
        for(int idxLevel = 2 ; idxLevel < FAbstractAlgorithm::upperWorkingLevel ; ++idxLevel){
351 352 353
            octreeIterator.moveDown();
        }

354 355
        typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);

356
        const int heightMinusOne = FAbstractAlgorithm::lowerWorkingLevel - 1;
COULAUD Olivier's avatar
COULAUD Olivier committed
357
        // for each levels excepted leaf level
358
        for(int idxLevel = FAbstractAlgorithm::upperWorkingLevel ; idxLevel < heightMinusOne ; ++idxLevel ){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
359
            FLOG(FTic counterTimeLevel);
360 361 362 363 364 365 366 367 368
            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
369
            FLOG(computationCounter.tic());
370
            const int chunkSize = getChunkSize(numberOfCells);
371
            #pragma omp parallel
372 373
            {
                KernelClass * const myThreadkernels = kernels[omp_get_thread_num()];
374
                #pragma omp for nowait schedule(dynamic, chunkSize)
375 376
                for(int idxCell = 0 ; idxCell < numberOfCells ; ++idxCell){
                    myThreadkernels->L2L( iterArray[idxCell].getCurrentCell() , iterArray[idxCell].getCurrentChild(), idxLevel);
377 378
                }
            }
BRAMAS Berenger's avatar
BRAMAS Berenger committed
379
            FLOG(computationCounter.tac());
BRAMAS Berenger's avatar
BRAMAS Berenger committed
380
            FLOG( FLog::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
381 382
        }

BRAMAS Berenger's avatar
BRAMAS Berenger committed
383 384
        FLOG( FLog::Controller << "\tFinished (@Downward Pass (L2L) = "  << counterTime.tacAndElapsed() << "s)\n" );
        FLOG( FLog::Controller << "\t\t Computation : " << computationCounter.cumulated() << " s\n" );
385 386
    }

387 388


berenger-bramas's avatar
berenger-bramas committed
389 390 391 392
    /////////////////////////////////////////////////////////////////////////////
    // Direct
    /////////////////////////////////////////////////////////////////////////////

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

404
        omp_lock_t lockShape[SizeShape];
405
        for(int idxShape = 0 ; idxShape < SizeShape ; ++idxShape){
406
            omp_init_lock(&lockShape[idxShape]);
407 408
        }

409 410 411
        struct LeafData{
            MortonIndex index;
            CellClass* cell;
berenger-bramas's avatar
berenger-bramas committed
412 413
            ContainerClass* targets;
            ContainerClass* sources;
414 415 416
        };
        LeafData* const leafsDataArray = new LeafData[this->leafsNumber];

417
        const int LeafIndex = OctreeHeight - 1;
418 419 420 421 422 423 424

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

425
        #pragma omp parallel
426
        {
427

428 429 430
            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)));
431
            const int end = (tempEnd > this->leafsNumber ? this->leafsNumber : tempEnd);
432

berenger-bramas's avatar
berenger-bramas committed
433
            typename OctreeClass::Iterator octreeIterator(tree);
434
            octreeIterator.gotoBottomLeft();
435 436 437 438 439

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

440
            // for each leafs
441
            for(int idxMyLeafs = start ; idxMyLeafs < end ; ++idxMyLeafs){
442
                //iterArray[leafs] = octreeIterator;
443
                //++leafs;
444
                const FTreeCoordinate& coord = octreeIterator.getCurrentGlobalCoordinate();
445
                const int shapePosition = P2PExclusionClass::GetShapeIdx(coord);
446

447 448 449
                omp_set_lock(&lockShape[shapePosition]);
                const int positionToWork = startPosAtShape[shapePosition]++;
                omp_unset_lock(&lockShape[shapePosition]);
450

COULAUD Olivier's avatar
COULAUD Olivier committed
451 452
                leafsDataArray[positionToWork].index   = octreeIterator.getCurrentGlobalIndex();
                leafsDataArray[positionToWork].cell    = octreeIterator.getCurrentCell();
453 454
                leafsDataArray[positionToWork].targets = octreeIterator.getCurrentListTargets();
                leafsDataArray[positionToWork].sources = octreeIterator.getCurrentListSrc();
455

456 457
                octreeIterator.moveRight();
            }
458

459
            #pragma omp barrier
460

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

berenger-bramas's avatar
berenger-bramas committed
463
            KernelClass& myThreadkernels = (*kernels[omp_get_thread_num()]);
464
            // There is a maximum of 26 neighbors
berenger-bramas's avatar
berenger-bramas committed
465
            ContainerClass* neighbors[27];
466
            int previous = 0;
467 468

            for(int idxShape = 0 ; idxShape < SizeShape ; ++idxShape){
469
                const int endAtThisShape = this->shapeLeaf[idxShape] + previous;
470
                const int chunkSize = getChunkSize(endAtThisShape-previous);
471
                #pragma omp for schedule(dynamic, chunkSize)
472 473
                for(int idxLeafs = previous ; idxLeafs < endAtThisShape ; ++idxLeafs){
                    LeafData& currentIter = leafsDataArray[idxLeafs];
474 475 476 477 478 479 480 481 482 483 484
                    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());
                    }
485
                }
486 487

                previous = endAtThisShape;
488 489
            }
        }
490

BRAMAS Berenger's avatar
BRAMAS Berenger committed
491
        FLOG(computationCounter.tac());
492

493
        delete [] leafsDataArray;
494 495 496 497
        for(int idxShape = 0 ; idxShape < SizeShape ; ++idxShape){
            omp_destroy_lock(&lockShape[idxShape]);
        }

498

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

503 504 505 506 507 508
    }

};


#endif //FFMMALGORITHMTHREAD_HPP