Actual source code: ex43.c

  1: /*$Id: ex43.c,v 1.19 2001/08/07 03:03:07 balay Exp $*/

  3: static char help[] = "Saves a dense matrix in a dense format (binary).nn";

 5:  #include petscmat.h

  7: int main(int argc,char **args)
  8: {
  9:   Mat         C;
 10:   PetscScalar v;
 11:   int         i,j,ierr,m = 4,n = 4,rank,size;
 12:   PetscViewer viewer;

 14:   PetscInitialize(&argc,&args,(char *)0,help);
 15:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 16:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 17:   PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);
 18:   PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);

 20:   /* PART 1:  Generate matrix, then write it in binary format */

 22:   /* Generate matrix */
 23:   MatCreateSeqDense(PETSC_COMM_WORLD,m,n,PETSC_NULL,&C);
 24:   for (i=0; i<m; i++) {
 25:     for (j=0; j<n; j++) {
 26:       v = i*m+j;
 27:       MatSetValues(C,1,&i,1,&j,&v,INSERT_VALUES);
 28:     }
 29:   }
 30:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 31:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
 32:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,"matrix.dat",PETSC_BINARY_CREATE,&viewer);
 33:   PetscViewerSetFormat(viewer,PETSC_VIEWER_BINARY_NATIVE);
 34:   MatView(C,viewer);
 35:   PetscViewerDestroy(viewer);
 36:   MatDestroy(C);
 37:   PetscFinalize();
 38:   return 0;
 39: }