testFmmAlgorithmProcPeriodic.cpp 11.2 KB
Newer Older
1
// ===================================================================================
2 3 4 5 6 7 8 9
// Logiciel initial: ScalFmm Version 0.5
// Co-auteurs : Olivier Coulaud, Bérenger Bramas.
// Propriétaires : INRIA.
// Copyright © 2011-2012, diffusé sous les termes et conditions d’une licence propriétaire.
// Initial software: ScalFmm Version 0.5
// Co-authors: Olivier Coulaud, Bérenger Bramas.
// Owners: INRIA.
// Copyright © 2011-2012, spread under the terms and conditions of a proprietary license.
10
// ===================================================================================
11

12 13 14
// ==== CMAKE =====
// @FUSE_MPI
// ================
15 16 17

#include <iostream>

18 19
#include <cstdio>
#include <cstdlib>
20 21 22 23 24 25 26 27 28 29 30 31 32

#include "../Src/Utils/FParameters.hpp"
#include "../Src/Utils/FTic.hpp"

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

#include "../Src/Components/FSimpleLeaf.hpp"

#include "../Src/Utils/F3DPosition.hpp"

#include "../Src/Components/FTestParticle.hpp"
#include "../Src/Components/FTestCell.hpp"
33
#include "../Src/Components/FTestKernels.hpp"
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

#include "../Src/Core/FFmmAlgorithmThreadProcPeriodic.hpp"

#include "../Src/Files/FMpiTreeBuilder.hpp"

#include "../Src/Utils/FAbstractSendable.hpp"


/** This program show an example of use of
  * the fmm basic algo
  * it also check that each particles is impacted each other particles
  */


