xref: /petsc/src/mat/impls/transpose/htransm.c (revision 543844c42a4a8266d7d662531c9f7ef45131b226)
1 #include <petsc/private/matimpl.h> /*I "petscmat.h" I*/
2 
3 PETSC_INTERN PetscErrorCode MatProductSetFromOptions_HT(Mat D)
4 {
5   Mat            A, B, C, Ain, Bin, Cin;
6   PetscBool      Aistrans, Bistrans, Cistrans;
7   PetscInt       Atrans, Btrans, Ctrans;
8   MatProductType ptype;
9 
10   PetscFunctionBegin;
11   MatCheckProduct(D, 1);
12   A = D->product->A;
13   B = D->product->B;
14   C = D->product->C;
15   PetscCall(PetscObjectTypeCompare((PetscObject)A, MATHERMITIANTRANSPOSEVIRTUAL, &Aistrans));
16   PetscCall(PetscObjectTypeCompare((PetscObject)B, MATHERMITIANTRANSPOSEVIRTUAL, &Bistrans));
17   PetscCall(PetscObjectTypeCompare((PetscObject)C, MATHERMITIANTRANSPOSEVIRTUAL, &Cistrans));
18   PetscCheck(Aistrans || Bistrans || Cistrans, PetscObjectComm((PetscObject)D), PETSC_ERR_PLIB, "This should not happen");
19   Atrans = 0;
20   Ain    = A;
21   while (Aistrans) {
22     Atrans++;
23     PetscCall(MatHermitianTransposeGetMat(Ain, &Ain));
24     PetscCall(PetscObjectTypeCompare((PetscObject)Ain, MATHERMITIANTRANSPOSEVIRTUAL, &Aistrans));
25   }
26   Btrans = 0;
27   Bin    = B;
28   while (Bistrans) {
29     Btrans++;
30     PetscCall(MatHermitianTransposeGetMat(Bin, &Bin));
31     PetscCall(PetscObjectTypeCompare((PetscObject)Bin, MATHERMITIANTRANSPOSEVIRTUAL, &Bistrans));
32   }
33   Ctrans = 0;
34   Cin    = C;
35   while (Cistrans) {
36     Ctrans++;
37     PetscCall(MatHermitianTransposeGetMat(Cin, &Cin));
38     PetscCall(PetscObjectTypeCompare((PetscObject)Cin, MATHERMITIANTRANSPOSEVIRTUAL, &Cistrans));
39   }
40   Atrans = Atrans % 2;
41   Btrans = Btrans % 2;
42   Ctrans = Ctrans % 2;
43   ptype  = D->product->type; /* same product type by default */
44   if (Ain->symmetric == PETSC_BOOL3_TRUE) Atrans = 0;
45   if (Bin->symmetric == PETSC_BOOL3_TRUE) Btrans = 0;
46   if (Cin && Cin->symmetric == PETSC_BOOL3_TRUE) Ctrans = 0;
47 
48   if (Atrans || Btrans || Ctrans) {
49     PetscCheck(!PetscDefined(USE_COMPLEX), PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "No support for complex Hermitian transpose matrices");
50     ptype = MATPRODUCT_UNSPECIFIED;
51     switch (D->product->type) {
52     case MATPRODUCT_AB:
53       if (Atrans && Btrans) { /* At * Bt we do not have support for this */
54         /* TODO custom implementation ? */
55       } else if (Atrans) { /* At * B */
56         ptype = MATPRODUCT_AtB;
57       } else { /* A * Bt */
58         ptype = MATPRODUCT_ABt;
59       }
60       break;
61     case MATPRODUCT_AtB:
62       if (Atrans && Btrans) { /* A * Bt */
63         ptype = MATPRODUCT_ABt;
64       } else if (Atrans) { /* A * B */
65         ptype = MATPRODUCT_AB;
66       } else { /* At * Bt we do not have support for this */
67         /* TODO custom implementation ? */
68       }
69       break;
70     case MATPRODUCT_ABt:
71       if (Atrans && Btrans) { /* At * B */
72         ptype = MATPRODUCT_AtB;
73       } else if (Atrans) { /* At * Bt we do not have support for this */
74         /* TODO custom implementation ? */
75       } else { /* A * B */
76         ptype = MATPRODUCT_AB;
77       }
78       break;
79     case MATPRODUCT_PtAP:
80       if (Atrans) { /* PtAtP */
81         /* TODO custom implementation ? */
82       } else { /* RARt */
83         ptype = MATPRODUCT_RARt;
84       }
85       break;
86     case MATPRODUCT_RARt:
87       if (Atrans) { /* RAtRt */
88         /* TODO custom implementation ? */
89       } else { /* PtAP */
90         ptype = MATPRODUCT_PtAP;
91       }
92       break;
93     case MATPRODUCT_ABC:
94       /* TODO custom implementation ? */
95       break;
96     default:
97       SETERRQ(PetscObjectComm((PetscObject)D), PETSC_ERR_SUP, "ProductType %s is not supported", MatProductTypes[D->product->type]);
98     }
99   }
100   PetscCall(MatProductReplaceMats(Ain, Bin, Cin, D));
101   PetscCall(MatProductSetType(D, ptype));
102   PetscCall(MatProductSetFromOptions(D));
103   PetscFunctionReturn(PETSC_SUCCESS);
104 }
105 static PetscErrorCode MatMult_HT(Mat N, Vec x, Vec y)
106 {
107   Mat A;
108 
109   PetscFunctionBegin;
110   PetscCall(MatShellGetContext(N, &A));
111   PetscCall(MatMultHermitianTranspose(A, x, y));
112   PetscFunctionReturn(PETSC_SUCCESS);
113 }
114 
115 static PetscErrorCode MatMultHermitianTranspose_HT(Mat N, Vec x, Vec y)
116 {
117   Mat A;
118 
119   PetscFunctionBegin;
120   PetscCall(MatShellGetContext(N, &A));
121   PetscCall(MatMult(A, x, y));
122   PetscFunctionReturn(PETSC_SUCCESS);
123 }
124 static PetscErrorCode MatDestroy_HT(Mat N)
125 {
126   Mat A;
127 
128   PetscFunctionBegin;
129   PetscCall(MatShellGetContext(N, &A));
130   PetscCall(MatDestroy(&A));
131   PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatHermitianTransposeGetMat_C", NULL));
132 #if !defined(PETSC_USE_COMPLEX)
133   PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatTransposeGetMat_C", NULL));
134 #endif
135   PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatProductSetFromOptions_anytype_C", NULL));
136   PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatShellSetContext_C", NULL));
137   PetscFunctionReturn(PETSC_SUCCESS);
138 }
139 
140 static PetscErrorCode MatDuplicate_HT(Mat N, MatDuplicateOption op, Mat *m)
141 {
142   Mat A;
143 
144   PetscFunctionBegin;
145   PetscCall(MatShellGetContext(N, &A));
146   if (op == MAT_COPY_VALUES) {
147     PetscCall(MatHermitianTranspose(A, MAT_INITIAL_MATRIX, m));
148   } else if (op == MAT_DO_NOT_COPY_VALUES) {
149     PetscCall(MatDuplicate(A, MAT_DO_NOT_COPY_VALUES, m));
150     PetscCall(MatHermitianTranspose(*m, MAT_INPLACE_MATRIX, m));
151   } else SETERRQ(PetscObjectComm((PetscObject)N), PETSC_ERR_SUP, "MAT_SHARE_NONZERO_PATTERN not supported for this matrix type");
152   PetscFunctionReturn(PETSC_SUCCESS);
153 }
154 
155 static PetscErrorCode MatCreateVecs_HT(Mat N, Vec *r, Vec *l)
156 {
157   Mat A;
158 
159   PetscFunctionBegin;
160   PetscCall(MatShellGetContext(N, &A));
161   PetscCall(MatCreateVecs(A, l, r));
162   PetscFunctionReturn(PETSC_SUCCESS);
163 }
164 
165 static PetscErrorCode MatHasOperation_HT(Mat mat, MatOperation op, PetscBool *has)
166 {
167   Mat A;
168 
169   PetscFunctionBegin;
170   PetscCall(MatShellGetContext(mat, &A));
171   *has = PETSC_FALSE;
172   if (op == MATOP_MULT || op == MATOP_MULT_ADD) {
173     PetscCall(MatHasOperation(A, MATOP_MULT_HERMITIAN_TRANSPOSE, has));
174     if (!*has) PetscCall(MatHasOperation(A, MATOP_MULT_TRANSPOSE, has));
175   } else if (op == MATOP_MULT_HERMITIAN_TRANSPOSE || op == MATOP_MULT_HERMITIAN_TRANS_ADD || op == MATOP_MULT_TRANSPOSE || op == MATOP_MULT_TRANSPOSE_ADD) {
176     PetscCall(MatHasOperation(A, MATOP_MULT, has));
177   } else if (((void **)mat->ops)[op]) *has = PETSC_TRUE;
178   PetscFunctionReturn(PETSC_SUCCESS);
179 }
180 
181 static PetscErrorCode MatHermitianTransposeGetMat_HT(Mat N, Mat *M)
182 {
183   PetscFunctionBegin;
184   PetscCall(MatShellGetContext(N, M));
185   PetscFunctionReturn(PETSC_SUCCESS);
186 }
187 
188 /*@
189   MatHermitianTransposeGetMat - Gets the `Mat` object stored inside a `MATHERMITIANTRANSPOSEVIRTUAL`
190 
191   Logically Collective
192 
193   Input Parameter:
194 . A - the `MATHERMITIANTRANSPOSEVIRTUAL` matrix
195 
196   Output Parameter:
197 . M - the matrix object stored inside A
198 
199   Level: intermediate
200 
201 .seealso: [](ch_matrices), `Mat`, `MATHERMITIANTRANSPOSEVIRTUAL`, `MatCreateHermitianTranspose()`
202 @*/
203 PetscErrorCode MatHermitianTransposeGetMat(Mat A, Mat *M)
204 {
205   PetscFunctionBegin;
206   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
207   PetscValidType(A, 1);
208   PetscAssertPointer(M, 2);
209   PetscUseMethod(A, "MatHermitianTransposeGetMat_C", (Mat, Mat *), (A, M));
210   PetscFunctionReturn(PETSC_SUCCESS);
211 }
212 
213 static PetscErrorCode MatGetDiagonal_HT(Mat N, Vec v)
214 {
215   Mat A;
216 
217   PetscFunctionBegin;
218   PetscCall(MatShellGetContext(N, &A));
219   PetscCall(MatGetDiagonal(A, v));
220   PetscCall(VecConjugate(v));
221   PetscFunctionReturn(PETSC_SUCCESS);
222 }
223 
224 static PetscErrorCode MatConvert_HT(Mat N, MatType newtype, MatReuse reuse, Mat *newmat)
225 {
226   Mat       A;
227   PetscBool flg;
228 
229   PetscFunctionBegin;
230   PetscCall(MatShellGetContext(N, &A));
231   PetscCall(MatHasOperation(A, MATOP_HERMITIAN_TRANSPOSE, &flg));
232   if (flg) {
233     Mat B;
234 
235     PetscCall(MatHermitianTranspose(A, MAT_INITIAL_MATRIX, &B));
236     if (reuse != MAT_INPLACE_MATRIX) {
237       PetscCall(MatConvert(B, newtype, reuse, newmat));
238       PetscCall(MatDestroy(&B));
239     } else {
240       PetscCall(MatConvert(B, newtype, MAT_INPLACE_MATRIX, &B));
241       PetscCall(MatHeaderReplace(N, &B));
242     }
243   } else { /* use basic converter as fallback */
244     PetscCall(MatConvert_Basic(N, newtype, reuse, newmat));
245   }
246   PetscFunctionReturn(PETSC_SUCCESS);
247 }
248 
249 static PetscErrorCode MatShellSetContext_HT(Mat mat, void *ctx)
250 {
251   PetscFunctionBegin;
252   SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONGSTATE, "Cannot set the MATSHELL context for a MATHERMITIANTRANSPOSEVIRTUAL, it is used by the MATHERMITIANTRANSPOSEVIRTUAL");
253   PetscFunctionReturn(PETSC_SUCCESS);
254 }
255 
256 static PetscErrorCode MatShellSetContextDestroy_HT(Mat mat, void (*f)(void))
257 {
258   PetscFunctionBegin;
259   SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONGSTATE, "Cannot set the MATSHELL context destroy for a MATHERMITIANTRANSPOSEVIRTUAL, it is used by the MATHERMITIANTRANSPOSEVIRTUAL");
260   PetscFunctionReturn(PETSC_SUCCESS);
261 }
262 
263 /*MC
264    MATHERMITIANTRANSPOSEVIRTUAL - "hermitiantranspose" - A matrix type that represents a virtual transpose of a matrix
265 
266   Level: advanced
267 
268   Developers Notes:
269   This is implemented on top of `MATSHELL` to get support for scaling and shifting without requiring duplicate code
270 
271   Users can not call `MatShellSetOperation()` operations on this class, there is some error checking for that incorrect usage
272 
273 .seealso: [](ch_matrices), `Mat`, `MATTRANSPOSEVIRTUAL`, `Mat`, `MatCreateHermitianTranspose()`, `MatCreateTranspose()`
274 M*/
275 
276 /*@
277   MatCreateHermitianTranspose - Creates a new matrix object of `MatType` `MATHERMITIANTRANSPOSEVIRTUAL` that behaves like A'*
278 
279   Collective
280 
281   Input Parameter:
282 . A - the (possibly rectangular) matrix
283 
284   Output Parameter:
285 . N - the matrix that represents A'*
286 
287   Level: intermediate
288 
289   Note:
290   The Hermitian transpose A' is NOT actually formed! Rather the new matrix
291   object performs the matrix-vector product, `MatMult()`, by using the `MatMultHermitianTranspose()` on
292   the original matrix
293 
294 .seealso: [](ch_matrices), `Mat`, `MatCreateNormal()`, `MatMult()`, `MatMultHermitianTranspose()`, `MatCreate()`,
295           `MATTRANSPOSEVIRTUAL`, `MatCreateTranspose()`, `MatHermitianTransposeGetMat()`, `MATNORMAL`, `MATNORMALHERMITIAN`
296 @*/
297 PetscErrorCode MatCreateHermitianTranspose(Mat A, Mat *N)
298 {
299   VecType vtype;
300 
301   PetscFunctionBegin;
302   PetscCall(MatCreate(PetscObjectComm((PetscObject)A), N));
303   PetscCall(PetscLayoutReference(A->rmap, &((*N)->cmap)));
304   PetscCall(PetscLayoutReference(A->cmap, &((*N)->rmap)));
305   PetscCall(MatSetType(*N, MATSHELL));
306   PetscCall(MatShellSetContext(*N, A));
307   PetscCall(PetscObjectReference((PetscObject)A));
308 
309   PetscCall(MatSetBlockSizes(*N, PetscAbs(A->cmap->bs), PetscAbs(A->rmap->bs)));
310   PetscCall(MatGetVecType(A, &vtype));
311   PetscCall(MatSetVecType(*N, vtype));
312 #if defined(PETSC_HAVE_DEVICE)
313   PetscCall(MatBindToCPU(*N, A->boundtocpu));
314 #endif
315   PetscCall(MatSetUp(*N));
316 
317   PetscCall(MatShellSetOperation(*N, MATOP_DESTROY, (void (*)(void))MatDestroy_HT));
318   PetscCall(MatShellSetOperation(*N, MATOP_MULT, (void (*)(void))MatMult_HT));
319   PetscCall(MatShellSetOperation(*N, MATOP_MULT_HERMITIAN_TRANSPOSE, (void (*)(void))MatMultHermitianTranspose_HT));
320 #if !defined(PETSC_USE_COMPLEX)
321   PetscCall(MatShellSetOperation(*N, MATOP_MULT_TRANSPOSE, (void (*)(void))MatMultHermitianTranspose_HT));
322 #endif
323   PetscCall(MatShellSetOperation(*N, MATOP_DUPLICATE, (void (*)(void))MatDuplicate_HT));
324   PetscCall(MatShellSetOperation(*N, MATOP_CREATE_VECS, (void (*)(void))MatCreateVecs_HT));
325   PetscCall(MatShellSetOperation(*N, MATOP_HAS_OPERATION, (void (*)(void))MatHasOperation_HT));
326   PetscCall(MatShellSetOperation(*N, MATOP_GET_DIAGONAL, (void (*)(void))MatGetDiagonal_HT));
327   PetscCall(MatShellSetOperation(*N, MATOP_CONVERT, (void (*)(void))MatConvert_HT));
328 
329   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatHermitianTransposeGetMat_C", MatHermitianTransposeGetMat_HT));
330 #if !defined(PETSC_USE_COMPLEX)
331   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatTransposeGetMat_C", MatHermitianTransposeGetMat_HT));
332 #endif
333   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatProductSetFromOptions_anytype_C", MatProductSetFromOptions_HT));
334   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatShellSetContext_C", MatShellSetContext_HT));
335   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatShellSetContextDestroy_C", MatShellSetContextDestroy_HT));
336   PetscCall(PetscObjectChangeTypeName((PetscObject)*N, MATHERMITIANTRANSPOSEVIRTUAL));
337   PetscFunctionReturn(PETSC_SUCCESS);
338 }
339