Actual source code: ex43.c

petsc-3.7.3 2016-08-01
Report Typos and Errors
  2: static char help[] = "Saves a dense matrix in a dense format (binary).\n\n";

  4: #include <petscmat.h>

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

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

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

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