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 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 66
    const int leafLevelSeperationCriteria;

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), leafLevelSeperationCriteria(inLeafLevelSeperationCriteria) {
BRAMAS Berenger's avatar
BRAMAS Berenger committed
83
        FAssertLF(tree, "tree cannot be null");
84
        
berenger-bramas's avatar
berenger-bramas committed
85
        this->kernels = new KernelClass*[MaxThreads];
86
        #pragma omp parallel for schedule(static)
berenger-bramas's avatar
berenger-bramas committed
87
        for(int idxThread = 0 ; idxThread < MaxThreads ; ++idxThread){
88
            #pragma omp critical (InitFFmmAlgorithmThread) 
89 90 91
            {
                this->kernels[idxThread] = new KernelClass(*inKernels);
            }
92
        }
93
        
94
        FAbstractAlgorithm::setNbLevelsInTree(tree->getHeight());
95
        
BRAMAS Berenger's avatar
BRAMAS Berenger committed
96
        FLOG(FLog::Controller << "FFmmAlgorithmThread (Max Thread " << omp_get_max_threads() << ")\n");
97
        FLOG(FLog::Controller << "\t static schedule " << (userChunkSize == -1 ? "static" : (userChunkSize == 0 ? "N/p^2" : std::to_string(userChunkSize))) << ")\n");
98
    }
99
    
100 101
    /** Default destructor */
    virtual ~FFmmAlgorithmThread(){
berenger-bramas's avatar
berenger-bramas committed
102
        for(int idxThread = 0 ; idxThread < MaxThreads ; ++idxThread){
103 104
            delete this->kernels[idxThread];
        }
berenger-bramas's avatar
berenger-bramas committed
105
        delete [] this->kernels;
106
    }
107 108 109
    
    template <class NumType>
    NumType getChunkSize(const NumType inSize) const {
110
        if(userChunkSize <= -1){
111
            return FMath::Max(NumType(1) , NumType(double(inSize)/double(omp_get_max_threads())) );
112 113 114 115
        } else if(userChunkSize == 0){
            return FMath::Max(NumType(1) , inSize/NumType(omp_get_max_threads()*omp_get_max_threads()));
        } else {
            return userChunkSize;
116
        }
117
    }    
118 119 120
    
    template <class NumType>
    void setChunkSize(const NumType size) {
121
            userChunkSize = size;
122 123
    }
    
124
protected:
125
    /**
126
      * Runs the complete algorithm.
127
      */
128
    void executeCore(const unsigned operationsToProceed) override {
129 130 131 132 133 134

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

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

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

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

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

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

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

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

        delete [] iterArray;
168
        iterArray = nullptr;
169 170
    }

berenger-bramas's avatar
berenger-bramas committed
171 172 173 174
    /////////////////////////////////////////////////////////////////////////////
    // P2M
    /////////////////////////////////////////////////////////////////////////////

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

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

189
        const int chunkSize = getChunkSize(leafs);
190

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

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

207 208
    }

berenger-bramas's avatar
berenger-bramas committed
209 210 211 212
    /////////////////////////////////////////////////////////////////////////////
    // Upward
    /////////////////////////////////////////////////////////////////////////////

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

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

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

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

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

242
            const int chunkSize = getChunkSize(numberOfCells);
243

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

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

berenger-bramas's avatar
berenger-bramas committed
260

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

264 265
    }

berenger-bramas's avatar
berenger-bramas committed
266
    /////////////////////////////////////////////////////////////////////////////
267
    // Transfer
berenger-bramas's avatar
berenger-bramas committed
268 269
    /////////////////////////////////////////////////////////////////////////////

270
    /** Runs the M2L kernel. */
271
    void transferPass(){
272

BRAMAS Berenger's avatar
BRAMAS Berenger committed
273
        FLOG( FLog::Controller.write("\tStart Downward Pass (M2L)\n").write(FLog::Flush); );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
274 275
        FLOG(FTic counterTime);
        FLOG(FTic computationCounter);
berenger-bramas's avatar
berenger-bramas committed
276

277 278
        typename OctreeClass::Iterator octreeIterator(tree);
        octreeIterator.moveDown();
279

280
        for(int idxLevel = 2 ; idxLevel < FAbstractAlgorithm::upperWorkingLevel ; ++idxLevel){
281 282 283
            octreeIterator.moveDown();
        }

284 285 286
        typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);

        // for each levels
