testFmmAdaptiveStats.cpp 14.9 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
// Keep in private GIT
// @SCALFMM_PRIVATE
23 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
#include <iostream>
#include <cstdio>


#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/FPoint.hpp"

#include "../../Src/Components/FBasicParticleContainer.hpp"
#include "../../Src/Components/FBasicCell.hpp"
#include "../../Src/Components/FTestKernels.hpp"

#include "../../Src/Core/FFmmAlgorithm.hpp"
#include "../../Src/Core/FFmmAlgorithmThread.hpp"
#include "../../Src/Core/FFmmAlgorithmTask.hpp"

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

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

49 50 51
#include "../../Src/Adaptive/FAdaptiveCell.hpp"
#include "../../Src/Adaptive/FAdaptiveKernelWrapper.hpp"
#include "../../Src/Adaptive/FAbstractAdaptiveKernel.hpp"
52

53 54
#include "../../Src/Utils/FParameterNames.hpp"

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
template< class CellClass, class ContainerClass>
class FAdaptiveStatsKernel : public FAbstractKernels<CellClass, ContainerClass>, public FAbstractAdaptiveKernel<CellClass, ContainerClass> {
    const int nbLevels;
    const int particlesThresh;

    int nbFmmOperations;
    int nbAdaptiveFmmOperations;
    int nbFmmOperationsVerbose[6];
    int nbAdaptiveFmmOperationsVerbose[8];
    int (*nbAdaptiveFmmOperationsVerbosePerLevel)[7];
    int (*nbAdaptiveFmmOperationsVerboseJump)[4];

public:
    FAdaptiveStatsKernel(const int inNbLevels, const int inParticlesThresh = 10)
        : nbLevels(inNbLevels), particlesThresh(inParticlesThresh) {

        nbFmmOperations = 0;
        nbAdaptiveFmmOperations = 0;

        for(int idx = 0 ; idx < 6 ; ++idx){
            nbFmmOperationsVerbose[idx] = 0;
        }
        for(int idx = 0 ; idx < 8 ; ++idx){
            nbAdaptiveFmmOperationsVerbose[idx] = 0;
        }

BRAMAS Berenger's avatar
BRAMAS Berenger committed
81
        nbAdaptiveFmmOperationsVerbosePerLevel = new int[nbLevels][7];
82 83
        memset(nbAdaptiveFmmOperationsVerbosePerLevel, 0, sizeof(int) * nbLevels * 7);

BRAMAS Berenger's avatar
BRAMAS Berenger committed
84
        nbAdaptiveFmmOperationsVerboseJump = new int[nbLevels][4];
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
        memset(nbAdaptiveFmmOperationsVerboseJump, 0, sizeof(int) * nbLevels * 4);
    }

    ~FAdaptiveStatsKernel(){
        delete[] nbAdaptiveFmmOperationsVerbosePerLevel;
        delete[] nbAdaptiveFmmOperationsVerboseJump;
    }

