<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){<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>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));<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>return 0;<br>}<br><br><br></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"><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>
<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 class="HOEnZb"><div class="h5"><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>