<div dir="ltr">Indeed the manual says (<a href="http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatGetValues.html">http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatGetValues.html</a>)<br><br><a href="http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatGetValues.html#MatGetValues">MatGetValues</a>() requires that the matrix has been assembled
with <a href="http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatAssemblyBegin.html#MatAssemblyBegin">MatAssemblyBegin</a>()/<a href="http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatAssemblyEnd.html#MatAssemblyEnd">MatAssemblyEnd</a>().  Thus, calls to
<a href="http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatSetValues.html#MatSetValues">MatSetValues</a>() and <a href="http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatGetValues.html#MatGetValues">MatGetValues</a>() CANNOT be made in succession
without intermediate matrix assembly.
<br><div class="gmail_extra"><br></div><div class="gmail_extra">However in your case, I don't think you want to Assemble the matrix every time you retrieve a value, since you want to update all the matrix it seems highly unefficient. I think you should first put all the values you're interested in (presumably all the values in your matrix) in an array, transform the values in the array with your sigmoid function, and then put them back in the matrix, and finally assemble it. <br></div><div class="gmail_extra"><br></div><div class="gmail_extra">Timothee<br></div><div class="gmail_extra"><br><div class="gmail_quote">2015-12-07 15:37 GMT+09:00 Bibrak Qamar <span dir="ltr"><<a href="mailto:bibrakc@gmail.com" target="_blank">bibrakc@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div>Yes, I called MatAssemblyBegin and MatAssemblyEnd.<br><br></div>But I figured out that you need to call it every time you use MatGetValues()<br><br></div>Here is the updated code:<br><br><br>PetscInt sigmoidMat(Mat x){<span class=""><br>printf("in sigmoid\n");<br><br>PetscErrorCode ierr;<br>PetscInt       Istart, Iend, Ii, Ji, rows, cols;<br><br><br>PetscScalar     v = 0.0;<br>ierr = MatGetOwnershipRange(x,&Istart,&Iend);CHKERRQ(ierr);<br>MatGetSize(x,&rows,&cols);<br><br><br>for (Ii=Istart; Ii<Iend; Ii++) {<br>     for (Ji=0; Ji<cols; Ji++){<br></span>MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);<br>MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);  <br>    ierr = MatGetValues(x,1,&Ii,1,&Ji,&v);CHKERRQ(ierr);<br>    v = 1.0/(1+exp(-1*v));<span class=""><br>    ierr = MatSetValues(x,1,&Ii,1,&Ji,&v,INSERT_VALUES);CHKERRQ(ierr);<br>    }<br>  }<br><br>MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);<br>MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);<br><br><br>MatView(x,PETSC_VIEWER_STDOUT_WORLD);<br></span>return 0;<br>}<span class="HOEnZb"><font color="#888888"><br><br><br></font></span></div><div class="gmail_extra"><span class="HOEnZb"><font color="#888888"><br clear="all"><div><div><div dir="ltr"><span style="color:rgb(0,0,0)">Bibrak Qamar</span><br style="color:rgb(0,0,0)"><span style="color:rgb(0,0,0)"><br></span><font color="#888888"></font></div></div></div></font></span><div><div class="h5">
<br><div class="gmail_quote">On Mon, Dec 7, 2015 at 1:29 AM, Timothée Nicolas <span dir="ltr"><<a href="mailto:timothee.nicolas@gmail.com" target="_blank">timothee.nicolas@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div>Hi,<br><br></div>The error message seems to tell you that your matrix is not assembled. Have you used the calls<br><br></div>MatAssemblyBegin <br><br>and<br></div><br>MatAssemblyEnd<br><br></div>When you build your matrix ?<br><br></div>Timothee<br><div><div><br></div></div></div><div><div><div class="gmail_extra"><br><div class="gmail_quote">2015-12-07 15:11 GMT+09:00 Bibrak Qamar <span dir="ltr"><<a href="mailto:bibrakc@gmail.com" target="_blank">bibrakc@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div>Hi,<br><br></div>I am trying to apply a function on each element of a matrix. The function is the sigmoid function which transforms each element of the matrix.<br><br></div>I am trying to approach it like this but get error. What could be the problem? Or is there a better way to do this?<br><div><br>int sigmoid(Mat x){<br>printf("in sigmoid\n");<br><br>PetscErrorCode ierr;<br>PetscInt       Istart, Iend, Ii, Ji, rows, cols;<br><br>PetscScalar     v = 0.0;<br>ierr = MatGetOwnershipRange(x,&Istart,&Iend);CHKERRQ(ierr);<br>MatGetSize(x,&rows,&cols);<br><br>for (Ii=Istart; Ii<Iend; Ii++) {<br>     for (Ji=0; Ji<cols; Ji++){<br>    ierr = MatGetValues(x,1,&Ii,1,&Ji,&v);CHKERRQ(ierr);<br>    v = mysigmoid(v);<br>    ierr = MatSetValues(x,1,&Ii,1,&Ji,&v,INSERT_VALUES);CHKERRQ(ierr);<br>    }<br>  }<br><br>MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);<br>MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);<br><br><br>MatView(x,PETSC_VIEWER_STDOUT_WORLD);<br>}<br><br><br></div><div>error msg is:<br><br>[0]PETSC ERROR: --------------------- Error Message --------------------------------------------------------------<br>[0]PETSC ERROR: Object is in wrong state<br>[0]PETSC ERROR: Not for unassembled matrix<br>[0]PETSC ERROR: See <a href="http://www.mcs.anl.gov/petsc/documentation/faq.html" target="_blank">http://www.mcs.anl.gov/petsc/documentation/faq.html</a> for trouble shooting.<br>[0]PETSC ERROR: Petsc Release Version 3.6.3, Dec, 03, 2015 <br>[<br>[0]PETSC ERROR: #1 MatGetValues() line 1780 in /gpfs/home/petsc-3.6.3/src/mat/interface/matrix.c<br>[0]PETSC ERROR: #2 sigmoid() line 21 in neural.c<br></div><div><div><br><br></div><div>Thanks<span><font color="#888888"><br clear="all"></font></span></div><span><font color="#888888"><div><div><div><div><div dir="ltr"><span style="color:rgb(0,0,0)">Bibrak Qamar</span><br style="color:rgb(0,0,0)"><span style="color:rgb(0,0,0)"><br></span><font color="#888888"></font></div></div></div>
</div></div></font></span></div></div>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div></div></div>
</blockquote></div><br></div></div>