testFmmAdaptiveStats.cpp 14.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

// ===================================================================================
// Copyright ScalFmm 2011 INRIA, Olivier Coulaud, Berenger Bramas
// olivier.coulaud@inria.fr, berenger.bramas@inria.fr
// This software is a computer program whose purpose is to compute the FMM.
//
// This software is governed by the CeCILL-C and LGPL licenses and
// abiding by the rules of distribution of free software.
//
// 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
// GNU General Public and CeCILL-C Licenses for more details.
// "http://www.cecill.info".
// "http://www.gnu.org/licenses".
// ===================================================================================

18 19
// Keep in private GIT
// @SCALFMM_PRIVATE
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#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"

46 47 48
#include "../../Src/Adaptive/FAdaptiveCell.hpp"
#include "../../Src/Adaptive/FAdaptiveKernelWrapper.hpp"
#include "../../Src/Adaptive/FAbstractAdaptiveKernel.hpp"
49

50 51
#include "../../Src/Utils/FParameterNames.hpp"

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
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
78
        nbAdaptiveFmmOperationsVerbosePerLevel = new int[nbLevels][7];
79 80
        memset(nbAdaptiveFmmOperationsVerbosePerLevel, 0, sizeof(int) * nbLevels * 7);

BRAMAS Berenger's avatar
BRAMAS Berenger committed
81
        nbAdaptiveFmmOperationsVerboseJump = new int[nbLevels][4];
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 143 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 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 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
        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";
        }
    }

    void P2M(CellClass* const /*pole*/, const ContainerClass* const /*particles*/) {
        nbFmmOperations += 1;
        nbFmmOperationsVerbose[0] += 1;
    }

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

    void M2L(CellClass* const FRestrict /*pole*/, const CellClass* /*distantNeighbors*/[343], const int /*size*/, const int /*level*/) {
        nbFmmOperations += 1;
        nbFmmOperationsVerbose[2] += 1;
    }

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

    void L2P(const CellClass* const  /*local*/, ContainerClass*const /*particles*/){
        nbFmmOperations += 1;
        nbFmmOperationsVerbose[4] += 1;
    }

    void P2P(const FTreeCoordinate& ,
                 ContainerClass* const FRestrict /*targets*/, const ContainerClass* const FRestrict /*sources*/,
                 ContainerClass* const /*directNeighborsParticles*/[27], const int /*size*/){
        nbFmmOperations += 1;
        nbFmmOperationsVerbose[5] += 1;
    }

    void P2PRemote(const FTreeCoordinate& ,
                 ContainerClass* const FRestrict /*targets*/, const ContainerClass* const FRestrict /*sources*/,
                 ContainerClass* const /*directNeighborsParticles*/[27], const int /*size*/){
        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;
        int counterParticles = 0;
        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){
255 256 257 258 259
    FHelpDescribeAndExit(argc, argv,
                         "Test the adaptive FMM and print information about the real computation which is done.",
                         FParameterDefinitions::NbParticles, FParameterDefinitions::OctreeHeight,
                         FParameterDefinitions::OctreeSubHeight);

260 261 262 263 264 265 266 267 268 269 270 271 272 273
    typedef FBasicCell                   CellClass;
    typedef FBasicParticleContainer<0>   ContainerClass;

    typedef FSimpleLeaf< ContainerClass >                     LeafClass;
    typedef FAdaptiveStatsKernel< CellClass, ContainerClass >         KernelClass;
    typedef FAdaptiveCell< CellClass, ContainerClass >         CellWrapperClass;
    typedef FAdaptiveKernelWrapper< KernelClass, CellClass, ContainerClass >         KernelWrapperClass;
    typedef FOctree< CellWrapperClass, ContainerClass , LeafClass >  OctreeClass;
    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";
    //////////////////////////////////////////////////////////////

274 275
    const int NbLevels      = FParameters::getValue(argc,argv,FParameterDefinitions::OctreeHeight.options, 7);
    const int SizeSubLevels = FParameters::getValue(argc,argv,FParameterDefinitions::OctreeSubHeight.options, 3);
276 277 278 279 280 281
    const int ParticlesThresh = FParameters::getValue(argc,argv,"-thresh", 10);
    FTic counter;

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

282
    FRandomLoader loader(FParameters::getValue(argc,argv,FParameterDefinitions::NbParticles.options, 2000000), 1, FPoint(0.5,0.5,0.5), 1);
283 284 285 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
    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();

    {
        FPoint particlePosition;
        for(int idxPart = 0 ; idxPart < loader.getNumberOfParticles() ; ++idxPart){
            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;
}