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.
That error is because you declared a variable inside a if statement and you want to used outside that if. In C that is not possible, when you do:
if(myrank==0)
{
int arr[2];
arr[0]=1;
}
the array int arr[2];
will only exist in the scope of that if statement. Therefore, if you try to access it outside it leads to a compilation error.
To get around you can either declare int arr[2]
outside the if
statement (but then all MPI process would allocated), or declare a pointer to int
outside and only allocate memory for that pointer for the process that you want, namely:
int *arr = NULL;
if(myrank==0)
{
arr = malloc(sizeof(int) * 2);
arr[0]=1;
}
when the array is not longer needed do not forget to free it:
free(arr);
Another option (that works in your example) is to rearrange the code so that you have all the code related with the one process in one if
branch, and the code from the other process in another if
branch, namely:
#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_Status status[2];
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;
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]);
}
if(myrank==1)
{
int arr[2];
arr[1]=2;
MPI_Send(&arr[1], 1 , MPI_INT, 0 /*dest*/ , 100 /*tag*/ , MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;
}