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

#include "../Containers/FOctree.hpp"
27
#include "FCoreCommon.hpp"
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

#include <omp.h>

/**
* @author Berenger Bramas (berenger.bramas@inria.fr)
* @class FFmmAlgorithmThreadTsm
* @brief
* Please read the license
*
* This class is a threaded FMM algorithm
* It just iterates on a tree and call the kernels with good arguments.
* It used the inspector-executor model :
* iterates on the tree and builds an array to work in parallel on this array
*
* Of course this class does not deallocate pointer given in arguements.
*
berenger-bramas's avatar
berenger-bramas committed
44
45
* Because this is a Target source model you do not need the P2P to be safe.
* You should not write on sources in the P2P method!
46
*/
47
template<class OctreeClass, class CellClass, class ContainerClass, class KernelClass, class LeafClass>
BRAMAS Berenger's avatar
BRAMAS Berenger committed
48
class FFmmAlgorithmThreadTsm : public FAbstractAlgorithm{
berenger-bramas's avatar
berenger-bramas committed
49
50
    OctreeClass* const tree;                  //< The octree to work on
    KernelClass** kernels;                    //< The kernels
51

berenger-bramas's avatar
berenger-bramas committed
52
    typename OctreeClass::Iterator* iterArray;
53

berenger-bramas's avatar
berenger-bramas committed
54
55
    const int MaxThreads;

56
57
    const int OctreeHeight;

58
public:
59
60
61
62
63
    /** The constructor need the octree and the kernels used for computation
      * @param inTree the octree to work on
      * @param inKernels the kernels to call
      * An assert is launched if one of the arguments is null
      */
berenger-bramas's avatar
berenger-bramas committed
64
    FFmmAlgorithmThreadTsm(OctreeClass* const inTree, KernelClass* const inKernels)
65
                      : tree(inTree) , kernels(nullptr), iterArray(nullptr),
66
                      MaxThreads(omp_get_max_threads()) , OctreeHeight(tree->getHeight()) {
67

BRAMAS Berenger's avatar
BRAMAS Berenger committed
68
        FAssertLF(tree, "tree cannot be null");
69

berenger-bramas's avatar
berenger-bramas committed
70
        this->kernels = new KernelClass*[MaxThreads];
71
        #pragma omp parallel for schedule(static)
berenger-bramas's avatar
berenger-bramas committed
72
        for(int idxThread = 0 ; idxThread < MaxThreads ; ++idxThread){
berenger-bramas's avatar
berenger-bramas committed
73
            this->kernels[idxThread] = new KernelClass(*inKernels);
74
75
        }

BRAMAS Berenger's avatar
BRAMAS Berenger committed
76
        FLOG(FLog::Controller << "FFmmAlgorithmThreadTsm\n");
77
78
79
80
    }

    /** Default destructor */
    virtual ~FFmmAlgorithmThreadTsm(){
berenger-bramas's avatar
berenger-bramas committed
81
        for(int idxThread = 0 ; idxThread < MaxThreads ; ++idxThread){
82
83
            delete this->kernels[idxThread];
        }
berenger-bramas's avatar
berenger-bramas committed
84
        delete [] this->kernels;
85
86
87
88
89
90
    }

    /**
      * To execute the fmm algorithm
      * Call this function to run the complete algorithm
      */
91
    void execute(const unsigned operationsToProceed = FFmmNearAndFarFields){
92
        // Count leaf
93
        int numberOfLeafs = 0;
berenger-bramas's avatar
berenger-bramas committed
94
        typename OctreeClass::Iterator octreeIterator(tree);
95
96
        octreeIterator.gotoBottomLeft();
        do{
97
            ++numberOfLeafs;
98
        } while(octreeIterator.moveRight());
berenger-bramas's avatar
berenger-bramas committed
99
        iterArray = new typename OctreeClass::Iterator[numberOfLeafs];
BRAMAS Berenger's avatar
BRAMAS Berenger committed
100
        FAssertLF(iterArray, "iterArray bad alloc");
101

102
        if(operationsToProceed & FFmmP2M) bottomPass();
103

104
        if(operationsToProceed & FFmmM2M) upwardPass();
105

106
        if(operationsToProceed & FFmmM2L) transferPass();
107

108
        if(operationsToProceed & FFmmL2L) downardPass();
109

110
        if((operationsToProceed & FFmmP2P) || (operationsToProceed & FFmmL2P)) directPass((operationsToProceed & FFmmP2P),(operationsToProceed & FFmmL2P));
111
112

        delete [] iterArray;
113
        iterArray = nullptr;
114

115

116
117
118
119
    }

