utestLagrangeMpi.cpp 16.1 KB
Newer Older
1
// ===================================================================================
2 3 4 5
// Copyright ScalFmm 2016 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.
6
//
7
// This software is governed by the CeCILL-C and LGPL licenses and
8
// abiding by the rules of distribution of free software.
9 10 11
// An extension to the license is given to allow static linking of scalfmm
// inside a proprietary application (no matter its license).
// See the main license file for more details.
12
//
13 14 15
// 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
16 17 18
// GNU General Public and CeCILL-C Licenses for more details.
// "http://www.cecill.info".
// "http://www.gnu.org/licenses".
19 20 21 22 23 24
// ===================================================================================

// ==== CMAKE =====
// @FUSE_FFT
// @FUSE_MPI
// ================
25
// Keep in private GIT
26
// 
27 28 29 30 31 32 33 34 35 36 37

#include "ScalFmmConfig.h"
#include "Utils/FGlobal.hpp"

#include "Containers/FOctree.hpp"

#include "Files/FFmaGenericLoader.hpp"
#include "Files/FMpiFmaGenericLoader.hpp"
#include "Files/FMpiTreeBuilder.hpp"

#include "Core/FFmmAlgorithmThreadProc.hpp"
38
#include "Utils/FLeafBalance.hpp"
39 40 41 42 43

#include "FUTester.hpp"

#include "Components/FSimpleLeaf.hpp"

COULAUD Olivier's avatar
COULAUD Olivier committed
44
#include "Kernels/Uniform/FUnifCell.hpp"
45
#include "Kernels/Interpolation/FInterpMatrixKernel.hpp"
COULAUD Olivier's avatar
COULAUD Olivier committed
46
#include "Kernels/Uniform/FUnifKernel.hpp"
47 48 49 50

#include "Kernels/P2P/FP2PParticleContainerIndexed.hpp"

/*
COULAUD Olivier's avatar
COULAUD Olivier committed
51 52
 * In this test, we compare the results of the Lagrange FMM on uniform point over
 * multiple processes and the direct results.
53 54 55
 */