    void printStats() const {
        std::cout << "Nb Fmm Operations " << nbFmmOperations << "\n";
        std::cout << "Nb Adaptive Fmm Operations " << nbAdaptiveFmmOperations << "\n";

        std::cout << "Details Fmm :\n";
        std::cout << "\t P2M " << nbFmmOperationsVerbose[0] << "\n";
        std::cout << "\t M2M " << nbFmmOperationsVerbose[1] << "\n";
        std::cout << "\t M2L " << nbFmmOperationsVerbose[2] << "\n";
        std::cout << "\t L2L " << nbFmmOperationsVerbose[3] << "\n";
        std::cout << "\t L2P " << nbFmmOperationsVerbose[4] << "\n";
        std::cout << "\t P2P " << nbFmmOperationsVerbose[5] << "\n";

        std::cout << "Details Adaptive Fmm :\n";
        std::cout << "\t P2M " << nbAdaptiveFmmOperationsVerbose[0] << "\n";
        std::cout << "\t M2M " << nbAdaptiveFmmOperationsVerbose[1] << "\n";
        std::cout << "\t P2L " << nbAdaptiveFmmOperationsVerbose[2] << "\n";
        std::cout << "\t M2L " << nbAdaptiveFmmOperationsVerbose[3] << "\n";
        std::cout << "\t M2P " << nbAdaptiveFmmOperationsVerbose[4] << "\n";
        std::cout << "\t L2L " << nbAdaptiveFmmOperationsVerbose[5] << "\n";
        std::cout << "\t L2P " << nbAdaptiveFmmOperationsVerbose[6] << "\n";
        std::cout << "\t P2P " << nbAdaptiveFmmOperationsVerbose[7] << "\n";

        std::cout << "Details Adaptive Fmm Per Level:\n";
        for(int idxLevel = 1 ; idxLevel < nbLevels ; ++idxLevel){
            std::cout << "\t Level " << idxLevel << "\n";
            std::cout << "\t\t P2M " << nbAdaptiveFmmOperationsVerbosePerLevel[idxLevel][0] << "\n";
            std::cout << "\t\t M2M " << nbAdaptiveFmmOperationsVerbosePerLevel[idxLevel][1] << "\n";
            std::cout << "\t\t P2L " << nbAdaptiveFmmOperationsVerbosePerLevel[idxLevel][2] << "\n";
            std::cout << "\t\t M2L " << nbAdaptiveFmmOperationsVerbosePerLevel[idxLevel][3] << "\n";
            std::cout << "\t\t M2P " << nbAdaptiveFmmOperationsVerbosePerLevel[idxLevel][4] << "\n";
            std::cout << "\t\t L2L " << nbAdaptiveFmmOperationsVerbosePerLevel[idxLevel][5] << "\n";
            std::cout << "\t\t L2P " << nbAdaptiveFmmOperationsVerbosePerLevel[idxLevel][6] << "\n";
        }

        std::cout << "Details Adaptive Fmm Level difference for M2M:\n";
        for(int idxLevel = 1 ; idxLevel < nbLevels ; ++idxLevel){
            std::cout << "\t " << idxLevel << " \t" << nbAdaptiveFmmOperationsVerboseJump[idxLevel][0] << "\n";
        }
        std::cout << "Details Adaptive Fmm Level difference for M2L:\n";
        for(int idxLevel = 1 ; idxLevel < nbLevels ; ++idxLevel){
            std::cout << "\t " << idxLevel << " \t" <<  nbAdaptiveFmmOperationsVerboseJump[idxLevel][1] << "\t";
            std::cout << "\t " << -idxLevel << " \t" <<  nbAdaptiveFmmOperationsVerboseJump[idxLevel][2] << "\n";
        }
        std::cout << "Details Adaptive Fmm Level difference for L2L:\n";
        for(int idxLevel = 1 ; idxLevel < nbLevels ; ++idxLevel){
            std::cout << "\t " << idxLevel << " \t" <<  nbAdaptiveFmmOperationsVerboseJump[idxLevel][3] << "\n";
        }
    }

142
    void P2M(CellClass* const /*pole*/, const ContainerClass* const /*particles*/) override {
143 144 145 146
        nbFmmOperations += 1;
        nbFmmOperationsVerbose[0] += 1;
    }

147
    void M2M(CellClass* const FRestrict /*pole*/, const CellClass *const FRestrict *const FRestrict /*child*/, const int /*level*/) override {
148 149 150 151
        nbFmmOperations += 1;
        nbFmmOperationsVerbose[1] += 1;
    }

152
    void M2L(CellClass* const FRestrict /*pole*/, const CellClass* /*distantNeighbors*/[], const int /*neighborPositions*/[], const int /*size*/, const int /*level*/) override {
153 154 155 156
        nbFmmOperations += 1;
        nbFmmOperationsVerbose[2] += 1;
    }

157
    void L2L(const CellClass*const FRestrict /*local*/, CellClass* FRestrict *const FRestrict /*child*/, const int /*level*/) override {
158 159 160 161
        nbFmmOperations += 1;
        nbFmmOperationsVerbose[3] += 1;
    }

162
    void L2P(const CellClass* const  /*local*/, ContainerClass*const /*particles*/) override{
163 164 165 166 167 168
        nbFmmOperations += 1;
        nbFmmOperationsVerbose[4] += 1;
    }

