testBlockedUniformCompare.cpp 31.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

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


#include "../../Src/Utils/FGlobal.hpp"

#include "../../Src/GroupTree/Core/FGroupTree.hpp"

#include "../../Src/Components/FSimpleLeaf.hpp"
#include "../../Src/Containers/FVector.hpp"

#include "../../Src/Kernels/P2P/FP2PParticleContainer.hpp"

#include "Kernels/Interpolation/FInterpMatrixKernel.hpp"
#include "../../Src/Kernels/Uniform/FUnifKernel.hpp"

#include "../../Src/GroupTree/Uniform/FUnifCellPOD.hpp"

#include "../../Src/Utils/FMath.hpp"
#include "../../Src/Utils/FMemUtils.hpp"
#include "../../Src/Utils/FParameters.hpp"

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

#include "../../Src/GroupTree/Core/FGroupSeqAlgorithm.hpp"
#include "../../Src/GroupTree/Core/FGroupTaskAlgorithm.hpp"
#ifdef SCALFMM_USE_OMP4
#include "../../Src/GroupTree/Core/FGroupTaskDepAlgorithm.hpp"
#endif
#ifdef SCALFMM_USE_STARPU
#include "../../Src/GroupTree/Core/FGroupTaskStarpuAlgorithm.hpp"
#include "../../Src/GroupTree/StarPUUtils/FStarPUKernelCapacities.hpp"
#endif
#include "../../Src/GroupTree/Core/FP2PGroupParticleContainer.hpp"

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

#include <memory>

#include "Core/FFmmAlgorithm.hpp"
#include "Core/FFmmAlgorithmThread.hpp"
#include "Core/FFmmAlgorithmSectionTask.hpp"
#include "Core/FFmmAlgorithmTask.hpp"
#include "Core/FFmmAlgorithmThreadBalance.hpp"
#include "Components/FSimpleLeaf.hpp"

#include "Kernels/Uniform/FUnifCell.hpp"
#include "Kernels/Interpolation/FInterpMatrixKernel.hpp"
#include "Kernels/Uniform/FUnifKernel.hpp"

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

60
#include "Utils/FTemplate.hpp"
61

62
#define RANDOM_PARTICLES
63

64 65 66 67 68
const FParameterNames LocalOrder { {"-order"}, "Order of the kernel"};
const FParameterNames LocalOptionOmpTask { {"-omp-task"}, "To use FFmmAlgorithmTask"};
const FParameterNames LocalOptionOmpSection { {"-omp-section"}, "To use FFmmAlgorithmSectionTask"};
const FParameterNames LocalOptionOmpBalance { {"-omp-balance"}, "To use FFmmAlgorithmThreadBalance"};

69
const FParameterNames LocalOptionClassic { {"-omp", "omp-classic"}, "In order to use classic parallelism"};
70 71
const FParameterNames LocalOptionBlocSize { {"-bs"}, "The size of the block of the blocked tree"};
const FParameterNames LocalOptionNoValidate { {"-no-validation"}, "To avoid comparing with direct computation"};
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 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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
const FParameterNames LocalOptionProlate { {"-prolate"}, "To generate prolate distribution"};

#include <cstdlib>
#include <time.h>

/**
* @author Berenger Bramas (berenger.bramas@inria.fr)
* @class FSphericalRandomLoader
* Please read the license
*/
template <class FReal>
class FSphericalRandomLoader : public FAbstractLoader<FReal> {
protected:
    const int nbParticles;            //< the number of particles
    const FReal boxWidth;             //< the box width
    const FPoint<FReal> centerOfBox;    //< The center of box
    const bool nu;
    const bool snu;
    const bool su;
    const bool elu;
    const bool ssnu;
    const bool elsu;


    FReal rotationMatrix[3][3];

