[petsc-users] Storing a matrix in PETSc binary form?

John Chludzinski jchludzinski at gmail.com
Wed Jul 13 02:24:32 CDT 2011


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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20110713/2d81c22f/attachment.htm>


More information about the petsc-users mailing list