Maj terminée. Pour consulter la release notes associée voici le lien :
https://about.gitlab.com/releases/2021/07/07/critical-security-release-gitlab-14-0-4-released/

FFmmAlgorithmThread.hpp 17.3 KB
Newer Older
1
// ===================================================================================
2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright ScalFmm 2011 INRIA, Olivier Coulaud, Bérenger Bramas, Matthias Messner
// 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 FFMMALGORITHMTHREAD_HPP
#define FFMMALGORITHMTHREAD_HPP
18

19 20 21 22 23 24 25 26 27

#include "../Utils/FAssertable.hpp"
#include "../Utils/FDebug.hpp"
#include "../Utils/FTrace.hpp"
#include "../Utils/FTic.hpp"
#include "../Utils/FGlobal.hpp"

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

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

#include <omp.h>

/**
* @author Berenger Bramas (berenger.bramas@inria.fr)
* @class FFmmAlgorithmThread
* @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
45
* When using this algorithm the P2P is thread safe.
46
*/
47
template<class OctreeClass, class CellClass, class ContainerClass, class KernelClass, class LeafClass>
48
class FFmmAlgorithmThread : protected FAssertable{
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
    int leafsNumber;
54 55

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

    const int MaxThreads;
59

60 61
    const int OctreeHeight;

62 63 64 65 66 67
public:
    /** 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
68
    FFmmAlgorithmThread(OctreeClass* const inTree, KernelClass* const inKernels)
69 70
        : tree(inTree) , kernels(0), iterArray(0), leafsNumber(0),
          MaxThreads(omp_get_max_threads()), OctreeHeight(tree->getHeight()) {
71

72
        fassert(tree, "tree cannot be null", __LINE__, __FILE__);
73

berenger-bramas's avatar
berenger-bramas committed
74
        this->kernels = new KernelClass*[MaxThreads];
berenger-bramas's avatar
berenger-bramas committed
75
        for(int idxThread = 0 ; idxThread < MaxThreads ; ++idxThread){
berenger-bramas's avatar
berenger-bramas committed
76
            this->kernels[idxThread] = new KernelClass(*inKernels);
77 78
        }

berenger-bramas's avatar
berenger-bramas committed
79
        FDEBUG(FDebug::Controller << "FFmmAlgorithmThread (Max Thread " << omp_get_max_threads() << ")\n");
80 81 82 83
    }

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

    /**
      * To execute the fmm algorithm
      * Call this function to run the complete algorithm
      */
94
    void execute(const unsigned operationsToProceed = FFmmNearAndFarFields){
95
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
96 97 98 99 100 101

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

        // Count leaf
102
        leafsNumber = 0;
berenger-bramas's avatar
berenger-bramas committed
103
        typename OctreeClass::Iterator octreeIterator(tree);
104 105
        octreeIterator.gotoBottomLeft();
        do{
106
            ++leafsNumber;
107
            const FTreeCoordinate& coord = octreeIterator.getCurrentCell()->getCoordinate();
108 109 110
            ++this->shapeLeaf[(coord.getX()%3)*9 + (coord.getY()%3)*3 + (coord.getZ()%3)];

        } while(octreeIterator.moveRight());
berenger-bramas's avatar
berenger-bramas committed
111
        iterArray = new typename OctreeClass::Iterator[leafsNumber];
112
        fassert(iterArray, "iterArray bad alloc", __LINE__, __FILE__);
113

114
        if(operationsToProceed & FFmmP2M) bottomPass();
115

116
        if(operationsToProceed & FFmmM2M) upwardPass();
117

118
        if(operationsToProceed & FFmmM2L) transferPass();
119

120
        if(operationsToProceed & FFmmL2L) downardPass();
121

BRAMAS Berenger's avatar
BRAMAS Berenger committed
122
        if((operationsToProceed & FFmmP2P) || (operationsToProceed & FFmmL2P)) directPass();
123 124

        delete [] iterArray;
125
        iterArray = 0;         
126 127
    }

128
private:
berenger-bramas's avatar
berenger-bramas committed
129 130 131 132
    /////////////////////////////////////////////////////////////////////////////
    // P2M
    /////////////////////////////////////////////////////////////////////////////

133 134
    /** P2M */
    void bottomPass(){
135
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
136
        FDEBUG( FDebug::Controller.write("\tStart Bottom Pass\n").write(FDebug::Flush) );
berenger-bramas's avatar
berenger-bramas committed
137
        FDEBUG(FTic counterTime);
138

berenger-bramas's avatar
berenger-bramas committed
139
        typename OctreeClass::Iterator octreeIterator(tree);
140 141 142 143 144 145 146 147
        int leafs = 0;
        // Iterate on leafs
        octreeIterator.gotoBottomLeft();
        do{
            iterArray[leafs] = octreeIterator;
            ++leafs;
        } while(octreeIterator.moveRight());

berenger-bramas's avatar
berenger-bramas committed
148
        FDEBUG(FTic computationCounter);
BRAMAS Berenger's avatar
BRAMAS Berenger committed
149
        #pragma omp parallel
150
        {
berenger-bramas's avatar
berenger-bramas committed
151
            KernelClass * const myThreadkernels = kernels[omp_get_thread_num()];
BRAMAS Berenger's avatar
BRAMAS Berenger committed
152
            #pragma omp for nowait schedule(dynamic, 50)
153 154 155 156 157 158
            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());
            }
        }
