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).\n\n";

 5:  #include petscmat.h

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

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

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

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