<div dir="ltr">Thanks<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 2:17 PM, Barry Smith <span dir="ltr"><<a href="mailto:bsmith@mcs.anl.gov" target="_blank">bsmith@mcs.anl.gov</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
Presumably you are using a MPIAIJ matrix. You can use<br>
<br>
Mat x,A,B;<br>
MatMPIAIJGetSeqAIJ(x,&A,&B,NULL);<br>
<br>
PetscScalar *a;<br>
MatSeqAIJGetArray(A,&a);<br>
MatInfo info;<br>
MatGetInfo(A,MAT_LOCAL,&info);<br>
for (i=0; i<info.nz_used; i++) {<br>
a[i] = yourfunction(a[i])<br>
}<br>
MatSeqAIJRestoreArray(A,&a);<br>
/* do same thing for B matrix */<br>
<div class="HOEnZb"><div class="h5"><br>
> On Dec 7, 2015, at 12:11 AM, Bibrak Qamar <<a href="mailto:bibrakc@gmail.com">bibrakc@gmail.com</a>> wrote:<br>
><br>
> Hi,<br>
><br>
> 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>
> 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>
><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>
> 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" rel="noreferrer" 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>
><br>
><br>
> Thanks<br>
> Bibrak Qamar<br>
><br>
<br>
</div></div></blockquote></div><br></div>