BalancePerfTest.cpp 15.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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
// ===================================================================================

// ==== CMAKE ====
// Keep in private GIT
// @SCALFMM_PRIVATE

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


/**
 * \file
 * \authors B. Bramas, O. Coulaud
 * \brief This program runs the balanced FMM Algorithm with the interpolation
 * kernel based on Chebyshev interpolation (1/r kernel)
 *
 * This program runs the FMM Algorithm with the Chebyshev kernel and compares
 * the results with a direct computation.
 *
 *
 *  This code is a short example to use the Chebyshev Interpolation approach for the 1/r kernel
 */


44 45
#include <iostream>
#include <string>
46

47 48 49 50 51
// Utilities
#include "ScalFmmConfig.h"
#include "Files/FFmaGenericLoader.hpp"
#include "Utils/FParameters.hpp"
#include "Utils/FParameterNames.hpp"
52

53 54 55 56 57
// Data structures
#include "Kernels/Chebyshev/FChebCell.hpp"
#include "Containers/FOctree.hpp"
#include "Components/FSimpleLeaf.hpp"
#include "Kernels/P2P/FP2PParticleContainerIndexed.hpp"
58

59 60 61 62
// Kernels
#include "Kernels/Interpolation/FInterpMatrixKernel.hpp"
#include "Kernels/Chebyshev/FChebSymKernel.hpp"
#include "BalanceTree/FChebSymCostKernel.hpp"
63

64 65 66
// Algorithms
#include "Core/FFmmAlgorithm.hpp"
#include "Core/FFmmAlgorithmThread.hpp"
67
#include "Core/FFmmAlgorithmTask.hpp"
68 69
#include "BalanceTree/FFmmAlgorithmThreadBalanced.hpp"
#include "BalanceTree/FCostZones.hpp"
70 71


72 73
// typedefs
using FReal = double;
74

75 76
// Chebyshev accuracy
const unsigned int ORDER = 7;
77 78


79 80
class AbstractPerfTest {
protected:
81 82
    FTic time;

83 84 85 86
    template <typename... Args>
    struct false_type {
        bool value = false;
    };  
87

88 89 90 91 92
    template <typename...Args>
    void loadTree(Args...) {
        static_assert(false_type<Args...>::value,
                      "I don't know how to load this tree with this loader...");
    }
93

94 95
    template <class OctreeClass>
    void loadTree(FFmaGenericLoader<FReal>& loader, OctreeClass& tree) {
96
        std::cout << "Creating & inserting particles" << std::flush;
97 98

        time.tic();
99

100 101
        FPoint<FReal> position;
        FReal physicalValue = 0.0;
102
        for(FSize idxPart = 0 ; idxPart < loader.getNumberOfParticles() ; ++idxPart) {
103 104 105 106 107 108 109
            // Read particle per particle from file
            loader.fillParticle(&position,&physicalValue);
            // put particle in octree
            tree.insert(position, idxPart, physicalValue);
        }

        time.tac();
110 111
        std::cout << "Done  (" << time.elapsed() << "s)." << std::endl;
    }
112

113 114 115 116 117 118
    virtual void setup() = 0;
    virtual void runAlgo() = 0;
    virtual void finalize() = 0;
    
    template <class LeafClass, class OctreeClass, class FmmClass, class LoaderClass>
    void finalize(OctreeClass& tree, FmmClass& algo, LoaderClass& loader) {
119 120 121 122 123 124 125 126 127
        std::cout << "Timers Far Field \n"
                  << "P2M " << algo.getTime(FAlgorithmTimers::P2MTimer) << " seconds\n"
                  << "M2M " << algo.getTime(FAlgorithmTimers::M2MTimer) << " seconds\n"
                  << "M2L " << algo.getTime(FAlgorithmTimers::M2LTimer) << " seconds\n"
                  << "L2L " << algo.getTime(FAlgorithmTimers::L2LTimer) << " seconds\n"
                  << "P2P and L2P " << algo.getTime(FAlgorithmTimers::NearTimer) << " seconds\n"
                  << std::endl;

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

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
        FSize N1 = 0, N2 = loader.getNumberOfParticles()/2, N3 = loader.getNumberOfParticles() - 1;
        FReal energy = 0.0;
        //
        //   Loop over all leaves
        //
        std::cout << 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 << "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] ;
                }
            });
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 189 190
        std::cout <<std::endl<<"Energy: "<< energy<<std::endl;        
    }   
    
public:
    virtual ~AbstractPerfTest(){};

    void run() {
        this->setup();
        this->runAlgo();
        this->finalize();
    }

};

template < template<typename...> class Algo > class PerfTest;

