xref: /petsc/src/mat/tests/ex208.c (revision ebead697dbf761eb322f829370bbe90b3bd93fa3)
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,(char*)0,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++) {
27       PetscCall(MatSetValue(A, i, i, 1.0, INSERT_VALUES));
28     }
29   }
30   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
31   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
32   PetscCall(MatView(A,PETSC_VIEWER_STDOUT_WORLD));
33 
34   PetscCall(MatCreateRedundantMatrix(A, nsubcomm, MPI_COMM_NULL, MAT_INITIAL_MATRIX, &B));
35   if (nsubcomm==size) { /* B is a sequential matrix */
36     if (rank == 0) {
37       PetscCall(MatView(B,PETSC_VIEWER_STDOUT_SELF));
38     }
39   } else {
40     MPI_Comm comm;
41     PetscCall(PetscObjectGetComm((PetscObject)B,&comm));
42     PetscCall(MatView(B,PETSC_VIEWER_STDOUT_(comm)));
43   }
44 
45   PetscCall(MatDestroy(&A));
46   PetscCall(MatDestroy(&B));
47   PetscCall(PetscFinalize());
48   return 0;
49 }
50 
51 /*TEST
52 
53    test:
54 
55    test:
56       suffix: 2
57       nsize: 3
58 
59    test:
60       suffix: baij
61       args: -mat_type baij
62 
63    test:
64       suffix: baij_2
65       nsize: 3
66       args: -mat_type baij
67 
68    test:
69       suffix: dense
70       args: -mat_type dense
71 
72    test:
73       suffix: dense_2
74       nsize: 3
75       args: -mat_type dense
76 
77 TEST*/
78