    void initRotationMatrix(){
        const FReal alpha = FMath::FPi<FReal>()/8;
        const FReal omega = FMath::FPi<FReal>()/4;

        FReal yrotation[3][3];
        yrotation[0][0] = FMath::Cos(alpha); yrotation[0][1] = 0.0; yrotation[0][2] = FMath::Sin(alpha);
        yrotation[1][0] = 0.0;               yrotation[1][1] = 1.0; yrotation[1][2] = 0.0;
        yrotation[2][0] = -FMath::Sin(alpha); yrotation[2][1] = 0.0;   yrotation[2][2] = FMath::Cos(alpha);

        FReal zrotation[3][3];
        zrotation[0][0] = FMath::Cos(omega); zrotation[0][1] = -FMath::Sin(omega); zrotation[0][2] = 0.0;
        zrotation[1][0] = FMath::Sin(omega); zrotation[1][1] = FMath::Cos(omega); zrotation[1][2] = 0.0;
        zrotation[2][0] = 0.0; zrotation[2][1] = 0.0;   zrotation[2][2] = 1.0;

        for(int i = 0 ; i < 3 ; ++i){
            for(int j = 0 ; j < 3 ; ++j){
                FReal sum = 0.0;
                for(int k = 0 ; k < 3 ; ++k){
                    sum += zrotation[i][k] * yrotation[k][j];
                }
                rotationMatrix[i][j] = sum;
            }
        }
    }

public:
    /**
    * The constructor need the simulation data
    */
    FSphericalRandomLoader(const int inNbParticles, const bool inNu = false,
                           const bool inSnu = false,
                           const bool inSu = false,
                           const bool inElu = false,
                           const bool inSsnu = false,
                           const bool inElsu = false)
        : nbParticles(inNbParticles), boxWidth(1.0), centerOfBox(0,0,0), nu(inNu),
          snu(inSnu), su(inSu), elu(inElu), ssnu(inSsnu), elsu(inElsu) {
        srand48(static_cast<unsigned int>(0));
        if( !nu && !snu && !su && !elu && !ssnu && !elsu ){
            std::cout << "UNIFORM" << std::endl;
        }
        else if( snu ){
            std::cout << "slightly NON UNIFORM" << std::endl;
        }
        else if( su ){
            std::cout << "SPHERICAL UNIFORM" << std::endl;
        }
        else if( elu ){
            std::cout << "ELLIPSE UNIFORM" << std::endl;
        }
        else if( elsu ){
            std::cout << "ELLIPSE NON UNIFORM" << std::endl;
            initRotationMatrix();
        }
        else if( ssnu ){
            std::cout << "spherical Slightly non UNIFORM" << std::endl;
        }
        else{
            std::cout << "NON UNIFORM" << std::endl;
        }
    }

    /**
    * Default destructor
    */
    virtual ~FSphericalRandomLoader(){
    }

    /**
      * @return true
      */
    bool isOpen() const{
        return true;
    }

    /**
      * To get the number of particles from this loader
      * @param the number of particles the loader can fill
      */
    FSize getNumberOfParticles() const{
        return FSize(this->nbParticles);
    }

    /**
      * The center of the box
      * @return box center
      */
    FPoint<FReal> getCenterOfBox() const{
        return this->centerOfBox;
    }

    /**
      * The box width
      * @return box width
      */
    FReal getBoxWidth() const{
        return this->boxWidth;
    }

