<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
The two output results are different in my test. </div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
If I use the way of assignment variables in current code. I got error output of VecView as below:</div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
<div>Vec Object: 2 MPI processes</div>
<div>  type: mpi</div>
<div>Vec Object: Vec_0x7fffd6895a90_0 2 MPI processes</div>
<div>  type: mpi</div>
<div>Process [0]<br>
</div>
<div>0.</div>
<div>0.</div>
<div>0.</div>
<div>Process [1]</div>
<div>0.</div>
<div>0.</div>
<div><br>
</div>
<div>Or if I use the previous way (DMDANaturalToGlobal), I got error output of PetscSynchronizedPrintf:</div>
<div>
<div>Rank=0</div>
<div>   0    0    0 </div>
<div>Rank=1</div>
<div>   0    0</div>
<div><br>
</div>
Both are fine if I run without mpiexec.</div>
<div><br>
</div>
<div>Besides, my development environment is CLion + WSL + GCC 7.3.</div>
<br>
</div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Matthew Knepley <knepley@gmail.com><br>
<b>Sent:</b> Tuesday, April 10, 2018 0:01<br>
<b>To:</b> Songtao Chu<br>
<b>Cc:</b> Hong; petsc-users@mcs.anl.gov<br>
<b>Subject:</b> Re: [petsc-users] How to access Vector data properly</font>
<div> </div>
</div>
<div>
<div dir="ltr">This works fine for me:
<div><br>
</div>
<div><span></span>
<p class="x_gmail-p1" style="margin:0px; font-style:normal; font-variant:normal; font-weight:normal; font-size:11px; line-height:normal; font-family:Menlo">
knepley/feature-pc-patch $:/PETSc3/petsc/petsc-pylith/tmp$ ../arch-master-debug/bin/mpiexec -n 2 ./tester</p>
<p class="x_gmail-p1" style="margin:0px; font-style:normal; font-variant:normal; font-weight:normal; font-size:11px; line-height:normal; font-family:Menlo">
Vec Object: 2 MPI processes</p>
<p class="x_gmail-p1" style="margin:0px; font-style:normal; font-variant:normal; font-weight:normal; font-size:11px; line-height:normal; font-family:Menlo">
<span class="x_gmail-Apple-converted-space">  </span>type: mpi</p>
<p class="x_gmail-p1" style="margin:0px; font-style:normal; font-variant:normal; font-weight:normal; font-size:11px; line-height:normal; font-family:Menlo">
Process [0]</p>
<p class="x_gmail-p1" style="margin:0px; font-style:normal; font-variant:normal; font-weight:normal; font-size:11px; line-height:normal; font-family:Menlo">
0.</p>
<p class="x_gmail-p1" style="margin:0px; font-style:normal; font-variant:normal; font-weight:normal; font-size:11px; line-height:normal; font-family:Menlo">
1.</p>
<p class="x_gmail-p1" style="margin:0px; font-style:normal; font-variant:normal; font-weight:normal; font-size:11px; line-height:normal; font-family:Menlo">
2.</p>
<p class="x_gmail-p1" style="margin:0px; font-style:normal; font-variant:normal; font-weight:normal; font-size:11px; line-height:normal; font-family:Menlo">
Process [1]</p>
<p class="x_gmail-p1" style="margin:0px; font-style:normal; font-variant:normal; font-weight:normal; font-size:11px; line-height:normal; font-family:Menlo">
3.</p>
<p class="x_gmail-p1" style="margin:0px; font-style:normal; font-variant:normal; font-weight:normal; font-size:11px; line-height:normal; font-family:Menlo">
4.</p>
<p class="x_gmail-p1" style="margin:0px; font-style:normal; font-variant:normal; font-weight:normal; font-size:11px; line-height:normal; font-family:Menlo">
Rank=0</p>
<p class="x_gmail-p1" style="margin:0px; font-style:normal; font-variant:normal; font-weight:normal; font-size:11px; line-height:normal; font-family:Menlo">
<span class="x_gmail-Apple-converted-space">   </span>0<span class="x_gmail-Apple-converted-space">   
</span>1<span class="x_gmail-Apple-converted-space">    </span>2<span class="x_gmail-Apple-converted-space"> </span></p>
<p class="x_gmail-p1" style="margin:0px; font-style:normal; font-variant:normal; font-weight:normal; font-size:11px; line-height:normal; font-family:Menlo">
Rank=1</p>
<p class="x_gmail-p1" style="margin:0px; font-style:normal; font-variant:normal; font-weight:normal; font-size:11px; line-height:normal; font-family:Menlo">
<span class="x_gmail-Apple-converted-space">   </span>3<span class="x_gmail-Apple-converted-space">   
</span>4<span class="x_gmail-Apple-converted-space"> </span></p>
<br>
</div>
<div>What is the problem?</div>
<div><br>
</div>
<div>   Matt</div>
<div><br>
</div>
</div>
<div class="x_gmail_extra"><br>
<div class="x_gmail_quote">On Mon, Apr 9, 2018 at 11:51 AM, Songtao Chu <span dir="ltr">
<<a href="mailto:st.chu@outlook.com" target="_blank">st.chu@outlook.com</a>></span> wrote:<br>
<blockquote class="x_gmail_quote" style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex">
<div dir="ltr">
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
<pre style="background-color:#2b2b2b; color:#a9b7c6; font-family:'Source Code Pro'; font-size:10.5pt"><span style="color:#cc7832; font-weight:bold">static char </span>help[] = <span style="color:#6a8759">"</span><span style="color:#cc7832">\n\n</span><span style="color:#6a8759">"</span><span style="color:#cc7832">;<br></span><span style="color:#bbb529">#include </span><span style="color:#6a8759"><petscdm.h><br></span><span style="color:#bbb529">#include </span><span style="color:#6a8759"><petscdmda.h></span><span style="color:#6a8759"><br></span><span style="color:#6a8759"><br></span><span style="color:#6a8759"><br></span><span style="color:#cc7832; font-weight:bold">int </span><span style="color:#ffc66d">main</span>(<span style="color:#cc7832; font-weight:bold">int </span>argc<span style="color:#cc7832">,</span><span style="color:#cc7832; font-weight:bold">char </span>**argv)<br>{<br>    <span style="color:#b9bcd1">PetscMPIInt     </span>rank<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span><span style="color:#b9bcd1">PetscErrorCode  </span>ierr<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span><span style="color:#b9bcd1">Vec             </span>global<span style="color:#cc7832">,</span>local<span style="color:#cc7832">,</span>natural<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span><span style="color:#b9bcd1">DM              </span>da<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span><span style="color:#b9bcd1">PetscReal       </span>*val<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span><span style="color:#b9bcd1">PetscInt        </span>i<span style="color:#cc7832">,</span>x<span style="color:#cc7832">,</span>xm<span style="color:#cc7832">;<br></span><span style="color:#cc7832"><br></span><span style="color:#cc7832"><br></span><span style="color:#cc7832">    </span>ierr = PetscInitialize(&argc<span style="color:#cc7832">,</span>&argv<span style="color:#cc7832">,</span>(<span style="color:#cc7832; font-weight:bold">c<wbr>har</span>*)<span style="color:#6897bb">0</span><span style="color:#cc7832">,</span>help)<span style="color:#cc7832">;</span><span style="color:#cc7832; font-weight:bold">if </span>(ierr) <span style="color:#cc7832; font-weight:bold">return </span>ierr<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span>ierr = MPI_Comm_rank(PETSC_COMM_WORLD<span style="color:#cc7832"><wbr>,</span>&rank)<span style="color:#cc7832">;</span><span style="color:#908b25">CHKERRQ</span>(ierr)<span style="color:#cc7832">;<br></span><span style="color:#cc7832"><br></span><span style="color:#cc7832">    </span>ierr = DMDACreate1d(PETSC_COMM_WORLD<span style="color:#cc7832">,</span><span style="color:#9876aa; font-style:italic"><wbr>DM_BOUNDARY_NONE</span><span style="color:#cc7832">,</span><span style="color:#6897bb">5</span><span style="color:#cc7832">,</span><span style="color:#6897bb">1</span><span style="color:#cc7832">,</span><span style="color:#6897bb">1</span><span style="color:#cc7832">,</span><span style="color:#908b25">NULL</span><span style="color:#cc7832">,</span>&<wbr>da)<span style="color:#cc7832">;</span><span style="color:#908b25">CHKERRQ</span>(ierr)<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span>ierr = DMSetUp(da)<span style="color:#cc7832">;</span><span style="color:#908b25">CHKERRQ</span>(ierr)<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span>ierr = DMCreateGlobalVector(da<span style="color:#cc7832">,</span>&<wbr>global)<span style="color:#cc7832">;</span><span style="color:#908b25">CHKERRQ</span>(ierr)<span style="color:#cc7832">;<br></span><span style="color:#808080"><br></span><span style="color:#808080">    </span>ierr = DMDAGetCorners(da<span style="color:#cc7832">,</span>&x<span style="color:#cc7832">,</span><span style="color:#908b25">NULL</span><span style="color:#cc7832">,</span><span style="color:#908b25">NULL</span><span style="color:#cc7832"><wbr>,</span>&xm<span style="color:#cc7832">,</span><span style="color:#908b25">NULL</span><span style="color:#cc7832">,</span><span style="color:#908b25">NULL</span>)<span style="color:#cc7832">;</span><span style="color:#908b25">CHKERRQ</span>(ierr)<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span>ierr = DMDAVecGetArray(da<span style="color:#cc7832">, </span>global<span style="color:#cc7832">, </span>&val)<span style="color:#cc7832">;</span><span style="color:#908b25">CHKERRQ</span>(ierr)<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span>ierr = PetscSynchronizedPrintf(<span style="color:#908b25">PETSC_<wbr>COMM_SELF</span><span style="color:#cc7832">, </span><span style="color:#6a8759">"Rank=%d</span><span style="color:#cc7832">\n</span><span style="color:#6a8759">"</span><span style="color:#cc7832">, </span>rank)<span style="color:#cc7832">;</span><span style="color:#908b25">CHKERRQ</span>(ierr)<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span><span style="color:#cc7832; font-weight:bold">for </span>(i = x<span style="color:#cc7832">; </span>i < x + xm<span style="color:#cc7832">; </span>++i) {<br>        val[i] = i<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span>}<br>    ierr = DMDAVecRestoreArray(da<span style="color:#cc7832">, </span>global<span style="color:#cc7832">, </span>&val)<span style="color:#cc7832">;</span><span style="color:#908b25">CHKERRQ</span>(ierr)<span style="color:#cc7832">;<br></span><span style="color:#cc7832"><br></span><span style="color:#cc7832">    </span>VecView(global<span style="color:#cc7832">,</span><span style="color:#908b25">PETSC_VIEWER_<wbr>STDOUT_WORLD</span>)<span style="color:#cc7832">;<br></span><span style="color:#cc7832"><br></span><span style="color:#cc7832">    </span>ierr = DMDAGetCorners(da<span style="color:#cc7832">,</span>&x<span style="color:#cc7832">,</span><span style="color:#908b25">NULL</span><span style="color:#cc7832">,</span><span style="color:#908b25">NULL</span><span style="color:#cc7832"><wbr>,</span>&xm<span style="color:#cc7832">,</span><span style="color:#908b25">NULL</span><span style="color:#cc7832">,</span><span style="color:#908b25">NULL</span>)<span style="color:#cc7832">;</span><span style="color:#908b25">CHKERRQ</span>(ierr)<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span>ierr = DMDAVecGetArray(da<span style="color:#cc7832">, </span>global<span style="color:#cc7832">, </span>&val)<span style="color:#cc7832">;</span><span style="color:#908b25">CHKERRQ</span>(ierr)<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span>ierr = PetscSynchronizedPrintf(<span style="color:#908b25">PETSC_<wbr>COMM_SELF</span><span style="color:#cc7832">, </span><span style="color:#6a8759">"Rank=%d</span><span style="color:#cc7832">\n</span><span style="color:#6a8759">"</span><span style="color:#cc7832">, </span>rank)<span style="color:#cc7832">;</span><span style="color:#908b25">CHKERRQ</span>(ierr)<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span><span style="color:#cc7832; font-weight:bold">for </span>(i = x<span style="color:#cc7832">; </span>i < x + xm<span style="color:#cc7832">; </span>++i) {<br>        ierr = PetscSynchronizedPrintf(<span style="color:#908b25">PETSC_<wbr>COMM_SELF</span><span style="color:#cc7832">, </span><span style="color:#6a8759">"%4.f "</span><span style="color:#cc7832">, </span>val[i])<span style="color:#cc7832">;</span><span style="color:#908b25">CHKERRQ</span>(ierr)<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span>}<br>    ierr = DMDAVecRestoreArray(da<span style="color:#cc7832">, </span>global<span style="color:#cc7832">, </span>&val)<span style="color:#cc7832">;</span><span style="color:#908b25">CHKERRQ</span>(ierr)<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span>PetscSynchronizedFlush(<span style="color:#908b25">PETSC_<wbr>COMM_SELF</span><span style="color:#cc7832">, </span>PETSC_STDOUT)<span style="color:#cc7832">;<br></span><span style="color:#808080"><br></span><span style="color:#808080">    </span>ierr = PetscFinalize()<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span><span style="color:#cc7832; font-weight:bold">return </span>ierr<span style="color:#cc7832">;<br></span>}</pre>
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<hr style="display:inline-block; width:98%">
<div id="x_m_-7389142704391856459divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" color="#000000" style="font-size:11pt"><b>From:</b> Matthew Knepley <<a href="mailto:knepley@gmail.com" target="_blank">knepley@gmail.com</a>><br>
<b>Sent:</b> Monday, April 9, 2018 23:29<br>
<b>To:</b> Chu Songtao<br>
<b>Cc:</b> Hong; <a href="mailto:petsc-users@mcs.anl.gov" target="_blank">petsc-users@mcs.anl.gov</a><br>
<b>Subject:</b> Re: [petsc-users] How to access Vector data properly</font>
<div> </div>
</div>
<div>
<div dir="ltr">
<div class="x_m_-7389142704391856459x_gmail_extra">
<div class="x_m_-7389142704391856459x_gmail_quote">On Mon, Apr 9, 2018 at 11:19 AM, Chu Songtao
<span dir="ltr"><<a href="mailto:st.chu@outlook.com" target="_blank">st.chu@outlook.com</a>></span> wrote:<br>
<blockquote class="x_m_-7389142704391856459x_gmail_quote" style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex">
<div dir="ltr">
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
Hong,</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
I added the function to the end, but the results didn't change.</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
Then I tested another thing, assigning global by Get(Restore)Array instead of DMDANaturalToGlobal. And the results switched. VecView outputs 0 and PetscSynchronizedPrintf outputs correct values. Strange.</div>
</div>
</blockquote>
<div><br>
</div>
<div>Send the whole code. You have a logic error somewhere.</div>
<div><br>
</div>
<div>   Matt</div>
<div> </div>
<blockquote class="x_m_-7389142704391856459x_gmail_quote" style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex">
<div dir="ltr">
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
<pre style="background-color:#2b2b2b; color:#a9b7c6; font-family:'Source Code Pro Semibold'; font-size:10.5pt">    ierr = DMDACreate1d(PETSC_COMM_WORLD<span style="color:#cc7832">,</span><span style="color:#9876aa; font-style:italic"><wbr>DM_BOUNDARY_NONE</span><span style="color:#cc7832">,</span><span style="color:#6897bb">5</span><span style="color:#cc7832">,</span><span style="color:#6897bb">1</span><span style="color:#cc7832">,</span><span style="color:#6897bb">1</span><span style="color:#cc7832">,</span><span style="color:#908b25">NULL</span><span style="color:#cc7832">,</span>&d<wbr>a)<span style="color:#cc7832">;</span><span style="color:#908b25">CHKERRQ</span>(ierr)<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span>ierr = DMSetUp(da)<span style="color:#cc7832">;</span><span style="color:#908b25">CHKERRQ</span>(ierr)<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span>ierr = DMCreateGlobalVector(da<span style="color:#cc7832">,</span>&globa<wbr>l)<span style="color:#cc7832">;</span><span style="color:#908b25">CHKERRQ</span>(ierr)<span style="color:#cc7832">;<br></span><span style="color:#808080"><br></span><span style="color:#808080">    </span>ierr = DMDAGetCorners(da<span style="color:#cc7832">,</span>&x<span style="color:#cc7832">,</span><span style="color:#908b25">NULL</span><span style="color:#cc7832">,</span><span style="color:#908b25">NULL</span><span style="color:#cc7832"><wbr>,</span>&xm<span style="color:#cc7832">,</span><span style="color:#908b25">NULL</span><span style="color:#cc7832">,</span><span style="color:#908b25">NULL</span>)<span style="color:#cc7832">;</span><span style="color:#908b25">CHKERRQ</span>(ierr)<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span>ierr = DMDAVecGetArray(da<span style="color:#cc7832">, </span>global<span style="color:#cc7832">, </span>&val)<span style="color:#cc7832">;</span><span style="color:#908b25">CHKERRQ</span>(ierr)<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span>ierr = PetscSynchronizedPrintf(<span style="color:#908b25">PETSC_<wbr>COMM_SELF</span><span style="color:#cc7832">, </span><span style="color:#6a8759">"Rank=%d</span><span style="color:#cc7832">\n</span><span style="color:#6a8759">"</span><span style="color:#cc7832">, </span>rank)<span style="color:#cc7832">;</span><span style="color:#908b25">CHKERRQ</span>(ierr)<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span><span style="color:#cc7832; font-weight:bold">for </span>(i = x<span style="color:#cc7832">; </span>i < x + xm<span style="color:#cc7832">; </span>++i) {<br>        val[i] = i<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span>}<br>    ierr = DMDAVecRestoreArray(da<span style="color:#cc7832">, </span>global<span style="color:#cc7832">, </span>&val)<span style="color:#cc7832">;</span><span style="color:#908b25">CHKERRQ</span>(ierr)<span style="color:#cc7832">;<br></span><span style="color:#cc7832"><br></span><span style="color:#cc7832">    </span>VecView(global<span style="color:#cc7832">,</span><span style="color:#908b25">PETSC_VIEWER_ST<wbr>DOUT_WORLD</span>)<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    <br></span><span style="color:#cc7832">    </span>ierr = DMDAGetCorners(da<span style="color:#cc7832">,</span>&x<span style="color:#cc7832">,</span><span style="color:#908b25">NULL</span><span style="color:#cc7832">,</span><span style="color:#908b25">NULL</span><span style="color:#cc7832"><wbr>,</span>&xm<span style="color:#cc7832">,</span><span style="color:#908b25">NULL</span><span style="color:#cc7832">,</span><span style="color:#908b25">NULL</span>)<span style="color:#cc7832">;</span><span style="color:#908b25">CHKERRQ</span>(ierr)<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span>ierr = DMDAVecGetArray(da<span style="color:#cc7832">, </span>global<span style="color:#cc7832">, </span>&val)<span style="color:#cc7832">;</span><span style="color:#908b25">CHKERRQ</span>(ierr)<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span>ierr = PetscSynchronizedPrintf(<span style="color:#908b25">PETSC_<wbr>COMM_SELF</span><span style="color:#cc7832">, </span><span style="color:#6a8759">"Rank=%d</span><span style="color:#cc7832">\n</span><span style="color:#6a8759">"</span><span style="color:#cc7832">, </span>rank)<span style="color:#cc7832">;</span><span style="color:#908b25">CHKERRQ</span>(ierr)<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span><span style="color:#cc7832; font-weight:bold">for </span>(i = x<span style="color:#cc7832">; </span>i < x + xm<span style="color:#cc7832">; </span>++i) {<br>        ierr = PetscSynchronizedPrintf(<span style="color:#908b25">PETSC_<wbr>COMM_SELF</span><span style="color:#cc7832">, </span><span style="color:#6a8759">"%4.f "</span><span style="color:#cc7832">, </span>val[i])<span style="color:#cc7832">;</span><span style="color:#908b25">CHKERRQ</span>(ierr)<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span>}<br>    ierr = DMDAVecRestoreArray(da<span style="color:#cc7832">, </span>global<span style="color:#cc7832">, </span>&val)<span style="color:#cc7832">;</span><span style="color:#908b25">CHKERRQ</span>(ierr)<span style="color:#cc7832">;<br></span><span style="color:#cc7832">    </span>PetscSynchronizedFlush(<span style="color:#908b25">PETSC_C<wbr>OMM_SELF</span><span style="color:#cc7832">, </span>PETSC_STDOUT)<span style="color:#cc7832">;</span></pre>
<div>$ mpiexec -n 2 ./test0</div>
<div>Rank=0</div>
<div>Rank=1</div>
<div>Vec Object: 2 MPI processes</div>
<div>  type: mpi</div>
<div>Vec Object: Vec_0x7fffd6629a90_0 2 MPI processes</div>
<div>  type: mpi</div>
<div>Rank=1</div>
<div>   3    4 Process [0]</div>
<div>0.</div>
<div>0.</div>
<div>0.</div>
<div>Process [1]</div>
<div>0.</div>
<div>0.</div>
<div>Rank=0</div>
<div>   0    1    2</div>
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<hr style="display:inline-block; width:98%">
<div id="x_m_-7389142704391856459x_m_8926363315591912394divRplyFwdMsg" dir="ltr">
<div> </div>
</div>
<div>
<div dir="ltr">
<div class="x_m_-7389142704391856459x_m_8926363315591912394x_gmail_extra">
<div class="x_m_-7389142704391856459x_m_8926363315591912394x_gmail_quote">Songtao :</div>
<div class="x_m_-7389142704391856459x_m_8926363315591912394x_gmail_quote">You may need adding PetscSynchronizedFlush() to the end. See petsc/src/dm/examples/test<wbr>s/ex43.c</div>
<div class="x_m_-7389142704391856459x_m_8926363315591912394x_gmail_quote">Hong<br>
<blockquote class="x_m_-7389142704391856459x_m_8926363315591912394x_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="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
Hello,</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
    I am just a beginner. Now I get confused on how to correctly use Vec and DMDA.</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
    I set 1D DMDA global vector with natural values for a test. Then use VecView and DMDAVecGetArray to view the data, but the results are different. I don't know why.</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
this is the code:</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
<pre style="background-color:rgb(43,43,43); color:rgb(169,183,198); font-family:"Source Code Pro Semibold"; font-size:10.5pt"><span style="color:rgb(185,188,209)">PetscMPIInt     </span>rank<span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(185,188,209)">PetscErrorCode  </span>ierr<span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(185,188,209)">Vec             </span>global<span style="color:rgb(204,120,50)">,</span>local<span style="color:rgb(204,120,50)">,</span>natural<span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(185,188,209)">DM              </span>da<span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(185,188,209)">PetscReal       </span>*xnatural<span style="color:rgb(204,120,50)">,</span>*val<span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(185,188,209)">PetscInt        </span>i<span style="color:rgb(204,120,50)">,</span>start<span style="color:rgb(204,120,50)">,</span>end<span style="color:rgb(204,120,50)">,</span>x<span style="color:rgb(204,120,50)">,</span>xm<span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(204,120,50)"><br></span><span style="color:rgb(204,120,50)"><br></span>ierr = PetscInitialize(&argc<span style="color:rgb(204,120,50)">,</span>&argv<span style="color:rgb(204,120,50)">,</span>(<span style="color:rgb(204,120,50); font-weight:bold">c<wbr>har</span>*)<span style="color:rgb(104,151,187)">0</span><span style="color:rgb(204,120,50)">,</span>help)<span style="color:rgb(204,120,50)">;</span><span style="color:rgb(204,120,50); font-weight:bold">if </span>(ierr) <span style="color:rgb(204,120,50); font-weight:bold">return </span>ierr<span style="color:rgb(204,120,50)">;<br></span>ierr = MPI_Comm_rank(PETSC_COMM_WORLD<span style="color:rgb(204,120,50)"><wbr>,</span>&rank)<span style="color:rgb(204,120,50)">;</span><span style="color:rgb(144,139,37)">CHKERRQ</span>(ierr)<span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(204,120,50)"><br></span>ierr = DMDACreate1d(PETSC_COMM_WORLD<span style="color:rgb(204,120,50)">,</span><span style="color:rgb(152,118,170); font-style:italic"><wbr>DM_BOUNDARY_NONE</span><span style="color:rgb(204,120,50)">,</span><span style="color:rgb(104,151,187)">5</span><span style="color:rgb(204,120,50)">,</span><span style="color:rgb(104,151,187)">1</span><span style="color:rgb(204,120,50)">,</span><span style="color:rgb(104,151,187)">1</span><span style="color:rgb(204,120,50)">,</span><span style="color:rgb(144,139,37)">NULL</span><span style="color:rgb(204,120,50)">,</span>&d<wbr>a)<span style="color:rgb(204,120,50)">;</span><span style="color:rgb(144,139,37)">CHKERRQ</span>(ierr)<span style="color:rgb(204,120,50)">;<br></span>ierr = DMSetUp(da)<span style="color:rgb(204,120,50)">;</span><span style="color:rgb(144,139,37)">CHKERRQ</span>(ierr)<span style="color:rgb(204,120,50)">;<br></span>ierr = DMCreateGlobalVector(da<span style="color:rgb(204,120,50)">,</span>&globa<wbr>l)<span style="color:rgb(204,120,50)">;</span><span style="color:rgb(144,139,37)">CHKERRQ</span>(ierr)<span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(204,120,50)"><br></span>ierr = DMDACreateNaturalVector(da<span style="color:rgb(204,120,50)">,</span>&na<wbr>tural)<span style="color:rgb(204,120,50)">;</span><span style="color:rgb(144,139,37)">CHKERRQ</span>(ierr)<span style="color:rgb(204,120,50)">;<br></span>ierr = VecGetOwnershipRange(natural<span style="color:rgb(204,120,50)">,</span>&<wbr>start<span style="color:rgb(204,120,50)">,</span>&end)<span style="color:rgb(204,120,50)">;</span><span style="color:rgb(144,139,37)">CHKERRQ</span>(ierr)<span style="color:rgb(204,120,50)">;<br></span>ierr = VecGetArray(natural<span style="color:rgb(204,120,50)">,</span>&xnatural)<span style="color:rgb(204,120,50)"><wbr>;</span><span style="color:rgb(144,139,37)">CHKERRQ</span>(ierr)<span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(204,120,50); font-weight:bold">for </span>(i=start<span style="color:rgb(204,120,50)">; </span>i<end<span style="color:rgb(204,120,50)">; </span>i++) {<br>    xnatural[i-start] = i<span style="color:rgb(204,120,50)">;<br></span>}<br>ierr = VecRestoreArray(natural<span style="color:rgb(204,120,50)">,</span>&xnatu<wbr>ral)<span style="color:rgb(204,120,50)">;</span><span style="color:rgb(144,139,37)">CHKERRQ</span>(ierr)<span style="color:rgb(204,120,50)">;<br></span>ierr = DMDANaturalToGlobalBegin(da<span style="color:rgb(204,120,50)">,</span>na<wbr>tural<span style="color:rgb(204,120,50)">,</span><span style="color:rgb(152,118,170); font-style:italic">INSERT_VALUES</span><span style="color:rgb(204,120,50)">,</span>global)<span style="color:rgb(204,120,50)">;</span><span style="color:rgb(144,139,37)">CH<wbr>KERRQ</span>(ierr)<span style="color:rgb(204,120,50)">;<br></span>ierr = DMDANaturalToGlobalEnd(da<span style="color:rgb(204,120,50)">,</span>natu<wbr>ral<span style="color:rgb(204,120,50)">,</span><span style="color:rgb(152,118,170); font-style:italic">INSERT_VALUES</span><span style="color:rgb(204,120,50)">,</span>global)<span style="color:rgb(204,120,50)">;</span><span style="color:rgb(144,139,37)">CHKE<wbr>RRQ</span>(ierr)<span style="color:rgb(204,120,50)">;<br></span>ierr = VecDestroy(&natural)<span style="color:rgb(204,120,50)">;</span><span style="color:rgb(144,139,37)">CHKERRQ</span>(i<wbr>err)<span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(204,120,50)"><br></span>VecView(global<span style="color:rgb(204,120,50)">,</span><span style="color:rgb(144,139,37)">PETSC_VIEWER_ST<wbr>DOUT_WORLD</span>)<span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(204,120,50)"><br></span>ierr = DMDAGetCorners(da<span style="color:rgb(204,120,50)">,</span>&x<span style="color:rgb(204,120,50)">,</span><span style="color:rgb(144,139,37)">NULL</span><span style="color:rgb(204,120,50)">,</span><span style="color:rgb(144,139,37)">NULL</span><span style="color:rgb(204,120,50)"><wbr>,</span>&xm<span style="color:rgb(204,120,50)">,</span><span style="color:rgb(144,139,37)">NULL</span><span style="color:rgb(204,120,50)">,</span><span style="color:rgb(144,139,37)">NULL</span>)<span style="color:rgb(204,120,50)">;</span><span style="color:rgb(144,139,37)">CHKERRQ</span>(ierr)<span style="color:rgb(204,120,50)">;<br></span>ierr = DMDAVecGetArray(da<span style="color:rgb(204,120,50)">, </span>global<span style="color:rgb(204,120,50)">, </span>&val)<span style="color:rgb(204,120,50)">;</span><span style="color:rgb(144,139,37)">CHKERRQ</span>(ierr)<span style="color:rgb(204,120,50)">;<br></span>ierr = PetscSynchronizedPrintf(<span style="color:rgb(144,139,37)">PETSC_<wbr>COMM_SELF</span><span style="color:rgb(204,120,50)">, </span><span style="color:rgb(106,135,89)">"Rank=%d</span><span style="color:rgb(204,120,50)">\n</span><span style="color:rgb(106,135,89)">"</span><span style="color:rgb(204,120,50)">, </span>rank)<span style="color:rgb(204,120,50)">;</span><span style="color:rgb(144,139,37)">CHKERRQ</span>(ierr)<span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(204,120,50); font-weight:bold">for </span>(i = x<span style="color:rgb(204,120,50)">; </span>i < x + xm<span style="color:rgb(204,120,50)">; </span>++i) {<br>    ierr = PetscSynchronizedPrintf(<span style="color:rgb(144,139,37)">PETSC_<wbr>COMM_SELF</span><span style="color:rgb(204,120,50)">, </span><span style="color:rgb(106,135,89)">"%4.f "</span><span style="color:rgb(204,120,50)">, </span>val[i])<span style="color:rgb(204,120,50)">;</span><span style="color:rgb(144,139,37)">CHKERRQ</span>(ierr)<span style="color:rgb(204,120,50)">;<br></span>}<br>ierr = DMDAVecRestoreArray(da<span style="color:rgb(204,120,50)">, </span>global<span style="color:rgb(204,120,50)">, </span>&val)<span style="color:rgb(204,120,50)">;</span><span style="color:rgb(144,139,37)">CHKERRQ</span>(ierr)<span style="color:rgb(204,120,50)">;<br></span></pre>
and the results are:</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
<div>$ ./test0</div>
<div>Vec Object: 1 MPI processes</div>
<div>  type: seq</div>
<div>Vec Object: Vec_0x7ffff3cbc500_0 1 MPI processes</div>
<div>  type: mpi</div>
<div>Process [0]</div>
<div>0.</div>
<div>1.</div>
<div>2.</div>
<div>3.</div>
<div>4.</div>
<div>Rank=0</div>
<div>   0    1    2    3    4 </div>
<div><br>
</div>
<div>$ mpiexec -n 2 ./test0</div>
<div>Vec Object: 2 MPI processes</div>
<div>  type: mpi</div>
<div>Vec Object: Vec_0x7fffcf948a90_0 2 MPI processes</div>
<div>  type: mpi</div>
<div>Process [0]</div>
<div>0.</div>
<div>1.</div>
<div>2.</div>
<div>Process [1]</div>
<div>3.</div>
<div>4.</div>
<div>Rank=0</div>
<div>   0    0    0 Rank=1</div>
<div>   0    0</div>
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
the seq version is correct, but the mpi version is wrong. Every element in val is 0.</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
<br>
</div>
</div>
</blockquote>
</div>
<br>
<span class="x_HOEnZb"><font color="#888888"></font></span></div>
<span class="x_HOEnZb"><font color="#888888"></font></span></div>
<span class="x_HOEnZb"><font color="#888888"></font></span></div>
<span class="x_HOEnZb"><font color="#888888"></font></span></div>
<span class="x_HOEnZb"><font color="#888888"></font></span></blockquote>
<span class="x_HOEnZb"><font color="#888888"></font></span></div>
<span class="x_HOEnZb"><font color="#888888"><br>
<br clear="all">
<div><br>
</div>
-- <br>
<div class="x_m_-7389142704391856459x_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/~<wbr>knepley/</a><br>
</div>
</div>
</div>
</div>
</div>
</font></span></div>
</div>
</div>
</div>
</blockquote>
</div>
<br>
<br clear="all">
<div><br>
</div>
-- <br>
<div class="x_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>
</body>
</html>