ChebyshevInterpolationMPIFMMSplit.cpp 9.47 KB
Newer Older
1
// See LICENCE file at project root
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

// ==== CMAKE =====
// @FUSE_MPI
// @FUSE_BLAS
// ================

#include <iostream>
#include <stdexcept>
#include <cstdio>
#include <cstdlib>


#include "ScalFmmConfig.h"
#include "Containers/FOctree.hpp"
#include "Utils/FMpi.hpp"
#include "Core/FFmmAlgorithmThreadProc.hpp"

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

23
#include "Utils/FLeafBalance.hpp"
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 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 82 83 84 85 86 87 88 89 90 91 92 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 140 141 142

#include "Kernels/Interpolation/FInterpMatrixKernel.hpp"
#include "Kernels/Chebyshev/FChebSymKernel.hpp"
#include "Kernels/Chebyshev/FChebCell.hpp"

#include "Components/FSimpleLeaf.hpp"
#include "Kernels/P2P/FP2PParticleContainerIndexed.hpp"

#include "Utils/FParameters.hpp"
#include "Utils/FParameterNames.hpp"


/// \file ChebyshevInterpolationMPIFMM
//!
//! \brief This program runs the MPI FMM with Chebyshev interpolation of 1/r kernel
//!  \authors B. Bramas, O. Coulaud
//!
//!  This code is a short example to use the FMM Algorithm Proc with Chebyshev Interpolation for the 1/r kernel