berenger-bramas's avatar
berenger-bramas committed
159
        FDEBUG(computationCounter.tac() );
160

161
        FDEBUG( FDebug::Controller << "\tFinished (@Bottom Pass (P2M) = "  << counterTime.tacAndElapsed() << "s)\n" );
162
        FDEBUG( FDebug::Controller << "\t\t Computation : " << computationCounter.elapsed() << " s\n" );
163

164 165
    }

berenger-bramas's avatar
berenger-bramas committed
166 167 168 169
    /////////////////////////////////////////////////////////////////////////////
    // Upward
    /////////////////////////////////////////////////////////////////////////////

170 171
    /** M2M */
    void upwardPass(){
172
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
173
        FDEBUG( FDebug::Controller.write("\tStart Upward Pass\n").write(FDebug::Flush); );
berenger-bramas's avatar
berenger-bramas committed
174 175
        FDEBUG(FTic counterTime);
        FDEBUG(FTic computationCounter);
176 177

        // Start from leal level - 1
berenger-bramas's avatar
berenger-bramas committed
178
        typename OctreeClass::Iterator octreeIterator(tree);
179 180
        octreeIterator.gotoBottomLeft();
        octreeIterator.moveUp();
berenger-bramas's avatar
berenger-bramas committed
181
        typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);
182 183 184

        // for each levels
        for(int idxLevel = OctreeHeight - 2 ; idxLevel > 1 ; --idxLevel ){
185
            FDEBUG(FTic counterTimeLevel);
186
            int numberOfCells = 0;
187 188
            // for each cells
            do{
189 190
                iterArray[numberOfCells] = octreeIterator;
                ++numberOfCells;
191 192 193 194 195
            } while(octreeIterator.moveRight());
            avoidGotoLeftIterator.moveUp();
            octreeIterator = avoidGotoLeftIterator;// equal octreeIterator.moveUp(); octreeIterator.gotoLeft();

            FDEBUG(computationCounter.tic());
196
            #pragma omp parallel
197
            {
berenger-bramas's avatar
berenger-bramas committed
198
                KernelClass * const myThreadkernels = kernels[omp_get_thread_num()];
BRAMAS Berenger's avatar
BRAMAS Berenger committed
199
                #pragma omp for nowait  schedule(dynamic, 50)
200
                for(int idxCell = 0 ; idxCell < numberOfCells ; ++idxCell){
201 202
                    // We need the current cell and the child
                    // child is an array (of 8 child) that may be null
203
                    myThreadkernels->M2M( iterArray[idxCell].getCurrentCell() , iterArray[idxCell].getCurrentChild(), idxLevel);
204 205
                }
            }
berenger-bramas's avatar
berenger-bramas committed
206

207
            FDEBUG(computationCounter.tac());
208
            FDEBUG( FDebug::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
209 210
        }

berenger-bramas's avatar
berenger-bramas committed
211

212
        FDEBUG( FDebug::Controller << "\tFinished (@Upward Pass (M2M) = "  << counterTime.tacAndElapsed() << "s)\n" );
berenger-bramas's avatar
berenger-bramas committed
213
        FDEBUG( FDebug::Controller << "\t\t Computation : " << computationCounter.cumulated() << " s\n" );
214

215 216
    }

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

221
    /** M2L L2L */
