xref: /petsc/src/mat/tests/ex43.c (revision ffa8c5705e8ab2cf85ee1d14dbe507a6e2eb5283)
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   PetscMPIInt    rank,size;
12   PetscViewer    viewer;
13 
14   PetscCall(PetscInitialize(&argc,&args,(char*)0,help));
15   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
16   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
17   PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL));
18   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
19 
20   /* PART 1:  Generate matrix, then write it in binary format */
21 
22   /* Generate matrix */
23   PetscCall(MatCreateSeqDense(PETSC_COMM_WORLD,m,n,NULL,&C));
24   for (i=0; i<m; i++) {
25     for (j=0; j<n; j++) {
26       v    = i*m+j;
27       PetscCall(MatSetValues(C,1,&i,1,&j,&v,INSERT_VALUES));
28     }
29   }
30   PetscCall(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY));
31   PetscCall(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY));
32   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,"matrix.dat",FILE_MODE_WRITE,&viewer));
33   PetscCall(MatView(C,viewer));
34   PetscCall(PetscViewerDestroy(&viewer));
35   PetscCall(MatDestroy(&C));
36   PetscCall(PetscFinalize());
37   return 0;
38 }
39 
40 /*TEST
41 
42    test:
43 
44 TEST*/
45