[petsc-users] How to get values from a matrix and set again?
Timothée Nicolas
timothee.nicolas at gmail.com
Mon Dec 7 00:43:29 CST 2015
Indeed the manual says (
http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatGetValues.html
)
MatGetValues
<http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatGetValues.html#MatGetValues>()
requires that the matrix has been assembled with MatAssemblyBegin
<http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatAssemblyBegin.html#MatAssemblyBegin>
()/MatAssemblyEnd
<http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatAssemblyEnd.html#MatAssemblyEnd>().
Thus, calls to MatSetValues
<http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatSetValues.html#MatSetValues>()
and MatGetValues
<http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatGetValues.html#MatGetValues>()
CANNOT be made in succession without intermediate matrix assembly.
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.
Timothee
2015-12-07 15:37 GMT+09:00 Bibrak Qamar <bibrakc at gmail.com>:
> Yes, I called MatAssemblyBegin and MatAssemblyEnd.
>
> But I figured out that you need to call it every time you use
> MatGetValues()
>
> Here is the updated code:
>
>
> PetscInt sigmoidMat(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++){
> MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);
> MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);
> ierr = MatGetValues(x,1,&Ii,1,&Ji,&v);CHKERRQ(ierr);
> v = 1.0/(1+exp(-1*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);
> return 0;
> }
>
>
>
> Bibrak Qamar
>
>
> On Mon, Dec 7, 2015 at 1:29 AM, Timothée Nicolas <
> timothee.nicolas at gmail.com> wrote:
>
>> Hi,
>>
>> The error message seems to tell you that your matrix is not assembled.
>> Have you used the calls
>>
>> MatAssemblyBegin
>>
>> and
>>
>> MatAssemblyEnd
>>
>> When you build your matrix ?
>>
>> Timothee
>>
>>
>> 2015-12-07 15:11 GMT+09:00 Bibrak Qamar <bibrakc at gmail.com>:
>>
>>> 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.
>>>
>>> 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
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20151207/b1eef752/attachment-0001.html>
More information about the petsc-users
mailing list