[petsc-users] Writing PETSc matrices

Benjamin Sanderse B.Sanderse at cwi.nl
Wed Oct 27 03:26:56 CDT 2010


I have a somewhat related question regarding sending data to Matlab.
For a while I have been sending vectors back and forth between Matlab and Petsc and that works perfect.

In addition I also want to send some information like number of iterations, norm of residual and solution time to Matlab. This gives me some headaches when I run the code in parallel:

- Can I simply send a PetscInt like number of iterations to Matlab? I tried PetscIntView, but this does not work:
PetscInt iterations;
ierr = KSPGetIterationNumber(ksp,&iterations);CHKERRQ(ierr);
fd   = PETSC_VIEWER_SOCKET_WORLD;
ierr = PetscIntView(1,iterations,fd);CHKERRQ(ierr);

Petsc-Matlab communication hangs without an error on the Petsc side.

- As alternative I tried to set a vector and set this to Matlab with VecView. This works, although it results (for this example) in a nx1 vector (n=no. of processors) that is received by Matlab, while I actually just want a 1x1 vector:
ierr = VecCreateMPI(PETSC_COMM_WORLD,1,PETSC_DECIDE,&to_matlab);CHKERRQ(ierr);
ierr = KSPGetIterationNumber(ksp,&iterations);CHKERRQ(ierr);
fd   = PETSC_VIEWER_SOCKET_WORLD;
ierr = VecSet(to_matlab,iterations);CHKERRQ(ierr);
ierr = VecView(to_matlab,fd);CHKERRQ(ierr);

- So although I am not too happy with a nx1 vector instead of a 1x1 vector I could live with that. A bigger problem is that if instead of the number of iterations I want to pass the solution time to a vector, I get an error:

PetscReal      time1,time2,t_solve;

ierr = VecCreateMPI(PETSC_COMM_WORLD,1,PETSC_DECIDE,&to_matlab);CHKERRQ(ierr);
fd   = PETSC_VIEWER_SOCKET_WORLD;
ierr = PetscGetTime(&time1);CHKERRQ(ierr);
// some matrix solve
ierr = PetscGetTime(&time2);CHKERRQ(ierr);
t_solve = time2-time1;
ierr = VecSet(to_matlab,t_solve);CHKERRQ(ierr);
ierr = VecView(to_matlab,fd);CHKERRQ(ierr);

this produces the following error:
[1]PETSC ERROR: --------------------- Error Message ------------------------------------
[1]PETSC ERROR: Invalid argument!
[1]PETSC ERROR: Same value should be used across all processors!
[1]PETSC ERROR: ------------------------------------------------------------------------

When I run Petsc with 1 processor there is no error. Any ideas?


Ben


----- Original Message -----
From: "Barry Smith" <bsmith at mcs.anl.gov>
To: "PETSc users list" <petsc-users at mcs.anl.gov>
Sent: Tuesday, October 26, 2010 10:57:04 PM
Subject: Re: [petsc-users] Writing PETSc matrices


   Use PetscBinaryWrite('filename',sparsematlabmatrix)  I do not know why your second argument has quotes around it.

   Barry


On Oct 26, 2010, at 3:33 PM, Nunion wrote:

> Hello, 
> 
> I am new to PETSc and programming.  I have a question concerning writing PETSc matrices in binary from binary matrices [compressessed/uncompressed] generated in Matlab.  I am attempting to use the files in the /bin/matlab directory, in particular the PetscBinaryWrite.m file.  However, the usage;
> 
> PetscBinaryWrite('matrix.mat','output.ex') does not seem to work.  I also tried using the examples in the /mat directory however, matlab does not support the writing of complex matrices in ASCII.
> 
> Thanks in advance,
> 
> Tom



More information about the petsc-users mailing list