<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Jan 3, 2018 at 11:47 AM, Franck Houssen <span dir="ltr"><<a href="mailto:franck.houssen@inria.fr" target="_blank">franck.houssen@inria.fr</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div style="font-family:times new roman,new york,times,serif;font-size:12pt;color:#000000"><div>How do I collect all the values from a sequential vector on the zeroth processor into a parallel PETSc vector ?<br></div><div>Shall I use VecScatterCreateToZero "backward", or, use VecScatterCreate instead ? If yes, how ? (my understanding is that VecScatterCreate can take only parallel vector as input)</div><div>Not sure how to do this.<br></div></div></div></blockquote><div><br></div><div>Its not clear what you want. Do you want a seq vector duplicated on all procs? Do you want it split up? The short</div><div>answer is, use the appropriate scatter.</div><div><br></div><div> Matt</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div style="font-family:times new roman,new york,times,serif;font-size:12pt;color:#000000"><div></div><div><br></div><div>Franck<br></div><div><br></div><div>In this example, the final VecView of the parallel globVec vector should be [-1., -2.]<br></div><div><br></div><div>>> mpirun -n 2 ./vecScatterGatherRoot.exe <br></div><div>Vec Object: 2 MPI processes<br> type: mpi<br>Process [0]<br>1.<br>Process [1]<br>2.<br>Vec Object: 1 MPI processes<br> type: seq<br>1.<br>2.<br>Vec Object: 1 MPI processes<br> type: seq<br>-1.<br>-2.<br>[1]PETSC ERROR: ------------------------------<wbr>------------------------------<wbr>------------<br>[1]PETSC ERROR: Caught signal number 11 SEGV: Segmentation Violation, probably memory access out of range<br><br></div><div>>> more vecScatterGatherRoot.cpp <br>// How do I collect all the values from a sequential vector on the zeroth processor into a parallel PETSc vector ?<br>//<br>// ~> g++ -o vecScatterGatherRoot.exe vecScatterGatherRoot.cpp -lpetsc -lm; mpirun -n X vecScatterGatherRoot.exe<br><br>#include "petsc.h"<br><br>int main(int argc,char **argv) {<br> PetscInitialize(&argc, &argv, NULL, NULL);<br> int size = 0; MPI_Comm_size(MPI_COMM_WORLD, &size);<br> int rank = 0; MPI_Comm_rank(MPI_COMM_WORLD, &rank);<br><br> PetscInt globSize = size;<br> Vec globVec; VecCreateMPI(PETSC_COMM_WORLD, 1, globSize, &globVec);<br> VecSetValue(globVec, rank, (PetscScalar) (1.+rank), INSERT_VALUES);<br> VecAssemblyBegin(globVec); VecAssemblyEnd(globVec);<br> VecView(globVec, PETSC_VIEWER_STDOUT_WORLD); PetscViewerFlush(PETSC_VIEWER_<wbr>STDOUT_WORLD);<br><br> // Collect all the values from a parallel PETSc vector into a vector on the zeroth processor.<br><br> Vec locVec = NULL;<br> if (rank == 0) {<br> PetscInt locSize = globSize;<br> VecCreateSeq(PETSC_COMM_SELF, locSize, &locVec); VecSet(locVec, -1.);<br> }<br> VecScatter scatCtx; VecScatterCreateToZero(<wbr>globVec, &scatCtx, &locVec);<br> VecScatterBegin(scatCtx, globVec, locVec, INSERT_VALUES, SCATTER_FORWARD);<br> VecScatterEnd (scatCtx, globVec, locVec, INSERT_VALUES, SCATTER_FORWARD);<br><br> // Modify sequential vector on the zeroth processor.<br><br> if (rank == 0) {<br> VecView(locVec, PETSC_VIEWER_STDOUT_SELF); PetscViewerFlush(PETSC_VIEWER_<wbr>STDOUT_SELF);<br> VecScale(locVec, -1.);<br> VecView(locVec, PETSC_VIEWER_STDOUT_SELF); PetscViewerFlush(PETSC_VIEWER_<wbr>STDOUT_SELF);<br> }<br> MPI_Barrier(MPI_COMM_WORLD);<br><br> // How do I collect all the values from a sequential vector on the zeroth processor into a parallel PETSc vector ?<br><br> VecSet(globVec, 0.);<br> VecScatterBegin(scatCtx, locVec, globVec, ADD_VALUES, SCATTER_REVERSE);<br> VecScatterEnd (scatCtx, locVec, globVec, ADD_VALUES, SCATTER_REVERSE);<br> VecView(globVec, PETSC_VIEWER_STDOUT_WORLD); PetscViewerFlush(PETSC_VIEWER_<wbr>STDOUT_WORLD);<br><br> PetscFinalize();<br>}<br><br></div></div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div>What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.<br>-- Norbert Wiener</div><div><br></div><div><a href="http://www.caam.rice.edu/~mk51/" target="_blank">https://www.cse.buffalo.edu/~knepley/</a><br></div></div></div></div></div>
</div></div>