The following code is my trial to write a fault-tolerant application on
OpenMPI; however, it still does not trap exceptions:
#include <mpi.h>
//#include <mpi++.h>
#include <iostream>
#include <unistd.h>
#include <cstdio>
#include <fstream>
#include <string>
#define BUFSIZE 100
using namespace std;
using namespace MPI;
static int nerr = 0;
static int iter = 0;
static ofstream fout;
void mpiErrorFunction( MPI::Comm &comm, int *code, ... )
{
cout << "Encountered an MPI error No. " << ++nerr << endl << flush;
fout << "Encountered an MPI error No. " << ++nerr << endl << flush;
fout.flush();
}
void errhandler()
{
cout << "Encountered an MPI error No. " << ++nerr << endl << flush;
fout << "Encountered an MPI error No. " << ++nerr << endl << flush;
fout.flush();
}
int main(int argc, char *argv[])
{
cout << "Welcome to MPI error handling testing." << endl << flush;
fout.flush();
int buf[BUFSIZE];
char msg[BUFSIZE];
MPI::Status status;
char fileName[100];
char buff[1000];
cout << "Initializing MPI." << endl << flush;
fout.flush();
MPI::Init_thread(MPI::THREAD_MULTIPLE);
//MPI::Init(argc, argv);
cout << "MPI Initialized." << endl << flush;
fout.flush();
int myrank = MPI::COMM_WORLD.Get_rank();
int numprocs = MPI::COMM_WORLD.Get_size();
sprintf(fileName,"MPItesting%i_%i.txt", myrank, numprocs);
fout.open(fileName);
sprintf(buff, "My task id is %d & No. of procs is %i\n", myrank,
numprocs);
cout << buff << flush;
fout << buff << flush;
fout.flush();
if (myrank == 0) {
//MPI::Errhandler errHandler =
// MPI::Comm::Create_errhandler (mpiErrorFunction);
//MPI::COMM_WORLD.Set_errhandler(errHandler);
//MPI::COMM_WORLD.Set_errhandler(MPI::ERRORS_RETURN);
MPI::COMM_WORLD.Set_errhandler(MPI::ERRORS_THROW_EXCEPTIONS);
while (nerr < 20 && iter++ < 100) {
for (int i=1; i<numprocs; i++) {
sprintf(msg,"%3i Hello there No. %i.", iter, i);
sprintf(buff, "Message %i is being sent to %i\n", iter, i);
cout << buff << flush;
fout << buff << flush;
fout.flush();
try {
MPI::COMM_WORLD.Send(msg, BUFSIZE, CHAR, i, 99);
}
catch (MPI::Exception ex) {
cout << "Could not send successfully to task No. " << i
<< endl << flush;
fout << "Could not send successfully to task No. " << i
<< endl << flush;
fout.flush();
}
sleep(1);
}
}
}
else {
while (nerr < 20 && iter++ < 100) {
try {
MPI::COMM_WORLD.Recv(msg, BUFSIZE, CHAR, 0, 99);
sprintf(buff,"%3i Receiving message No. %s\n", iter, msg);
cout << buff << flush;
fout << buff << flush;
fout.flush();
}
catch (MPI::Exception ex) {
cout << "Could not receive successfully "<< endl << flush;
fout << "Could not receive successfully "<< endl << flush;
fout.flush();
}
//sleep(myrank);
}
}
cout << "Task no. " << myrank << " is existing now." << endl << flush;
fout << "Task no. " << myrank << " is existing now." << endl << flush;
fout.flush();
fout.close();
MPI::Finalize(); // MPI
return 0;
}
--
Regards,
Mohammad Huwaidi
We can't resolve problems by using the same kind of thinking we used
when we created them.
--Albert Einstein
|