    void P2P(const FTreeCoordinate& ,
                 ContainerClass* const FRestrict /*targets*/, const ContainerClass* const FRestrict /*sources*/,
169
                 ContainerClass* const /*directNeighborsParticles*/[], const int /*neighborPosition*/[], const int /*size*/) override{
170 171 172 173 174 175 176
        nbFmmOperations += 1;
        nbFmmOperationsVerbose[5] += 1;
    }

    void P2POuter(const FTreeCoordinate& ,
                 ContainerClass* const FRestrict /*targets*/,
                 ContainerClass* const /*directNeighborsParticles*/[], const int /*neighborPosition*/[], const int /*size*/) override{
177 178 179 180 181 182
        nbFmmOperations += 1;
        nbFmmOperationsVerbose[5] += 1;
    }

    void P2PRemote(const FTreeCoordinate& ,
                 ContainerClass* const FRestrict /*targets*/, const ContainerClass* const FRestrict /*sources*/,
183
                 const ContainerClass* const /*directNeighborsParticles*/[],  const int /*neighborPosition*/[], const int /*size*/) override{
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 245 246 247 248 249
        nbFmmOperations += 1;
        nbFmmOperationsVerbose[5] += 1;
    }

    void P2M(CellClass* const /*pole*/, const int cellLevel, const ContainerClass* const /*particles*/) override {
        nbAdaptiveFmmOperations += 1;
        nbAdaptiveFmmOperationsVerbose[0] += 1;
        nbAdaptiveFmmOperationsVerbosePerLevel[cellLevel][0] += 1;
    }

    void M2M(CellClass* const /*pole*/, const int poleLevel, const CellClass* const /*subCell*/, const int subCellLevel) override {
        nbAdaptiveFmmOperations += 1;
        nbAdaptiveFmmOperationsVerbose[1] += 1;
        nbAdaptiveFmmOperationsVerbosePerLevel[poleLevel][1] += 1;
        nbAdaptiveFmmOperationsVerboseJump[subCellLevel-poleLevel][0] += 1;
    }

    void P2L(CellClass* const /*local*/, const int localLevel, const ContainerClass* const /*particles*/) override {
        nbAdaptiveFmmOperations += 1;
        nbAdaptiveFmmOperationsVerbose[2] += 1;
        nbAdaptiveFmmOperationsVerbosePerLevel[localLevel][2] += 1;
    }

    void M2L(CellClass* const /*local*/, const int localLevel, const CellClass* const /*aNeighbor*/, const int neighborLevel) override {
        nbAdaptiveFmmOperations += 1;
        nbAdaptiveFmmOperationsVerbose[3] += 1;
        nbAdaptiveFmmOperationsVerbosePerLevel[localLevel][3] += 1;
        if(neighborLevel-localLevel >= 0 ){
            nbAdaptiveFmmOperationsVerboseJump[(neighborLevel-localLevel)][1] += 1;
        }
        else{
            nbAdaptiveFmmOperationsVerboseJump[(localLevel-neighborLevel)][2] += 1;
        }
    }

    void M2P(const CellClass* const /*pole*/, const int poleLevel, ContainerClass* const /*particles*/) override {
        nbAdaptiveFmmOperations += 1;
        nbAdaptiveFmmOperationsVerbose[4] += 1;
        nbAdaptiveFmmOperationsVerbosePerLevel[poleLevel][4] += 1;
    }

    void L2L(const CellClass* const /*local*/, const int localLevel, CellClass* const /*subCell*/, const int subCellLevel) override {
        nbAdaptiveFmmOperations += 1;
        nbAdaptiveFmmOperationsVerbose[5] += 1;
        nbAdaptiveFmmOperationsVerbosePerLevel[localLevel][5] += 1;
        nbAdaptiveFmmOperationsVerboseJump[subCellLevel-localLevel][3] += 1;
    }