222
    void transferPass(){
223
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
224

225 226 227
        FDEBUG( FDebug::Controller.write("\tStart Downward Pass (M2L)\n").write(FDebug::Flush); );
        FDEBUG(FTic counterTime);
        FDEBUG(FTic computationCounter);
berenger-bramas's avatar
berenger-bramas committed
228

229 230 231 232 233 234
        typename OctreeClass::Iterator octreeIterator(tree);
        octreeIterator.moveDown();
        typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);

        // for each levels
        for(int idxLevel = 2 ; idxLevel < OctreeHeight ; ++idxLevel ){
235
            FDEBUG(FTic counterTimeLevel);
236 237 238 239 240 241 242 243 244 245
            int numberOfCells = 0;
            // for each cells
            do{
                iterArray[numberOfCells] = octreeIterator;
                ++numberOfCells;
            } while(octreeIterator.moveRight());
            avoidGotoLeftIterator.moveDown();
            octreeIterator = avoidGotoLeftIterator;

            FDEBUG(computationCounter.tic());
246
            #pragma omp parallel
247 248
            {
                KernelClass * const myThreadkernels = kernels[omp_get_thread_num()];
249
                const CellClass* neighbors[343];
250

BRAMAS Berenger's avatar
BRAMAS Berenger committed
251
                #pragma omp for  schedule(dynamic, 50) nowait
252
                for(int idxCell = 0 ; idxCell < numberOfCells ; ++idxCell){
253
                    const int counter = tree->getInteractionNeighbors(neighbors,  iterArray[idxCell].getCurrentGlobalCoordinate(),idxLevel);
254
                    if(counter) myThreadkernels->M2L( iterArray[idxCell].getCurrentCell() , neighbors, counter, idxLevel);
255
                }
256 257 258 259

                FDEBUG(computationCounter.tic());
                myThreadkernels->finishedLevelM2L(idxLevel);
                FDEBUG(computationCounter.tac());
260
            }
261
            FDEBUG(computationCounter.tac());
262
            FDEBUG( FDebug::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
263 264
        }

265 266 267
        FDEBUG( FDebug::Controller << "\tFinished (@Downward Pass (M2L) = "  << counterTime.tacAndElapsed() << "s)\n" );
        FDEBUG( FDebug::Controller << "\t\t Computation : " << computationCounter.cumulated() << " s\n" );
    }
berenger-bramas's avatar
berenger-bramas committed
268

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
    /////////////////////////////////////////////////////////////////////////////
    // Downard
    /////////////////////////////////////////////////////////////////////////////

    void downardPass(){ // second L2L
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );

        FDEBUG( FDebug::Controller.write("\tStart Downward Pass (L2L)\n").write(FDebug::Flush); );
        FDEBUG(FTic counterTime);
        FDEBUG(FTic computationCounter);

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

        typename OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);

        const int heightMinusOne = OctreeHeight - 1;
        // for each levels exepted leaf level
        for(int idxLevel = 2 ; idxLevel < heightMinusOne ; ++idxLevel ){
288
            FDEBUG(FTic counterTimeLevel);
289 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;

            FDEBUG(computationCounter.tic());
299
            #pragma omp parallel
300 301
            {
                KernelClass * const myThreadkernels = kernels[omp_get_thread_num()];
BRAMAS Berenger's avatar
BRAMAS Berenger committed
302
                #pragma omp for nowait schedule(dynamic, 50)
303 304
                for(int idxCell = 0 ; idxCell < numberOfCells ; ++idxCell){
                    myThreadkernels->L2L( iterArray[idxCell].getCurrentCell() , iterArray[idxCell].getCurrentChild(), idxLevel);
305 306
                }
            }
307
            FDEBUG(computationCounter.tac());
308
            FDEBUG( FDebug::Controller << "\t\t>> Level " << idxLevel << " = "  << counterTimeLevel.tacAndElapsed() << "s\n" );
309 310
        }

311 312
        FDEBUG( FDebug::Controller << "\tFinished (@Downward Pass (L2L) = "  << counterTime.tacAndElapsed() << "s)\n" );
        FDEBUG( FDebug::Controller << "\t\t Computation : " << computationCounter.cumulated() << " s\n" );
313 314
    }

315 316


berenger-bramas's avatar
berenger-bramas committed
317 318 319 320
    /////////////////////////////////////////////////////////////////////////////
    // Direct
    /////////////////////////////////////////////////////////////////////////////

