<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Sat, Jun 7, 2014 at 1:02 PM, LikunTan <span dir="ltr"><<a href="mailto:tlk0812@hotmail.com" target="_blank">tlk0812@hotmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<div><div dir="ltr"><div>Hello,</div><div><br></div><div>Thank you for your reply.  Sorry I did not make it clear. I added the part where I set values for M before calling VecGetArray(). Below is a more complete code. I am not sure how to access the elements consistently by using aM.  </div>
</div></div></blockquote><div><br></div><div>As Barry pointed out, this cannot work in parallel. A Vec stores contiguous chunks of memory in process order. VecGetArray()</div><div>has access to the local chunk, starting from the process offset. This is detalied in the manual. Random access in parallel is</div>
<div>not efficiently achievable.</div><div><br></div><div>  Thanks,</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 dir="ltr">**************************************************************<br>VecCreate(PETSC_COMM_WORLD, &M);<br>VecSetSizes(M, LEN*DOF, NODE*DOF); <div><br></div><div>//setting LEN and NODENP[] in the function get_nodes_process</div>
<div>//the stored data are not contiguous</div><div>MPI_Comm_rank(PETSC_COMM_WORLD, &rank);</div><div>MPI_Comm_size(PETSC_COMM_WORLD, &size);</div><div>get_nodes_process(NODE, size, rank)<br><div><br></div><div>//set the values of M</div>
<div>for(int i=0; i<LEN; i++)</div><div>{</div><div>       node=NODENP[i];</div><div>       for(int j=0; j<DOF; j++)</div><div>       {</div><div>              col[j]=node*DOF+j;</div><div>              val[j]=1.0*j;</div>
<div>       }</div><div>       VecSetValues(M, DOF, col, val, INSERT_VALUES);</div><div>}</div><div>VecAssemblyBegin(M);</div><div>VecAssemblyEnd(M);</div><div> <br>//change values of M <br>VecGetArray(M, &aM); <br>for(int i=0; i<LEN; i++)<br>
{<br>       node=NODENP[i];<br>       for(int j=0; j<DOF; j++)<br>      {<br>           aM[node*DOF+j] or aM[i*DOF+j] ? //accessing the elements of M<br>     }<br>}<br>VecRestoreArray(M, &aM); <br>*********************************************************<br>
<br>best,</div><div><br></div><div><br><div>> Subject: Re: [petsc-users] about VecGetArray()<br>> From: <a href="mailto:bsmith@mcs.anl.gov" target="_blank">bsmith@mcs.anl.gov</a><br>> Date: Sat, 7 Jun 2014 10:45:58 -0500<br>
> CC: <a href="mailto:petsc-users@mcs.anl.gov" target="_blank">petsc-users@mcs.anl.gov</a><br>> To: <a href="mailto:tlk0812@hotmail.com" target="_blank">tlk0812@hotmail.com</a><br>> <br>> <br>>   It looks like you are trying to access the entire vector on all processes. You cannot do this. VecGetArray() only gives you access to the local values. If you need to access the entire vector on each process (which is not scalable or a good idea) you can use VecScatterCreateToAll() or VecScatterCreateToZero() then scatter the vector then use VecGetArray() on the now new sequential vector.<br>
> <br>>    Barry<br>> <br>> On Jun 7, 2014, at 1:40 AM, LikunTan <<a href="mailto:tlk0812@hotmail.com" target="_blank">tlk0812@hotmail.com</a>> wrote:<br>> <br>> > Hello,<br>> > <br>> > I defined the partition of Vector, which is not stored contiguously. Here is a part of my code. The total number of nodes is NODE*DOF. Before executing this following code, I defined LEN and an array NODENP[] to store the number of nodes and the list of nodes in each processor. I accessed the element using aM[node*DOF+j] and aM[i*DOF+j], but none of them gave me the correct answer. Your help is well appreciated.<br>
> > <br>> > **************************************************************<br>> > VecCreate(PETSC_COMM_WORLD, &M);<br>> > VecSetSizes(M, LEN*DOF, NODE*DOF);  <br>> > <br>> > VecGetArray(M, &aM); <br>
> > <br>> > for(int i=0; i<LEN; i++)<br>> > {<br>> >     node=NODENP[i];<br>> >     for(int j=0; j<DOF; j++)<br>> >     {<br>> >         aM[node*DOF+j] or aM[i*DOF+j] ?  //accessing the elements of M<br>
> >         <br>> >     }<br>> > }<br>> > VecRestoreArray(M, &aM); <br>> > *********************************************************<br>> <br></div></div></div>                                       </div></div>

</blockquote></div><br><br clear="all"><div><br></div>-- <br>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>