    /** P2M */
    void bottomPass(){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
120
        FLOG( FLog::Controller.write("\tStart Bottom Pass\n").write(FLog::Flush) );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
121
        FLOG( FTic counterTime );
122

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

132
133
        const int chunkSize = FMath::Max(1 , numberOfLeafs/(omp_get_max_threads()*omp_get_max_threads()));

BRAMAS Berenger's avatar
BRAMAS Berenger committed
134
        FLOG(FTic computationCounter);
berenger-bramas's avatar
berenger-bramas committed
135
        #pragma omp parallel
136
        {
berenger-bramas's avatar
berenger-bramas committed
137
            KernelClass * const myThreadkernels = kernels[omp_get_thread_num()];
138
            #pragma omp for nowait schedule(dynamic, chunkSize)
139
            for(int idxLeafs = 0 ; idxLeafs < numberOfLeafs ; ++idxLeafs){
140
141
                // We need the current cell that represent the leaf
                // and the list of particles
berenger-bramas's avatar
berenger-bramas committed
142
                ContainerClass* const sources = iterArray[idxLeafs].getCurrentListSrc();
143
                if(sources->getNbParticles()){
144
145
146
                    iterArray[idxLeafs].getCurrentCell()->setSrcChildTrue();
                    myThreadkernels->P2M( iterArray[idxLeafs].getCurrentCell() , sources);
                }
147
                if(iterArray[idxLeafs].getCurrentListTargets()->getNbParticles()){
148
149
150
151
                    iterArray[idxLeafs].getCurrentCell()->setTargetsChildTrue();
                }
            }
        }
BRAMAS Berenger's avatar
BRAMAS Berenger committed
152
        FLOG(computationCounter.tac());
153

BRAMAS Berenger's avatar
BRAMAS Berenger committed
154
        FLOG( counterTime.tac() );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
155
156
        FLOG( FLog::Controller << "\tFinished (@Bottom Pass (P2M) = "  << counterTime.elapsed() << "s)\n" );
        FLOG( FLog::Controller << "\t\t Computation : " << computationCounter.elapsed() << " s\n" );
157

158
159
160
161
    }

    /** M2M */
    void upwardPass(){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
162
        FLOG( FLog::Controller.write("\tStart Upward Pass\n").write(FLog::Flush); );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
163
164
        FLOG(FTic counterTime);
        FLOG(FTic computationCounter);
165
166

        // Start from leal level - 1
berenger-bramas's avatar
berenger-bramas committed
167
        typename OctreeClass::Iterator octreeIterator(tree);
168
169
        octreeIterator.gotoBottomLeft();
        octreeIterator.moveUp();
berenger-bramas's avatar
berenger-bramas committed
170
        typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);
171
172
173