template <>
class PerfTest<FFmmAlgorithmThread> : public AbstractPerfTest {
public: // typedefs
    using CellClass          = FChebCell<FReal, ORDER>;
    using ContainerClass     = FP2PParticleContainerIndexed<FReal>;
    using LeafClass          = FSimpleLeaf<FReal, ContainerClass >;
    using OctreeClass        = FOctree<FReal, CellClass, ContainerClass, LeafClass>;
    using MatrixKernelClass  = FInterpMatrixKernelR<FReal>;
    using KernelClass        = FChebSymKernel      <FReal, CellClass, ContainerClass,
                                                    MatrixKernelClass, ORDER>;

    using FmmClass = FFmmAlgorithmThread<OctreeClass, CellClass, ContainerClass, KernelClass, LeafClass>;

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
protected:    
    int _nbThreads;
    FFmaGenericLoader<FReal> _loader;
    OctreeClass _tree;
    FmmClass* _algo;

    bool _ompStaticScheduling;

public:
    PerfTest(const std::string& fileName, const int nbThreads, const int treeHeight, const int subTreeHeight, bool ompStaticScheduling) :
        _nbThreads(nbThreads) ,
        _loader(fileName),
        _tree(treeHeight, subTreeHeight,_loader.getBoxWidth(),_loader.getCenterOfBox()),
        _ompStaticScheduling(ompStaticScheduling) {
    }

    ~PerfTest() {
        if(_algo != nullptr)
            delete _algo;
    }

protected:
    virtual void setup() {
        omp_set_num_threads(_nbThreads);
        std::cout << "\n>> Using " << omp_get_max_threads() << " threads.\n" << std::endl;

        loadTree(_loader,_tree);
    }

    virtual void runAlgo() {
        time.tic();
        const MatrixKernelClass MatrixKernel;
        KernelClass kernels(_tree.getHeight(), _loader.getBoxWidth(), _loader.getCenterOfBox(),&MatrixKernel);
        _algo = new FmmClass(&_tree, &kernels,_ompStaticScheduling);

        _algo->execute();
        time.tac();
    }

    void finalize() {
        AbstractPerfTest::finalize<LeafClass>(_tree, *_algo, _loader);
    }
};

template <>
class PerfTest<FFmmAlgorithmTask> : public AbstractPerfTest {
public: // typedefs
    using CellClass          = FChebCell<FReal, ORDER>;
    using ContainerClass     = FP2PParticleContainerIndexed<FReal>;
    using LeafClass          = FSimpleLeaf<FReal, ContainerClass >;
    using OctreeClass        = FOctree<FReal, CellClass, ContainerClass, LeafClass>;
    using MatrixKernelClass  = FInterpMatrixKernelR<FReal>;
    using KernelClass        = FChebSymKernel      <FReal, CellClass, ContainerClass,
                                                    MatrixKernelClass, ORDER>;

    using FmmClass = FFmmAlgorithmTask<OctreeClass, CellClass, ContainerClass, KernelClass, LeafClass>;

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
protected:    
    int _nbThreads;
    FFmaGenericLoader<FReal> _loader;
    OctreeClass _tree;
    FmmClass* _algo;

public:
    PerfTest(const std::string& fileName, const int nbThreads, const int treeHeight, const int subTreeHeight) :
        _nbThreads(nbThreads) ,
        _loader(fileName),
        _tree(treeHeight, subTreeHeight, _loader.getBoxWidth(), _loader.getCenterOfBox()) {
    }

    ~PerfTest() {
        if(_algo != nullptr)
            delete _algo;
    }

protected:
    virtual void setup() {
        omp_set_num_threads(_nbThreads);
        std::cout << "\n>> Using " << omp_get_max_threads() << " threads.\n" << std::endl;
270

271
        loadTree(_loader,_tree);
272 273
    }

274 275 276 277 278 279 280 281 282 283 284 285
    virtual void runAlgo() {
        time.tic();
        const MatrixKernelClass MatrixKernel;
        KernelClass kernels(_tree.getHeight(), _loader.getBoxWidth(), _loader.getCenterOfBox(),&MatrixKernel);
        _algo = new FmmClass(&_tree, &kernels);

        _algo->execute();
        time.tac();
    }

    void finalize() {
        AbstractPerfTest::finalize<LeafClass>(_tree, *_algo, _loader);
286
    }    
287 288
};

289 290


291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
template <>
class PerfTest<FFmmAlgorithmThreadBalanced> : public AbstractPerfTest {
public: // typedefs
    using ContainerClass     = FP2PParticleContainerIndexed<FReal>;
    using CellClass          = FCostCell  <FChebCell<FReal, ORDER>>;
    using LeafClass          = FSimpleLeaf<FReal, ContainerClass >;
    using OctreeClass        = FOctree    <FReal, CellClass, ContainerClass, LeafClass>;
    using MatrixKernelClass  = FInterpMatrixKernelR<FReal>;
    using KernelClass        = FChebSymKernel      <FReal, CellClass, ContainerClass,
                                                    MatrixKernelClass, ORDER>;
    using CostKernelClass = FChebSymCostKernel     <FReal, CellClass, ContainerClass, 
                                                    MatrixKernelClass, ORDER, OctreeClass>;

