Actual source code: ex31.c

petsc-3.14.6 2021-03-30
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. */

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

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

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

 31:   PetscLogEventRegister("Generate Matrix",0,&MATRIX_GENERATE);
 32:   PetscLogEventBegin(MATRIX_GENERATE,0,0,0,0);

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

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

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

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

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

 74:   /* Free data structures */
 75:   MatDestroy(&C);

 77:   PetscFinalize();
 78:   return ierr;
 79: }




 84: /*TEST

 86:    test:
 87:       filter: grep -v "MPI processes"

 89: TEST*/