Changeset 942

Show
Ignore:
Timestamp:
10/19/06 00:21:36 (7 years ago)
Author:
leo
Message:

modified the datatype definition

Location:
sandbox/trunk/pdp/src
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • sandbox/trunk/pdp/src/N-body.cpp

    r941 r942  
    2121// $Author$  
    2222//-------------------------------------------------- 
    23 #include <mpi.h> 
     23 
     24#include "N-body.hpp" 
    2425#include <iostream> 
    25 #include "N-body.hpp" 
     26#include <boost/random.hpp> 
    2627#include "MPIInitializer.hpp" 
    27 #include <boost/random.hpp> 
    2828#include "xerial/util/cui/OptionParser.hpp" 
    2929#include "xerial/log/Logger.hpp" 
     
    3939Particle::toString() const 
    4040{ 
    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()); 
    4242} 
    4343 
     
    4545: _particle(particle), _gravity(gravity), _time(0) 
    4646{ 
    47          
     47        prepareMPIDatatype(); 
    4848} 
    4949 
     
    9999        // When this MPIInitializer instance is deallocated, MPI::Finalize() will be invoked automatically. 
    100100        MPIInitializer m(argc, argv);    
    101         Particle::prepareMPIDatatype();  
     101 
    102102        int processID = MPI::COMM_WORLD.Get_rank(); 
    103103        XERIAL_INFO(_logger, "process id = " << processID); 
     
    137137                for(int i=0; i<numParticle; i++) 
    138138                { 
    139                         Particle p(die(), die(), die(), die()); 
     139                        Particle p(i, die(), die(), die(), die()); 
    140140                        p.setVelocity(Velocity(die(), die(), die())); 
    141141                        XERIAL_DEBUG(_logger, p); 
     
    165165 
    166166void 
    167 Particle::prepareMPIDatatype()  
    168 { 
     167NBodySimulator::prepareMPIDatatype()  
     168{ 
     169        using namespace MPI; 
     170 
    169171        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]; 
    175177        for(int i=0; i<3; ++i) 
    176178                tripletDisplacement[i] -= base; 
    177179        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>))); 
    179183        TripletType.Commit(); 
    180184        XERIAL_DEBUG(_logger, "Triplet displacement: " <<  tripletDisplacement); 
     
    182186        XERIAL_DEBUG(_logger, "sizeof(Triplet) = " << sizeof(Triplet<double>)); 
    183187 
    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 
     191MPI::Datatype 
     192Particle::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]; 
    191204        for(size_t i=0; i<particleDisplacement.size(); ++i) 
    192205                particleDisplacement[i] -= base; 
    193206 
    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))); 
    197212        ParticleType.Commit(); 
    198213 
    199214        XERIAL_DEBUG(_logger, "particle displacement: " <<  particleDisplacement); 
    200215        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); 
    201219        XERIAL_DEBUG(_logger, "sizeof(Particle) = " << sizeof(Particle)); 
    202220 
    203  
    204 } 
     221        return ParticleType; 
     222} 
  • sandbox/trunk/pdp/src/N-body.hpp

    r941 r942  
    2525#define __NBODY_HPP20061016161026 
    2626 
     27#include <mpi.h> 
    2728#include <cmath> 
    2829#include <vector> 
     
    6768public: 
    6869        Particle() 
    69                 : _mass(0) 
     70                : _id(0), _mass(0) 
    7071        {} 
    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) 
    7374        {} 
    7475        virtual ~Particle() {} 
     
    107108        } 
    108109 
     110        int getID() const { return _id; } 
    109111        double getMass() const { return _mass; }  
    110112 
     
    122124        } 
    123125 
    124         static void prepareMPIDatatype(); 
     126        static MPI::Datatype createMPIDatatype(const MPI::Datatype& tripletDataType); 
    125127private: 
     128        int _id; 
    126129        Triplet<double> _coordinate; 
    127130        Velocity _velocity; 
     
    139142        void parallelSimulation(double dt, size_t times); 
    140143 
     144         
    141145protected: 
    142146        void naiveUpdate(double dt); 
    143147 
    144148private: 
     149        void prepareMPIDatatype(); 
     150 
    145151        xerial::Vector<Particle>& _particle; 
    146152        double _time; 
    147153        double _gravity; 
     154 
     155        MPI::Datatype ParticleType; 
     156        MPI::Datatype TripletType; 
    148157}; 
    149158