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

FFmmAlgorithmThread.hpp 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 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
    int userChunkSize;
64

65
    const int leafLevelSeparationCriteria;
66

67
public:
68 69 70 71 72
    /** 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.
73 74
     * \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
75 76 77
     *
     * \except An exception is thrown if one of the arguments is NULL.
     */
78
    FFmmAlgorithmThread(OctreeClass* const inTree, KernelClass* const inKernels,
79
                        const int inUserChunkSize = 10, const int inLeafLevelSeperationCriteria = 1)
80
        : tree(inTree) , kernels(nullptr), iterArray(nullptr), leafsNumber(0),
81
          MaxThreads(omp_get_max_threads()), OctreeHeight(tree->getHeight()),
82
          userChunkSize(inUserChunkSize), leafLevelSeparationCriteria(inLeafLevelSeperationCriteria) {
BRAMAS Berenger's avatar
BRAMAS Berenger committed
83
        FAssertLF(tree, "tree cannot be null");
84 85
        FAssertLF(leafLevelSeparationCriteria < 3, "Separation criteria should be < 3");
        FAssertLF(0 < userChunkSize, "Chunk size should be > 0");
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 " << (userChunkSize == -1 ? "static" : (userChunkSize == 0 ? "N/p^2" : std::to_string(userChunkSize))) << ")\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
    
    template <class NumType>
    NumType getChunkSize(const NumType inSize) const {
112
        if(userChunkSize <= -1){
113
            return FMath::Max(NumType(1) , NumType(double(inSize)/double(omp_get_max_threads())) );
114 115 116 117
        } else if(userChunkSize == 0){
            return FMath::Max(NumType(1) , inSize/NumType(omp_get_max_threads()*omp_get_max_threads()));
        } else {
            return userChunkSize;
118
        }
119
    }    
120 121 122
    
    template <class NumType>
    void setChunkSize(const NumType size) {
123
            userChunkSize = size;
124 125
    }
    
126
protected:
127
    /**
128
      * Runs the complete algorithm.
129
      */
130
    void executeCore(const unsigned operationsToProceed) override {
131 132 133 134 135 136

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

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

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

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

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

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

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

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

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

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

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

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

191
        const int chunkSize = getChunkSize(leafs);
192

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

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

209 210
    }

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

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

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

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

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

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

244
            const int chunkSize = getChunkSize(numberOfCells);
245

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

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

berenger-bramas's avatar
berenger-bramas committed
262

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

266 267
    }

berenger-bramas's avatar
berenger-bramas committed
268
    /////////////////////////////////////////////////////////////////////////////
269
    // Transfer
berenger-bramas's avatar
berenger-bramas committed
270
    /////////////////////////////////////////////////////////////////////////////
271 272 273 274 275 276 277 278 279 280
	/** 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
281

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

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

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

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

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

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

311
            const int chunkSize = getChunkSize(numberOfCells);
312

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

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

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

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

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

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

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

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

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

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

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

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

386 387


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

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

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

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

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

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

424
        #pragma omp parallel
425
        {
426

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

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

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

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

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

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

455 456
                octreeIterator.moveRight();
            }
457

458
            #pragma omp barrier
459

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

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

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

                previous = endAtThisShape;
487 488
            }
        }
489

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

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

497

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

502 503 504 505 506 507
    }

};


#endif //FFMMALGORITHMTHREAD_HPP