RotationMPIFMM.cpp 9.65 KB
Newer Older
COULAUD Olivier's avatar
COULAUD Olivier committed
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
// ===================================================================================
// 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".
// ===================================================================================

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

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


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

#include "../../Src/Files/FFmaGenericLoader.hpp"
33
#include "../../Src/Files/FMpiFmaGenericLoader.hpp"
COULAUD Olivier's avatar
COULAUD Olivier committed
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
#include "../../Src/Files/FMpiTreeBuilder.hpp"

#include "../../Src/BalanceTree/FLeafBalance.hpp"

#include "../../Src/Kernels/Rotation/FRotationKernel.hpp"
#include "../../Src/Kernels/Rotation/FRotationCell.hpp"

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

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


/// \file  RotationFMMProc.cpp
//!
//! \brief This program runs the MPI FMM  with harmonic spherical approximation of 1/r kernel
//!  \authors B. Bramas, O. Coulaud
//!
//!  This code is a short example to use the rotation harmonic spherical approximation for the 1/r kernel
//!
//!
//!  <b> General arguments:</b>
//!     \param   -help(-h)      to see the parameters available in this driver
//!     \param   -depth          The depth of the octree
//!     \param   -subdepth     Specifies the size of the sub octree
//!     \param   -t                   The number of threads
//!
61
//!     \param   -f name          Name of the particles file with extension (.fma or .bfma). The data in  file have to be in our FMA format
COULAUD Olivier's avatar
COULAUD Olivier committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
//!
//

void usage() {
  std::cout << "Driver for Rotation Spherical kernel using MPI  (1/r kernel)" << std::endl;
  std::cout <<	 "Options  "<< std::endl
	    <<     "      -help         to see the parameters    " << std::endl
	    <<	  "      -depth       the depth of the octree   "<< std::endl
	    <<	  "      -subdepth  specifies the size of the sub octree   " << std::endl
	    <<     "      -f   name    name specifies the name of the particle distribution" << std::endl
	    <<     "      -t  n  specifies the number of threads used in the computations" << std::endl
	    <<     "  CMD >> mpirun -np nb_proc_needed ./RotationFMMProc ....."  << std::endl;
}

