xref: /petsc/src/mat/tests/ex208.c (revision 503c0ea9b45bcfbcebbb1ea5341243bbc69f0bea)
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   PetscCall(PetscInitialize(&argc,&args,(char*)0,help));
12   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
13   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
14 
15   nsubcomm = size;
16   PetscCall(PetscOptionsGetInt(NULL,NULL,"-nsubcomm",&nsubcomm,NULL));
17 
18   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
19   PetscCall(MatSetSizes(A, m, n, PETSC_DETERMINE, PETSC_DETERMINE));
20   PetscCall(MatSetType(A, MATAIJ));
21   PetscCall(MatSetFromOptions(A));
22   PetscCall(MatSetUp(A));
23 
24   if (rank == 0) {
25     for (i=0;i<size*PetscMin(m,n);i++) {
26       PetscCall(MatSetValue(A, i, i, 1.0, INSERT_VALUES));
27     }
28   }
29   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
30   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
31   PetscCall(MatView(A,PETSC_VIEWER_STDOUT_WORLD));
32 
33   PetscCall(MatCreateRedundantMatrix(A, nsubcomm, MPI_COMM_NULL, MAT_INITIAL_MATRIX, &B));
34   if (nsubcomm==size) { /* B is a sequential matrix */
35     if (rank == 0) {
36       PetscCall(MatView(B,PETSC_VIEWER_STDOUT_SELF));
37     }
38   } else {
39     MPI_Comm comm;
40     PetscCall(PetscObjectGetComm((PetscObject)B,&comm));
41     PetscCall(MatView(B,PETSC_VIEWER_STDOUT_(comm)));
42   }
43 
44   PetscCall(MatDestroy(&A));
45   PetscCall(MatDestroy(&B));
46   PetscCall(PetscFinalize());
47   return 0;
48 }
49 
50 /*TEST
51 
52    test:
53 
54    test:
55       suffix: 2
56       nsize: 3
57 
58    test:
59       suffix: baij
60       args: -mat_type baij
61 
62    test:
63       suffix: baij_2
64       nsize: 3
65       args: -mat_type baij
66 
67    test:
68       suffix: dense
69       args: -mat_type dense
70 
71    test:
72       suffix: dense_2
73       nsize: 3
74       args: -mat_type dense
75 
76 TEST*/
77