Changeset 942
- Timestamp:
- 10/19/06 00:21:36 (7 years ago)
- Location:
- sandbox/trunk/pdp/src
- Files:
-
- 2 modified
-
N-body.cpp (modified) (7 diffs)
-
N-body.hpp (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
sandbox/trunk/pdp/src/N-body.cpp
r941 r942 21 21 // $Author$ 22 22 //-------------------------------------------------- 23 #include <mpi.h> 23 24 #include "N-body.hpp" 24 25 #include <iostream> 25 #include "N-body.hpp"26 #include <boost/random.hpp> 26 27 #include "MPIInitializer.hpp" 27 #include <boost/random.hpp>28 28 #include "xerial/util/cui/OptionParser.hpp" 29 29 #include "xerial/log/Logger.hpp" … … 39 39 Particle::toString() const 40 40 { 41 return XERIAL_MESSAGE(_ coordinate.toString() << format(" m=%1%, v=%2%, a=%3%") % _mass % _velocity.toString() % _acceleration.toString());41 return XERIAL_MESSAGE(_id << ": " << _coordinate.toString() << format(" m=%1%, v=%2%, a=%3%") % _mass % _velocity.toString() % _acceleration.toString()); 42 42 } 43 43 … … 45 45 : _particle(particle), _gravity(gravity), _time(0) 46 46 { 47 47 prepareMPIDatatype(); 48 48 } 49 49 … … 99 99 // When this MPIInitializer instance is deallocated, MPI::Finalize() will be invoked automatically. 100 100 MPIInitializer m(argc, argv); 101 Particle::prepareMPIDatatype(); 101 102 102 int processID = MPI::COMM_WORLD.Get_rank(); 103 103 XERIAL_INFO(_logger, "process id = " << processID); … … 137 137 for(int i=0; i<numParticle; i++) 138 138 { 139 Particle p( die(), die(), die(), die());139 Particle p(i, die(), die(), die(), die()); 140 140 p.setVelocity(Velocity(die(), die(), die())); 141 141 XERIAL_DEBUG(_logger, p); … … 165 165 166 166 void 167 Particle::prepareMPIDatatype() 168 { 167 NBodySimulator::prepareMPIDatatype() 168 { 169 using namespace MPI; 170 169 171 Triplet<double> t; 170 Vector< MPI::Aint> tripletDisplacement;171 tripletDisplacement.add( MPI::Get_address(&t.x));172 tripletDisplacement.add( MPI::Get_address(&t.y));173 tripletDisplacement.add( MPI::Get_address(&t.z));174 MPI::Aint base = tripletDisplacement[0];172 Vector<Aint> tripletDisplacement; 173 tripletDisplacement.add(Get_address(&t.x)); 174 tripletDisplacement.add(Get_address(&t.y)); 175 tripletDisplacement.add(Get_address(&t.z)); 176 Aint base = tripletDisplacement[0]; 175 177 for(int i=0; i<3; ++i) 176 178 tripletDisplacement[i] -= base; 177 179 int tripletBlockLength[] = {1, 1, 1}; 178 MPI::Datatype TripletType = MPI::DOUBLE.Create_hindexed(3, tripletBlockLength, &(tripletDisplacement[0])); 180 TripletType = DOUBLE.Create_hindexed(3, tripletBlockLength, &(tripletDisplacement[0])); 181 if(TripletType.Get_size() != sizeof(Triplet<double>)) 182 TripletType = TripletType.Create_resized(Aint(0), Aint(sizeof(Triplet<double>))); 179 183 TripletType.Commit(); 180 184 XERIAL_DEBUG(_logger, "Triplet displacement: " << tripletDisplacement); … … 182 186 XERIAL_DEBUG(_logger, "sizeof(Triplet) = " << sizeof(Triplet<double>)); 183 187 184 Particle p[3]; 185 Vector<MPI::Aint> particleDisplacement;; 186 particleDisplacement.add(MPI::Get_address(&p[0]._coordinate)); 187 particleDisplacement.add(MPI::Get_address(&p[0]._velocity)); 188 particleDisplacement.add(MPI::Get_address(&p[0]._acceleration)); 189 particleDisplacement.add(MPI::Get_address(&p[0]._mass)); 190 base = particleDisplacement[0]; 188 ParticleType = Particle::createMPIDatatype(TripletType); 189 } 190 191 MPI::Datatype 192 Particle::createMPIDatatype(const MPI::Datatype& TripletType) 193 { 194 using namespace MPI; 195 196 Particle p; 197 Vector<Aint> particleDisplacement; 198 particleDisplacement.add(Get_address(&p._id)); 199 particleDisplacement.add(Get_address(&p._coordinate)); 200 particleDisplacement.add(Get_address(&p._velocity)); 201 particleDisplacement.add(Get_address(&p._acceleration)); 202 particleDisplacement.add(Get_address(&p._mass)); 203 Aint base = particleDisplacement[0]; 191 204 for(size_t i=0; i<particleDisplacement.size(); ++i) 192 205 particleDisplacement[i] -= base; 193 206 194 int particleBlockLength[] = {1, 1, 1, 1}; 195 MPI::Datatype particleDataType[] = {TripletType, TripletType, TripletType, MPI::DOUBLE}; 196 MPI::Datatype ParticleType = MPI::Datatype::Create_struct(4, particleBlockLength, &(particleDisplacement[0]), particleDataType); 207 int particleBlockLength[] = {1, 1, 1, 1, 1}; 208 Datatype particleDataType[] = {MPI::INT, TripletType, TripletType, TripletType, DOUBLE}; 209 Datatype ParticleType = Datatype::Create_struct(5, particleBlockLength, &(particleDisplacement[0]), particleDataType); 210 if(ParticleType.Get_size() != sizeof(Particle)) 211 ParticleType = ParticleType.Create_resized(Aint(0), Aint(sizeof(Particle))); 197 212 ParticleType.Commit(); 198 213 199 214 XERIAL_DEBUG(_logger, "particle displacement: " << particleDisplacement); 200 215 XERIAL_DEBUG(_logger, "particle size = " << ParticleType.Get_size()); 216 Aint lb, ub; 217 ParticleType.Get_extent(lb, ub); 218 XERIAL_DEBUG(_logger, "extent of particle = " << format("(%1%, %2%)") % lb % ub); 201 219 XERIAL_DEBUG(_logger, "sizeof(Particle) = " << sizeof(Particle)); 202 220 203 204 } 221 return ParticleType; 222 } -
sandbox/trunk/pdp/src/N-body.hpp
r941 r942 25 25 #define __NBODY_HPP20061016161026 26 26 27 #include <mpi.h> 27 28 #include <cmath> 28 29 #include <vector> … … 67 68 public: 68 69 Particle() 69 : _ mass(0)70 : _id(0), _mass(0) 70 71 {} 71 Particle( double x, double y, double z, double mass)72 : _ coordinate(x, y, z), _mass(mass)72 Particle(int id, double x, double y, double z, double mass) 73 : _id(id), _coordinate(x, y, z), _mass(mass) 73 74 {} 74 75 virtual ~Particle() {} … … 107 108 } 108 109 110 int getID() const { return _id; } 109 111 double getMass() const { return _mass; } 110 112 … … 122 124 } 123 125 124 static void prepareMPIDatatype();126 static MPI::Datatype createMPIDatatype(const MPI::Datatype& tripletDataType); 125 127 private: 128 int _id; 126 129 Triplet<double> _coordinate; 127 130 Velocity _velocity; … … 139 142 void parallelSimulation(double dt, size_t times); 140 143 144 141 145 protected: 142 146 void naiveUpdate(double dt); 143 147 144 148 private: 149 void prepareMPIDatatype(); 150 145 151 xerial::Vector<Particle>& _particle; 146 152 double _time; 147 153 double _gravity; 154 155 MPI::Datatype ParticleType; 156 MPI::Datatype TripletType; 148 157 }; 149 158