// Simply create particles and try the kernels
int main(int argc, char* argv[])
{
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
79 80 81 82 83 84 85 86 87
  const std::string defaultFile(/*SCALFMMDataPath+*/"../Data/test20k.fma");
  const std::string filename                = FParameters::getStr(argc,argv,"-f", defaultFile.c_str());
  const unsigned int TreeHeight       = FParameters::getValue(argc, argv, "-depth", 5);
  const unsigned int SubTreeHeight  = FParameters::getValue(argc, argv, "-subdepth", 2);
  const unsigned int NbThreads        = FParameters::getValue(argc, argv, "-t", 1);
  if(FParameters::existParameter(argc, argv, "-h")||FParameters::existParameter(argc, argv, "-help")){
    usage() ;
    exit(-1);
  }
COULAUD Olivier's avatar
COULAUD Olivier committed
88
#ifdef _OPENMP
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
89 90
  omp_set_num_threads(NbThreads);
  std::cout << "\n>> Using " << omp_get_max_threads() << " threads.\n" << std::endl;
COULAUD Olivier's avatar
COULAUD Olivier committed
91
#else
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
92
  std::cout << "\n>> Sequential version.\n" << std::endl;
COULAUD Olivier's avatar
COULAUD Olivier committed
93
#endif
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
94 95 96 97 98 99 100 101 102 103 104 105 106
  //
  std::cout <<	 "Parameters  "<< std::endl
	    <<     "      Octree Depth      "<< TreeHeight <<std::endl
	    <<	 "      SubOctree depth "<< SubTreeHeight <<std::endl
	    <<     "      Input file  name: " <<filename <<std::endl
	    <<     "      Thread number:  " << NbThreads <<std::endl
	    <<std::endl;
  //init values for MPI
  FMpi app(argc,argv);
  //
  // init timer
  FTic time;

107
  FMpiFmaGenericLoader* loader = new FMpiFmaGenericLoader(filename,app.global());
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
108

109
  if(!loader->isOpen()) throw std::runtime_error("Particle file couldn't be opened!") ;
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  ////////////////////////////////////////////////////////////////////



  // begin spherical kernel

  // accuracy
  const unsigned int P = 22;
  // typedefs
  typedef FP2PParticleContainerIndexed<>                     ContainerClass;
  typedef FSimpleLeaf< ContainerClass >                       LeafClass;
  typedef FRotationCell<P>                                             CellClass;
  typedef FOctree<CellClass,ContainerClass,LeafClass>  OctreeClass;
  //
  typedef FRotationKernel< CellClass, ContainerClass , P>   KernelClass;
  //
  typedef FFmmAlgorithmThreadProc<OctreeClass,CellClass,ContainerClass,KernelClass,LeafClass> FmmClassProc;

  // init oct-tree
129
  OctreeClass tree(TreeHeight, SubTreeHeight, loader->getBoxWidth(), loader->getCenterOfBox());
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
130 131 132


  { // -----------------------------------------------------
133 134 135 136 137
    if(app.global().processId() == 0){
      std::cout << "Creating & Inserting " << loader->getNumberOfParticles()
		<< " particles ..." << std::endl;
      std::cout << "\tHeight : " << TreeHeight << " \t sub-height : " << SubTreeHeight << std::endl;
    }
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
138 139 140 141 142 143 144 145 146 147 148
    time.tic();
    //

    struct TestParticle{
      int indexInFile;
      FPoint position;
      FReal physicalValue;
      const FPoint& getPosition(){
	return position;
      }
    };
149 150
    TestParticle* particles = new TestParticle[loader->getMyNumberOfParticles()];
    memset(particles, 0, (unsigned int) (sizeof(TestParticle) * loader->getMyNumberOfParticles()));
COULAUD Olivier's avatar
COULAUD Olivier committed
151
    
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
152
    //idx (in file) of the first part that will be used by this proc. 
153
    int idxStart = loader->getStart();
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
154
        
155
    for(int idxPart = 0 ; idxPart < loader->getMyNumberOfParticles() ; ++idxPart){
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
156 157 158 159
      //Storage of the index (in the original file) of each part.
      particles[idxPart].indexInFile = idxPart + idxStart;
      
      // Read particles from file
160
      loader->fillParticle(&particles[idxPart].position,&particles[idxPart].physicalValue);
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
161 162 163 164
    }
    
    FVector<TestParticle> finalParticles;
    FLeafBalance balancer;
165
    FMpiTreeBuilder< TestParticle >::DistributeArrayToContainer(app.global(), particles, loader->getMyNumberOfParticles(),
COULAUD Olivier's avatar
COULAUD Olivier committed
166 167 168 169
						 tree.getBoxCenter(),
						 tree.getBoxWidth(),
						 tree.getHeight(), &finalParticles,&balancer);

PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
170 171 172 173 174
    for(int idx = 0 ; idx < finalParticles.getSize(); ++idx){
      tree.insert(finalParticles[idx].position,finalParticles[idx].indexInFile,finalParticles[idx].physicalValue);
    }
    
    delete[] particles;
COULAUD Olivier's avatar
COULAUD Olivier committed
175
	  
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
176 177 178 179
    time.tac();
    std::cout << "Done  " << "(@Creating and Inserting Particles = "
	      << time.elapsed() << "s)." << std::endl;
  } // -----------------------------------------------------
COULAUD Olivier's avatar
COULAUD Olivier committed
180

PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
181 182
  { // -----------------------------------------------------
    std::cout << "\nRotation harmonic Spherical FMM Proc (P="<< P << ") ... " << std::endl;
COULAUD Olivier's avatar
COULAUD Olivier committed
183
	  
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
184 185 186 187
    time.tic();
    //
    // Here we use a pointer due to the limited size of the stack
    //
188
    KernelClass *kernels = new KernelClass(TreeHeight, loader->getBoxWidth(), loader->getCenterOfBox());
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    //
    FmmClassProc algorithm(app.global(),&tree, kernels);
    //
    algorithm.execute();   // Here the call of the FMM algorithm
    //
    time.tac();
    std::cout << "Done  " << "(@Algorithm = " << time.elapsed() << "s)." << std::endl;
  }
  // -----------------------------------------------------
  //
  // Some output
  //
  //

  { // -----------------------------------------------------
204
    long int N1=0, N2= loader->getNumberOfParticles()/2, N3= (loader->getNumberOfParticles()-1); ;
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
    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 int nbParticlesInLeaf = leaf->getTargets()->getNbParticles();
	const FReal*const physicalValues = leaf->getTargets()->getPhysicalValues();
COULAUD Olivier's avatar
COULAUD Olivier committed
223
	      
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
224
	const FVector<int>& indexes = leaf->getTargets()->getIndexes();
COULAUD Olivier's avatar
COULAUD Olivier committed
225
	      
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
226 227 228 229 230 231 232 233
	for(int idxPart = 0 ; idxPart < nbParticlesInLeaf ; ++idxPart){
	  const int 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] ;
COULAUD Olivier's avatar
COULAUD Olivier committed
234
	}
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
235 236 237 238 239 240 241 242
      });
	  
    FReal gloEnergy = app.global().reduceSum(energy);
    if(0 == app.global().processId()){
      std::cout <<std::endl << "Proc "<< app.global().processId() << " Energy: "<< gloEnergy <<std::endl;
      std::cout <<std::endl <<" &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& "<<std::endl<<std::endl;
    }
  }
243
  delete loader;
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
244
  // -----------------------------------------------------
245
  
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
246
  return 0;
COULAUD Olivier's avatar
COULAUD Olivier committed
247
}