    template <template <typename...> class T, typename Kernel>
    using FmmAlgoClass = T<OctreeClass, CellClass, ContainerClass, Kernel, LeafClass>;

    using FmmClass     = FmmAlgoClass<FFmmAlgorithmThreadBalanced, KernelClass>;
    using CostFmmClass = FmmAlgoClass<FFmmAlgorithm, CostKernelClass>;

    const FReal epsilon = 1e-4;

protected:    

    int _nbThreads;
    FFmaGenericLoader<FReal> _loader;
    OctreeClass _tree;
    FmmClass* _algo;

public:
    PerfTest<FFmmAlgorithmThreadBalanced>(
        const std::string& fileName, const int nbThreads,
        const int treeHeight, const int subTreeHeight) :
        _nbThreads(nbThreads) ,
        _loader(fileName),
        _tree(treeHeight, subTreeHeight, _loader.getBoxWidth(), _loader.getCenterOfBox()) {
    }

    ~PerfTest<FFmmAlgorithmThreadBalanced>() {
        if(_algo != nullptr)
            delete _algo;
    }

protected:
    virtual void setup() {
        omp_set_num_threads(_nbThreads);
        std::cout << "\n>> Using " << omp_get_max_threads() << " threads.\n" << std::endl;

        loadTree(_loader,_tree);
    }

    virtual void runAlgo() {
        // Compute tree cells costs
        CostKernelClass balanceKernel(&_tree, epsilon);
        CostFmmClass    costAlgo(&_tree, &balanceKernel);

        time.tic();
        costAlgo.execute();
        time.tac();
        std::cout << "Generating tree cost: " << time.elapsed() << "s.\n";

        FCostZones<OctreeClass, CellClass> costzones(&_tree, omp_get_max_threads());

        time.tic();
        costzones.run();
        time.tac();
        std::cout << "Generating cost zones: " << time.elapsed() << "s.\n";

        time.tic();
        const MatrixKernelClass MatrixKernel;
        KernelClass kernels(_tree.getHeight(), _loader.getBoxWidth(), _loader.getCenterOfBox(),&MatrixKernel);
        _algo = new FmmClass(&_tree, &kernels, costzones.getZoneBounds(), costzones.getLeafZoneBounds());

        _algo->execute();
        time.tac();
    }

    void finalize() {
        AbstractPerfTest::finalize<LeafClass>(_tree, *_algo, _loader);
    }
};



// Simply create particles and try the kernels
int main(int argc, char* argv[])
{
    FHelpDescribeAndExit(argc, argv,
                         "Driver for Chebyshev interpolation kernel  (1/r kernel).",
                         FParameterDefinitions::InputFile,
                         FParameterDefinitions::OctreeHeight,
                         FParameterDefinitions::OctreeSubHeight,
                         FParameterDefinitions::NbThreads,
383
                         {{"--algo"},"Algorithm to run (costzones, basic-static, basic-dynamic, task)"});
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400

    const std::string  defaultFile("../Data/unitCubeXYZQ100.bfma" );
    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);
    const std::string  algoChoice =
        FParameters::getStr(argc,argv,{"--algo"},"costzones");

    std::cout << "file: " << filename << "  tree height: " << TreeHeight
              << "(" << SubTreeHeight << ")  algo: " << algoChoice << std::endl;

    if(algoChoice == "costzones") {
401 402
        PerfTest<FFmmAlgorithmThreadBalanced>
            balancePerfTest(filename, NbThreads, TreeHeight, SubTreeHeight);
403
        balancePerfTest.run();
404 405 406 407 408 409 410 411 412 413 414 415
    } else if (algoChoice == "basic-static") {
        PerfTest<FFmmAlgorithmThread>
            threadPerfTestStatic(filename, NbThreads, TreeHeight, SubTreeHeight, true);
        threadPerfTestStatic.run();
    } else if (algoChoice == "basic-dynamic") {
        PerfTest<FFmmAlgorithmThread>
            threadPerfTestDynamic(filename, NbThreads, TreeHeight, SubTreeHeight,false);
        threadPerfTestDynamic.run();
    } else if (algoChoice == "task") {
        PerfTest<FFmmAlgorithmTask>
            taskPerfTest(filename, NbThreads, TreeHeight, SubTreeHeight);
        taskPerfTest.run();
416 417 418
    } else {
        std::cerr << "Wrong algorithm choice. Try 'basic' or 'costzones'." << std::endl;
    }
419 420

}