287
        for(int idxLevel = FAbstractAlgorithm::upperWorkingLevel ; idxLevel < FAbstractAlgorithm::lowerWorkingLevel ; ++idxLevel ){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
288
            FLOG(FTic counterTimeLevel);
289
            const int separationCriteria = (idxLevel != FAbstractAlgorithm::lowerWorkingLevel-1 ? 1 : leafLevelSeperationCriteria);
290 291 292 293 294 295 296 297 298
            int numberOfCells = 0;
            // for each cells
            do{
                iterArray[numberOfCells] = octreeIterator;
                ++numberOfCells;
            } while(octreeIterator.moveRight());
            avoidGotoLeftIterator.moveDown();
            octreeIterator = avoidGotoLeftIterator;

299
            const int chunkSize = getChunkSize(numberOfCells);
300

BRAMAS Berenger's avatar
BRAMAS Berenger committed
301
            FLOG(computationCounter.tic());
302
            #pragma omp parallel
303 304
            {
                KernelClass * const myThreadkernels = kernels[omp_get_thread_num()];
305
                const CellClass* neighbors[343];
306

307
                #pragma omp for  schedule(dynamic, chunkSize) nowait
308
                for(int idxCell = 0 ; idxCell < numberOfCells ; ++idxCell){
309
                    const int counter = tree->getInteractionNeighbors(neighbors, iterArray[idxCell].getCurrentGlobalCoordinate(), idxLevel, separationCriteria);
310
                    if(counter) myThreadkernels->M2L( iterArray[idxCell].getCurrentCell() , neighbors, counter, idxLevel);
311
                }
312 313

                myThreadkernels->finishedLevelM2L(idxLevel);
314
            }
BRAMAS Berenger's avatar
BRAMAS Berenger committed
315
            FLOG(computationCounter.tac());
BRAMAS Berenger's avatar
BRAMAS Berenger committed
316
            FLOG( FLog::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
317 318
        }

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

323
    /////////////////////////////////////////////////////////////////////////////
COULAUD Olivier's avatar
COULAUD Olivier committed
324
    // Downward
325 326
    /////////////////////////////////////////////////////////////////////////////

327 328
    /** Runs the L2L kernel. */
    void downardPass(){
329

BRAMAS Berenger's avatar
BRAMAS Berenger committed
330
        FLOG( FLog::Controller.write("\tStart Downward Pass (L2L)\n").write(FLog::Flush); );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
331 332
        FLOG(FTic counterTime);
        FLOG(FTic computationCounter);
333 334 335 336

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

337
        for(int idxLevel = 2 ; idxLevel < FAbstractAlgorithm::upperWorkingLevel ; ++idxLevel){
338 339 340
            octreeIterator.moveDown();
        }

341 342
        typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);

343
        const int heightMinusOne = FAbstractAlgorithm::lowerWorkingLevel - 1;
COULAUD Olivier's avatar
COULAUD Olivier committed
344
        // for each levels excepted leaf level
345
        for(int idxLevel = FAbstractAlgorithm::upperWorkingLevel ; idxLevel < heightMinusOne ; ++idxLevel ){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
346
            FLOG(FTic counterTimeLevel);
347 348 349 350 351 352 353 354 355
            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
356
            FLOG(computationCounter.tic());
357
            const int chunkSize = getChunkSize(numberOfCells);
358
            #pragma omp parallel
359 360
            {
                KernelClass * const myThreadkernels = kernels[omp_get_thread_num()];
361
                #pragma omp for nowait schedule(dynamic, chunkSize)
362 363
                for(int idxCell = 0 ; idxCell < numberOfCells ; ++idxCell){
                    myThreadkernels->L2L( iterArray[idxCell].getCurrentCell() , iterArray[idxCell].getCurrentChild(), idxLevel);
364 365
                }
            }
BRAMAS Berenger's avatar
BRAMAS Berenger committed
366
            FLOG(computationCounter.tac());
BRAMAS Berenger's avatar
BRAMAS Berenger committed
367
            FLOG( FLog::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
368 369
        }

BRAMAS Berenger's avatar
BRAMAS Berenger committed
370 371
        FLOG( FLog::Controller << "\tFinished (@Downward Pass (L2L) = "  << counterTime.tacAndElapsed() << "s)\n" );
        FLOG( FLog::Controller << "\t\t Computation : " << computationCounter.cumulated() << " s\n" );
372 373
    }

374 375


berenger-bramas's avatar
berenger-bramas committed
376 377 378 379
    /////////////////////////////////////////////////////////////////////////////
    // Direct
    /////////////////////////////////////////////////////////////////////////////

380 381 382 383 384
    /** Runs the P2P & L2P kernels.
      *
     * \param p2pEnabled Run the P2P kernel.
     * \param l2pEnabled Run the L2P kernel.
     */
385
    void directPass(const bool p2pEnabled, const bool l2pEnabled){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
386
        FLOG( FLog::Controller.write("\tStart Direct Pass\n").write(FLog::Flush); );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
387 388 389
        FLOG(FTic counterTime);
        FLOG(FTic computationCounter);
        FLOG(FTic computationCounterP2P);
390

391
        omp_lock_t lockShape[SizeShape];
392
        for(int idxShape = 0 ; idxShape < SizeShape ; ++idxShape){
393
            omp_init_lock(&lockShape[idxShape]);
394 395
        }

396 397 398
        struct LeafData{
            MortonIndex index;
            CellClass* cell;
berenger-bramas's avatar
berenger-bramas committed
399 400
            ContainerClass* targets;
            ContainerClass* sources;
401 402 403
        };
        LeafData* const leafsDataArray = new LeafData[this->leafsNumber];

404
        const int LeafIndex = OctreeHeight - 1;
405 406 407 408 409 410 411

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

412
        #pragma omp parallel
413
        {
414

415 416 417
            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)));
