[petsc-users] How to get values from a matrix and set again?

Matthew Knepley knepley at gmail.com
Mon Dec 7 05:23:35 CST 2015


On Mon, Dec 7, 2015 at 12:11 AM, Bibrak Qamar <bibrakc at gmail.com> wrote:

> Hi,
>
> 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.
>

This pattern is problematic because we have no good way to indicate to
PETSc that only 1 process intends
to change a value, so after each SetValues() you need an assembly. You want
something like MatChop(),


http://www.mcs.anl.gov/petsc/petsc-current/src/mat/utils/axpy.c.html#MatChop

  Thanks,

     Matt


> 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?
>
> int sigmoid(Mat x){
> printf("in sigmoid\n");
>
> PetscErrorCode ierr;
> PetscInt       Istart, Iend, Ii, Ji, rows, cols;
>
> PetscScalar     v = 0.0;
> ierr = MatGetOwnershipRange(x,&Istart,&Iend);CHKERRQ(ierr);
> MatGetSize(x,&rows,&cols);
>
> for (Ii=Istart; Ii<Iend; Ii++) {
>      for (Ji=0; Ji<cols; Ji++){
>     ierr = MatGetValues(x,1,&Ii,1,&Ji,&v);CHKERRQ(ierr);
>     v = mysigmoid(v);
>     ierr = MatSetValues(x,1,&Ii,1,&Ji,&v,INSERT_VALUES);CHKERRQ(ierr);
>     }
>   }
>
> MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);
> MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);
>
>
> MatView(x,PETSC_VIEWER_STDOUT_WORLD);
> }
>
>
> error msg is:
>
> [0]PETSC ERROR: --------------------- Error Message
> --------------------------------------------------------------
> [0]PETSC ERROR: Object is in wrong state
> [0]PETSC ERROR: Not for unassembled matrix
> [0]PETSC ERROR: See http://www.mcs.anl.gov/petsc/documentation/faq.html
> for trouble shooting.
> [0]PETSC ERROR: Petsc Release Version 3.6.3, Dec, 03, 2015
> [
> [0]PETSC ERROR: #1 MatGetValues() line 1780 in
> /gpfs/home/petsc-3.6.3/src/mat/interface/matrix.c
> [0]PETSC ERROR: #2 sigmoid() line 21 in neural.c
>
>
> Thanks
> Bibrak Qamar
>
>


-- 
What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which their
experiments lead.
-- Norbert Wiener
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20151207/d93f69e6/attachment.html>


More information about the petsc-users mailing list