Actual source code: ex31.c

petsc-3.5.4 2015-05-23
Report Typos and Errors
  2: static char help[] = "Tests binary I/O of matrices and illustrates user-defined event logging.\n\n";

  4: #include <petscmat.h>

  6: /* Note:  Most applications would not read and write the same matrix within
  7:   the same program.  This example is intended only to demonstrate
  8:   both input and output. */

 12: int main(int argc,char **args)
 13: {
 14:   Mat            C;
 15:   PetscScalar    v;
 16:   PetscInt       i,j,Ii,J,Istart,Iend,N,m = 4,n = 4;
 17:   PetscMPIInt    rank,size;
 19:   PetscViewer    viewer;
 20: #if defined(PETSC_USE_LOG)
 21:   PetscLogEvent MATRIX_GENERATE,MATRIX_READ;
 22: #endif

 24:   PetscInitialize(&argc,&args,(char*)0,help);
 25:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 26:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 27:   PetscOptionsGetInt(NULL,"-m",&m,NULL);
 28:   PetscOptionsGetInt(NULL,"-n",&n,NULL);
 29:   N    = m*n;

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

 33:   PetscLogEventRegister("Generate Matrix",0,&MATRIX_GENERATE);
 34:   PetscLogEventBegin(MATRIX_GENERATE,0,0,0,0);

 36:   /* Generate matrix */
 37:   MatCreate(PETSC_COMM_WORLD,&C);
 38:   MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,N,N);
 39:   MatSetFromOptions(C);
 40:   MatSetUp(C);
 41:   MatGetOwnershipRange(C,&Istart,&Iend);
 42:   for (Ii=Istart; Ii<Iend; Ii++) {
 43:     v = -1.0; i = Ii/n; j = Ii - i*n;
 44:     if (i>0)   {J = Ii - n; MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES);}
 45:     if (i<m-1) {J = Ii + n; MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES);}
 46:     if (j>0)   {J = Ii - 1; MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES);}
 47:     if (j<n-1) {J = Ii + 1; MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES);}
 48:     v = 4.0; MatSetValues(C,1,&Ii,1,&Ii,&v,ADD_VALUES);
 49:   }
 50:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 51:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
 52:   MatView(C,PETSC_VIEWER_STDOUT_WORLD);

 54:   PetscPrintf(PETSC_COMM_WORLD,"writing matrix in binary to matrix.dat ...\n");
 55:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,"matrix.dat",FILE_MODE_WRITE,&viewer);
 56:   MatView(C,viewer);
 57:   PetscViewerDestroy(&viewer);
 58:   MatDestroy(&C);
 59:   PetscLogEventEnd(MATRIX_GENERATE,0,0,0,0);

 61:   /* PART 2:  Read in matrix in binary format */

 63:   /* All processors wait until test matrix has been dumped */
 64:   MPI_Barrier(PETSC_COMM_WORLD);

 66:   PetscLogEventRegister("Read Matrix",0,&MATRIX_READ);
 67:   PetscLogEventBegin(MATRIX_READ,0,0,0,0);
 68:   PetscPrintf(PETSC_COMM_WORLD,"reading matrix in binary from matrix.dat ...\n");
 69:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,"matrix.dat",FILE_MODE_READ,&viewer);
 70:   MatCreate(PETSC_COMM_WORLD,&C);
 71:   MatLoad(C,viewer);
 72:   PetscViewerDestroy(&viewer);
 73:   PetscLogEventEnd(MATRIX_READ,0,0,0,0);
 74:   MatView(C,PETSC_VIEWER_STDOUT_WORLD);

 76:   /* Free data structures */
 77:   MatDestroy(&C);

 79:   PetscFinalize();
 80:   return 0;
 81: }