'MPI process won't increase
I am trying to write a program with 64 processes in 4 nodes (16 processes per node). Each process will print, increment, and send a value to the next process. The value starts from 0 and continues until it has reached 31. Once it reaches 31, the value should start decrement until it's back to 0.
Like this: process 0 prints value 0, increments it to 1, and sends it to process 1. Process 1 receives the value 1, prints it, increments it to 2, and sends it to process 2, and so on. Processes 0 to 31 should increment the value from 0 to 31, and processes 32 to 63 should decrement the value from 31 back to 0.
I am running into an issue where the program stops when process 32 receives the value 30 from process 31. Below is my code so far.
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define NUM_PROCESSES 64
int main (int argc, char** argv) {
int world_rank;
int world_size;
int value;
int namelen;
char processor_name[MPI_MAX_PROCESSOR_NAME];
bool increment = true;
int count = 0;
MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
MPI_Get_processor_name(processor_name, &namelen);
/* increments from 0 to 31 for 32 processes */
value = 0;
//printf("Start incrementing\n");
while (value < NUM_PROCESSES / 2) {
if (world_rank == value) {
printf("Process %d of node %s has value %d\n",
world_rank, processor_name, value);
value++;
if (world_rank+1 < NUM_PROCESSES / 2) {
MPI_Send(&value, 1, MPI_INT, world_rank+1, 0, MPI_COMM_WORLD);
printf("Process %d of node %s sent the new value %d to process %d\n",
world_rank, processor_name, value, world_rank+1);
}
}
else {
MPI_Recv(&value, 1, MPI_INT, world_rank-1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("Process %d of node %s received the value %d from process %d\n",
world_rank, processor_name, value, world_rank-1);
}
}
value--;
while (value >= 0) {
if (world_rank - value == count) {
printf("Process %d of node %s has value %d\n",
world_rank, processor_name, value);
value--;
count += 2;
if (world_rank+1 < NUM_PROCESSES) {
MPI_Send(&value, 1, MPI_INT, world_rank+1, 0, MPI_COMM_WORLD);
printf("Process %d of node %s sent the new value %d to process %d\n",
world_rank, processor_name, value, world_rank+1);
}
}
else {
MPI_Recv(&value, 1, MPI_INT, world_rank-1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("Process %d of node %s received the value %d from process %d\n",
world_rank, processor_name, value, world_rank-1);
}
}
MPI_Finalize();
return 0;
}
This is the last few lines of the output:
Process 30 of node c0376 sent the new value 31 to process 31
Process 31 of node c0376 received the value 31 from process 30
Process 31 of node c0376 has value 31
Process 31 of node c0376 has value 31
Process 31 of node c0376 sent the new value 30 to process 32
Process 32 of node c0738 received the value 30 from process 31
What is the problem here?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