COULAUD Olivier's avatar
COULAUD Olivier committed
56
class TestLagrangeMpiDirect : public FUTesterMpi<TestLagrangeMpiDirect>{
57

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    template <class FReal, class CellClass, class ContainerClass, class KernelClass, class MatrixKernelClass,
              class LeafClass, class OctreeClass, class FmmClassProc>
    void RunTest(){
        const std::string parFile( (sizeof(FReal) == sizeof(float))?
                                       "Test/DirectFloatbfma":
                                       "UTest/DirectDouble.bfma");
        std::string filename(SCALFMMDataPath+parFile);
        //    std::string filename("./sphere120Solved.bfma");

        FMpiFmaGenericLoader<FReal> loader(filename,app.global());
        Print("Number of particles :");
        Print(loader.getNumberOfParticles());

        const int nbLevels = 4;
        const int sizeOfSubLevel = 2;

        // Create Matrix Kernel
        const MatrixKernelClass MatrixKernel; // FUKernelTester is only designed to work with 1/R, i.e. matrix kernel ctor takes no argument.

        // Create octree

        struct TestParticle : public FmaRWParticle<FReal, 8,8>{
            FSize index;
            // const FPoint<FReal>& getPosition(){
82
            //  return position;
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
            // }
        };

        FSize nbParticles = loader.getMyNumberOfParticles();
        TestParticle* const particles = new TestParticle[nbParticles];
        memset(particles,0,sizeof(TestParticle)*nbParticles);

        //idx (in file) of the first part that will be used by this proc.
        FSize idxStart = loader.getStart();

        for(FSize idxPart = 0 ; idxPart < nbParticles ; ++idxPart){
            //Storage of the index (in the original file) of each part.
            particles[idxPart].index = idxPart + idxStart;
            // Read particles from file
            loader.fillParticle(particles[idxPart]);
        }

        FVector<TestParticle> finalParticles;
        FLeafBalance balancer;
        OctreeClass tree(nbLevels,sizeOfSubLevel,loader.getBoxWidth(),loader.getCenterOfBox());
        // FMpiTreeBuilder< FReal,TestParticle >::ArrayToTree(app.global(), particles, loader.getMyNumberOfParticles(),
104 105 106
        //                                               tree.getBoxCenter(),
        //                                               tree.getBoxWidth(),
        //                                               tree.getHeight(), &finalParticles,&balancer);
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
        FMpiTreeBuilder< FReal,TestParticle >::DistributeArrayToContainer(app.global(),particles,
                                                                    loader.getMyNumberOfParticles(),
                                                                    tree.getBoxCenter(),
                                                                    tree.getBoxWidth(),tree.getHeight(),
                                                                    &finalParticles, &balancer);
        for(int idx = 0 ; idx < finalParticles.getSize(); ++idx){
            tree.insert(finalParticles[idx].getPosition(),int(finalParticles[idx].index),finalParticles[idx].getPhysicalValue());
        }


        KernelClass* kernels= new KernelClass(nbLevels, loader.getBoxWidth(), loader.getCenterOfBox(),&MatrixKernel);
        FmmClassProc algorithm(app.global(),&tree, kernels);
        algorithm.execute();

        //Check datas
        {
            Print("Comput Differences with direct computation\n");
            FMath::FAccurater<FReal> potentialDiff;
            FMath::FAccurater<FReal> fx, fy, fz;
            FReal energy = 0.0;
            FReal * datas = new FReal[loader.getNbRecordPerline()];
            memset(datas,0,loader.getNbRecordPerline()*sizeof(FReal));
            tree.forEachLeaf([&](LeafClass* leaf){
                const FReal*const potentials        = leaf->getTargets()->getPotentials();
                const FReal*const physicalValues    = leaf->getTargets()->getPhysicalValues();
                const FReal*const forcesX           = leaf->getTargets()->getForcesX();
                const FReal*const forcesY           = leaf->getTargets()->getForcesY();
                const FReal*const forcesZ           = leaf->getTargets()->getForcesZ();
135 136
                const FSize nbParticlesInLeaf         = leaf->getTargets()->getNbParticles();
                const FVector<FSize>& indexes = leaf->getTargets()->getIndexes();
137

138
                for(FSize idxPart = 0 ; idxPart < nbParticlesInLeaf ; ++idxPart){
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
                    const FSize indexPartOrig = FSize(indexes[idxPart])-idxStart;
                    //It's a proc on my left that used to keep this part
                    if(indexPartOrig < 0){
                        loader.fill1Particle(datas,indexes[idxPart]);
                        potentialDiff.add(datas[4],potentials[idxPart]);
                        fx.add(datas[5],forcesX[idxPart]);
                        fy.add(datas[6],forcesY[idxPart]);
                        fz.add(datas[7],forcesZ[idxPart]);
                        energy   += potentials[idxPart]*physicalValues[idxPart];
                        memset(datas,0,loader.getNbRecordPerline()*sizeof(FReal));
                    }
                    else{
                        //It's a proc on my right that used to keep this part
                        if(indexPartOrig >= loader.getMyNumberOfParticles()){
                            loader.fill1Particle(datas,FSize(indexes[idxPart]));
                            potentialDiff.add(datas[4],potentials[idxPart]);
                            fx.add(datas[5],forcesX[idxPart]);
                            fy.add(datas[6],forcesY[idxPart]);
                            fz.add(datas[7],forcesZ[idxPart]);
                            energy   += potentials[idxPart]*physicalValues[idxPart];
                            if(datas[0] != leaf->getTargets()->getPositions()[0][idxPart]){
160
                                printf("- %d - Problem %lld !! \t [%e,%e,%e,%e] [%e,%e,%e,%e] \n",
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
                                       app.global().processId(),indexes[idxPart],
                                       datas[0],datas[1],datas[2],datas[3],
                                        potentials[idxPart],forcesX[idxPart],
                                        forcesY[idxPart],forcesZ[idxPart]);
                            }
                            memset(datas,0,loader.getNbRecordPerline()*sizeof(FReal));
                        }
                        //I already have this part
                        else{
                            potentialDiff.add(particles[indexPartOrig].getPotential(),potentials[idxPart]);
                            fx.add(particles[indexPartOrig].getForces()[0],forcesX[idxPart]);
                            fy.add(particles[indexPartOrig].getForces()[1],forcesY[idxPart]);
                            fz.add(particles[indexPartOrig].getForces()[2],forcesZ[idxPart]);
                            energy   += potentials[idxPart]*physicalValues[idxPart];
                            //if(particles[indexPartOrig].getPosition().getX() != leaf->getTargets()->getPositions()[0][idxPart]){
                            // printf("%d - Problem %d !! [%e,%e,%e,%e] [%e,%e,%e,%e] \n",
                            //        app.global().processId(),indexPartOrig,particles[indexPartOrig].forces[0],particles[indexPartOrig].forces[1],
                            //        particles[indexPartOrig].forces[2],particles[indexPartOrig].potential,
                            //        forcesX[idxPart],forcesY[idxPart],
                            //        forcesZ[idxPart],potentials[idxPart]);
                            //}
                        }
                    }
                }
            });

            //Compute Direct Energy
            FReal energyD = 0.0;
189
            for(FSize idx = 0 ; idx <  loader.getMyNumberOfParticles()  ; ++idx){
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 220 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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
                energyD +=  particles[idx].getPotential()*particles[idx].getPhysicalValue() ;
            }

            FReal energyDTot = 0.0;
            FReal energyFMMTot = 0.0;
            //Double reduce !! could be made into one ...
            MPI_Reduce(&energyD,&energyDTot,1,FMpi::GetType(energyD),MPI_SUM,0,app.global().getComm());
            MPI_Reduce(&energy,&energyFMMTot,1,FMpi::GetType(energy),MPI_SUM,0,app.global().getComm());


            //Summarize
            FMath::FAccurater<FReal>* FXYZP = new FMath::FAccurater<FReal>[app.global().processCount()*4];
            FMath::FAccurater<FReal>* fxyzp = new FMath::FAccurater<FReal>[4];
            fxyzp[0] = fx;
            fxyzp[1] = fy;
            fxyzp[2] = fz;
            fxyzp[3] = potentialDiff;
            MPI_Gather(fxyzp,4*sizeof(FMath::FAccurater<FReal>),MPI_BYTE,FXYZP,4*sizeof(FMath::FAccurater<FReal>),MPI_BYTE,0,app.global().getComm());
            if(app.global().processId() == 0){
                for(int k=1 ; k<app.global().processCount(); ++k){
                    FXYZP[0].add(FXYZP[k*4+0]);
                    FXYZP[1].add(FXYZP[k*4+1]);
                    FXYZP[2].add(FXYZP[k*4+2]);
                    FXYZP[3].add(FXYZP[k*4+3]);
                }
                printf("Potential diff is = \n");
                printf("         Pot L2Norm     %e\n",FXYZP[3].getL2Norm());
                printf("         Pot RL2Norm    %e\n",FXYZP[3].getRelativeL2Norm());
                printf("         Pot RMSError   %e\n",FXYZP[3].getRMSError());
                printf("Fx diff is = \n");
                printf("         Fx L2Norm     %e\n",FXYZP[0].getL2Norm());
                printf("         Fx RL2Norm    %e\n",FXYZP[0].getRelativeL2Norm());
                printf("         Fx RMSError   %e\n",FXYZP[0].getRMSError());
                printf("Fy diff is = \n");
                printf("        Fy L2Norm     %e\n",FXYZP[1].getL2Norm());
                printf("        Fy RL2Norm    %e\n",FXYZP[1].getRelativeL2Norm());
                printf("        Fy RMSError   %e\n",FXYZP[1].getRMSError());
                printf("Fz diff is = \n");
                printf("        Fz L2Norm     %e\n",FXYZP[2].getL2Norm());
                printf("        Fz RL2Norm    %e\n",FXYZP[2].getRelativeL2Norm());
                printf("        Fz RMSError   %e\n",FXYZP[2].getRMSError());
                FReal L2error = (FXYZP[0].getRelativeL2Norm()*FXYZP[0].getRelativeL2Norm()
                        + FXYZP[1].getRelativeL2Norm()*FXYZP[1].getRelativeL2Norm()
                        + FXYZP[2].getRelativeL2Norm() *FXYZP[2].getRelativeL2Norm());

                printf("  Total L2 Force Error= %e\n",FMath::Sqrt(L2error)) ;
                printf("  Energy Error  =   %.12e\n",FMath::Abs(energyFMMTot-energyDTot));
                printf("  Energy FMM    =   %.12e\n",FMath::Abs(energyFMMTot));
                printf("  Energy DIRECT =   %.12e\n",FMath::Abs(energyDTot));

                // Assert
                const FReal MaximumDiffPotential = FReal(9e-3);
                const FReal MaximumDiffForces     = FReal(9e-2);

                Print("Test1 - Error Relative L2 norm Potential ");
                uassert(FXYZP[3].getRelativeL2Norm() < MaximumDiffPotential);    //1
                Print("Test2 - Error RMS L2 norm Potential ");
                uassert(FXYZP[3].getRMSError() < MaximumDiffPotential);  //2
                Print("Test3 - Error Relative L2 norm FX ");
                uassert(FXYZP[0].getRelativeL2Norm()  < MaximumDiffForces);                       //3
                Print("Test4 - Error RMS L2 norm FX ");
                uassert(FXYZP[0].getRMSError() < MaximumDiffForces);                      //4
                Print("Test5 - Error Relative L2 norm FY ");
                uassert(FXYZP[1].getRelativeL2Norm()  < MaximumDiffForces);                       //5
                Print("Test6 - Error RMS L2 norm FY ");
                uassert(FXYZP[1].getRMSError() < MaximumDiffForces);                      //6
                Print("Test7 - Error Relative L2 norm FZ ");
                uassert(FXYZP[2].getRelativeL2Norm()  < MaximumDiffForces);                      //8
                Print("Test8 - Error RMS L2 norm FZ ");
                uassert(FXYZP[2].getRMSError() < MaximumDiffForces);                                           //8
                Print("Test9 - Error Relative L2 norm F ");
                uassert(L2error              < MaximumDiffForces);                                            //9   Total Force
                Print("Test10 - Relative error Energy ");
                uassert(FMath::Abs(energyFMMTot-energyDTot) /energyDTot < MaximumDiffPotential);                     //10  Total Energy

            }
        }
267
    }
268 269 270 271 272 273 274 275 276 277 278

