Actual source code: ex43.c
petsc-3.10.5 2019-03-28
2: static char help[] = "Saves a dense matrix in a dense format (binary).\n\n";
4: #include <petscmat.h>
6: int main(int argc,char **args)
7: {
8: Mat C;
9: PetscScalar v;
10: PetscInt i,j,m = 4,n = 4;
12: PetscMPIInt rank,size;
13: PetscViewer viewer;
15: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
16: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
17: MPI_Comm_size(PETSC_COMM_WORLD,&size);
18: PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
19: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
21: /* PART 1: Generate matrix, then write it in binary format */
23: /* Generate matrix */
24: MatCreateSeqDense(PETSC_COMM_WORLD,m,n,NULL,&C);
25: for (i=0; i<m; i++) {
26: for (j=0; j<n; j++) {
27: v = i*m+j;
28: MatSetValues(C,1,&i,1,&j,&v,INSERT_VALUES);
29: }
30: }
31: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
32: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
33: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"matrix.dat",FILE_MODE_WRITE,&viewer);
34: MatView(C,viewer);
35: PetscViewerDestroy(&viewer);
36: MatDestroy(&C);
37: PetscFinalize();
38: return ierr;
39: }
41: /*TEST
43: test:
45: TEST*/