0
votes

I'm an mpi newbie. I'm trying to parallelize my code with mpi (need to run some experiments faster). It should work like this: master sends an array of strings to the slaves, they do some job and send status_ready back to the master. When all slaves are ready, master goes into a loop and iterativelly sends a vector of doubles to the slaves, slaves process this vector and send their results (2 vectors) back to the master. When all tje messages are received, master will process it and the loop iterates (master sends the results to the slaves, etc.) It should work like this

// master - init
sendVectorWithStrings2Slaves();

// slaves
doSomeStuff();
sendReady();

// master
receiveStatuses();

// master - when all slaves are ready
while(some condition)
{
  // master
  sendVector2Slaves()

  //slaves
  receiveVector();
  process();
  sendTwoVectorsBack2Master();

  // master
  receiveAllVectors();
  checkThatAllMessagesReceived();
  processResults();
}

stopSlaves();

can anyone help me with mpi?

1

1 Answers

0
votes

Some guidelines for your pseudo-code can be found below:

int myrank,ntasks;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
MPI_Comm_size(MPI_COMM_WORLD, &ntasks);

if (myrank == 0) {
  // master - init
  for (int rank = 1; rank < ntasks; ++rank)
    sendVectorWithStrings2Slave(rank);
  for (int rank = 1; rank < ntasks; ++rank)
    recieveReady();
} else {
  // slaves
  recieveVectorWithStringsFromMaster();
  doSomeStuff();
  sendReady();
}

// master - when all slaves are ready
while(some condition)
{
  if (myrank == 0) {
    // master
    for (int rank = 1; rank < ntasks; ++rank)
      sendVector2Slave(rank)

    // master
    for (int rank = 1; rank < ntasks; ++rank)
      receiveVector();

    processResults();
  } else {
    //slaves
    receiveVector();
    process();
    sendTwoVectorsBack2Master();
  }
}