        // for each levels
        for(int idxLevel = OctreeHeight - 2 ; idxLevel > 1 ; --idxLevel ){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
174
            FLOG(FTic counterTimeLevel);
175
            int numberOfCells = 0;
176
177
            // for each cells
            do{
178
179
                iterArray[numberOfCells] = octreeIterator;
                ++numberOfCells;
180
181
182
183
            } while(octreeIterator.moveRight());
            avoidGotoLeftIterator.moveUp();
            octreeIterator = avoidGotoLeftIterator;// equal octreeIterator.moveUp(); octreeIterator.gotoLeft();

184
185
            const int chunkSize = FMath::Max(1 , numberOfCells/(omp_get_max_threads()*omp_get_max_threads()));

BRAMAS Berenger's avatar
BRAMAS Berenger committed
186
            FLOG(computationCounter.tic());
berenger-bramas's avatar
berenger-bramas committed
187
            #pragma omp parallel
188
            {
berenger-bramas's avatar
berenger-bramas committed
189
                KernelClass * const myThreadkernels = kernels[omp_get_thread_num()];
190
                #pragma omp for nowait schedule(dynamic, chunkSize)
191
                for(int idxCell = 0 ; idxCell < numberOfCells ; ++idxCell){
192
193
194
                    // We need the current cell and the child
                    // child is an array (of 8 child) that may be null
                    CellClass* potentialChild[8];
195
196
                    CellClass** const realChild = iterArray[idxCell].getCurrentChild();
                    CellClass* const currentCell = iterArray[idxCell].getCurrentCell();
197
                    for(int idxChild = 0 ; idxChild < 8 ; ++idxChild){
198
                        potentialChild[idxChild] = nullptr;
199
200
201
202
203
204
205
206
207
208
209
210
211
                        if(realChild[idxChild]){
                            if(realChild[idxChild]->hasSrcChild()){
                                currentCell->setSrcChildTrue();
                                potentialChild[idxChild] = realChild[idxChild];
                            }
                            if(realChild[idxChild]->hasTargetsChild()){
                                currentCell->setTargetsChildTrue();
                            }
                        }
                    }
                    myThreadkernels->M2M( currentCell , potentialChild, idxLevel);
                }
            }
BRAMAS Berenger's avatar
BRAMAS Berenger committed
212
            FLOG(computationCounter.tac());
BRAMAS Berenger's avatar
BRAMAS Berenger committed
213
            FLOG( FLog::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
214
215
        }

BRAMAS Berenger's avatar
BRAMAS Berenger committed
216
        FLOG( counterTime.tac() );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
217
218
        FLOG( FLog::Controller << "\tFinished (@Upward Pass (M2M) = "  << counterTime.elapsed() << "s)\n" );
        FLOG( FLog::Controller << "\t\t Computation : " << computationCounter.cumulated() << " s\n" );
219

220
221
    }

222
223
    /** M2L */
    void transferPass(){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
224
            FLOG( FLog::Controller.write("\tStart Downward Pass (M2L)\n").write(FLog::Flush); );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
225
226
            FLOG(FTic counterTime);
            FLOG(FTic computationCounter);
berenger-bramas's avatar
berenger-bramas committed
227

berenger-bramas's avatar
berenger-bramas committed
228
            typename OctreeClass::Iterator octreeIterator(tree);
229
            octreeIterator.moveDown();
berenger-bramas's avatar
berenger-bramas committed
230
            typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);
231
232
233

