I am very new to MPI programing. In the following code, I am trying to add first 3 elements using process 1 and last 3 elements using process 2. Result should show Sum is 11
for the first three elements and Sum is 18
for the last three elements. I see the reason for my problem is that at rank 2 (process 2) it reads the arr[3] as the very first element of the array. I am really lost about why it doesn't read the 3rd element of the array correctly at rank 2
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
MPI_Init(&argc, &argv);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
int tag2 = 1;
int tag1 = 2;
int arr[7] = { 6,2,3,9,4,5};
printf ("\n--Current Rank: %d\n", world_rank);
int index;
int source = 0;
int dest;
if (world_rank == 0)
{
printf("* Rank 0 excecuting");
index = 0;
dest = 1;
MPI_Send(&index, 1, MPI_INT, dest, tag1, MPI_COMM_WORLD);
MPI_Send(&arr, 2, MPI_INT, dest, tag2, MPI_COMM_WORLD);
index = 3;
dest = 2;
MPI_Send(&index, 1, MPI_INT, dest, tag1, MPI_COMM_WORLD);
MPI_Send(&arr, 1, MPI_INT, dest, tag2, MPI_COMM_WORLD);
}
else
{
int sum = 0;
int i;
MPI_Recv(&index, 1, MPI_INT, source, tag1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv(&arr[index], 20, MPI_INT, source, tag2, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("At Rank: %d index is: %d\n", world_rank, index);
for(i = index; i<=index+2; i++)
{
printf("i: %d and arr[i]: %d\n", i, arr[i]);
sum = arr[i]+sum;
}
printf("\n Sum is: %d and arr[3]: %d\n", sum, arr[3]);
}
MPI_Finalize();
}
Result I get after using the command: mpirun -np 3 test1
--Current Rank: 0
--Current Rank: 1
At Rank: 1 index is: 0
i: 0 and arr[i]: 6
i: 1 and arr[i]: 2
i: 2 and arr[i]: 3
Sum is: 11 and arr[3]: 9 //here it shows the correct value of 3rd array element
--Current Rank: 2
At Rank: 2 index is: 3
i: 3 and arr[i]: 6 //Error happens here
i: 4 and arr[i]: 4
i: 5 and arr[i]: 5
Sum is: 15 and arr[3]: 6 //Show the wrong array value for 3rd element
* Rank 0 excecuting