parallel programming using MPI and C++

Discussion in 'C++' started by aaragon, Aug 28, 2007.

  1. aaragon

    aaragon Guest

    Hi everyone,

    I started parallelizing some code that I had and for that I'm using
    the mpich library. I was able to run simulations in my one processor
    laptop as if it were a cluster of machines but now I have a problem
    for which the solution might be trivial. I would like to use master-
    slave parallelization to speed-up some computations but then I need
    only a UNIQUE instantiation of a class (the master node) and then
    communicate information of that class to other processes in remote
    nodes.

    I can do this by writing everything in the main function that I
    compile:

    int main(int argc,char *argv[]) {

    MPI::Init(argc,argv);
    int rank = MPI::COMM_WORLD.Get_rank();
    int size = MPI::COMM_WORLD.Get_size();

    if(rank == 0) {
    ClassA ca;
    ca.initialize;
    ca.compute;
    } else {
    // wait for instructions from master node
    // more code
    }

    MPI::Finalize();
    }

    However, I would like to abstract all this from the main() function
    and I don't know how to do this. My first attempt was to call MPI
    instructions from ClassA functions but of course this doesn't work
    because there are already several instantiations of ClassA. My last
    attempt was to provide a wrapper to ClassA, a class called
    ParallelClassA that had a static member varible, an instantiation of
    ClassA. This looks like follows:

    template <class ClassA>
    class ParallelClassA {
    static ClassA instance_;
    public:
    // public member functions, including initialize() and compute()
    };

    However, I think this is not feasible either because I guess that the
    same program will run in all the nodes so there won't be a unique
    instantiation even if the variable was declared static, right?

    Can anyone point me in the right direction to solve this problem? Does
    the concept of singleton solve this problem?

    Thank you,

     
    aaragon, Aug 28, 2007
    #1
    1. Advertising

  2. aaragon wrote:
    > I started parallelizing some code that I had and for that I'm using
    > the mpich library. I was able to run simulations in my one processor
    > laptop as if it were a cluster of machines but now I have a problem
    > for which the solution might be trivial. I would like to use master-
    > slave parallelization to speed-up some computations but then I need
    > only a UNIQUE instantiation of a class (the master node) and then
    > communicate information of that class to other processes in remote
    > nodes.
    >
    > I can do this by writing everything in the main function that I
    > compile:
    >
    > int main(int argc,char *argv[]) {
    >
    > MPI::Init(argc,argv);
    > int rank = MPI::COMM_WORLD.Get_rank();
    > int size = MPI::COMM_WORLD.Get_size();
    >
    > if(rank == 0) {
    > ClassA ca;
    > ca.initialize;
    > ca.compute;
    > } else {
    > // wait for instructions from master node
    > // more code
    > }
    >
    > MPI::Finalize();
    > }
    >
    > However, I would like to abstract all this from the main() function
    > and I don't know how to do this. My first attempt was to call MPI
    > instructions from ClassA functions but of course this doesn't work
    > because there are already several instantiations of ClassA. My last
    > attempt was to provide a wrapper to ClassA, a class called
    > ParallelClassA that had a static member varible, an instantiation of
    > ClassA. This looks like follows:
    >
    > template <class ClassA>
    > class ParallelClassA {
    > static ClassA instance_;
    > public:
    > // public member functions, including initialize() and compute()
    > };
    >
    > However, I think this is not feasible either because I guess that the
    > same program will run in all the nodes so there won't be a unique
    > instantiation even if the variable was declared static, right?


    Nope, not right. Unique instantiation is only relevant inside the
    bounds of one program. What you instantiate in other programs is
    not relevant to this one.

    >
    > Can anyone point me in the right direction to solve this problem? Does
    > the concept of singleton solve this problem?


    I think you're on the right track.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 29, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Natsu Mizutani
    Replies:
    0
    Views:
    594
    Natsu Mizutani
    Feb 20, 2004
  2. simo

    Parallel quicksort (MPI)

    simo, Mar 7, 2007, in forum: C Programming
    Replies:
    2
    Views:
    2,197
    user923005
    Mar 7, 2007
  3. Soren
    Replies:
    4
    Views:
    1,346
    c d saunter
    Feb 14, 2008
  4. Vivek Menon
    Replies:
    5
    Views:
    3,498
    Paul Uiterlinden
    Jun 8, 2011
  5. Vivek Menon
    Replies:
    0
    Views:
    1,809
    Vivek Menon
    Jun 10, 2011
Loading...

Share This Page