    /**
      * Fill a particle
      * @warning to work with the loader, particles has to expose a setPosition method
      * @param the particle to fill
      */
    void fillParticle(FPoint<FReal>* partPtr){
        FPoint<FReal>& inParticle = *partPtr;
        if( !nu && !snu && !su && !elu && !ssnu && !elsu ){
            inParticle.setPosition(
                        (getRandom() * boxWidth) + centerOfBox.getX() - boxWidth/2,
                        (getRandom() * boxWidth) + centerOfBox.getY() - boxWidth/2,
                        (getRandom() * boxWidth) + centerOfBox.getZ() - boxWidth/2);
        }
        else if( snu ){
            const FReal XCenter = centerOfBox.getX();
            const FReal YCenter = centerOfBox.getY();
            const FReal ZCenter = centerOfBox.getZ();

            const FReal rayon = FReal(0.4);
            const FReal thresh = FReal(0.15);
            const FReal threshDiv2 = thresh/2;

            // Generate particles
            const FReal theta = getRandom() * FMath::FPi<FReal>();
            const FReal omega = getRandom() * FMath::FPi<FReal>() * FReal(2);

            const FReal px = rayon * FMath::Cos(omega) * FMath::Sin(theta) + XCenter + thresh * getRandom() - threshDiv2;
            const FReal py = rayon * FMath::Sin(omega) * FMath::Sin(theta) + YCenter + thresh * getRandom() - threshDiv2;
            const FReal pz = rayon * FMath::Cos(theta) + ZCenter + thresh * getRandom() - threshDiv2;

            inParticle.setPosition(px,py,pz);
        }
        else if( su ){
            //http://www.cs.cmu.edu/~mws/rpos.html
            const FReal r = 0.4;

            const FReal pz = getRandom()*2.0*r - r;
            const FReal omega = getRandom() * FMath::FPi<FReal>() * FReal(2);

            const FReal theta = FMath::ASin(pz/r);

            const FReal px = r * cos(theta) * cos(omega);
            const FReal py = r * cos(theta) * sin(omega);

            inParticle.setPosition(px,py,pz);
        }
        else if( elu ){
            const FReal a = 0.4;
            const FReal b = 0.15;

            const FReal maxPerimeter = 2.0 * FMath::FPi<FReal>() * a;

            FReal px   = 0;
            // rayon du cercle pour ce x
            FReal subr = 0;

            do {
                px   = (getRandom() * a * 2) - a;
                subr = FMath::Sqrt( (1.0 - ((px*px)/(a*a))) * (b*b) );
            } while( (getRandom()*maxPerimeter) > subr );

            // on genere un angle
            const FReal omega = getRandom() * FMath::FPi<FReal>() * FReal(2);
            // on recupere py et pz sur le cercle
            const FReal py = FMath::Cos(omega) * subr;
            const FReal pz = FMath::Sin(omega) * subr;

            inParticle.setPosition(px,py,pz);
        }
        else if( elsu ){
            const FReal a = 0.5;
            const FReal b = 0.1;

            const FReal MaxDensity = 10.0;
            const FReal maxPerimeter = 2.0 * FMath::FPi<FReal>() * a ;

            FReal px   = 0;
            // rayon du cercle pour ce x
            FReal subr = 0;
            FReal coef = 1.0;

            do {
                //px   = ( ((getRandom()*8.0+getRandom())/9.0) * a * 2) - a;
                px = (getRandom() * a * 2.0) - a;

                coef = FMath::Abs(px) * MaxDensity/a + 1.0;

                subr = FMath::Sqrt( (1.0 - ((px*px)/(a*a))) * (b*b) );

            } while( (getRandom()*maxPerimeter) > subr * coef );

            // on genere un angle
            const FReal omega = getRandom() * FMath::FPi<FReal>() * FReal(2);
            // on recupere py et pz sur le cercle
            const FReal py = FMath::Cos(omega) * subr;
            const FReal pz = FMath::Sin(omega) * subr;

           // inParticle.setPosition(px,py,pz);
            inParticle.setPosition(px * rotationMatrix[0][0] + py * rotationMatrix[0][1]+ pz * rotationMatrix[0][2],
                                   px * rotationMatrix[1][0] + py * rotationMatrix[1][1]+ pz * rotationMatrix[1][2],
                                   px * rotationMatrix[2][0] + py * rotationMatrix[2][1]+ pz * rotationMatrix[2][2]);

        }
        else if( ssnu ){
            const FReal XCenter = centerOfBox.getX();
            const FReal YCenter = centerOfBox.getY();
            const FReal ZCenter = centerOfBox.getZ();

            const FReal rayon = FReal(0.4);

            // Generate particles
            /*static const int NbAcc = 2;
            FReal acc = 0;
            for(int idx = 0 ; idx < NbAcc ; ++idx){
                acc += getRandom()/FReal(NbAcc);
            }*/
            FReal acc = ((getRandom()*8)+getRandom())/9;

            const FReal theta = acc * FMath::FPi<FReal>();
            const FReal omega = getRandom() * FMath::FPi<FReal>() * FReal(2);

            const FReal px = rayon * FMath::Cos(omega) * FMath::Sin(theta) + XCenter ;
            const FReal py = rayon * FMath::Sin(omega) * FMath::Sin(theta) + YCenter ;
            const FReal pz = rayon * FMath::Cos(theta) + ZCenter ;

            inParticle.setPosition(px,py,pz);
        }
        else{
            const FReal XCenter = centerOfBox.getX();
            const FReal YCenter = centerOfBox.getY();
            const FReal ZCenter = centerOfBox.getZ();

            const FReal rayon = FReal(0.4);

            const FReal theta = getRandom() * FMath::FPi<FReal>();
            const FReal omega = getRandom() * FMath::FPi<FReal>() * FReal(2);

            const FReal px = rayon * FMath::Cos(omega) * FMath::Sin(theta) + XCenter ;
            const FReal py = rayon * FMath::Sin(omega) * FMath::Sin(theta) + YCenter ;
            const FReal pz = rayon * FMath::Cos(theta) + ZCenter ;

            inParticle.setPosition(px,py,pz);
        }
    }

