xref: /petsc/src/mat/tests/ex104.c (revision 58d68138c660dfb4e9f5b03334792cd4f2ffd7cc)
1 static char help[] = "Test MatMatMult(), MatTranspose(), MatTransposeMatMult() for Dense and Elemental matrices.\n\n";
2 /*
3  Example:
4    mpiexec -n <np> ./ex104 -mat_type elemental
5 */
6 
7 #include <petscmat.h>
8 
9 int main(int argc, char **argv) {
10   Mat             A, B, C, D;
11   PetscInt        i, M = 10, N = 5, j, nrows, ncols, am, an, rstart, rend;
12   PetscRandom     r;
13   PetscBool       equal, Aiselemental;
14   PetscReal       fill = 1.0;
15   IS              isrows, iscols;
16   const PetscInt *rows, *cols;
17   PetscScalar    *v, rval;
18 #if defined(PETSC_HAVE_ELEMENTAL)
19   PetscBool Test_MatMatMult = PETSC_TRUE;
20 #else
21   PetscBool Test_MatMatMult = PETSC_FALSE;
22 #endif
23   PetscMPIInt size;
24 
25   PetscFunctionBeginUser;
26   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
27   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
28 
29   PetscCall(PetscOptionsGetInt(NULL, NULL, "-M", &M, NULL));
30   PetscCall(PetscOptionsGetInt(NULL, NULL, "-N", &N, NULL));
31   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
32   PetscCall(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, M, N));
33   PetscCall(MatSetType(A, MATDENSE));
34   PetscCall(MatSetFromOptions(A));
35   PetscCall(MatSetUp(A));
36   PetscCall(PetscRandomCreate(PETSC_COMM_WORLD, &r));
37   PetscCall(PetscRandomSetFromOptions(r));
38 
39   /* Set local matrix entries */
40   PetscCall(MatGetOwnershipIS(A, &isrows, &iscols));
41   PetscCall(ISGetLocalSize(isrows, &nrows));
42   PetscCall(ISGetIndices(isrows, &rows));
43   PetscCall(ISGetLocalSize(iscols, &ncols));
44   PetscCall(ISGetIndices(iscols, &cols));
45   PetscCall(PetscMalloc1(nrows * ncols, &v));
46   for (i = 0; i < nrows; i++) {
47     for (j = 0; j < ncols; j++) {
48       PetscCall(PetscRandomGetValue(r, &rval));
49       v[i * ncols + j] = rval;
50     }
51   }
52   PetscCall(MatSetValues(A, nrows, rows, ncols, cols, v, INSERT_VALUES));
53   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
54   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
55   PetscCall(ISRestoreIndices(isrows, &rows));
56   PetscCall(ISRestoreIndices(iscols, &cols));
57   PetscCall(ISDestroy(&isrows));
58   PetscCall(ISDestroy(&iscols));
59   PetscCall(PetscRandomDestroy(&r));
60 
61   PetscCall(PetscObjectTypeCompare((PetscObject)A, MATELEMENTAL, &Aiselemental));
62 
63   /* Test MatCreateTranspose() and MatTranspose() */
64   PetscCall(MatCreateTranspose(A, &C));
65   PetscCall(MatTranspose(A, MAT_INITIAL_MATRIX, &B)); /* B = A^T */
66   PetscCall(MatMultEqual(C, B, 10, &equal));
67   PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_PLIB, "A^T*x != (x^T*A)^T");
68   PetscCall(MatDestroy(&B));
69 
70   PetscCall(MatDuplicate(A, MAT_COPY_VALUES, &B));
71   if (!Aiselemental) {
72     PetscCall(MatTranspose(B, MAT_INPLACE_MATRIX, &B));
73     PetscCall(MatMultEqual(C, B, 10, &equal));
74     PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_PLIB, "C*x != B*x");
75   }
76   PetscCall(MatDestroy(&B));
77 
78   /* Test B = C*A for matrix type transpose and seqdense */
79   if (size == 1 && !Aiselemental) {
80     PetscCall(MatMatMult(C, A, MAT_INITIAL_MATRIX, fill, &B));
81     PetscCall(MatMatMultEqual(C, A, B, 10, &equal));
82     PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_PLIB, "B != C*A for matrix type transpose and seqdense");
83     PetscCall(MatDestroy(&B));
84   }
85   PetscCall(MatDestroy(&C));
86 
87   /* Test MatMatMult() */
88   if (Test_MatMatMult) {
89 #if !defined(PETSC_HAVE_ELEMENTAL)
90     PetscCheck(size == 1, PETSC_COMM_SELF, PETSC_ERR_SUP, "This test requires ELEMENTAL");
91 #endif
92     PetscCall(MatTranspose(A, MAT_INITIAL_MATRIX, &B));        /* B = A^T */
93     PetscCall(MatMatMult(B, A, MAT_INITIAL_MATRIX, fill, &C)); /* C = B*A = A^T*A */
94     PetscCall(MatMatMult(B, A, MAT_REUSE_MATRIX, fill, &C));
95     PetscCall(MatMatMultEqual(B, A, C, 10, &equal));
96     PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_PLIB, "B*A*x != C*x");
97 
98     /* Test MatDuplicate for matrix product */
99     PetscCall(MatDuplicate(C, MAT_COPY_VALUES, &D));
100 
101     PetscCall(MatDestroy(&D));
102     PetscCall(MatDestroy(&C));
103     PetscCall(MatDestroy(&B));
104   }
105 
106   /* Test MatTransposeMatMult() */
107   if (!Aiselemental) {
108     PetscCall(MatTransposeMatMult(A, A, MAT_INITIAL_MATRIX, fill, &D)); /* D = A^T*A */
109     PetscCall(MatTransposeMatMult(A, A, MAT_REUSE_MATRIX, fill, &D));
110     PetscCall(MatTransposeMatMultEqual(A, A, D, 10, &equal));
111     PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_PLIB, "D*x != A^T*A*x");
112 
113     /* Test MatDuplicate for matrix product */
114     PetscCall(MatDuplicate(D, MAT_COPY_VALUES, &C));
115     PetscCall(MatDestroy(&C));
116     PetscCall(MatDestroy(&D));
117 
118     /* Test D*x = A^T*C*A*x, where C is in AIJ format */
119     PetscCall(MatGetLocalSize(A, &am, &an));
120     PetscCall(MatCreate(PETSC_COMM_WORLD, &C));
121     if (size == 1) {
122       PetscCall(MatSetSizes(C, PETSC_DECIDE, PETSC_DECIDE, am, am));
123     } else {
124       PetscCall(MatSetSizes(C, am, am, PETSC_DECIDE, PETSC_DECIDE));
125     }
126     PetscCall(MatSetFromOptions(C));
127     PetscCall(MatSetUp(C));
128     PetscCall(MatGetOwnershipRange(C, &rstart, &rend));
129     v[0] = 1.0;
130     for (i = rstart; i < rend; i++) { PetscCall(MatSetValues(C, 1, &i, 1, &i, v, INSERT_VALUES)); }
131     PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
132     PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
133 
134     /* B = C*A, D = A^T*B */
135     PetscCall(MatMatMult(C, A, MAT_INITIAL_MATRIX, 1.0, &B));
136     PetscCall(MatTransposeMatMult(A, B, MAT_INITIAL_MATRIX, fill, &D));
137     PetscCall(MatTransposeMatMultEqual(A, B, D, 10, &equal));
138     PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_PLIB, "D*x != A^T*B*x");
139 
140     PetscCall(MatDestroy(&D));
141     PetscCall(MatDestroy(&C));
142     PetscCall(MatDestroy(&B));
143   }
144 
145   /* Test MatMatTransposeMult() */
146   if (!Aiselemental) {
147     PetscReal diff, scale;
148     PetscInt  am, an, aM, aN;
149 
150     PetscCall(MatGetLocalSize(A, &am, &an));
151     PetscCall(MatGetSize(A, &aM, &aN));
152     PetscCall(MatCreateDense(PetscObjectComm((PetscObject)A), PETSC_DECIDE, an, aM + 10, aN, NULL, &B));
153     PetscCall(MatSetRandom(B, NULL));
154     PetscCall(MatMatTransposeMult(A, B, MAT_INITIAL_MATRIX, fill, &D)); /* D = A*A^T */
155 
156     /* Test MatDuplicate for matrix product */
157     PetscCall(MatDuplicate(D, MAT_COPY_VALUES, &C));
158 
159     PetscCall(MatMatTransposeMult(A, B, MAT_REUSE_MATRIX, fill, &D));
160     PetscCall(MatAXPY(C, -1., D, SAME_NONZERO_PATTERN));
161 
162     PetscCall(MatNorm(C, NORM_FROBENIUS, &diff));
163     PetscCall(MatNorm(D, NORM_FROBENIUS, &scale));
164     PetscCheck(diff <= PETSC_SMALL * scale, PetscObjectComm((PetscObject)D), PETSC_ERR_PLIB, "MatMatTransposeMult() differs between MAT_INITIAL_MATRIX and MAT_REUSE_MATRIX");
165     PetscCall(MatDestroy(&C));
166 
167     PetscCall(MatMatTransposeMultEqual(A, B, D, 10, &equal));
168     PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_PLIB, "D*x != A^T*A*x");
169     PetscCall(MatDestroy(&D));
170     PetscCall(MatDestroy(&B));
171   }
172 
173   PetscCall(MatDestroy(&A));
174   PetscCall(PetscFree(v));
175   PetscCall(PetscFinalize());
176   return 0;
177 }
178 
179 /*TEST
180 
181     test:
182       output_file: output/ex104.out
183 
184     test:
185       suffix: 2
186       nsize: 2
187       output_file: output/ex104.out
188 
189     test:
190       suffix: 3
191       nsize: 4
192       output_file: output/ex104.out
193       args: -M 23 -N 31
194 
195     test:
196       suffix: 4
197       nsize: 4
198       output_file: output/ex104.out
199       args: -M 23 -N 31 -matmattransmult_mpidense_mpidense_via cyclic
200 
201     test:
202       suffix: 5
203       nsize: 4
204       output_file: output/ex104.out
205       args: -M 23 -N 31 -matmattransmult_mpidense_mpidense_via allgatherv
206 
207     test:
208       suffix: 6
209       args: -mat_type elemental
210       requires: elemental
211       output_file: output/ex104.out
212 
213     test:
214       suffix: 7
215       nsize: 2
216       args: -mat_type elemental
217       requires: elemental
218       output_file: output/ex104.out
219 
220 TEST*/
221