            // for each levels
            for(int idxLevel = 2 ; idxLevel < OctreeHeight ; ++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.moveDown();
                octreeIterator = avoidGotoLeftIterator;

244
245
                const int chunkSize = FMath::Max(1 , numberOfCells/(omp_get_max_threads()*omp_get_max_threads()));

BRAMAS Berenger's avatar
BRAMAS Berenger committed
246
                FLOG(computationCounter.tic());
berenger-bramas's avatar
berenger-bramas committed
247
                #pragma omp parallel
248
                {
berenger-bramas's avatar
berenger-bramas committed
249
                    KernelClass * const myThreadkernels = kernels[omp_get_thread_num()];
250
                    const CellClass* neighbors[343];
251

252
                    #pragma omp for  schedule(dynamic, chunkSize) nowait
253
254
                    for(int idxCell = 0 ; idxCell < numberOfCells ; ++idxCell){
                        CellClass* const currentCell = iterArray[idxCell].getCurrentCell();
255
                        if(currentCell->hasTargetsChild()){
256
257
258
259
260
261
262
263
                            const int counter = tree->getInteractionNeighbors(neighbors, iterArray[idxCell].getCurrentGlobalCoordinate(),idxLevel);
                            if( counter ){
                                int counterWithSrc = 0;
                                for(int idxRealNeighbors = 0 ; idxRealNeighbors < 343 ; ++idxRealNeighbors ){
                                    if(neighbors[idxRealNeighbors] && neighbors[idxRealNeighbors]->hasSrcChild()){
                                        ++counterWithSrc;
                                    }
                                    else{
264
                                        neighbors[idxRealNeighbors] = nullptr;
265
266
                                    }
                                }
267
268
                                if(counterWithSrc){
                                    myThreadkernels->M2L( currentCell , neighbors, counterWithSrc, idxLevel);
269
270
271
272
                                }
                            }
                        }
                    }
273

BRAMAS Berenger's avatar
BRAMAS Berenger committed
274
                    FLOG(computationCounter.tic());
275
                    myThreadkernels->finishedLevelM2L(idxLevel);
BRAMAS Berenger's avatar
BRAMAS Berenger committed
276
                    FLOG(computationCounter.tac());
277
                }
BRAMAS Berenger's avatar
BRAMAS Berenger committed
278
                FLOG(computationCounter.tac());
BRAMAS Berenger's avatar
BRAMAS Berenger committed
279
                FLOG( FLog::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
280
            }
BRAMAS Berenger's avatar
BRAMAS Berenger committed
281
282
            FLOG( FLog::Controller << "\tFinished (@Downward Pass (M2L) = "  << counterTime.tacAndElapsed() << "s)\n" );
            FLOG( FLog::Controller << "\t\t Computation : " << computationCounter.cumulated() << " s\n" );
283
284
        }

285
286
        /* L2L */
        void downardPass(){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
287
            FLOG( FLog::Controller.write("\tStart Downward Pass (L2L)\n").write(FLog::Flush); );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
288
289
            FLOG(FTic counterTime);
            FLOG(FTic computationCounter);
berenger-bramas's avatar
berenger-bramas committed
290

berenger-bramas's avatar
berenger-bramas committed
291
            typename OctreeClass::Iterator octreeIterator(tree);
292
293
            octreeIterator.moveDown();

berenger-bramas's avatar
berenger-bramas committed
294
            typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);
295
296
297
298

