The exercise I'm trying to do asks us to build a ring of processes where each of them passes the number x to the following process, only the process with rank=0 decreases the x. When x is equal to zero the program ends. The working code is the following:
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include "mpi.h"
int main(int argc, char** argv) {
int x = 0;
int tag = 99;
MPI_Status status;
MPI_Init(&argc, &argv);
int myRank;
MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
int size;
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (myRank == 0) {
printf("Process %d enter a value: \n", myRank);
fflush(stdout);
scanf_s("%d", &x);
while (x>0) {
MPI_Send(&x, 1, MPI_INT, 1, tag, MPI_COMM_WORLD);
MPI_Recv(&x, 1, MPI_INT, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &status);
printf("Process %d : lap number %d \n", myRank, x);
x--;
}
}
else {
do {
MPI_Recv(&x, 1, MPI_INT, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &status);
printf("Process %d : lap number %d \n", myRank, x);
MPI_Send(&x, 1, MPI_INT, (myRank 1) % size, tag, MPI_COMM_WORLD);
} while (x > 1);
}
MPI_Finalize();
return 0;
}
but if i change the last do while cycle like this:
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include "mpi.h"
int main(int argc, char** argv) {
int x = 0;
int tag = 99;
MPI_Status status;
MPI_Init(&argc, &argv);
int myRank;
MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
int size;
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (myRank == 0) {
printf("Process %d enter a value: \n", myRank);
fflush(stdout);
scanf_s("%d", &x);
while (x>0) {
MPI_Send(&x, 1, MPI_INT, 1, tag, MPI_COMM_WORLD);
MPI_Recv(&x, 1, MPI_INT, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &status);
printf("Processo %d : giro numero %d \n", myRank, x);
x--;
}
}
else {
while (x>0) {
MPI_Recv(&x, 1, MPI_INT, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &status);
printf("Processo %d : giro numero %d \n", myRank, x);
MPI_Send(&x, 1, MPI_INT, (myRank 1) % size, tag, MPI_COMM_WORLD);
}
}
MPI_Finalize();
return 0;
}
I get this error:
job aborted:
[ranks] message
[0] fatal error
Fatal error in MPI_Send: Other MPI error, error stack:
MPI_Send(buf=0x000000F6100FF514, count=1, MPI_INT, dest=1, tag=99, MPI_COMM_WORLD) failed
failed to attach to a bootstrap queue - 10616:296
[1-2] terminated
But I don't understand why... Shouldn't the two codes be equivalent?
CodePudding user response:
You set x
to zero, and then all processes but zero do while (x>0) { stuff }
. So they don't do anything.