// Simply create particles and try the kernels
int main(int argc, char* argv[])
{
    ///////// PARAMETERS HANDLING //////////////////////////////////////
    FHelpDescribeAndExit(
        argc, argv,
        "Driver for Chebyshev Interpolation kernel using MPI  (1/r kernel).\n\n"
        "usage: mpirun -np nb_proc_needed ./ChebyshevInterpolationAlgorithm [params]\n",
        FParameterDefinitions::InputFile,
        FParameterDefinitions::OctreeHeight,
        FParameterDefinitions::OctreeSubHeight,
        FParameterDefinitions::NbThreads);

    const std::string defaultFile("../Data/test20k.main.fma");
    const std::string filename       = FParameters::getStr(argc,argv,FParameterDefinitions::InputFile.options, defaultFile.c_str());
    const unsigned int TreeHeight    = FParameters::getValue(argc, argv, FParameterDefinitions::OctreeHeight.options, 5);
    const unsigned int SubTreeHeight = FParameters::getValue(argc, argv, FParameterDefinitions::OctreeSubHeight.options, 2);
    const unsigned int NbThreads     = FParameters::getValue(argc, argv, FParameterDefinitions::NbThreads.options, 1);

    omp_set_num_threads(NbThreads);
    std::cout << "\n>> Using " << omp_get_max_threads() << " threads.\n" << std::endl;

    //
    std::cout << "Parameters"<< std::endl
              << "      Octree Depth      " << TreeHeight    << std::endl
              << "      SubOctree depth   " << SubTreeHeight << std::endl
              << "      Input file  name: " << filename      << std::endl
              << "      Thread count :    " << NbThreads     << std::endl
              << std::endl;


    ///////// VAR INIT /////////////////////////////////////////////////

    using FReal = double;
    // Begin spherical kernel
    // Accuracy
    const unsigned int ORDER = 7;
    // Typedefs
    using ContainerClass = FP2PParticleContainerIndexed<FReal>;
    using LeafClass      = FSimpleLeaf<FReal, ContainerClass>;
    using CellClass      = FChebCell<FReal,ORDER>;
    using OctreeClass    = FOctree<FReal,CellClass,ContainerClass,LeafClass>;

    using MatrixKernelClass = FInterpMatrixKernelR<FReal>;
    const MatrixKernelClass MatrixKernel;

    using KernelClass    = FChebSymKernel<FReal, CellClass,ContainerClass,MatrixKernelClass,ORDER>;
    using FmmClassProc   = FFmmAlgorithmThreadProc<OctreeClass,CellClass,ContainerClass,KernelClass,LeafClass>;


    // Initialize values for MPI
    FMpi app(argc,argv);
    //
    // Initialize timer
    FTic time;


    // Creation of the particle loader
    FMpiSplitFmaLoader<FReal> loader(filename,app.global().processId());
    if(!loader.isOpen())
        throw std::runtime_error("Particle file couldn't be opened!") ;

    // Initialize empty oct-tree
    OctreeClass tree(TreeHeight, SubTreeHeight, loader.getBoxWidth(), loader.getCenterOfBox());


    { // -----------------------------------------------------
        if(app.global().processId() == 0){
            std::cout << "Creating & Inserting "
                      << loader.getNumberOfParticles()
                      << " particles..."
                      << std::endl;
            std::cout << "\tHeight : "
                      << TreeHeight
                      << "\tsub-height : "
                      << SubTreeHeight
                      << std::endl;
        }
        time.tic();


        FPoint<FReal> position;  // Spatial position of the particle.
        FReal physicalValue;     // Physical value of the particle.

        // Read particles from parts.
        for(FSize idxPart = 0 ; idxPart < loader.getMyNumberOfParticles() ; ++idxPart){
            // Read particle from file
            loader.fillParticle(&position,
                                &physicalValue);

            tree.insert(position, idxPart, physicalValue);
        }


        time.tac();
        double timeUsed = time.elapsed();
        double minTime,maxTime;
        std::cout << "Proc:" << app.global().processId()
                  << " "     << loader.getMyNumberOfParticles()
143
                  << "particles have been inserted in the tree. (@Reading and Inserting Particles = "
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
                  << time.elapsed() << " s)."
                  << std::endl;

        MPI_Reduce(&timeUsed,&minTime,1,MPI_DOUBLE,MPI_MIN,0,app.global().getComm());
        MPI_Reduce(&timeUsed,&maxTime,1,MPI_DOUBLE,MPI_MAX,0,app.global().getComm());
        if(app.global().processId() == 0){
            std::cout << "readinsert-time-min:" << minTime
                      << " readinsert-time-max:" << maxTime
                      << std::endl;
        }
    } // -----------------------------------------------------

    { // -----------------------------------------------------
        std::cout << "\nChebyshev Interpolation  FMM Proc (P="<< ORDER << ") ... " << std::endl;

        time.tic();

        // Kernels to use (pointer because of the limited size of the stack)
        KernelClass *kernels = new KernelClass(TreeHeight, loader.getBoxWidth(), loader.getCenterOfBox(),&MatrixKernel);
        // MPI FMM algorithm
        FmmClassProc algorithm(app.global(),&tree, kernels);
        // FMM exectution
        algorithm.execute();

        time.tac();
        double timeUsed = time.elapsed();
        double minTime,maxTime;
        std::cout << "Done  " << "(@Algorithm = " << time.elapsed() << " s)." << std::endl;
        MPI_Reduce(&timeUsed,&minTime,1,MPI_DOUBLE,MPI_MIN,0,app.global().getComm());
        MPI_Reduce(&timeUsed,&maxTime,1,MPI_DOUBLE,MPI_MAX,0,app.global().getComm());
        if(app.global().processId() == 0){
            std::cout << "exec-time-min:" << minTime
176
                      << " exec-time-max:" << maxTime
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 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
                      << std::endl;
        }

        // Free kernels from memory
        delete kernels;
    }
    // -----------------------------------------------------
    //
    // Some output
    //
    //
    { // -----------------------------------------------------
        FSize N1=0, N2= loader.getNumberOfParticles()/2, N3= (loader.getNumberOfParticles()-1); ;
        FReal energy =0.0 ;
        //
        //   Loop over all leaves
        //
        std::cout <<std::endl<<" &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& "<<std::endl;
        std::cout << std::scientific;
        std::cout.precision(10) ;

        tree.forEachLeaf([&](LeafClass* leaf){
            const FReal*const posX = leaf->getTargets()->getPositions()[0];
            const FReal*const posY = leaf->getTargets()->getPositions()[1];
            const FReal*const posZ = leaf->getTargets()->getPositions()[2];

            const FReal*const potentials = leaf->getTargets()->getPotentials();
            const FReal*const forcesX = leaf->getTargets()->getForcesX();
            const FReal*const forcesY = leaf->getTargets()->getForcesY();
            const FReal*const forcesZ = leaf->getTargets()->getForcesZ();
            const FSize nbParticlesInLeaf = leaf->getTargets()->getNbParticles();
            const FReal*const physicalValues = leaf->getTargets()->getPhysicalValues();

            const FVector<FSize>& indexes = leaf->getTargets()->getIndexes();

            for(FSize idxPart = 0 ; idxPart < nbParticlesInLeaf ; ++idxPart){
                const FSize indexPartOrig = indexes[idxPart];
                if ((indexPartOrig == N1)
                    || (indexPartOrig == N2)
                    || (indexPartOrig == N3)  ) {
                    std::cout << "Proc "<< app.global().processId()
                              << " Index "<< indexPartOrig
                              <<"  potential  " << potentials[idxPart]
                              << " Pos " << posX[idxPart]
                              << " "     << posY[idxPart]
                              << " "     << posZ[idxPart]
                              << "   Forces: " << forcesX[idxPart]
                              << " "           << forcesY[idxPart]
                              << " "           << forcesZ[idxPart]
                              << std::endl;
                }
                energy += potentials[idxPart]*physicalValues[idxPart] ;
            }
        });
        FReal gloEnergy = app.global().reduceSum(energy);
        std::cout << std::endl
                  << "Proc " << app.global().processId()
                  << " Energy: " << gloEnergy
                  << std::endl;
        std::cout << std::endl
                  << " &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& "
                  << std::endl
                  << std::endl;
    }
    // -----------------------------------------------------

    return 0;
}