    void L2P(const CellClass* const /*local*/, const int cellLevel, ContainerClass* const /*particles*/)  override {
        nbAdaptiveFmmOperations += 1;
        nbAdaptiveFmmOperationsVerbose[6] += 1;
        nbAdaptiveFmmOperationsVerbosePerLevel[cellLevel][6] += 1;
    }

    void P2P(ContainerClass* /*target*/, const ContainerClass* /*sources*/)  override {
        nbAdaptiveFmmOperations += 1;
        nbAdaptiveFmmOperationsVerbose[7] += 1;
    }

    bool preferP2M(const ContainerClass* const particles) override {
        nbAdaptiveFmmOperations += 1;
        return particles->getNbParticles() < particlesThresh;
    }

    bool preferP2M(const int /*atLevel*/, const ContainerClass*const particles[], const int nbContainers) override {
        nbAdaptiveFmmOperations += 1;
250
        FSize counterParticles = 0;
251 252 253 254 255 256 257 258 259 260 261 262 263 264
        for(int idxContainer = 0 ; idxContainer < nbContainers ; ++idxContainer){
            counterParticles += particles[idxContainer]->getNbParticles();
        }
        return counterParticles < particlesThresh;
    }
};


/** 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){
265 266 267 268 269
    FHelpDescribeAndExit(argc, argv,
                         "Test the adaptive FMM and print information about the real computation which is done.",
                         FParameterDefinitions::NbParticles, FParameterDefinitions::OctreeHeight,
                         FParameterDefinitions::OctreeSubHeight);

270
    typedef double FReal;
271
    typedef FBasicCell                   CellClass;
272
    typedef FBasicParticleContainer<FReal, 0, FReal>   ContainerClass;
273

274
    typedef FSimpleLeaf<FReal, ContainerClass >                     LeafClass;
275 276 277
    typedef FAdaptiveStatsKernel< CellClass, ContainerClass >         KernelClass;
    typedef FAdaptiveCell< CellClass, ContainerClass >         CellWrapperClass;
    typedef FAdaptiveKernelWrapper< KernelClass, CellClass, ContainerClass >         KernelWrapperClass;
278
    typedef FOctree< FReal, CellWrapperClass, ContainerClass , LeafClass >  OctreeClass;
279 280 281 282 283 284
    typedef FFmmAlgorithm<OctreeClass, CellWrapperClass, ContainerClass, KernelWrapperClass, LeafClass >     FmmClass;

    ///////////////////////What we do/////////////////////////////
    std::cout << ">> This executable has to be used to test the FMM algorithm.\n";
    //////////////////////////////////////////////////////////////

285 286
    const int NbLevels      = FParameters::getValue(argc,argv,FParameterDefinitions::OctreeHeight.options, 7);
    const int SizeSubLevels = FParameters::getValue(argc,argv,FParameterDefinitions::OctreeSubHeight.options, 3);
287 288 289 290 291 292
    const int ParticlesThresh = FParameters::getValue(argc,argv,"-thresh", 10);
    FTic counter;

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

293
    FRandomLoader<FReal> loader(FParameters::getValue(argc,argv,FParameterDefinitions::NbParticles.options, 2000000), 1, FPoint<FReal>(0.5,0.5,0.5), 1);
294 295 296 297 298 299 300 301 302 303
    OctreeClass tree(NbLevels, SizeSubLevels, loader.getBoxWidth(), loader.getCenterOfBox());

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

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

    {
304
        FPoint<FReal> particlePosition;
305
        for(FSize idxPart = 0 ; idxPart < loader.getNumberOfParticles() ; ++idxPart){
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
            loader.fillParticle(&particlePosition);
            tree.insert(particlePosition);
        }
    }

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

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

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

    KernelWrapperClass kernels(NbLevels, ParticlesThresh);
    FmmClass algo(&tree,&kernels);
    algo.execute();

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

    kernels.getKernel().printStats();

    return 0;
}