[petsc-users] Wrong variable name in MatView with PETSC_VIEWER_ASCII_MATLAB

Michael Souza souza.michael at gmail.com
Sun Jan 18 06:59:05 CST 2015


​​Barry,

You are right about the importance of preallocation at MATLAB, but
unfortunately this code doesn't do it.​

When the variable zzz is instantiated by zzz = zeros(m,n), MATLAB creates a
zero dense matrix whose memory size is m*n*sizeof(doubles). However, the
next code line is zzz = [i j x] which releases the previously preallocated
space and creates a new one whose size is equal to the triple of the size
of i (= sum of the sizes of i, j, and x). And the last line, A =
spconvert(zzz), finally creates a new sparse matrix whose memory is not
associated to the one of zzz. So, there is no preallocation.

I tested two codes to illustrate this. The first code uses the supposed
preallocation and the second one doesn't do it. As you can see from the
figure below (the same figure follows attached), the second version is much
more faster.


Cheers,
Michael

On Sat, Jan 17, 2015 at 7:56 PM, Barry Smith <bsmith at mcs.anl.gov> wrote:

>
>   So it turns out my fix broke something else (I should have done more
> testing). I've attached the patch to the patch I provided for you. Apply
> this after the you apply the patch I previously sent you
>   Or better start using the copy of the PETSc in the repository:
> http://www.mcs.anl.gov/petsc/developers/index.html  the branch maint
> corresponds to the last release plus some additional fixes. The branch
> master corresponds to the development copy that we are currently adding
> features to.
>
>    Barry
>
> > On Jan 17, 2015, at 3:46 PM, Barry Smith <bsmith at mcs.anl.gov> wrote:
> >
> >
> >  Michael,
> >
> >    Thanks for reporting this. I have fixed it in out maint and master
> branch and it will be fixed in our next patch update. I've also attached a
> patch file that you can use to update your copy of PETSc.
> > <fix-matview-matlab-name.patch>
> >
> > Note 1:  The reason for the zzz = zeros(3,3); is that for large matrices
> in Matlab it is (was?) much better to allocate a array of the correct final
> size before actually providing the entries in the matrix, otherwise Matlab
> would slowly dynamically keep increasing the size of the matrix as it found
> more entries in the matrix you provided. This is why we have this line.
> >
> > zzz = zeros(3,3);
> > zzz = [
> > 1 1  1.0000000000000000e+00
> > 2 2  1.0000000000000000e+00
> > 3 3  1.0000000000000000e+00
> > ];
> > A = spconvert(zzz);
> >
> > Note: 2 If the matrix is large at all you do not want to save as an ASII
> matlab file. Instead save using the binary viewer and use the PETSc Matlab
> script PetscBinaryRead() to read it into Matlab. This will be much much
> faster for large matrices. For tiny matrices using the ASCII viewer is fine.
> >
> >   Thanks
> >   Barry
> >
> >
> >
> >> On Jan 17, 2015, at 2:29 PM, Michael Souza <souza.michael at gmail.com>
> wrote:
> >>
> >> The name of variable created by MatView using PETSC_VIEWER_ASCII_MATLAB
> is not the expected one. More clearly, the name of variable is "zzz" no
> matter what you set by calling PetscObjectSetName.
> >>
> >> The code below reproduces the unexpected behaviour. There, I create a
> diagonal MATMPIAIJ matrix and I set its name to "A", but the name of
> created variable in MATLAB's script is "zzz".
> >>
> >> I also would like to point that there is no need to print the line "zzz
> = [m n];" once its value will not be used and it will produce an warning at
> Matlab's editor. So I think it could be removed.
> >>
> >> Note: This strange behaviour doesn't occur with MATSEQAIJ matrices.
> >>
> >> Cheers,
> >> Michael Souza
> >> ----------------------------------------------------------
> >> int main(int argc, char **args) {
> >>    PetscErrorCode ierr;
> >>    Mat A;
> >>    PetscInt i, N = 3;
> >>
> >>    ierr = PetscInitialize(&argc, &args, (char *) 0, help);CHKERRQ(ierr);
> >>
> >>    // matrix creation and setup
> >>    ierr = MatCreate(PETSC_COMM_WORLD, &A);CHKERRQ(ierr);
> >>    ierr = MatSetType(A, MATMPIAIJ);CHKERRQ(ierr);
> >>    ierr = MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, N,
> N);CHKERRQ(ierr);
> >>    ierr = MatSetUp(A);CHKERRQ(ierr);
> >>    for (i = 0; i < N; i++) {
> >>        ierr = MatSetValue(A, i, i, 1.0, INSERT_VALUES);CHKERRQ(ierr);
> >>    }
> >>    ierr = MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
> >>    ierr = MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
> >>
> >>    // matlab viewer
> >>    ierr = PetscViewerSetFormat(PETSC_VIEWER_STDOUT_WORLD,
> PETSC_VIEWER_ASCII_MATLAB);CHKERRQ(ierr);
> >>    ierr = PetscObjectSetName((PetscObject) A, "A");CHKERRQ(ierr);
> >>    ierr = MatView(A, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
> >>
> >>    // free memory
> >>    ierr = MatDestroy(&A);CHKERRQ(ierr);
> >>
> >>    ierr = PetscFinalize();CHKERRQ(ierr);
> >>
> >>    return EXIT_SUCCESSSS;
> >> }
> >> ----------------------------------------------------------
> >>
> >
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20150118/00694514/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: prealloc.jpg
Type: image/jpeg
Size: 24381 bytes
Desc: not available
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20150118/00694514/attachment-0002.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: prealloc.jpg
Type: image/jpeg
Size: 24381 bytes
Desc: not available
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20150118/00694514/attachment-0003.jpg>


More information about the petsc-users mailing list