Closed Ross-Li closed 1 year ago
Hi @Ross-Li,
There might have been a confusion in the understanding of MPI_Scatterv
.
According to the MPI standard (version 4.0) available at https://www.mpi-forum.org/docs/mpi-4.0/mpi40-report.pdf, in Section 6.6, on page 208, for MPI_Scatterv
it states that sendcounts
is a "non-negative integer array (of length group size) specifying the number of elements to send to each rank (significant only at root)", similarly for the displacements.
In other words, a precondition of MPI_Scatterv
is that the two variables mentioned above must have a number of elements equal to the size of the group from which the communicator used in the collective operation is built. Not respecting this precondition therefore implies an undefined behaviour, which is this instance is not a crash it appears.
As a result, if the communicator that you pass to MPI_Scatterv
is built from a group of 4 MPI processes, your send buffer, send counts and displacements must all be arrays of 4 elements.
Ah, I should have read the official documentation first and told you the specific version of MPI I was using. If the documentation specifies that these arrays should be of group / communicator size, then indeed my program will produce undefined behavior and it was just luck that the program didn't crash. Thanks for your explanation!!!
This is something I discovered about what you can do with
MPI_Scatterv()
that is only slightly different with what your existing example program. I am not sure whether this is worth to be added as another example program to even better demonstrate the functionalities ofMPI_Scatterv()
, or everyone already knows about this and I am just late to figure it out. I would really appreciate it if you can share your opinion about it.My goal
What I was trying to do is basically:
It should be a simple and straightforward thing to MPI to do, but the regular
MPI_Scatter()
always sends a piece from sender to itself. It is truly a bit annoying.My method (still not perfect)
I finally achieved this with
MPI_Scatterv()
, though still not perfect. The core code snippet is:Sample output is:
The imperfect aspect, as commented in the code snippet, is that I still have to create a
useless
variable to fill therecvbuf
argument inMPI_Scatterv()
, otherwise it will produce error. But it is pretty perfect now.Contribution
The key difference between this pseudocode and the existing example code are:
to_send
buffer equalsnumber_of_slaves
which is 3, instead ofnumber_of_all_processes
which is 4.counts
array equalsnumber_of_slaves
which is 3, instead ofnumber_of_all_processes
which is 4.displacements
array equalsnumber_of_slaves
which is 3, instead ofnumber_of_all_processes
which is 4.In a word, my point is that the length of
to_send
buffer,counts
array anddisplacements
array DON'T HAVE TO BE equal tonumber_of_all_processes
. You can customize not only the content of these variables BUT ALSO the lengths of them to suit your needs. This is a point that took me a while to realize.If you want to integrate this idea...
If you would like to integrate this idea into your website, I think it should be sufficient for you to just note that the lengths of these 3 variables doesn't have to necessarily equals the number of all processes in the parameter description section.
Improvement idea
I would also really appreciate it if you can provide suggestions on how to do exactly what I am trying to do, especially about that "not creating useless variables" part.