    /** Get a random number between 0 & 1 */
    FReal getRandom() const{
        return FReal(drand48());
    }
};
347 348 349 350 351 352 353 354 355 356

struct RunContainer{
    template <const int ORDER>
    static void Run(int argc, char* argv[]){
        std::cout << "Unif kernel ORDER " << ORDER << std::endl;
        // Initialize the types
        typedef double FReal;
        typedef FInterpMatrixKernelR<FReal> MatrixKernelClass;
        const MatrixKernelClass MatrixKernel;
        const int NbLevels      = FParameters::getValue(argc,argv,FParameterDefinitions::OctreeHeight.options, 5);
357
        omp_set_num_threads(FParameters::getValue(argc,argv,FParameterDefinitions::NbThreads.options, omp_get_max_threads()));
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372

        if(FParameters::existParameter(argc, argv, LocalOptionClassic.options)){
            std::cout << "\n>> Using " << omp_get_max_threads() << " omp threads.\n" << std::endl;
            const unsigned int SubTreeHeight = FParameters::getValue(argc, argv, FParameterDefinitions::OctreeSubHeight.options, 2);

            // init particles position and physical value
            struct TestParticle{
                FPoint<FReal> position;
                FReal forces[3];
                FReal physicalValue;
                FReal potential;
            };

            // open particle file
    #ifdef RANDOM_PARTICLES
373 374 375
            const bool prolate = FParameters::existParameter(argc,argv,LocalOptionProlate.options);
            FSphericalRandomLoader<FReal> loader(FParameters::getValue(argc,argv,FParameterDefinitions::NbParticles.options, 2000),
                                                 !prolate, false, false, prolate, false, false);
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    #else
            const char* const filename = FParameters::getStr(argc,argv,FParameterDefinitions::InputFile.options, "../Data/test20k.fma");
            FFmaGenericLoader<FReal> loader(filename);
    #endif
            FAssertLF(loader.isOpen());

            TestParticle* const particles = new TestParticle[loader.getNumberOfParticles()];
            for(FSize idxPart = 0 ; idxPart < loader.getNumberOfParticles() ; ++idxPart){
                FPoint<FReal> position;
                FReal physicalValue = 0.0;
    #ifdef RANDOM_PARTICLES
                physicalValue = 0.10;
                loader.fillParticle(&position);
    #else
                loader.fillParticle(&position, &physicalValue);
    #endif
                // get copy
                particles[idxPart].position       = position;
                particles[idxPart].physicalValue  = physicalValue;
                particles[idxPart].potential      = 0.0;
                particles[idxPart].forces[0]      = 0.0;
                particles[idxPart].forces[1]      = 0.0;
                particles[idxPart].forces[2]      = 0.0;
399 400
            }

401
            ////////////////////////////////////////////////////////////////////
402

403
            FTic time;
404

405 406 407
            if(FParameters::existParameter(argc, argv, LocalOptionNoValidate.options) == false){
                // begin direct computation
                std::cout << "\nDirect computation ... " << std::endl;
408
                time.tic();
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
                {
                    for(FSize idxTarget = 0 ; idxTarget < loader.getNumberOfParticles() ; ++idxTarget){
                        for(FSize idxOther =  idxTarget + 1 ; idxOther < loader.getNumberOfParticles() ; ++idxOther){
                            FP2P::MutualParticles(particles[idxTarget].position.getX(), particles[idxTarget].position.getY(),
                                                  particles[idxTarget].position.getZ(), particles[idxTarget].physicalValue,
                                                  &particles[idxTarget].forces[0], &particles[idxTarget].forces[1],
                                    &particles[idxTarget].forces[2], &particles[idxTarget].potential,
                                    particles[idxOther].position.getX(), particles[idxOther].position.getY(),
                                    particles[idxOther].position.getZ(), particles[idxOther].physicalValue,
                                    &particles[idxOther].forces[0], &particles[idxOther].forces[1],
                                    &particles[idxOther].forces[2], &particles[idxOther].potential,
                                    &MatrixKernel);
                        }
                    }
                }
424
                time.tac();
425 426
                std::cout << "Done  " << "(@Direct computation = "
                          << time.elapsed() << "s)." << std::endl;
427

428
            } // end direct computation
429

430 431 432 433 434 435
            // typedefs
            typedef FP2PParticleContainerIndexed<FReal> ContainerClass;
            typedef FSimpleLeaf<FReal, ContainerClass >  LeafClass;
            typedef FUnifCell<FReal,ORDER> CellClass;
            typedef FOctree<FReal, CellClass,ContainerClass,LeafClass> OctreeClass;
            typedef FUnifKernel<FReal,CellClass,ContainerClass,MatrixKernelClass,ORDER> KernelClass;
436

437 438
            // init oct-tree
            OctreeClass tree(NbLevels, SubTreeHeight, loader.getBoxWidth(), loader.getCenterOfBox());
439 440


441 442 443 444 445 446 447 448 449 450
            { // -----------------------------------------------------
                std::cout << "Creating & Inserting " << loader.getNumberOfParticles()
                          << " particles ..." << std::endl;
                std::cout << "\tHeight : " << NbLevels << " \t sub-height : " << SubTreeHeight << std::endl;
                time.tic();

                for(FSize idxPart = 0 ; idxPart < loader.getNumberOfParticles() ; ++idxPart){
                    // put in tree
                    tree.insert(particles[idxPart].position, idxPart, particles[idxPart].physicalValue);
                }
451

452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
                time.tac();
                std::cout << "Done  " << "(@Creating and Inserting Particles = "
                          << time.elapsed() << "s)." << std::endl;
            } // -----------------------------------------------------

            { // -----------------------------------------------------
                std::cout << "\nLagrange/Uniform grid FMM (ORDER="<< ORDER << ") ... " << std::endl;
                KernelClass kernels(NbLevels, loader.getBoxWidth(), loader.getCenterOfBox(),&MatrixKernel);
                if(FParameters::existParameter(argc, argv, LocalOptionOmpBalance.options)){
                    typedef FFmmAlgorithmThreadBalance<OctreeClass,CellClass,ContainerClass,KernelClass,LeafClass> FmmClass;
                    std::cout << "Using FFmmAlgorithmThreadBalance " << std::endl;
                    FmmClass algorithm(&tree, &kernels);
                    time.tic();
                    algorithm.execute();
                    time.tac();
                    std::cout << "Done  " << "(@Algorithm = " << time.elapsed() << "s)." << std::endl;
                }
                else if(FParameters::existParameter(argc, argv, LocalOptionOmpTask.options)){
                    typedef FFmmAlgorithmTask<OctreeClass,CellClass,ContainerClass,KernelClass,LeafClass> FmmClass;
                    std::cout << "Using FFmmAlgorithmTask " << std::endl;
                    FmmClass algorithm(&tree, &kernels);
                    time.tic();
                    algorithm.execute();
                    time.tac();
                    std::cout << "Done  " << "(@Algorithm = " << time.elapsed() << "s)." << std::endl;
                }
                else if(FParameters::existParameter(argc, argv, LocalOptionOmpSection.options)){
                    typedef FFmmAlgorithmSectionTask<OctreeClass,CellClass,ContainerClass,KernelClass,LeafClass> FmmClass;
                    std::cout << "Using FFmmAlgorithmSectionTask " << std::endl;
                    FmmClass algorithm(&tree, &kernels);
                    time.tic();
                    algorithm.execute();
                    time.tac();
                    std::cout << "Done  " << "(@Algorithm = " << time.elapsed() << "s)." << std::endl;
                }
                else {
                    typedef FFmmAlgorithmThread<OctreeClass,CellClass,ContainerClass,KernelClass,LeafClass> FmmClass;
                    std::cout << "Using FFmmAlgorithmThread " << std::endl;
                    FmmClass algorithm(&tree, &kernels);
                    time.tic();
                    algorithm.execute();
                    time.tac();
                    std::cout << "Done  " << "(@Algorithm = " << time.elapsed() << "s)." << std::endl;
                }
            } // -----------------------------------------------------


            if(FParameters::existParameter(argc, argv, LocalOptionNoValidate.options) == false){
                // -----------------------------------------------------
                FMath::FAccurater<FReal> potentialDiff;
                FMath::FAccurater<FReal> fx, fy, fz;

                { // Check that each particle has been summed with all other

                    tree.forEachLeaf([&](LeafClass* leaf){
                        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 FVector<FSize>& indexes = leaf->getTargets()->getIndexes();

                        for(FSize idxPart = 0 ; idxPart < nbParticlesInLeaf ; ++idxPart){
                            const FSize indexPartOrig = indexes[idxPart];

                            potentialDiff.add(particles[indexPartOrig].potential,potentials[idxPart]);
                            fx.add(particles[indexPartOrig].forces[0],forcesX[idxPart]);
                            fy.add(particles[indexPartOrig].forces[1],forcesY[idxPart]);
                            fz.add(particles[indexPartOrig].forces[2],forcesZ[idxPart]);
                        }
                    });
                }
524

525 526 527 528 529 530 531 532 533 534 535 536 537
                // Print for information
                std::cout << "Potential " << potentialDiff << std::endl;
                std::cout << "Fx " << fx << std::endl;
                std::cout << "Fy " << fy << std::endl;
                std::cout << "Fz " << fz << std::endl;
            } // -----------------------------------------------------
        }
        else{
            typedef FUnifCellPODCore         GroupCellSymbClass;
            typedef FUnifCellPODPole<FReal,ORDER>  GroupCellUpClass;
            typedef FUnifCellPODLocal<FReal,ORDER> GroupCellDownClass;
            typedef FUnifCellPOD<FReal,ORDER>      GroupCellClass;

538 539
            std::cout << "\n>> Using " << omp_get_max_threads() << " omp threads.\n" << std::endl;

540 541 542 543 544 545
            typedef FP2PGroupParticleContainer<FReal>          GroupContainerClass;
            typedef FGroupTree< FReal, GroupCellClass, GroupCellSymbClass, GroupCellUpClass, GroupCellDownClass, GroupContainerClass, 1, 4, FReal>  GroupOctreeClass;
    #ifdef SCALFMM_USE_STARPU
            typedef FStarPUAllCpuCapacities<FUnifKernel<FReal,GroupCellClass,GroupContainerClass,MatrixKernelClass,ORDER>> GroupKernelClass;
            typedef FStarPUCpuWrapper<typename GroupOctreeClass::CellGroupClass, GroupCellClass, GroupKernelClass, typename GroupOctreeClass::ParticleGroupClass, GroupContainerClass> GroupCpuWrapper;
            typedef FGroupTaskStarPUAlgorithm<GroupOctreeClass, typename GroupOctreeClass::CellGroupClass, GroupKernelClass, typename GroupOctreeClass::ParticleGroupClass, GroupCpuWrapper > GroupAlgorithm;
546
            std::cout << "Using FGroupTaskStarPUAlgorithm" << std::endl;
547 548 549 550 551 552
    #elif defined(SCALFMM_USE_OMP4)
            typedef FUnifKernel<FReal,GroupCellClass,GroupContainerClass,MatrixKernelClass,ORDER> GroupKernelClass;
            // Set the number of threads
            omp_set_num_threads(FParameters::getValue(argc,argv,FParameterDefinitions::NbThreads.options, omp_get_max_threads()));
            typedef FGroupTaskDepAlgorithm<GroupOctreeClass, typename GroupOctreeClass::CellGroupClass, GroupCellClass,
                    GroupCellSymbClass, GroupCellUpClass, GroupCellDownClass, GroupKernelClass, typename GroupOctreeClass::ParticleGroupClass, GroupContainerClass > GroupAlgorithm;
553
            std::cout << "Using FGroupTaskDepAlgorithm" << std::endl;
554 555 556 557
    #else
            typedef FUnifKernel<FReal,GroupCellClass,GroupContainerClass,MatrixKernelClass,ORDER> GroupKernelClass;
            //typedef FGroupSeqAlgorithm<GroupOctreeClass, typename GroupOctreeClass::CellGroupClass, GroupCellClass, GroupKernelClass, typename GroupOctreeClass::ParticleGroupClass, GroupContainerClass > GroupAlgorithm;
            typedef FGroupTaskAlgorithm<GroupOctreeClass, typename GroupOctreeClass::CellGroupClass, GroupCellClass, GroupKernelClass, typename GroupOctreeClass::ParticleGroupClass, GroupContainerClass > GroupAlgorithm;
558
            std::cout << "Using FGroupTaskAlgorithm" << std::endl;
559 560 561 562 563 564
    #endif
            // Get params
            const int groupSize     = FParameters::getValue(argc,argv,LocalOptionBlocSize.options, 250);

            // Load the particles
    #ifdef RANDOM_PARTICLES
565 566 567
            const bool prolate = FParameters::existParameter(argc,argv,LocalOptionProlate.options);
            FSphericalRandomLoader<FReal> loader(FParameters::getValue(argc,argv,FParameterDefinitions::NbParticles.options, 2000),
                                                 !prolate, false, false, prolate, false, false);
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
    #else
            const char* const filename = FParameters::getStr(argc,argv,FParameterDefinitions::InputFile.options, "../Data/test20k.fma");
            FFmaGenericLoader<FReal> loader(filename);
    #endif
            FAssertLF(loader.isOpen());
            FTic timer;

            FP2PParticleContainer<FReal> allParticles;
            for(FSize idxPart = 0 ; idxPart < loader.getNumberOfParticles() ; ++idxPart){
                FPoint<FReal> particlePosition;
                FReal physicalValue;
    #ifdef RANDOM_PARTICLES
                physicalValue = 0.10;
                loader.fillParticle(&particlePosition);
    #else
                loader.fillParticle(&particlePosition, &physicalValue);
    #endif
                allParticles.push(particlePosition, physicalValue);
            }

            // Put the data into the tree
            timer.tic();
            GroupOctreeClass groupedTree(NbLevels, loader.getBoxWidth(), loader.getCenterOfBox(), groupSize, &allParticles);
            groupedTree.printInfoBlocks();
            std::cout << "Tree created in " << timer.tacAndElapsed() << "s\n";

            // Run the algorithm
            GroupKernelClass groupkernel(NbLevels, loader.getBoxWidth(), loader.getCenterOfBox(), &MatrixKernel);
            GroupAlgorithm groupalgo(&groupedTree,&groupkernel);

            timer.tic();
            groupalgo.execute();
600
            std::cout << "Done  " << "(@Algorithm = " << timer.tacAndElapsed() << "s)." << std::endl;
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621

            // Validate the result
            if(FParameters::existParameter(argc, argv, LocalOptionNoValidate.options) == false){
                FSize offsetParticles = 0;
                FReal*const allPhysicalValues = allParticles.getPhysicalValues();
                FReal*const allPosX = const_cast<FReal*>( allParticles.getPositions()[0]);
                FReal*const allPosY = const_cast<FReal*>( allParticles.getPositions()[1]);
                FReal*const allPosZ = const_cast<FReal*>( allParticles.getPositions()[2]);

                groupedTree.template forEachCellLeaf<FP2PGroupParticleContainer<FReal> >([&](GroupCellClass cellTarget, FP2PGroupParticleContainer<FReal> * leafTarget){
                    const FReal*const physicalValues = leafTarget->getPhysicalValues();
                    const FReal*const posX = leafTarget->getPositions()[0];
                    const FReal*const posY = leafTarget->getPositions()[1];
                    const FReal*const posZ = leafTarget->getPositions()[2];
                    const FSize nbPartsInLeafTarget = leafTarget->getNbParticles();

                    for(FSize idxPart = 0 ; idxPart < nbPartsInLeafTarget ; ++idxPart){
                        allPhysicalValues[offsetParticles + idxPart] = physicalValues[idxPart];
                        allPosX[offsetParticles + idxPart] = posX[idxPart];
                        allPosY[offsetParticles + idxPart] = posY[idxPart];
                        allPosZ[offsetParticles + idxPart] = posZ[idxPart];
622
                    }
623 624

                    offsetParticles += nbPartsInLeafTarget;
625 626
                });

627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
                FAssertLF(offsetParticles == loader.getNumberOfParticles());

                FReal*const allDirectPotentials = allParticles.getPotentials();
                FReal*const allDirectforcesX = allParticles.getForcesX();
                FReal*const allDirectforcesY = allParticles.getForcesY();
                FReal*const allDirectforcesZ = allParticles.getForcesZ();

                for(int idxTgt = 0 ; idxTgt < offsetParticles ; ++idxTgt){
                    for(int idxMutual = idxTgt + 1 ; idxMutual < offsetParticles ; ++idxMutual){
                        FP2PR::MutualParticles(
                                    allPosX[idxTgt],allPosY[idxTgt],allPosZ[idxTgt], allPhysicalValues[idxTgt],
                                    &allDirectforcesX[idxTgt], &allDirectforcesY[idxTgt], &allDirectforcesZ[idxTgt], &allDirectPotentials[idxTgt],
                                    allPosX[idxMutual],allPosY[idxMutual],allPosZ[idxMutual], allPhysicalValues[idxMutual],
                                    &allDirectforcesX[idxMutual], &allDirectforcesY[idxMutual], &allDirectforcesZ[idxMutual], &allDirectPotentials[idxMutual]
                                    );
                    }
643 644
                }

645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
                FMath::FAccurater<FReal> potentialDiff;
                FMath::FAccurater<FReal> fx, fy, fz;
                offsetParticles = 0;
                groupedTree.template forEachCellLeaf<FP2PGroupParticleContainer<FReal> >([&](GroupCellClass cellTarget, FP2PGroupParticleContainer<FReal> * leafTarget){
                    const FReal*const potentials = leafTarget->getPotentials();
                    const FReal*const forcesX = leafTarget->getForcesX();
                    const FReal*const forcesY = leafTarget->getForcesY();
                    const FReal*const forcesZ = leafTarget->getForcesZ();
                    const FSize nbPartsInLeafTarget = leafTarget->getNbParticles();

                    for(int idxTgt = 0 ; idxTgt < nbPartsInLeafTarget ; ++idxTgt){
                        potentialDiff.add(allDirectPotentials[idxTgt + offsetParticles], potentials[idxTgt]);
                        fx.add(allDirectforcesX[idxTgt + offsetParticles], forcesX[idxTgt]);
                        fy.add(allDirectforcesY[idxTgt + offsetParticles], forcesY[idxTgt]);
                        fz.add(allDirectforcesZ[idxTgt + offsetParticles], forcesZ[idxTgt]);
                    }
661

662 663
                    offsetParticles += nbPartsInLeafTarget;
                });
664

665 666 667 668
                std::cout << "Error : Potential " << potentialDiff << "\n";
                std::cout << "Error : fx " << fx << "\n";
                std::cout << "Error : fy " << fy << "\n";
                std::cout << "Error : fz " << fz << "\n";
669
            }
670
        }
671

672 673
    }
};
674 675


676 677 678 679
int main(int argc, char* argv[]){
    FHelpDescribeAndExit(argc, argv, "Test the blocked tree by counting the particles.",
                         FParameterDefinitions::OctreeHeight, FParameterDefinitions::OctreeSubHeight,
                     #ifdef RANDOM_PARTICLES
680
                         FParameterDefinitions::NbParticles, LocalOptionProlate,
681 682 683 684 685 686 687 688 689
                     #else
                         FParameterDefinitions::InputFile,
                     #endif
                         FParameterDefinitions::NbThreads,
                         LocalOptionBlocSize, LocalOptionNoValidate, LocalOptionClassic,
                         LocalOptionOmpTask, LocalOptionOmpSection, LocalOptionOmpBalance,
                         LocalOrder);

    const int order = FParameters::getValue(argc,argv,LocalOrder.options, 5);
690
    FRunIf::Run<int, 3, 8, 1, RunContainer>(order, argc, argv);
691 692 693

    return 0;
}
694 695 696 697 698