// Simply create particles and try the kernels
int main(int argc, char ** argv){
    typedef FTestParticle               ParticleClass;
berenger-bramas's avatar
berenger-bramas committed
51
    typedef FTestCell                   CellClass;
52 53 54 55
    typedef FVector<ParticleClass>      ContainerClass;

    typedef FSimpleLeaf<ParticleClass, ContainerClass >                     LeafClass;
    typedef FOctree<ParticleClass, CellClass, ContainerClass , LeafClass >  OctreeClass;
56
    typedef FTestKernels<ParticleClass, CellClass, ContainerClass >         KernelClass;
57 58 59 60 61 62 63 64

    typedef FFmmAlgorithmThreadProcPeriodic<OctreeClass, ParticleClass, CellClass, ContainerClass, KernelClass, LeafClass >     FmmClass;
    ///////////////////////What we do/////////////////////////////
    std::cout << ">> This executable has to be used to test the FMM algorithm.\n";
    //////////////////////////////////////////////////////////////

    const int NbLevels          = FParameters::getValue(argc,argv,"-h", 7);
    const int SizeSubLevels     = FParameters::getValue(argc,argv,"-sh", 3);
65
    const int NbPartPerBoxesPerProc   = FParameters::getValue(argc,argv,"-nb", 1);
66 67 68

    FMpi app(argc, argv);

69
    const int NbPartPerBoxes = NbPartPerBoxesPerProc * app.global().processCount();
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

    FTic counter;

    srand ( 1 ); // volontary set seed to constant

    //////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////

    const FReal BoxWidth = 1.0;
    const FReal BoxCenter = 0.5;

    OctreeClass tree(NbLevels, SizeSubLevels, BoxWidth, F3DPosition(BoxCenter,BoxCenter,BoxCenter));

    //////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////

    std::cout << "Creating & Inserting " << NbPartPerBoxes << " particles per boxes ..." << std::endl;
    std::cout << "\tHeight : " << NbLevels << " \t sub-height : " << SizeSubLevels << std::endl;
    counter.tic();

90
    int NbPart = 0;
91
    {
92
        const int NbSmallBoxesPerSide = (1 << (NbLevels-1));
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
        const FReal SmallBoxWidth = BoxWidth / FReal(NbSmallBoxesPerSide);
        const FReal SmallBoxWidthDiv2 = SmallBoxWidth / 2;

        NbPart = NbSmallBoxesPerSide * NbSmallBoxesPerSide * NbSmallBoxesPerSide * NbPartPerBoxesPerProc;

        FTestParticle*const particles = new FTestParticle[NbPart];
        int indexPart = 0;

        for(int idxX = 0 ; idxX < NbSmallBoxesPerSide ; ++idxX){
            for(int idxY = 0 ; idxY < NbSmallBoxesPerSide ; ++idxY){
                for(int idxZ = 0 ; idxZ < NbSmallBoxesPerSide ; ++idxZ){
                    for(int idxPart = 0 ; idxPart < NbPartPerBoxesPerProc ; ++idxPart ){
                        particles[indexPart++].setPosition(FReal(idxX)*SmallBoxWidth + SmallBoxWidthDiv2,
                                                   FReal(idxY)*SmallBoxWidth + SmallBoxWidthDiv2,
                                                   FReal(idxZ)*SmallBoxWidth + SmallBoxWidthDiv2);
                    }
                }
            }
        }

        FMpiTreeBuilder<ParticleClass>::ArrayToTree(app.global(), particles, NbPart, F3DPosition(BoxCenter,BoxCenter,BoxCenter), BoxWidth, tree);
        delete[] particles;
    }

    counter.tac();
    std::cout << "Done  " << "(@Creating and Inserting Particles = " << counter.elapsed() << "s)." << std::endl;

    //////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////

    std::cout << "Working on particles ..." << std::endl;
    counter.tic();

    const int PeriodicDeep = 3;
    // FTestKernels FBasicKernels
    KernelClass kernels;
    //FFmmAlgorithm FFmmAlgorithmThread
    FmmClass algo( app.global(), &tree, &kernels, PeriodicDeep);
    algo.execute();

    counter.tac();
    std::cout << "Done  " << "(@Algorithm = " << counter.elapsed() << "s)." << std::endl;

    //////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////

    { // Check that each particle has been summed with all other
140
        OctreeClass::Iterator octreeIterator(&tree);
141 142 143 144 145 146 147 148 149 150 151
        octreeIterator.gotoBottomLeft();
        do{
            if( octreeIterator.getCurrentListTargets()->getSize() != NbPartPerBoxes){
                std::cout << "Incorrect number of particles per leaf, should be " << NbPartPerBoxes <<
                             " iter.data().getDataDown() "<< octreeIterator.getCurrentListTargets()->getSize() << std::endl;
            }
        } while(octreeIterator.moveRight());
    }
    { // Check that each particle has been summed with all other
        long long int counterNbPart = 0;

152
        OctreeClass::Iterator octreeIterator(&tree);
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
        octreeIterator.gotoBottomLeft();
        do{
            // on each leaf we should have the same number of particles
            if(octreeIterator.getCurrentCell()->getDataUp() != octreeIterator.getCurrentListSrc()->getSize()
                    || NbPartPerBoxes != octreeIterator.getCurrentCell()->getDataUp() ){
                    std::cout << "Problem P2M NbPartPerBoxes = " << NbPartPerBoxes <<
                                 " Data up = " << octreeIterator.getCurrentCell()->getDataUp() <<
                                 " Size = " << octreeIterator.getCurrentListSrc()->getSize() << "\n";
            }
            // we also count the number of particles.
            counterNbPart += octreeIterator.getCurrentListSrc()->getSize();
        } while(octreeIterator.moveRight());

        const long long int allPart = app.global().reduceSum( counterNbPart );
        if( app.global().processId() == 0 && allPart != NbPart * app.global().processCount()){
            std::cout << "Problem global nb part, counter = " << allPart << " created = " << NbPart * app.global().processCount() << std::endl;
        }
    }
    { // Ceck if there is number of NbPart summed at level 1
        long long particlesPerBox = NbPartPerBoxes;

174
        OctreeClass::Iterator octreeIterator(&tree);
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
        octreeIterator.gotoBottomLeft();
        for(int idxLevel = NbLevels - 1 ; idxLevel >= 1 ; --idxLevel ){
            if(algo.hasWorkAtLevel(idxLevel)){
                while(octreeIterator.getCurrentGlobalIndex() != algo.getWorkingInterval(idxLevel).min){
                    octreeIterator.moveRight();
                }

                do{
                    if(octreeIterator.getCurrentCell()->getDataUp() != particlesPerBox){
                        std::cout << "Problem M2M particlesPerBox = " << particlesPerBox <<
                                     " Data up = " << octreeIterator.getCurrentCell()->getDataUp() <<
                                     " level = " << idxLevel << "\n";
                    }
                } while(octreeIterator.moveRight());
            }

            octreeIterator.moveUp();
            octreeIterator.gotoLeft();

            particlesPerBox *= 8;
        }
    }
    {
        long long int counterL2L = 0;
        if( PeriodicDeep ){
            long long int particlesPerBox = NbPartPerBoxes * FMath::pow(8,NbLevels-1);

            long long int counterUp[PeriodicDeep];
            for( int idxLevel = 0 ; idxLevel < PeriodicDeep ; ++idxLevel ){
                counterUp[idxLevel] = particlesPerBox;
                particlesPerBox *= 8;
            }

            long long int counterM2L[PeriodicDeep];
            for( int idxLevel = 0 ; idxLevel < PeriodicDeep ; ++idxLevel ){
                counterM2L[idxLevel] = counterUp[idxLevel] * 189;
            }

            for( int idxLevel = PeriodicDeep - 1 ; idxLevel >= 0 ; --idxLevel ){
                counterL2L += counterM2L[idxLevel];
            }
        }
        {
            long long int particlesPerBox = NbPartPerBoxes * FMath::pow(8,NbLevels-2);

220
            OctreeClass::Iterator octreeIterator(&tree);
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
            for(int idxLevel = 1 ; idxLevel < NbLevels ; ++idxLevel ){
                counterL2L = particlesPerBox * 189 + counterL2L;

                if(algo.hasWorkAtLevel(idxLevel)){
                    while(octreeIterator.getCurrentGlobalIndex() != algo.getWorkingInterval(idxLevel).min){
                        octreeIterator.moveRight();
                    }

                    do{
                        if(octreeIterator.getCurrentCell()->getDataDown() != counterL2L){
                            std::cout << app.global().processId() <<  " >>Problem L2L counterL2L = " << counterL2L <<
                                         " Data Down = " << octreeIterator.getCurrentCell()->getDataDown() <<
                                         " level = " << idxLevel << "\n";
                        }
                    } while(octreeIterator.moveRight());
                }

                octreeIterator.gotoLeft();
                octreeIterator.moveDown();

                particlesPerBox /= 8;
            }
        }
    }
    { // Check that each particle has been summed with all other
246
        OctreeClass::Iterator octreeIterator(&tree);
247 248
        octreeIterator.gotoBottomLeft();
        do{
249
            ContainerClass::BasicIterator iter(*octreeIterator.getCurrentListTargets());
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270

            const long long int sumParticles = octreeIterator.getCurrentCell()->getDataDown() + (26 * NbPartPerBoxes) + (NbPartPerBoxes - 1);

            while( iter.hasNotFinished() ){
                if( sumParticles != iter.data().getDataDown()){
                    std::cout << "P2P probleme, should be " << sumParticles <<
                                 " iter.data().getDataDown() "<< iter.data().getDataDown() << std::endl;
                }

                iter.gotoNext();
            }
        } while(octreeIterator.moveRight());
    }

    //////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////

    return 0;
}


271