parallel programming using MPI and C++

A

aaragon

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,

 
V

Victor Bazarov

aaragon said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top