xref: /petsc/src/mat/tests/ex208.c (revision 1404853cd07923df46691f50dfd5de8bf2e82a6a)
1 static char help[] = "Test MatCreateRedundantMatrix for rectangular matrix.\n\
2                       Contributed by Jose E. Roman, July 2017\n\n";
3 
4 #include <petscmat.h>
5 int main(int argc, char **args)
6 {
7   Mat         A, B;
8   PetscInt    m = 3, n = 4, i, nsubcomm;
9   PetscMPIInt size, rank;
10 
11   PetscFunctionBeginUser;
12   PetscCall(PetscInitialize(&argc, &args, NULL, help));
13   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
14   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
15 
16   nsubcomm = size;
17   PetscCall(PetscOptionsGetInt(NULL, NULL, "-nsubcomm", &nsubcomm, NULL));
18 
19   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
20   PetscCall(MatSetSizes(A, m, n, PETSC_DETERMINE, PETSC_DETERMINE));
21   PetscCall(MatSetType(A, MATAIJ));
22   PetscCall(MatSetFromOptions(A));
23   PetscCall(MatSetUp(A));
24 
25   if (rank == 0) {
26     for (i = 0; i < size * PetscMin(m, n); i++) PetscCall(MatSetValue(A, i, i, 1.0, INSERT_VALUES));
27   }
28   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
29   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
30   PetscCall(MatView(A, PETSC_VIEWER_STDOUT_WORLD));
31 
32   PetscCall(MatCreateRedundantMatrix(A, nsubcomm, MPI_COMM_NULL, MAT_INITIAL_MATRIX, &B));
33   if (nsubcomm == size) { /* B is a sequential matrix */
34     if (rank == 0) PetscCall(MatView(B, PETSC_VIEWER_STDOUT_SELF));
35   } else {
36     MPI_Comm comm;
37     PetscCall(PetscObjectGetComm((PetscObject)B, &comm));
38     PetscCall(MatView(B, PETSC_VIEWER_STDOUT_(comm)));
39   }
40 
41   PetscCall(MatDestroy(&A));
42   PetscCall(MatDestroy(&B));
43   PetscCall(PetscFinalize());
44   return 0;
45 }
46 
47 /*TEST
48 
49    test:
50 
51    test:
52       suffix: 2
53       nsize: 3
54 
55    test:
56       suffix: baij
57       args: -mat_type baij
58 
59    test:
60       suffix: baij_2
61       nsize: 3
62       args: -mat_type baij
63 
64    test:
65       suffix: dense
66       args: -mat_type dense
67 
68    test:
69       suffix: dense_2
70       nsize: 3
71       args: -mat_type dense
72 
73 TEST*/
74