-
February 14th, 2012, 11:23 AM
#1
[RESOLVED] Using MPI in C++ Solution
Hi guys,
I'm wondering whether it's possible to implement MPI to execute a process in parallel from deep within a C++ solution.
I've created a solution on VS2005 that links to several static libraries, and within one of these libraries there is a set series of tasks that require execution many times - they're independent so I'd like them to execute them using MPI if possible to speed up the run time.
The pseudo-code is:
Code:
for(i = 0; i < n; i++) // n is number of times to execute process
{
PerformTask(data[i]); // perform the tasks required for each iteration of process
}
So, instead of conducting the tasks within PerformTask() in series n times, I want to split the array data between multiple processes such that they can each be allocated an even proportion of data to perform the tasks on. Pretty textbook reason for wanting MPI, right?
Now, I've read up and understood the basics of MPI implementation, but all the examples I've seen are called within the main() function of the program. But I need to do all this from within a static library, is this possible?
I've made some early attempts at implementing this, but get an error indicating the process wasn't initialised: "Error encounted before initializing MPICH", which I assumed would be due to trying to make the MPI calls outside of the main() function.
By the way, I have tested the set-up of MPI within VS2005 with some simple examples, and they execute without issue.
Cheers,
Jonathan
P.S. If there are any alternative methods to MPI, then I'm afraid I can't use them, as I want this to project to eventually run on the department supercomputer, and that requires MPI-implemented parallel programming
-
February 14th, 2012, 02:22 PM
#2
Re: Using MPI in C++ Solution
If I understand you correctly, you want to execute PerformTask() by different processors, but you have other code that executes only once. You can put all that code in rank=0 to be executed only once and then rank=0 processor sends data to others.
Code:
if(rank = 0) {
//prepare data
.....
for(each processor) MPI_send(data[i])
} else {
MPI_Recieve(data[i]); //blocking
//process data & send back result
}
I am assuming that you are preparing the data which is why you can't automatically start executing. All the processors are started at start up of the program (mpirun -np 2), so you can't just start a certain number of processors in the middle of your program AFAIK. If you don't engage the processors immediately, you have to block them with a barrier or some other blocking call such as MPI_Reciev.
Hope it helps.
-
February 15th, 2012, 04:43 AM
#3
Re: Using MPI in C++ Solution
Oki doki. I think I understand that. So, for example, code could be something like this:
Code:
int main(int argc, char *argv[])
{
int active, nTasks, rank, n;
active = MPI_Init(&argc, &argv);
if(active == MPI_SUCCESS) // run with MPI
{
MPI_Status status;
int stacksize = (n / nTasks);
int offset;
MPI_Comm_size(MPI_COMM_WORLD, &nTasks);
MPI_Comm_rank(MPI_COMM_WORLD, &mpiTask);
if(task == 0)
{
// set up process using static libraries
offset = 0;
for(int dest = 1; test < nTasks; dest++, offset += stacksize)
{
MPI_Send(&offset, 1, MPI_INT, dest, 1, MPI_COMM_WORLD);
MPI_Send(&data[offset], stacksize, MPI_DOUBLE, dest, 2, MPI_COMM_WORLD);
}
offset = 0;
for(int source= 1; source< nTasks; source++)
{
MPI_Recv(&offset, 1, MPI_INT, source, 1, MPI_COMM_WORLD, &status);
MPI_Recv(&data[offset], stacksize, MPI_DOUBLE, source, 2, MPI_COMM_WORLD, &status);
}
}
else
{
MPI_Recv(&offset, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &status);
MPI_Recv(&data[offset], stacksize, MPI_DOUBLE, 0, 2, MPI_COMM_WORLD, &status);
// perform tasks on stacksize of data array size n
MPI_Send(&offset, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
MPI_Send(&data[offset], stacksize, MPI_DOUBLE, 0, 2, MPI_COMM_WORLD);
}
MPI_Finalize();
}
else // run without MPI
{
MPI_Finalize();
// set up process using static libraries
// perform tasks on all of data array size n
}
}
Note, this permits running without MPI if it fails to initialise.
So, as long as my calls to processors are within main(), they will be called? I can let the master processor do all the hard work in setting up the process, before sending the messages via MPI to the other processors?
Cheers,
Jonathan
-
February 15th, 2012, 09:00 AM
#4
Re: Using MPI in C++ Solution
I think you got the message exchange between slaves and master right. Mpi starts the number of processors you specified at start up and sets them up for message passing. Any other process you fork or threads you start from there onwards, you will have to manage your self. So if you want to use those processors mpi started again and again for different tasks, the slave processors should be put in an idle loop. Otherwise they will exit after performing one task (remember you can't start them within the program AFAIK). The slaves can continuously poll for messages f.i using MPI_Iprobe , and then check what type of message it is and what data to receive, and then do the required task.
So, as long as my calls to processors are within main(), they will be called? I can let the master processor do all the hard work in setting up the process, before sending the messages via MPI to the other processors?
You can call MPI_Send() from anywhere so that is no problem. If you don't want to overload the master processor with preparation work, then you will have to share that work too using the polling method I mentioned above. Even better is to add one more send - recieve exchange b/n slave and master to share the preparation work as well, since you know the tasks you will execute apriori.
Cheers
-
February 15th, 2012, 09:18 AM
#5
Re: Using MPI in C++ Solution
Sweet, sounds good, I think I get all that, I'll give it a go and be back on if there are any problems...
Thanks for you help!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|