<div dir="ltr">Glad it worked!<br clear="all"><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr">--Junchao Zhang</div></div></div><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Dec 6, 2023 at 1:20 PM Sreeram R Venkat <<a href="mailto:srvenkat@utexas.edu">srvenkat@utexas.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Thank you for your help. It turned out the problem was that I forgot to assemble the "x" vector before doing the scatter. It seems to be working now.<div><br></div><div>Thanks,</div><div>Sreeram</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Dec 6, 2023 at 11:12 AM Junchao Zhang <<a href="mailto:junchao.zhang@gmail.com" target="_blank">junchao.zhang@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Hi, Sreeram,</div><div> I made an example with your approach. It worked fine as you see the output at the end</div><div><br></div><div><div style="color:rgb(0,0,0);font-family:Menlo,Monaco,"Courier New",monospace;font-size:14px;line-height:21px;white-space:pre-wrap"><div><span style="color:rgb(0,0,255)">#include </span><span style="color:rgb(163,21,21)"><petscvec.h></span></div><div><span style="color:rgb(0,0,255)">int</span> main(<span style="color:rgb(0,0,255)">int</span> argc, <span style="color:rgb(0,0,255)">char</span> **argv)</div><div>{</div><div> PetscInt i, j, rstart, rend, n, N, *indices;</div><div> PetscMPIInt size, rank;</div><div> IS ix;</div><div> VecScatter vscat;</div><div> Vec x, y;</div><br><div> <span style="color:rgb(0,0,255)">PetscFunctionBeginUser</span>;</div><div> <span style="color:rgb(0,0,255)">PetscCall</span>(PetscInitialize(&argc, &argv, (<span style="color:rgb(0,0,255)">char</span> *)<span style="color:rgb(9,134,88)">0</span>, <span style="color:rgb(0,0,255)">NULL</span>));</div><div> <span style="color:rgb(0,0,255)">PetscCallMPI</span>(MPI_Comm_size(PETSC_COMM_WORLD, &size));</div><div> <span style="color:rgb(0,0,255)">PetscCallMPI</span>(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));</div><br><div> <span style="color:rgb(0,0,255)">PetscCall</span>(VecCreate(PETSC_COMM_WORLD, &x));</div><div> <span style="color:rgb(0,0,255)">PetscCall</span>(VecSetFromOptions(x));</div><div> <span style="color:rgb(0,0,255)">PetscCall</span>(PetscObjectSetName((PetscObject)x, <span style="color:rgb(163,21,21)">"Vec X"</span>));</div><div> n = (rank < <span style="color:rgb(9,134,88)">4</span>) ? <span style="color:rgb(9,134,88)">9</span> : <span style="color:rgb(9,134,88)">0</span>;</div><div> <span style="color:rgb(0,0,255)">PetscCall</span>(VecSetSizes(x, n, <span style="color:rgb(0,0,255)">PETSC_DECIDE</span>));</div><br><div> <span style="color:rgb(0,0,255)">PetscCall</span>(VecGetOwnershipRange(x, &rstart, &rend));</div><div> <span style="color:rgb(0,0,255)">for</span> (i = rstart; i < rend; i++) <span style="color:rgb(0,0,255)">PetscCall</span>(VecSetValue(x, i, (PetscScalar)i, <span style="color:rgb(9,134,88)">INSERT_VALUES</span>));</div><div> <span style="color:rgb(0,0,255)">PetscCall</span>(VecAssemblyBegin(x));</div><div> <span style="color:rgb(0,0,255)">PetscCall</span>(VecAssemblyEnd(x));</div><div> <span style="color:rgb(0,0,255)">PetscCall</span>(VecGetSize(x, &N));</div><br><div> <span style="color:rgb(0,0,255)">PetscCall</span>(VecCreate(PETSC_COMM_WORLD, &y));</div><div> <span style="color:rgb(0,0,255)">PetscCall</span>(VecSetFromOptions(y));</div><div> <span style="color:rgb(0,0,255)">PetscCall</span>(PetscObjectSetName((PetscObject)y, <span style="color:rgb(163,21,21)">"Vec Y"</span>));</div><div> <span style="color:rgb(0,0,255)">PetscCall</span>(VecSetSizes(y, <span style="color:rgb(0,0,255)">PETSC_DECIDE</span>, N));</div><br><div> <span style="color:rgb(0,0,255)">PetscCall</span>(VecGetOwnershipRange(y, &rstart, &rend));</div><div> <span style="color:rgb(0,0,255)">PetscCall</span>(<span style="color:rgb(0,0,255)">PetscMalloc1</span>(rend - rstart, &indices));</div><div> <span style="color:rgb(0,0,255)">for</span> (i = rstart, j = <span style="color:rgb(9,134,88)">0</span>; i < rend; i++, j++) indices[j] = rank + size * j;</div><br><div> <span style="color:rgb(0,0,255)">PetscCall</span>(ISCreateGeneral(PETSC_COMM_WORLD, rend - rstart, indices, <span style="color:rgb(9,134,88)">PETSC_OWN_POINTER</span>, &ix));</div><div> <span style="color:rgb(0,0,255)">PetscCall</span>(VecScatterCreate(x, ix, y, <span style="color:rgb(0,0,255)">NULL</span>, &vscat));</div><br><div> <span style="color:rgb(0,0,255)">PetscCall</span>(VecScatterBegin(vscat, x, y, <span style="color:rgb(9,134,88)">INSERT_VALUES</span>, <span style="color:rgb(9,134,88)">SCATTER_FORWARD</span>));</div><div> <span style="color:rgb(0,0,255)">PetscCall</span>(VecScatterEnd(vscat, x, y, <span style="color:rgb(9,134,88)">INSERT_VALUES</span>, <span style="color:rgb(9,134,88)">SCATTER_FORWARD</span>));</div><br><div> <span style="color:rgb(0,0,255)">PetscCall</span>(ISView(ix, <span style="color:rgb(0,0,255)">PETSC_VIEWER_STDOUT_WORLD</span>));</div><div> <span style="color:rgb(0,0,255)">PetscCall</span>(VecView(x, <span style="color:rgb(0,0,255)">PETSC_VIEWER_STDERR_WORLD</span>));</div><div> <span style="color:rgb(0,0,255)">PetscCall</span>(VecView(y, <span style="color:rgb(0,0,255)">PETSC_VIEWER_STDERR_WORLD</span>));</div><br><div> <span style="color:rgb(0,0,255)">PetscCall</span>(VecScatterDestroy(&vscat));</div><div> <span style="color:rgb(0,0,255)">PetscCall</span>(ISDestroy(&ix));</div><div> <span style="color:rgb(0,0,255)">PetscCall</span>(VecDestroy(&x));</div><div> <span style="color:rgb(0,0,255)">PetscCall</span>(VecDestroy(&y));</div><div> <span style="color:rgb(0,0,255)">PetscCall</span>(PetscFinalize());</div><div> <span style="color:rgb(0,0,255)">return</span> <span style="color:rgb(9,134,88)">0</span>;</div><div>}</div><div><br></div><div>$ mpirun -n 12 ./ex100<br></div><div>IS Object: 12 MPI processes<br> type: general<br>[0] Number of indices in set 3<br>[0] 0 0<br>[0] 1 12<br>[0] 2 24<br>[1] Number of indices in set 3<br>[1] 0 1<br>[1] 1 13<br>[1] 2 25<br>[2] Number of indices in set 3<br>[2] 0 2<br>[2] 1 14<br>[2] 2 26<br>[3] Number of indices in set 3<br>[3] 0 3<br>[3] 1 15<br>[3] 2 27<br>[4] Number of indices in set 3<br>[4] 0 4<br>[4] 1 16<br>[4] 2 28<br>[5] Number of indices in set 3<br>[5] 0 5<br>[5] 1 17<br>[5] 2 29<br>[6] Number of indices in set 3<br>[6] 0 6<br>[6] 1 18<br>[6] 2 30<br>[7] Number of indices in set 3<br>[7] 0 7<br>[7] 1 19<br>[7] 2 31<br>[8] Number of indices in set 3<br>[8] 0 8<br>[8] 1 20<br>[8] 2 32<br>[9] Number of indices in set 3<br>[9] 0 9<br>[9] 1 21<br>[9] 2 33<br>[10] Number of indices in set 3<br>[10] 0 10<br>[10] 1 22<br>[10] 2 34<br>[11] Number of indices in set 3<br>[11] 0 11<br>[11] 1 23<br>[11] 2 35<br>Vec Object: Vec X 12 MPI processes<br> type: mpi<br>Process [0]<br>0.<br>1.<br>2.<br>3.<br>4.<br>5.<br>6.<br>7.<br>8.<br>Process [1]<br>9.<br>10.<br>11.<br>12.<br>13.<br>14.<br>15.<br>16.<br>17.<br>Process [2]<br>18.<br>19.<br>20.<br>21.<br>22.<br>23.<br>24.<br>25.<br>26.<br>Process [3]<br>27.<br>28.<br>29.<br>30.<br>31.<br>32.<br>33.<br>34.<br>35.<br>Process [4]<br>Process [5]<br>Process [6]<br>Process [7]<br>Process [8]<br>Process [9]<br>Process [10]<br>Process [11]<br>Vec Object: Vec Y 12 MPI processes<br> type: mpi<br>Process [0]<br>0.<br>12.<br>24.<br>Process [1]<br>1.<br>13.<br>25.<br>Process [2]<br>2.<br>14.<br>26.<br>Process [3]<br>3.<br>15.<br>27.<br>Process [4]<br>4.<br>16.<br>28.<br>Process [5]<br>5.<br>17.<br>29.<br>Process [6]<br>6.<br>18.<br>30.<br>Process [7]<br>7.<br>19.<br>31.<br>Process [8]<br>8.<br>20.<br>32.<br>Process [9]<br>9.<br>21.<br>33.<br>Process [10]<br>10.<br>22.<br>34.<br>Process [11]<br>11.<br>23.<br>35.<br></div></div></div><br clear="all"><div><div dir="ltr" class="gmail_signature"><div dir="ltr">--Junchao Zhang</div></div></div><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Dec 5, 2023 at 10:09 PM Sreeram R Venkat <<a href="mailto:srvenkat@utexas.edu" target="_blank">srvenkat@utexas.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Yes, I have an example code at <a href="http://github.com/s769/petsc-test" target="_blank">github.com/s769/petsc-test</a>. Only thing is, when I described the example before, I simplified the actual use case in the code to make things simpler. Here are the extra details relevant to this code:<div><ul><li>We assume a 2D processor grid, given by the command-line args -proc_rows and -proc_cols</li><li>The total length of the vector is n_m*n_t given by command-line args -nm and -nt. n_m corresponds to a space index and n_t a time index.</li><li>In the "Start" phase, the vector is divided into n_m blocks each of size n_t (indexed space->time). The blocks are partitioned over the first row of processors. For example if -nm = 4 and -proc_cols = 4, each processor in the first row will get one block of size n_t. Each processor in the first row gets n_m_local blocks of size n_t, where the sum of all n_m_locals for the first row of processors is n_m.</li><li>In the "Finish" phase, the vector is divided into n_t blocks each of size n_m (indexed time->space; this is the reason for the permutation of indices). The blocks are partitioned over all processors. Each processor will get n_t_local blocks of size n_m, where the sum of all n_t_locals for all processors is n_t.</li></ul><div>I think the basic idea is similar to the previous example, but these details make things a bit more complicated. Please let me know if anything is unclear, and I can try to explain more.</div></div><div><br></div><div>Thanks for your help,</div><div>Sreeram</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Dec 5, 2023 at 9:30 PM Junchao Zhang <<a href="mailto:junchao.zhang@gmail.com" target="_blank">junchao.zhang@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><div><div dir="ltr" class="gmail_signature"><div dir="ltr">I think your approach is correct. Do you have an example code?</div><div dir="ltr"><br></div><div dir="ltr">--Junchao Zhang</div></div></div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Dec 5, 2023 at 5:15 PM Sreeram R Venkat <<a href="mailto:srvenkat@utexas.edu" target="_blank">srvenkat@utexas.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi, I have a follow up question on this. <div><br></div><div>Now, I'm trying to do a scatter and permutation of the vector. Under the same setup as the original example, here are the new Start and Finish states I want to achieve:</div><div><div> Start Finish</div><div>Proc | Entries Proc | Entries</div><div> 0 | 0,...,8 0 | 0, 12, 24</div><div> 1 | 9,...,17 1 | 1, 13, 25<br></div><div> 2 | 18,...,26 2 | 2, 14, 26<br></div><div> 3 | 27,...,35 3 | 3, 15, 27<br></div><div> 4 | None 4 | 4, 16, 28<br></div><div> 5 | None 5 | 5, 17, 29<br></div><div> 6 | None 6 | 6, 18, 30<br></div><div> 7 | None 7 | 7, 19, 31<br></div><div> 8 | None 8 | 8, 20, 32<br></div><div> 9 | None 9 | 9, 21, 33<br></div><div> 10 | None 10 | 10, 22, 34<br></div><div> 11 | None 11 | 11, 23, 35</div></div><div><br></div><div>So far, I've tried to use ISCreateGeneral(), with each process giving an idx array corresponding to the indices it wants (i.e. idx on P0 looks like [0,12,24] P1 -> [1,13, 25], and so on). </div><div>Then I use this to create the VecScatter with VecScatterCreate(x, is, y, NULL, &scatter).</div><div><br></div><div>However, when I try to do the scatter, I get some illegal memory access errors. </div><div><br></div><div>Is there something wrong with how I define the index sets?</div><div><br></div><div>Thanks,</div><div>Sreeram</div><div><br></div><div><br></div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Oct 5, 2023 at 12:57 PM Sreeram R Venkat <<a href="mailto:srvenkat@utexas.edu" target="_blank">srvenkat@utexas.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Thank you. This works for me.<div><br></div><div>Sreeram</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Oct 4, 2023 at 6:41 PM Junchao Zhang <<a href="mailto:junchao.zhang@gmail.com" target="_blank">junchao.zhang@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div style="line-height:21px"><div><div style="line-height:21px">Hi, Sreeram,<br>You can try this code. Since x, y are both MPI vectors, we just need to say we want to scatter x[0:N] to y[0:N]. The 12 index sets with your example on the 12 processes would be [0..8], [9..17], [18..26], [27..35], [], ..., []. Actually, you can do it arbitrarily, say, with 12 index sets [0..17], [18..35], .., []. PETSc will figure out how to do the communication. <div style="color:rgb(0,0,0);font-family:Menlo,Monaco,"Courier New",monospace;font-size:14px;white-space:pre-wrap"><br></div><div style="color:rgb(0,0,0);font-family:Menlo,Monaco,"Courier New",monospace;font-size:14px;white-space:pre-wrap"> PetscInt rstart, rend, N;</div><div style="color:rgb(0,0,0);font-family:Menlo,Monaco,"Courier New",monospace;font-size:14px;white-space:pre-wrap"> IS ix;</div><div style="color:rgb(0,0,0);font-family:Menlo,Monaco,"Courier New",monospace;font-size:14px;white-space:pre-wrap"> VecScatter vscat;</div><div style="color:rgb(0,0,0);font-family:Menlo,Monaco,"Courier New",monospace;font-size:14px;white-space:pre-wrap"> Vec y;</div><div style="color:rgb(0,0,0);font-family:Menlo,Monaco,"Courier New",monospace;font-size:14px;white-space:pre-wrap"> MPI_Comm comm;</div><div style="color:rgb(0,0,0);font-family:Menlo,Monaco,"Courier New",monospace;font-size:14px;white-space:pre-wrap"> VecType type;</div><br><div style="color:rgb(0,0,0);font-family:Menlo,Monaco,"Courier New",monospace;font-size:14px;white-space:pre-wrap"> PetscObjectGetComm((PetscObject)x, &comm);</div><div style="color:rgb(0,0,0);font-family:Menlo,Monaco,"Courier New",monospace;font-size:14px;white-space:pre-wrap"> VecGetType(x, &type);</div><div style="color:rgb(0,0,0);font-family:Menlo,Monaco,"Courier New",monospace;font-size:14px;white-space:pre-wrap"> VecGetSize(x, &N);</div><div style="color:rgb(0,0,0);font-family:Menlo,Monaco,"Courier New",monospace;font-size:14px;white-space:pre-wrap"> VecGetOwnershipRange(x, &rstart, &rend);</div><br><div style="color:rgb(0,0,0);font-family:Menlo,Monaco,"Courier New",monospace;font-size:14px;white-space:pre-wrap"> VecCreate(comm, &y);</div><div style="color:rgb(0,0,0);font-family:Menlo,Monaco,"Courier New",monospace;font-size:14px;white-space:pre-wrap"> VecSetSizes(y, <span style="color:rgb(0,0,255)">PETSC_DECIDE</span>, N);</div><div style="color:rgb(0,0,0);font-family:Menlo,Monaco,"Courier New",monospace;font-size:14px;white-space:pre-wrap"> VecSetType(y, type);</div><br><div style="color:rgb(0,0,0);font-family:Menlo,Monaco,"Courier New",monospace;font-size:14px;white-space:pre-wrap"> ISCreateStride(PetscObjectComm((PetscObject)x), rend - rstart, rstart, <span style="color:rgb(9,134,88)">1</span>, &ix);</div><div style="color:rgb(0,0,0);font-family:Menlo,Monaco,"Courier New",monospace;font-size:14px;white-space:pre-wrap"> VecScatterCreate(x, ix, y, ix, &vscat);</div></div></div><div style="color:rgb(0,0,0);font-family:Menlo,Monaco,"Courier New",monospace;font-size:14px;white-space:pre-wrap"><br></div></div><div><div dir="ltr" class="gmail_signature"><div dir="ltr">--Junchao Zhang</div></div></div><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Oct 4, 2023 at 6:03 PM Sreeram R Venkat <<a href="mailto:srvenkat@utexas.edu" target="_blank">srvenkat@utexas.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr" class="gmail_signature"><div dir="ltr"><div style="color:rgb(34,34,34)">Suppose I am running on 12 processors, and I have a vector "v" of size 36 partitioned over the first 4. v still uses the PETSC_COMM_WORLD, so it has a layout of (9, 9, 9, 9, 0, 0, ..., 0). Now, I would like to repartition it over all 12 processors, so that the layout becomes (3, 3, 3, ..., 3). I've been trying to use VecScatter to do this, but I'm not sure what IndexSets to use for the sender and receiver.</div><div style="color:rgb(34,34,34)"><br></div><div style="color:rgb(34,34,34)">The result I am trying to achieve is this:</div><div style="color:rgb(34,34,34)"><br></div><div style="color:rgb(34,34,34)">Assume the vector is v = <0, 1, 2, ..., 35> </div><div style="color:rgb(34,34,34)"><br></div><div style="color:rgb(34,34,34)"> Start Finish</div><div style="color:rgb(34,34,34)">Proc | Entries Proc | Entries</div><div style="color:rgb(34,34,34)"> 0 | 0,...,8 0 | 0, 1, 2</div><div style="color:rgb(34,34,34)"> 1 | 9,...,17 1 | 3, 4, 5<br></div><div style="color:rgb(34,34,34)"> 2 | 18,...,26 2 | 6, 7, 8<br></div><div style="color:rgb(34,34,34)"> 3 | 27,...,35 3 | 9, 10, 11<br></div><div style="color:rgb(34,34,34)"> 4 | None 4 | 12, 13, 14<br></div><div style="color:rgb(34,34,34)"> 5 | None 5 | 15, 16, 17<br></div><div style="color:rgb(34,34,34)"> 6 | None 6 | 18, 19, 20<br></div><div style="color:rgb(34,34,34)"> 7 | None 7 | 21, 22, 23<br></div><div style="color:rgb(34,34,34)"> 8 | None 8 | 24, 25, 26<br></div><div style="color:rgb(34,34,34)"> 9 | None 9 | 27, 28, 29<br></div><div style="color:rgb(34,34,34)"> 10 | None 10 | 30, 31, 32<br></div><div style="color:rgb(34,34,34)"> 11 | None 11 | 33, 34, 35<br></div><div style="color:rgb(34,34,34)"><br></div><div style="color:rgb(34,34,34)">Appreciate any help you can provide on this.</div><div style="color:rgb(34,34,34)"><br></div><div style="color:rgb(34,34,34)">Thanks,</div><div style="color:rgb(34,34,34)">Sreeram</div></div></div></div>
</blockquote></div>
</blockquote></div>
</blockquote></div>
</blockquote></div>
</blockquote></div>
</blockquote></div>
</blockquote></div>
</blockquote></div>