1

I want to employ the MPI function MPI_GATHERV, where each MPI rank has a certain buffer with varying sizes that needs to be Gathered at the Root process. My root process will only gather the buffers, but has no send buffer itself, as usual.

E.g.

RANK1

 buf_sz =3
 buf(1) = 1 
 buf(2) = 2 
 buf(3) = 3

Rank 2

 buf_sz =3
 buf(1) = 4 
 buf(2) = 5 
 buf(3) = 6

After MPI_GATHERV, my Root should have

 buf(1) = 1 
 buf(2) = 2 
 buf(3) = 3
 buf(4) = 4 
 buf(5) = 5 
 buf(6) = 6

As for now, this will mean that at the Root I will have to apply a sendbuf which is allocated with zero elements and zero count. I am not sure how well defined and safe this is generally?

Sample code

 if(myrankid == root)then 
    allocate(displ(3)) 
   allocate(counts(3)) 
   displ(1) = 0 
   displ(2) = 0 
   displ(3) = 3
   counts(1) = 0 
   counts(2) = 3 
   counts(3) = 3 
   allocate(buf_send(0))
    sendcount = 0
   allocate(recvbuf(6))
  else
   allocate(displ(1)) 
   allocate(counts(1)) 
   allocate(buf_send(3)) 
   allocate(recvbuf(1))
    sendcount = 3
  endif

  MPI_Gatherv(buf_send, sendcount, mpi_integer,&
            recvbuf, counts, displs,&
            mpi_integer,  root, mpi_comm_world)
ATK
  • 1,296
  • 10
  • 26
  • 1
    Show us your code. – John Zwinck Oct 27 '19 at 08:02
  • Is there anything wrong with your code? – Gilles Gouaillardet Oct 27 '19 at 08:37
  • My question is if it is safe to use zero counts and buffer allocated with zero elements. When I read around the answers are mixed with regards to this. So I want to know whether that is possible or maybe I should look for a different solution. As you also can seen in my sample, I am allocating counts and displs with one elements for the other ranks, and I am doing so just to avoid any problems that could occur when using unallocated arrays. Essentially, those two arrays are of no imporantance with regards to the other ranks than root – ATK Oct 27 '19 at 08:44
  • It is safe and you do not need to allocate things when the standard states they are not significant on non root ranks. – Gilles Gouaillardet Oct 27 '19 at 09:11
  • 2
    @GillesGouaillardet - 100% disagree. It is always non-standard to pass an unallocated allocatable array if the corresponding dummy argument doesn't have the allocatable attribute. This is totally irrespective of whether the called routine accesses the argument or not. As MPI has no arguments with the allocatable attribute it is therefore always illegal for MPI calls, and the calling routine should allocate, probably to zero size, in such cases as the above. – Ian Bush Oct 27 '19 at 10:14
  • @IanBush : Is it legal and standard to allocate an array with zero size and insert a zero for it corresponding size count and call a MPI routine? – ATK Oct 27 '19 at 11:52
  • What I can say is that we have experienced problems when passed unallocated variables to MPI routine. That is way we pass a dummy ALLOCATED array with size 1 for the those ranks that the arrays are essentially not significant . Beside, I was more questioning whether I could apply a MPI_GATHERV where the "root" had nothing to contribute in terms of setting the "gathered" array. Hence, its associated send_buf and count would be zero. I am questioning whether this also could to potential problems i.e. whether the sample code shown could lead to problems – ATK Oct 27 '19 at 11:54
  • @A2LBK if you face such an issue with Open MPI, please report this at https://github.com/open-mpi/ompi/issues or simply drop me a note. – Gilles Gouaillardet Oct 27 '19 at 12:41
  • 2
    @A2LBK Yes, it is legal to allocate to zero size and pass that. That is precisely what I do – Ian Bush Oct 27 '19 at 15:23
  • @Gilles, we atm mostly use Intel MPI, however, I know this has been an issue for us especially when using MPI_Gatherv as all except root rank has some array which are insignificant. Don’t recall which distribution it was, probably both as we usually test on both. So we safely did an allocation of size one and never ran into the problem. I will remember you if I would face such an issue with OpenMPI again – ATK Oct 27 '19 at 15:58
  • @IanBush The standard says that on non-root processes the receive buffer is ignored. That seems to imply to me that passing an allocatable, non-allocated, variable is fine. – Victor Eijkhout Jul 27 '21 at 03:36
  • 2
    @VictorEijkhout No, it is irrelevant that the array is not accessed. In standard Fortran it is *always* illegal to pass an unallocated allocatable array if an interface is not in scope. *Always*. It is irrelevant what the procedure does with it. If an interface is in scope (with minor exceptions which are not relevant here) for an unallocated allocatable array to be passed the corresponding dummy argument must also be allocatable. MPI has no allocatable dummy arguments. Thus it is *always* wrong to pass an unallocated allocatable array to an MPI routine. – Ian Bush Jul 27 '21 at 07:15
  • 1
    @VictorEijkhout See https://stackoverflow.com/questions/13496510/is-there-anything-wrong-with-passing-an-unallocated-array-to-a-routine-without-a for more discussion, espcially francescalus's (much more recent) version that gives references to the relevant parts of the Fortran standard – Ian Bush Jul 27 '21 at 07:31
  • @IanBush Thanks for the clarification. Since the OP's code does not have the trailing error parameter, I think they are using the `mpi_f08` standard, which means that the interface of the `MPI_Gatherv` *is* in scope. (I think). But then your link says "it is only legal if the dummy argument is also allocatable" and that sounds like a stumbling block. – Victor Eijkhout Jul 27 '21 at 15:16

1 Answers1

3

(Turning my comment into an answer)

Yes, it is legal to allocate to zero size and pass that. That is precisely what I do

Ian Bush
  • 6,996
  • 1
  • 21
  • 27