Changeset 939

Show
Ignore:
Timestamp:
10/17/06 17:46:07 (7 years ago)
Author:
leo
Message:
  • add utilities from xerial
  • add antlr
Location:
sandbox/trunk/pdp
Files:
86 added
5 modified
1 moved

Legend:

Unmodified
Added
Removed
  • sandbox/trunk/pdp/README

    r936 r939  
     1 
     2 
     3* required libraries 
     4- Boost C++ Library (http://www.boost.org) 
     5- MPICH library  
     6 
     7 
  • sandbox/trunk/pdp/build/vc71/N-body.sln

    r936 r939  
    11Microsoft Visual Studio Solution File, Format Version 8.00 
    2 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "N-body", "N-body.vcproj", "{A053B03E-06E7-4AA8-AD0E-B5017B668233}" 
     2Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "N-body", "N-body\N-body.vcproj", "{A053B03E-06E7-4AA8-AD0E-B5017B668233}" 
     3        ProjectSection(ProjectDependencies) = postProject 
     4                {6B5B4036-E8C8-4C33-85C0-AA519562D9AC} = {6B5B4036-E8C8-4C33-85C0-AA519562D9AC} 
     5        EndProjectSection 
     6EndProject 
     7Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Replication", "Replication\Replication.vcproj", "{8730E6B6-A280-4DD1-ABDA-7C4631F40635}" 
     8        ProjectSection(ProjectDependencies) = postProject 
     9                {6B5B4036-E8C8-4C33-85C0-AA519562D9AC} = {6B5B4036-E8C8-4C33-85C0-AA519562D9AC} 
     10        EndProjectSection 
     11EndProject 
     12Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "xerial_util", "xerial_util\xerial_util.vcproj", "{6B5B4036-E8C8-4C33-85C0-AA519562D9AC}" 
     13        ProjectSection(ProjectDependencies) = postProject 
     14                {A31A9E8B-57D0-4C84-824C-17EAE8F6AE3A} = {A31A9E8B-57D0-4C84-824C-17EAE8F6AE3A} 
     15        EndProjectSection 
     16EndProject 
     17Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "antlr", "antlr\antlr.vcproj", "{A31A9E8B-57D0-4C84-824C-17EAE8F6AE3A}" 
    318        ProjectSection(ProjectDependencies) = postProject 
    419        EndProjectSection 
     
    1631                {A053B03E-06E7-4AA8-AD0E-B5017B668233}.Release.ActiveCfg = Release|Win32 
    1732                {A053B03E-06E7-4AA8-AD0E-B5017B668233}.Release.Build.0 = Release|Win32 
     33                {8730E6B6-A280-4DD1-ABDA-7C4631F40635}.Debug.ActiveCfg = Debug|Win32 
     34                {8730E6B6-A280-4DD1-ABDA-7C4631F40635}.Debug.Build.0 = Debug|Win32 
     35                {8730E6B6-A280-4DD1-ABDA-7C4631F40635}.Release.ActiveCfg = Release|Win32 
     36                {8730E6B6-A280-4DD1-ABDA-7C4631F40635}.Release.Build.0 = Release|Win32 
     37                {6B5B4036-E8C8-4C33-85C0-AA519562D9AC}.Debug.ActiveCfg = Debug|Win32 
     38                {6B5B4036-E8C8-4C33-85C0-AA519562D9AC}.Debug.Build.0 = Debug|Win32 
     39                {6B5B4036-E8C8-4C33-85C0-AA519562D9AC}.Release.ActiveCfg = Release|Win32 
     40                {6B5B4036-E8C8-4C33-85C0-AA519562D9AC}.Release.Build.0 = Release|Win32 
     41                {A31A9E8B-57D0-4C84-824C-17EAE8F6AE3A}.Debug.ActiveCfg = Debug|Win32 
     42                {A31A9E8B-57D0-4C84-824C-17EAE8F6AE3A}.Debug.Build.0 = Debug|Win32 
     43                {A31A9E8B-57D0-4C84-824C-17EAE8F6AE3A}.Release.ActiveCfg = Release|Win32 
     44                {A31A9E8B-57D0-4C84-824C-17EAE8F6AE3A}.Release.Build.0 = Release|Win32 
    1845        EndGlobalSection 
    1946        GlobalSection(ExtensibilityGlobals) = postSolution 
  • sandbox/trunk/pdp/build/vc71/N-body/N-body.vcproj

    r936 r939  
    2323                                MinimalRebuild="TRUE" 
    2424                                BasicRuntimeChecks="3" 
    25                                 RuntimeLibrary="5" 
     25                                RuntimeLibrary="1" 
     26                                TreatWChar_tAsBuiltInType="TRUE" 
     27                                ForceConformanceInForLoopScope="TRUE" 
     28                                RuntimeTypeInfo="TRUE" 
    2629                                UsePrecompiledHeader="0" 
    2730                                WarningLevel="3" 
     
    3235                        <Tool 
    3336                                Name="VCLinkerTool" 
     37                                AdditionalDependencies="cxxd.lib mpi.lib xerial_utild.lib" 
    3438                                OutputFile="$(OutDir)/N-body.exe" 
    3539                                LinkIncremental="2" 
     40                                AdditionalLibraryDirectories="../lib" 
     41                                IgnoreAllDefaultLibraries="FALSE" 
    3642                                GenerateDebugInformation="TRUE" 
    3743                                ProgramDatabaseFile="$(OutDir)/N-body.pdb" 
     
    114120                        UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"> 
    115121                        <File 
     122                                RelativePath="..\..\..\src\MPIInitializer.cpp"> 
     123                        </File> 
     124                        <File 
     125                                RelativePath="..\..\..\src\MPIInitializer.hpp"> 
     126                        </File> 
     127                        <File 
    116128                                RelativePath="..\..\..\src\N-body.cpp"> 
    117129                        </File> 
  • sandbox/trunk/pdp/src/N-body.cpp

    r936 r939  
    2121// $Author$  
    2222//-------------------------------------------------- 
     23#include <mpi.h> 
     24#include <iostream> 
    2325#include "N-body.hpp" 
     26#include "MPIInitializer.hpp" 
     27#include <boost/random.hpp> 
     28#include "xerial/util/cui/OptionParser.hpp" 
     29#include "xerial/log/Logger.hpp" 
     30#include "xerial/util/Message.hpp" 
     31#include "xerial/util/Format.hpp" 
     32#include "xerial/util/StringUtil.hpp" 
     33#include "xerial/util/Integer.hpp" 
     34#include "xerial/common/xerial_last.hpp" 
     35 
     36XERIAL_LOGGER(_logger, NBodySimulator) 
     37 
     38std::string 
     39Particle::toString() const 
     40{ 
     41        return XERIAL_MESSAGE(Triplet<double>::toString() << format(" m=%1%, v=%2%, a=%3%") % _mass % _velocity.toString() % _acceleration.toString()); 
     42} 
     43 
     44NBodySimulator::NBodySimulator(Vector<Particle>& particle, double gravity)  
     45: _particle(particle), _gravity(gravity), _time(0) 
     46{ 
     47         
     48} 
     49 
     50NBodySimulator::~NBodySimulator() 
     51{ 
     52 
     53} 
     54 
     55void 
     56NBodySimulator::naiveUpdate(double dt)  
     57{ 
     58        for(Vector<Particle>::iterator i = _particle.begin(); i != _particle.end(); ++i) 
     59        { 
     60                // update the acceleration of each particle by computing mutual gravitational force between it and the other particles 
     61                i->updateAcceleration(_particle.begin(), _particle.end(), _gravity); 
     62                i->move(dt);            // update the location  
     63        } 
     64        _time += dt; 
     65} 
     66 
     67void 
     68NBodySimulator::naiveSimulation(double dt, size_t times)  
     69{ 
     70        for(size_t step=0; step<times; ++step) 
     71        { 
     72                XERIAL_DEBUG(_logger, "step " << step); 
     73                naiveUpdate(dt); 
     74                XERIAL_DEBUG(_logger, "\n" << StringUtil::join(_particle, "\n")); 
     75        } 
     76} 
     77 
     78void 
     79NBodySimulator::parallelSimulation(double dt, size_t times)  
     80{ 
     81        int numParticle = _particle.size(); 
     82        MPI::COMM_WORLD.BCast(&numParticle, ) 
     83 
     84        for(size_t step=0; step<times; ++step) 
     85        { 
     86                XERIAL_DEBUG(_logger, "step " << step); 
     87                naiveUpdate(dt); 
     88                XERIAL_DEBUG(_logger, "\n" << StringUtil::join(_particle, "\n")); 
     89        } 
     90} 
    2491 
    2592 
     93int main(int argc, char** argv) 
     94{ 
     95        MPIInitializer m(argc, argv); 
     96        int processID = MPI::COMM_WORLD.Get_rank(); 
     97 
     98        XERIAL_INFO(_logger, "process id = " << processID); 
     99        enum 
     100        { 
     101                OPT_HELP, 
     102                OPT_VERBOSE, 
     103                OPT_DISTRIBUTE, 
     104                OPT_STEP, 
     105                OPT_NUM_PARTICLE 
     106        }; 
     107    OptionParser opt; 
     108        opt.add(Option(OPT_HELP, "h", "help", "display help messages")); 
     109        opt.add(Option(OPT_VERBOSE, "v", "verbose", "display verbose log messages")); 
     110        opt.add(Option(OPT_DISTRIBUTE, "d", "distribute", "distribute mode")); 
     111        opt.add(OptionWithArgument(OPT_STEP, "s", "step", "STEP", "num calculation steps (default = 100)")); 
     112        opt.add(OptionWithArgument(OPT_PARTICLE, "p", "particle", "NUM", "num particles (default = 10)")); 
     113 
     114        try 
     115        { 
     116                opt.getContext(argc, argv); 
     117                if(opt.isSet(OPT_HELP)) 
     118                { 
     119                        cerr << opt.createHelpMessage() << endl;  
     120                        return RETURN_WITH_SUCCESS; 
     121                } 
     122 
     123                // prepare random particles 
     124                boost::mt19937 mt; 
     125                boost::uniform_real<double> randomNumberRange(0, 10000); 
     126                boost::variate_generator<boost::mt19937, boost::uniform_real<double> > die(mt, randomNumberRange); 
     127                Vector<Particle> randomParticle; 
     128                int numParticle = Integer::parseInt(opt.getValue(OPT_NUM_PARTICLE, "10")); 
     129                for(int i=0; i<numParticle; i++) 
     130                { 
     131                        Particle p(die(), die(), die(), die()); 
     132                        p.setVelocity(Velocity(die(), die(), die())); 
     133                        XERIAL_DEBUG(_logger, p); 
     134                        randomParticle.add(p); 
     135                } 
     136                NBodySimulator simulator(randomParticle); 
     137                 
     138                int step = Integer::parseInt(opt.getValue(OPT_STEP, "100")); 
     139                if(opt.isSet(OPT_DISTRIBUTE)) 
     140                        simulator.parallelSimulation(1, step); 
     141                else 
     142                        simulator.naiveSimulation(1, step); 
     143 
     144        } 
     145        catch(InvalidParameterException& e) 
     146        { 
     147                cerr << e.what(); 
     148                cerr << opt.createHelpMessage() << endl; 
     149        } 
     150        catch(exception& e) 
     151        { 
     152                cerr << e.what(); 
     153        } 
     154 
     155} 
     156 
  • sandbox/trunk/pdp/src/N-body.hpp

    r936 r939  
    2828#include <vector> 
    2929#include "Triplet.hpp" 
     30#include <boost/utility/addressof.hpp> 
     31#include "xerial/util/Printable.hpp" 
     32#include "xerial/util/Vector.hpp" 
    3033 
    3134/* 
    32  * The class Universe defines global constant values; 
     35 * The class Universe defines global constant values. 
    3336 */  
    3437class Universe 
     
    4144struct Force : public Triplet<double> 
    4245{ 
     46        Force() : Triplet<double>(0, 0, 0) {} 
    4347        Force(double fx, double fy, double fz)  : Triplet<double>(fx, fy, fz) {} 
    4448 
     
    5559typedef Triplet<double> Velocity; 
    5660typedef Triplet<double> Acceleration; 
    57  
    5861 
    5962/** 
     
    7982                // double r = std::sqrt(distSquare); 
    8083                // double F = (Universe::gravity() * _mass * other._mass) / (r * r); 
    81                 // return Force(F * (dx / r), F * (dy / r), F * (dz / r));       
     84                // return Force(F * (dx / r), F * (dy / r), F * (dz / r));      //  dx / r = cosign of the angle between F and x axis 
    8285        } 
    8386 
     
    8891                for(ParticleIter i = begin; i != end; ++i) 
    8992                { 
    90                         if(this != i) 
     93                        if(this != boost::addressof(*i)) 
    9194                                force.push_back(gravitationalForce(*i, G)); 
    9295                } 
    9396                Force currentForce = Force::superposition(force.begin(), force.end()); 
    94                 _acceleration = currentForce / _mass;   // F = ma 
     97                _acceleration = currentForce / _mass;   // F = ma i.e. a = F / m 
    9598        } 
    9699 
     
    108111        const Acceleration& getAcceleration() const { return _acceleration; } 
    109112        void setAcceleration(const Acceleration& a) { _acceleration = a; } 
     113 
     114        std::string toString() const; 
     115 
     116        bool operator==(const Particle& other) const 
     117        { 
     118                return this == boost::addressof(other); 
     119        } 
    110120private: 
    111121        double _mass; 
     
    114124}; 
    115125 
     126class NBodySimulator 
     127{ 
     128public: 
     129        NBodySimulator(xerial::Vector<Particle>& particle, double gravity = Universe::gravity()); 
     130        virtual ~NBodySimulator(); 
     131 
     132        void naiveSimulation(double dt, size_t times); 
     133        void parallelSimulation(double dt, size_t times); 
     134 
     135protected: 
     136        void naiveUpdate(double dt); 
     137 
     138private: 
     139        xerial::Vector<Particle>& _particle; 
     140        double _time; 
     141        double _gravity; 
     142}; 
     143 
    116144 
    117145#endif //__NBODY_HPP20061016161026 
  • sandbox/trunk/pdp/src/Triplet.hpp

    r936 r939  
    2525#define __TRIPLET_HPP20061016161105 
    2626 
     27#include "xerial/util/Printable.hpp" 
     28#include "xerial/util/Message.hpp" 
     29#include "xerial/util/Format.hpp" 
    2730/* 
    2831 * Triplet class represents a 3-dimensional vector 
    2932 */  
    3033template <class T> 
    31 struct Triplet 
     34struct Triplet : public xerial::Printable 
    3235{ 
    3336        T x; 
     
    5962                return Triplet(x / val, y / val, z / val); 
    6063        } 
     64 
     65        std::string toString() const 
     66        { 
     67                using namespace xerial; 
     68                return XERIAL_MESSAGE(format("(%1%, %2%, %3%)") % x % y % z); 
     69        } 
    6170}; 
    6271