-1

I am coding an MPI fortran program and have, let say, three vectors of different length in three ranks. I would like to combine them together in a "concatenate" way such as:

Rank 0: a0 = [1 2 3 4 5]
Rank 1: a1 = [3 5 7 9]
Rank 2: a2 = [2 4 6 8 10 12]

Combine them to:

Rank 0: a = [1 2 3 4 5 3 5 7 9 2 4 6 8 10 12]

Could you please tell me how I can do that ?

alfC
  • 14,261
  • 4
  • 67
  • 118
ctnguyen
  • 99
  • 2
  • 2
  • 6
  • MPI_Gather or MPI_Gatherv http://mpitutorial.com/tutorials/mpi-scatter-gather-and-allgather/ http://www.mcs.anl.gov/research/projects/mpi/mpi-standard/mpi-report-1.1/node70.htm – alfC Aug 28 '17 at 00:01
  • Thanks @alfC for your answer. I also found mpi_gatherv and read the examples given in the send link; but I am still not really sure how to do it correctly in my case. It seems that I should send the length of the vector beforehand so I can use mpi_gatherv later. Is it correct ? – ctnguyen Aug 28 '17 at 16:17
  • Have you actually tried it? What did you struggle with? We will not write your entire code for you. Furthermore, the linked pages include detailed examples. – Ross Aug 28 '17 at 16:35
  • @ctnguyen, yes, there is no magic, if only each individual process knows the size then that information has to be passed around before calling `gatherv` (for example using `gather` on the sizes). Chances are that you can know this information from a centralized point. I encourage you to put some code in your question to improve your chances of getting help. – alfC Aug 28 '17 at 19:24
  • @alfC Thank you for your answer. I am totally clear now and I am success with that example. I appreciate your support. PS: This is my first day with MPI so my question seems to be dumb, sorry for that and thanks again for your kind help. – ctnguyen Aug 28 '17 at 22:53
  • @ctnguyen, there are no dumb questions, but the community appreciates if you show some effort and when you solve the problem are post your solution that we all helped you to obtain. – alfC Aug 28 '17 at 23:32
  • @alfC Thanks for your suggestions. Btw, this link includes an excellent example for my question. https://stackoverflow.com/questions/31890523/how-to-use-mpi-gatherv-for-collecting-strings-of-diiferent-length-from-different – ctnguyen Aug 29 '17 at 11:26

1 Answers1

1

Since vectors have different sizes based on the ranks, you can use MPI_Gatherv() in order to achieve the expected result

Gilles Gouaillardet
  • 8,193
  • 11
  • 24
  • 30
  • Thanks @Gilles for your answer. I also found mpi_gatherv and read the examples given in the send link; but I am still not really sure how to do it correctly in my case. It seems that I should send the length of the vector beforehand so I can use mpi_gatherv later. Is it correct ? – ctnguyen Aug 28 '17 at 14:18
  • if the root task (rank `0` in your example) does not know all the vector lengths, then yes, you need to collect this information first (`MPI_Gather()` is a good fit). and then you can `MPI_Gatherv()` the data. – Gilles Gouaillardet Aug 28 '17 at 22:26
  • thanks for the clarification. I did as you suggested. Btw, this link includes an excellent example for my question. https://stackoverflow.com/questions/31890523/how-to-use-mpi-gatherv-for-collecting-strings-of-diiferent-length-from-different – ctnguyen Aug 29 '17 at 11:27