    /** If memstas is running print the memory used */
    void PostTest() {
        if( FMemStats::controler.isUsed() ){
            std::cout << app.global().processId() << "-> Memory used at the end " << FMemStats::controler.getCurrentAllocated()
                      << " Bytes (" << FMemStats::controler.getCurrentAllocatedMB() << "MB)\n";
            std::cout << app.global().processId() << "-> Max memory used " << FMemStats::controler.getMaxAllocated()
                      << " Bytes (" << FMemStats::controler.getMaxAllocatedMB() << "MB)\n";
            std::cout << app.global().processId() << "-> Total memory used " << FMemStats::controler.getTotalAllocated()
                      << " Bytes (" << FMemStats::controler.getTotalAllocatedMB() << "MB)\n";
        }
279
    }
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296





    /** TestChebSymKernel */
    void TestChebSymKernel(){
        typedef double FReal;
        const unsigned int ORDER = 6;
        typedef FP2PParticleContainerIndexed<FReal> ContainerClass;
        typedef FSimpleLeaf<FReal, ContainerClass> LeafClass;
        typedef FInterpMatrixKernelR<FReal> MatrixKernelClass;
        typedef FUnifCell<FReal,ORDER> CellClass;
        typedef FOctree<FReal, CellClass,ContainerClass,LeafClass> OctreeClass;
        typedef FUnifKernel<FReal,CellClass,ContainerClass,MatrixKernelClass,ORDER> KernelClass;
        typedef FFmmAlgorithmThreadProc<OctreeClass,CellClass,ContainerClass,KernelClass,LeafClass> FmmClassProc;
        // run test
297
        RunTest<FReal,CellClass,ContainerClass,KernelClass,MatrixKernelClass,LeafClass,OctreeClass,FmmClassProc>();
298
    }
299 300 301 302 303 304 305 306

    ///////////////////////////////////////////////////////////
    // Set the tests!
    ///////////////////////////////////////////////////////////

    /** set test */
    void SetTests(){
        AddTest(&TestLagrangeMpiDirect::TestChebSymKernel,"Test Chebyshev Kernel with 16 small SVDs and symmetries");
307 308 309
    }

public:
310 311 312
    TestLagrangeMpiDirect(int argc,char ** argv) : FUTesterMpi(argc,argv){
    }

313 314
};

315
TestClassMpi(TestLagrangeMpiDirect);