418
            const int end = (tempEnd > this->leafsNumber ? this->leafsNumber : tempEnd);
419

berenger-bramas's avatar
berenger-bramas committed
420
            typename OctreeClass::Iterator octreeIterator(tree);
421
            octreeIterator.gotoBottomLeft();
422 423 424 425 426

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

427
            // for each leafs
428
            for(int idxMyLeafs = start ; idxMyLeafs < end ; ++idxMyLeafs){
429
                //iterArray[leafs] = octreeIterator;
430
                //++leafs;
431
                const FTreeCoordinate& coord = octreeIterator.getCurrentGlobalCoordinate();
432
                const int shapePosition = (coord.getX()%3)*9 + (coord.getY()%3)*3 + (coord.getZ()%3);
433

434 435 436
                omp_set_lock(&lockShape[shapePosition]);
                const int positionToWork = startPosAtShape[shapePosition]++;
                omp_unset_lock(&lockShape[shapePosition]);
437

COULAUD Olivier's avatar
COULAUD Olivier committed
438 439
                leafsDataArray[positionToWork].index   = octreeIterator.getCurrentGlobalIndex();
                leafsDataArray[positionToWork].cell    = octreeIterator.getCurrentCell();
440 441
                leafsDataArray[positionToWork].targets = octreeIterator.getCurrentListTargets();
                leafsDataArray[positionToWork].sources = octreeIterator.getCurrentListSrc();
442

443 444
                octreeIterator.moveRight();
            }
445

446
            #pragma omp barrier
447

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

berenger-bramas's avatar
berenger-bramas committed
450
            KernelClass& myThreadkernels = (*kernels[omp_get_thread_num()]);
451
            // There is a maximum of 26 neighbors
berenger-bramas's avatar
berenger-bramas committed
452
            ContainerClass* neighbors[27];
453
            int previous = 0;
454 455

            for(int idxShape = 0 ; idxShape < SizeShape ; ++idxShape){
456
                const int endAtThisShape = this->shapeLeaf[idxShape] + previous;
457
                const int chunkSize = getChunkSize(endAtThisShape-previous);
458
                #pragma omp for schedule(dynamic, chunkSize)
459 460
                for(int idxLeafs = previous ; idxLeafs < endAtThisShape ; ++idxLeafs){
                    LeafData& currentIter = leafsDataArray[idxLeafs];
461 462 463 464 465 466 467 468 469 470 471
                    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());
                    }
472
                }
473 474

                previous = endAtThisShape;
475 476
            }
        }
477

BRAMAS Berenger's avatar
BRAMAS Berenger committed
478
        FLOG(computationCounter.tac());
479

480
        delete [] leafsDataArray;
481 482 483 484
        for(int idxShape = 0 ; idxShape < SizeShape ; ++idxShape){
            omp_destroy_lock(&lockShape[idxShape]);
        }

485

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

490 491 492 493 494 495
    }

};


#endif //FFMMALGORITHMTHREAD_HPP