            const int heightMinusOne = OctreeHeight - 1;
            // for each levels exepted leaf level
            for(int idxLevel = 2 ; idxLevel < heightMinusOne ; ++idxLevel ){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
299
                FLOG(FTic counterTimeLevel);
300
                int numberOfCells = 0;
301
302
                // for each cells
                do{
303
304
                    iterArray[numberOfCells] = octreeIterator;
                    ++numberOfCells;
305
306
307
308
                } while(octreeIterator.moveRight());
                avoidGotoLeftIterator.moveDown();
                octreeIterator = avoidGotoLeftIterator;

309
310
                const int chunkSize = FMath::Max(1 , numberOfCells/(omp_get_max_threads()*omp_get_max_threads()));

BRAMAS Berenger's avatar
BRAMAS Berenger committed
311
                FLOG(computationCounter.tic());
berenger-bramas's avatar
berenger-bramas committed
312
                #pragma omp parallel
313
                {
berenger-bramas's avatar
berenger-bramas committed
314
                    KernelClass * const myThreadkernels = kernels[omp_get_thread_num()];
315
                    #pragma omp for nowait schedule(dynamic, chunkSize)
316
                    for(int idxCell = 0 ; idxCell < numberOfCells ; ++idxCell){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
317
318
319
320
321
322
323
324
325
                        if( iterArray[idxCell].getCurrentCell()->hasTargetsChild() ){
                            CellClass* potentialChild[8];
                            CellClass** const realChild = iterArray[idxCell].getCurrentChild();
                            CellClass* const currentCell = iterArray[idxCell].getCurrentCell();
                            for(int idxChild = 0 ; idxChild < 8 ; ++idxChild){
                                if(realChild[idxChild] && realChild[idxChild]->hasTargetsChild()){
                                    potentialChild[idxChild] = realChild[idxChild];
                                }
                                else{
326
                                    potentialChild[idxChild] = nullptr;
BRAMAS Berenger's avatar
BRAMAS Berenger committed
327
                                }
328
                            }
BRAMAS Berenger's avatar
BRAMAS Berenger committed
329
                            myThreadkernels->L2L( currentCell , potentialChild, idxLevel);
330
331
332
                        }
                    }
                }
BRAMAS Berenger's avatar
BRAMAS Berenger committed
333
                FLOG(computationCounter.tac());
BRAMAS Berenger's avatar
BRAMAS Berenger committed
334
                FLOG( FLog::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
335
            }
BRAMAS Berenger's avatar
BRAMAS Berenger committed
336
337
            FLOG( FLog::Controller << "\tFinished (@Downward Pass (L2L) = "  << counterTime.tacAndElapsed() << "s)\n" );
            FLOG( FLog::Controller << "\t\t Computation : " << computationCounter.cumulated() << " s\n" );
338
339
        }

340

341
    /** P2P */
342
    void directPass(const bool p2pEnabled, const bool l2pEnabled){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
343
        FLOG( FLog::Controller.write("\tStart Direct Pass\n").write(FLog::Flush); );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
344
        FLOG(FTic counterTime);
345

346
        int numberOfLeafs = 0;
347
        {
berenger-bramas's avatar
berenger-bramas committed
348
            typename OctreeClass::Iterator octreeIterator(tree);
349
            octreeIterator.gotoBottomLeft();
350
            // for each leaf
351
            do{
352
353
                iterArray[numberOfLeafs] = octreeIterator;
                ++numberOfLeafs;
354
355
356
            } while(octreeIterator.moveRight());
        }

357
358
        const int chunkSize = FMath::Max(1 , numberOfLeafs/(omp_get_max_threads()*omp_get_max_threads()));

359
        const int heightMinusOne = OctreeHeight - 1;
BRAMAS Berenger's avatar
BRAMAS Berenger committed
360
        FLOG(FTic computationCounter);
berenger-bramas's avatar
berenger-bramas committed
361
        #pragma omp parallel
362
        {
berenger-bramas's avatar
berenger-bramas committed
363
            KernelClass * const myThreadkernels = kernels[omp_get_thread_num()];
364
            // There is a maximum of 26 neighbors
berenger-bramas's avatar
berenger-bramas committed
365
            ContainerClass* neighbors[27];
366

367
            #pragma omp for schedule(dynamic, chunkSize) nowait
368
            for(int idxLeafs = 0 ; idxLeafs < numberOfLeafs ; ++idxLeafs){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
369
                if( iterArray[idxLeafs].getCurrentCell()->hasTargetsChild() ){
370
371
372
373
374
375
376
377
                    if(l2pEnabled){
                        myThreadkernels->L2P(iterArray[idxLeafs].getCurrentCell(), iterArray[idxLeafs].getCurrentListTargets());
                    }
                    if(p2pEnabled){
                        // need the current particles and neighbors particles
                        const int counter = tree->getLeafsNeighbors(neighbors, iterArray[idxLeafs].getCurrentGlobalCoordinate(),heightMinusOne);
                        neighbors[13] = iterArray[idxLeafs].getCurrentListSrc();
                        myThreadkernels->P2PRemote( iterArray[idxLeafs].getCurrentGlobalCoordinate(), iterArray[idxLeafs].getCurrentListTargets(),
BRAMAS Berenger's avatar
BRAMAS Berenger committed
378
                                      iterArray[idxLeafs].getCurrentListSrc() , neighbors, counter + 1);
379
                    }
BRAMAS Berenger's avatar
BRAMAS Berenger committed
380
                }
381
382
            }
        }
BRAMAS Berenger's avatar
BRAMAS Berenger committed
383
        FLOG(computationCounter.tac());
384

BRAMAS Berenger's avatar
BRAMAS Berenger committed
385
        FLOG( counterTime.tac() );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
386
387
        FLOG( FLog::Controller << "\tFinished (@Direct Pass (L2P + P2P) = "  << counterTime.tacAndElapsed() << "s)\n" );
        FLOG( FLog::Controller << "\t\t Computation L2P + P2P : " << computationCounter.elapsed() << " s\n" );
388

389
390
391
392
393
394
395
    }

};


#endif //FFMMALGORITHMTHREADTSM_HPP

396