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,
aČ