I am attaching a Minimal Reproducing Example of the error that I am facing in a large code. Say I have 2 processes, P0 and P1. I am declaring an array int arr[2] inside P0 and storing a value in arr[0]. Then I am redeclaring int arr[2] for P1 only, but whenever I am trying to access it inside P1, I am getting ‘arr’ undeclared (first use in this function) 37 | arr[1]=1; error. I am attaching the code here.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"
int main( int argc, char *argv[])
{
MPI_Init(&argc, &argv);
int myrank, size; //size will take care of number of processes
double sTime, eTime, time,max_time;
MPI_Comm_rank(MPI_COMM_WORLD, &myrank) ;
MPI_Comm_size(MPI_COMM_WORLD, &size);
int r=0, c;
if(myrank==0)
{
int arr[2];
arr[0]=1;
}
if(myrank!=0)
{
int arr[2];
}
if(myrank==1)
{
arr[1]=2;
MPI_Send(&arr[1], 1 , MPI_INT, 0 /*dest*/ , 100 /*tag*/ , MPI_COMM_WORLD);
}
MPI_Status status[2];
if(myrank==0)
{
MPI_Recv(&arr[1],1 , MPI_INT, 1 /*src*/ , 100 /*tag*/, MPI_COMM_WORLD,&status[1]);
for(int i=0;i<2;i++)
{
printf(" %d",arr[i]);
}
}
MPI_Finalize();
return 0;
}
To prevent this error I have tried declaring int arr[2] inside
if(myrank==1){
int arr[2];
arr[1]=2;
MPI_Send(arr[1], 1 , MPI_INT, 0 /*dest*/ , 100 /*tag*/ , MPI_COMM_WORLD);
}
But this time I get this error,
‘arr’ undeclared (first use in this function) 47 | MPI_Recv(arr[0],1 , MPI_INT, 1 /src/ , 100 /tag/, MPI_COMM_WORLD,&status[1]);
I am not able to get what is wrong here. Any suggestions will be really helpful.