0

The problem is that MPI structure declared inside main() is not visible inside functions. Compiler gives following error:

my_prog.c: In function ‘void get(int)’:
my_prog.c:585:21: error: ‘MPI_GETSET’ was not declared in this scope
MPI_Send(&msg, 1, MPI_GETSET, rank, 0, MPI_COMM_WORLD);

Code sections are:

struct mpi_send{
  int job;
  int z;
  int img;
  short grp;
  short stp;
};

mpi_send msg;

int main(int argc, char** argv) {
    MPI_Init(&argc, &argv);
...
    MPI_Datatype MPI_GETSET;
    MPI_Datatype type[5] = {MPI_INT,MPI_INT,MPI_INT,MPI_SHORT,MPI_SHORT};
    int blocklen[5] = {1,1,1,1,1};
    MPI_Aint disp[5];
    disp[0]=sizeof(int); disp[1]=sizeof(int); disp[2]=sizeof(int);
    disp[3]=sizeof(short); disp[4]=sizeof(short);
    MPI_Type_create_struct(5, blocklen, disp, type, &MPI_GETSET);
    MPI_Type_commit(&MPI_GETSET);
...
    get(13);
}

void get(int z){
...
    MPI_Send(&msg, 1, MPI_GETSET, rank, 0, MPI_COMM_WORLD);
    MPI_Recv(&msg, 1, MPI_GETSET, rank, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
...
}

I would appreciate clarification why I can not use MPI_GETSET inside functions. Thanks in advance.

Gilles Gouaillardet
  • 8,193
  • 11
  • 24
  • 30
N.A.
  • 1

1 Answers1

1

The scope of MPI_GETSET is the main() subroutine.

A direct consequence is this variable is not defined inside the get() subroutine.

You basically have two options in order to move forward :

  • pass MPI_GETSET as a parameter of the get() subroutine
  • declare MPI_GETSET as a global variable

If your compiler is recent enough (note you might need some extra flags), a third option would be to declare the get() subroutine inside the main() subroutine. (feel free to refer to Nested function in C)

Gilles Gouaillardet
  • 8,193
  • 11
  • 24
  • 30
  • Thank You. At first I did try to make it global with declaration `MPI_GETSET msg` which was not working, since it was defined later in `main()`. But when I set (as global var.) `MPI_Datatype MPI_GETSET` it compiles. – N.A. Jul 24 '18 at 13:08
  • Sure, feel free to accept this answer if it solved your problem. – Gilles Gouaillardet Jul 25 '18 at 09:12