321 322
    /** P2P */
    void directPass(){
323
        FTRACE( FTrace::FFunction functionTrace(__FUNCTION__, "Fmm" , __FILE__ , __LINE__) );
324
        FDEBUG( FDebug::Controller.write("\tStart Direct Pass\n").write(FDebug::Flush); );
berenger-bramas's avatar
berenger-bramas committed
325 326
        FDEBUG(FTic counterTime);
        FDEBUG(FTic computationCounter);
327
        FDEBUG(FTic computationCounterP2P);
328

329
        omp_lock_t lockShape[SizeShape];
330
        for(int idxShape = 0 ; idxShape < SizeShape ; ++idxShape){
331
            omp_init_lock(&lockShape[idxShape]);
332 333
        }

334 335 336
        struct LeafData{
            MortonIndex index;
            CellClass* cell;
berenger-bramas's avatar
berenger-bramas committed
337 338
            ContainerClass* targets;
            ContainerClass* sources;
339 340 341
        };
        LeafData* const leafsDataArray = new LeafData[this->leafsNumber];

342
        const int LeafIndex = OctreeHeight - 1;
343 344 345 346 347 348 349

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

350
        #pragma omp parallel
351
        {
352

353 354 355
            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)));
356
            const int end = (tempEnd > this->leafsNumber ? this->leafsNumber : tempEnd);
357

berenger-bramas's avatar
berenger-bramas committed
358
            typename OctreeClass::Iterator octreeIterator(tree);
359
            octreeIterator.gotoBottomLeft();
360 361 362 363 364

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

365
            // for each leafs
366
            for(int idxMyLeafs = start ; idxMyLeafs < end ; ++idxMyLeafs){
367
                //iterArray[leafs] = octreeIterator;
368
                //++leafs;
369
                const FTreeCoordinate& coord = octreeIterator.getCurrentGlobalCoordinate();
370
                const int shapePosition = (coord.getX()%3)*9 + (coord.getY()%3)*3 + (coord.getZ()%3);
371

372 373 374
                omp_set_lock(&lockShape[shapePosition]);
                const int positionToWork = startPosAtShape[shapePosition]++;
                omp_unset_lock(&lockShape[shapePosition]);
375

376 377 378 379
                leafsDataArray[positionToWork].index = octreeIterator.getCurrentGlobalIndex();
                leafsDataArray[positionToWork].cell = octreeIterator.getCurrentCell();
                leafsDataArray[positionToWork].targets = octreeIterator.getCurrentListTargets();
                leafsDataArray[positionToWork].sources = octreeIterator.getCurrentListSrc();
380

381 382
                octreeIterator.moveRight();
            }
383

384
            #pragma omp barrier
385

berenger-bramas's avatar
berenger-bramas committed
386
            FDEBUG(if(!omp_get_thread_num()) computationCounter.tic());
387

berenger-bramas's avatar
berenger-bramas committed
388
            KernelClass& myThreadkernels = (*kernels[omp_get_thread_num()]);
389
            // There is a maximum of 26 neighbors
berenger-bramas's avatar
berenger-bramas committed
390
            ContainerClass* neighbors[27];
391
            int previous = 0;
392 393

            for(int idxShape = 0 ; idxShape < SizeShape ; ++idxShape){
394
                const int endAtThisShape = this->shapeLeaf[idxShape] + previous;
395

BRAMAS Berenger's avatar
BRAMAS Berenger committed
396
                #pragma omp for schedule(dynamic, 50)
397 398 399
                for(int idxLeafs = previous ; idxLeafs < endAtThisShape ; ++idxLeafs){
                    LeafData& currentIter = leafsDataArray[idxLeafs];
                    myThreadkernels.L2P(currentIter.cell, currentIter.targets);
400
                    // need the current particles and neighbors particles
401
                    FDEBUG(if(!omp_get_thread_num()) computationCounterP2P.tic());
402
                    const int counter = tree->getLeafsNeighbors(neighbors, currentIter.cell->getCoordinate(), LeafIndex);
403 404
                    myThreadkernels.P2P(currentIter.cell->getCoordinate(), currentIter.targets,
                                        currentIter.sources, neighbors, counter);
405
                    FDEBUG(if(!omp_get_thread_num()) computationCounterP2P.tac());
406
                }
407 408

                previous = endAtThisShape;
409 410
            }
        }
411

412 413
        FDEBUG(computationCounter.tac());

414
        delete [] leafsDataArray;
415 416 417 418
        for(int idxShape = 0 ; idxShape < SizeShape ; ++idxShape){
            omp_destroy_lock(&lockShape[idxShape]);
        }

419

420 421
        FDEBUG( FDebug::Controller << "\tFinished (@Direct Pass (L2P + P2P) = "  << counterTime.tacAndElapsed() << "s)\n" );
        FDEBUG( FDebug::Controller << "\t\t Computation L2P + P2P : " << computationCounter.cumulated() << " s\n" );
422
        FDEBUG( FDebug::Controller << "\t\t Computation P2P : " << computationCounterP2P.cumulated() << " s\n" );
423

424 425 426 427 428 429 430
    }

};


#endif //FFMMALGORITHMTHREAD_HPP

431