<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<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);">
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:#2b2b2b;color:#a9b7c6;font-family:'Source Code Pro Semibold';font-size:10.5pt;"><span style="color:#b9bcd1;">PetscMPIInt     </span>rank<span style="color:#cc7832;">;<br></span><span style="color:#b9bcd1;">PetscErrorCode  </span>ierr<span style="color:#cc7832;">;<br></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:#b9bcd1;">DM              </span>da<span style="color:#cc7832;">;<br></span><span style="color:#b9bcd1;">PetscReal       </span>*xnatural<span style="color:#cc7832;">,</span>*val<span style="color:#cc7832;">;<br></span><span style="color:#b9bcd1;">PetscInt        </span>i<span style="color:#cc7832;">,</span>start<span style="color:#cc7832;">,</span>end<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>ierr = PetscInitialize(&argc<span style="color:#cc7832;">,</span>&argv<span style="color:#cc7832;">,</span>(<span style="color:#cc7832;font-weight:bold;">char</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>ierr = MPI_Comm_rank(PETSC_COMM_WORLD<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;"><br></span>ierr = DMDACreate1d(PETSC_COMM_WORLD<span style="color:#cc7832;">,</span><span style="color:#9876aa;font-style:italic;">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>&da)<span style="color:#cc7832;">;</span><span style="color:#908b25;">CHKERRQ</span>(ierr)<span style="color:#cc7832;">;<br></span>ierr = DMSetUp(da)<span style="color:#cc7832;">;</span><span style="color:#908b25;">CHKERRQ</span>(ierr)<span style="color:#cc7832;">;<br></span>ierr = DMCreateGlobalVector(da<span style="color:#cc7832;">,</span>&global)<span style="color:#cc7832;">;</span><span style="color:#908b25;">CHKERRQ</span>(ierr)<span style="color:#cc7832;">;<br></span><span style="color:#cc7832;"><br></span>ierr = DMDACreateNaturalVector(da<span style="color:#cc7832;">,</span>&natural)<span style="color:#cc7832;">;</span><span style="color:#908b25;">CHKERRQ</span>(ierr)<span style="color:#cc7832;">;<br></span>ierr = VecGetOwnershipRange(natural<span style="color:#cc7832;">,</span>&start<span style="color:#cc7832;">,</span>&end)<span style="color:#cc7832;">;</span><span style="color:#908b25;">CHKERRQ</span>(ierr)<span style="color:#cc7832;">;<br></span>ierr = VecGetArray(natural<span style="color:#cc7832;">,</span>&xnatural)<span style="color:#cc7832;">;</span><span style="color:#908b25;">CHKERRQ</span>(ierr)<span style="color:#cc7832;">;<br></span><span style="color:#cc7832;font-weight:bold;">for </span>(i=start<span style="color:#cc7832;">; </span>i<end<span style="color:#cc7832;">; </span>i++) {<br>    xnatural[i-start] = i<span style="color:#cc7832;">;<br></span>}<br>ierr = VecRestoreArray(natural<span style="color:#cc7832;">,</span>&xnatural)<span style="color:#cc7832;">;</span><span style="color:#908b25;">CHKERRQ</span>(ierr)<span style="color:#cc7832;">;<br></span>ierr = DMDANaturalToGlobalBegin(da<span style="color:#cc7832;">,</span>natural<span style="color:#cc7832;">,</span><span style="color:#9876aa;font-style:italic;">INSERT_VALUES</span><span style="color:#cc7832;">,</span>global)<span style="color:#cc7832;">;</span><span style="color:#908b25;">CHKERRQ</span>(ierr)<span style="color:#cc7832;">;<br></span>ierr = DMDANaturalToGlobalEnd(da<span style="color:#cc7832;">,</span>natural<span style="color:#cc7832;">,</span><span style="color:#9876aa;font-style:italic;">INSERT_VALUES</span><span style="color:#cc7832;">,</span>global)<span style="color:#cc7832;">;</span><span style="color:#908b25;">CHKERRQ</span>(ierr)<span style="color:#cc7832;">;<br></span>ierr = VecDestroy(&natural)<span style="color:#cc7832;">;</span><span style="color:#908b25;">CHKERRQ</span>(ierr)<span style="color:#cc7832;">;<br></span><span style="color:#cc7832;"><br></span>VecView(global<span style="color:#cc7832;">,</span><span style="color:#908b25;">PETSC_VIEWER_STDOUT_WORLD</span>)<span style="color:#cc7832;">;<br></span><span style="color:#cc7832;"><br></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;">,</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>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>ierr = PetscSynchronizedPrintf(<span style="color:#908b25;">PETSC_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;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_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>}<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></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>
</body>
</html>