[petsc-users] Storing a matrix in PETSc binary form?
Barry Smith
bsmith at mcs.anl.gov
Wed Jul 13 08:55:17 CDT 2011
On Jul 13, 2011, at 2:40 AM, John Chludzinski wrote:
> $ ls -l
> -rw-r--r-- 1 John None 192208072 Jul 13 03:31 binaryoutput
> -rw-r--r-- 1 John None 0 Jul 13 03:31 binaryoutput.info
> -rw-r--r-- 1 John None 128128032 Jul 13 01:43 DOF4002_k_double.bin
> -rw-r--r-- 1 John None 128128032 Jul 13 01:43 DOF4002_m_double.bin
>
> where DOF4002_k_double.bin and DOF4002_m_double.bin are the file to be converted.
>
> What is binaryoutput.info (size=0)?
It can store additional meta information about the matrix. You can ignore it.
> And why is binaryoutput so much fatter than either DOF4002_k_double.bin or DOF4002_m_double.bin?
The default PETSc binary storage is for sparse matrices so it stores both the nonzero values AND the column indices of the nonzero values (which are all of them).
If you call PetscViewerSetFormat(viewer,PETSC_VIEWER_NATIVE); before the MatView() then it will store the Petsc binary matrix without the column indices. BUT then you can only read it back in as a MATSEQDENSE or MATMPIDENSE matrix.
Barry
>
> ---John
>
> On Wed, Jul 13, 2011 at 3:24 AM, John Chludzinski <jchludzinski at gmail.com> wrote:
> Is this what you had in mind (more or less)? ---John
>
> //-----------------------------------CONVERTER---------------------------------------------------------------------------------------------------------
>
> static char help[] = "Reads matrix: \n -f <input_file> : file to load \n\n";
>
> #define SIZE 4002
>
> #include <stdio.h>
> #include <stdlib.h>
> #include "petscmat.h"
>
> int main(int argc,char **args)
> {
> Mat A;
> char file[PETSC_MAX_PATH_LEN];
> PetscErrorCode ierr;
> PetscScalar *a;
> PetscTruth flg;
> PetscInt n=SIZE;
> int i, j;
>
> PetscInitialize(&argc,&args,(char *)0,help);
>
> ierr = PetscOptionsGetString(PETSC_NULL,"-f",file,PETSC_MAX_PATH_LEN-1,&flg);CHKERRQ(ierr);
> if (!flg) SETERRQ(1,"Must indicate binary file with the -f option");
>
> ierr = PetscMalloc(SIZE*SIZE*sizeof(PetscScalar),&a);CHKERRQ(ierr);
>
> FILE *fpK;
>
> double *K = (double *)calloc( sizeof(double), SIZE*SIZE );
>
> if((fpK = fopen(file, "rb")) == NULL)
> {
> printf("Cannot open joe_DOF4002_k_double.bin\n");
> exit(1);
> }
>
> fprintf(stderr, "Doubles read for K = %d\n", fread( (void *)K, sizeof(double), SIZE*SIZE, fpK ));
>
> for ( i = 0; i < SIZE; i++ )
> for ( j = i; j < SIZE; j++ ) a[i+j*SIZE]= *(K+i*SIZE+j);
>
> MatCreateSeqDense(PETSC_COMM_SELF, n, n, a, &A);
>
> MatView(A,PETSC_VIEWER_BINARY_(PETSC_COMM_WORLD));
>
> ierr = PetscFinalize();CHKERRQ(ierr);
> return 0;
> }
>
>
>
> On Wed, Jul 13, 2011 at 1:02 AM, Barry Smith <bsmith at mcs.anl.gov> wrote:
>
> If it is truly a dense matrix then it isn't clear that you want to store it as a PETSc binary matrix but you can using the following code
>
> PetscInt n;
> PetscScalar *values;
>
> allocate values the correct size; read the entries into values then call MatCreateSeqDense(PETSC_COMM_SELF,n,n,values,&A); the call MatView(A,PETSC_VIEWER_BINARY_(PETSC_COMM_WORLD));
>
> Barry
>
> On Jul 12, 2011, at 11:41 PM, John Chludzinski wrote:
>
> > I have a matrix stored as a sequential list of binary values (not PETSc binary form) that I read in with a single block read:
> >
> > read( (void *)K, sizeof(double), SIZE*SIZE, fpK ).
> >
> > How do I convert this simple binary file of (sequentially stored) doubles into a "PETSc binary matrix file"?
> >
> > ---John
>
>
>
More information about the petsc-users
mailing list