xref: /petsc/src/mat/tests/ex43.c (revision 609caa7c8c030312b00807b4f015fd827bb80932)
1 static char help[] = "Saves a dense matrix in a dense format (binary).\n\n";
2 
3 #include <petscmat.h>
4 
main(int argc,char ** args)5 int main(int argc, char **args)
6 {
7   Mat         C;
8   PetscScalar v;
9   PetscInt    i, j, m = 4, n = 4;
10   PetscMPIInt rank, size;
11   PetscViewer viewer;
12 
13   PetscFunctionBeginUser;
14   PetscCall(PetscInitialize(&argc, &args, NULL, 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      output_file: output/empty.out
44 
45 TEST*/
46