<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:΢ÈíÑźÚ
}
--></style></head>
<body class='hmmessage'><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><br></div>**************************************************************<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: bsmith@mcs.anl.gov<br>> Date: Sat, 7 Jun 2014 10:45:58 -0500<br>> CC: petsc-users@mcs.anl.gov<br>> To: tlk0812@hotmail.com<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 <tlk0812@hotmail.com> 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></body>
</html>