xref: /petsc/src/mat/tests/ex43.c (revision 40badf4fbc550ac1f60bd080eaff6de6d55b946d)
1 
2 static char help[] = "Saves a dense matrix in a dense format (binary).\n\n";
3 
4 #include <petscmat.h>
5 
6 int main(int argc,char **args)
7 {
8   Mat            C;
9   PetscScalar    v;
10   PetscInt       i,j,m = 4,n = 4;
11   PetscErrorCode ierr;
12   PetscMPIInt    rank,size;
13   PetscViewer    viewer;
14 
15   ierr = PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
16   CHKERRMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
17   CHKERRMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
18   CHKERRQ(PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL));
19   CHKERRQ(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
20 
21   /* PART 1:  Generate matrix, then write it in binary format */
22 
23   /* Generate matrix */
24   CHKERRQ(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       CHKERRQ(MatSetValues(C,1,&i,1,&j,&v,INSERT_VALUES));
29     }
30   }
31   CHKERRQ(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY));
32   CHKERRQ(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY));
33   CHKERRQ(PetscViewerBinaryOpen(PETSC_COMM_WORLD,"matrix.dat",FILE_MODE_WRITE,&viewer));
34   CHKERRQ(MatView(C,viewer));
35   CHKERRQ(PetscViewerDestroy(&viewer));
36   CHKERRQ(MatDestroy(&C));
37   ierr = PetscFinalize();
38   return ierr;
39 }
40 
41 /*TEST
42 
43    test:
44 
45 TEST*/
46