static char help[] = "Tests binary I/O of matrices and illustrates user-defined event logging.\n\n"; #include /* Note: Most applications would not read and write the same matrix within the same program. This example is intended only to demonstrate both input and output. */ int main(int argc,char **args) { Mat C; PetscScalar v; PetscInt i,j,Ii,J,Istart,Iend,N,m = 4,n = 4; PetscMPIInt rank,size; PetscViewer viewer; #if defined(PETSC_USE_LOG) PetscLogEvent MATRIX_GENERATE,MATRIX_READ; #endif PetscCall(PetscInitialize(&argc,&args,(char*)0,help)); PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank)); PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size)); PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL)); PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL)); N = m*n; /* PART 1: Generate matrix, then write it in binary format */ PetscCall(PetscLogEventRegister("Generate Matrix",0,&MATRIX_GENERATE)); PetscCall(PetscLogEventBegin(MATRIX_GENERATE,0,0,0,0)); /* Generate matrix */ PetscCall(MatCreate(PETSC_COMM_WORLD,&C)); PetscCall(MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,N,N)); PetscCall(MatSetFromOptions(C)); PetscCall(MatSetUp(C)); PetscCall(MatGetOwnershipRange(C,&Istart,&Iend)); for (Ii=Istart; Ii0) {J = Ii - n; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES));} if (i0) {J = Ii - 1; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES));} if (j