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 MatHasOperation_HT(Mat mat, MatOperation op, PetscBool *has) 156 { 157 Mat A; 158 159 PetscFunctionBegin; 160 PetscCall(MatShellGetContext(mat, &A)); 161 *has = PETSC_FALSE; 162 if (op == MATOP_MULT || op == MATOP_MULT_ADD) { 163 PetscCall(MatHasOperation(A, MATOP_MULT_HERMITIAN_TRANSPOSE, has)); 164 if (!*has) PetscCall(MatHasOperation(A, MATOP_MULT_TRANSPOSE, has)); 165 } else if (op == MATOP_MULT_HERMITIAN_TRANSPOSE || op == MATOP_MULT_HERMITIAN_TRANS_ADD || op == MATOP_MULT_TRANSPOSE || op == MATOP_MULT_TRANSPOSE_ADD) { 166 PetscCall(MatHasOperation(A, MATOP_MULT, has)); 167 } else if (((void **)mat->ops)[op]) *has = PETSC_TRUE; 168 PetscFunctionReturn(PETSC_SUCCESS); 169 } 170 171 static PetscErrorCode MatHermitianTransposeGetMat_HT(Mat N, Mat *M) 172 { 173 PetscFunctionBegin; 174 PetscCall(MatShellGetContext(N, M)); 175 PetscFunctionReturn(PETSC_SUCCESS); 176 } 177 178 /*@ 179 MatHermitianTransposeGetMat - Gets the `Mat` object stored inside a `MATHERMITIANTRANSPOSEVIRTUAL` 180 181 Logically Collective 182 183 Input Parameter: 184 . A - the `MATHERMITIANTRANSPOSEVIRTUAL` matrix 185 186 Output Parameter: 187 . M - the matrix object stored inside A 188 189 Level: intermediate 190 191 .seealso: [](ch_matrices), `Mat`, `MATHERMITIANTRANSPOSEVIRTUAL`, `MatCreateHermitianTranspose()` 192 @*/ 193 PetscErrorCode MatHermitianTransposeGetMat(Mat A, Mat *M) 194 { 195 PetscFunctionBegin; 196 PetscValidHeaderSpecific(A, MAT_CLASSID, 1); 197 PetscValidType(A, 1); 198 PetscAssertPointer(M, 2); 199 PetscUseMethod(A, "MatHermitianTransposeGetMat_C", (Mat, Mat *), (A, M)); 200 PetscFunctionReturn(PETSC_SUCCESS); 201 } 202 203 static PetscErrorCode MatGetDiagonal_HT(Mat N, Vec v) 204 { 205 Mat A; 206 207 PetscFunctionBegin; 208 PetscCall(MatShellGetContext(N, &A)); 209 PetscCall(MatGetDiagonal(A, v)); 210 PetscCall(VecConjugate(v)); 211 PetscFunctionReturn(PETSC_SUCCESS); 212 } 213 214 static PetscErrorCode MatConvert_HT(Mat N, MatType newtype, MatReuse reuse, Mat *newmat) 215 { 216 Mat A; 217 PetscBool flg; 218 219 PetscFunctionBegin; 220 PetscCall(MatShellGetContext(N, &A)); 221 PetscCall(MatHasOperation(A, MATOP_HERMITIAN_TRANSPOSE, &flg)); 222 if (flg) { 223 Mat B; 224 225 PetscCall(MatHermitianTranspose(A, MAT_INITIAL_MATRIX, &B)); 226 if (reuse != MAT_INPLACE_MATRIX) { 227 PetscCall(MatConvert(B, newtype, reuse, newmat)); 228 PetscCall(MatDestroy(&B)); 229 } else { 230 PetscCall(MatConvert(B, newtype, MAT_INPLACE_MATRIX, &B)); 231 PetscCall(MatHeaderReplace(N, &B)); 232 } 233 } else { /* use basic converter as fallback */ 234 PetscCall(MatConvert_Basic(N, newtype, reuse, newmat)); 235 } 236 PetscFunctionReturn(PETSC_SUCCESS); 237 } 238 239 static PetscErrorCode MatShellSetContext_HT(Mat mat, void *ctx) 240 { 241 PetscFunctionBegin; 242 SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONGSTATE, "Cannot set the MATSHELL context for a MATHERMITIANTRANSPOSEVIRTUAL, it is used by the MATHERMITIANTRANSPOSEVIRTUAL"); 243 PetscFunctionReturn(PETSC_SUCCESS); 244 } 245 246 static PetscErrorCode MatShellSetContextDestroy_HT(Mat mat, void (*f)(void)) 247 { 248 PetscFunctionBegin; 249 SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONGSTATE, "Cannot set the MATSHELL context destroy for a MATHERMITIANTRANSPOSEVIRTUAL, it is used by the MATHERMITIANTRANSPOSEVIRTUAL"); 250 PetscFunctionReturn(PETSC_SUCCESS); 251 } 252 253 /*MC 254 MATHERMITIANTRANSPOSEVIRTUAL - "hermitiantranspose" - A matrix type that represents a virtual transpose of a matrix 255 256 Level: advanced 257 258 Developers Notes: 259 This is implemented on top of `MATSHELL` to get support for scaling and shifting without requiring duplicate code 260 261 Users can not call `MatShellSetOperation()` operations on this class, there is some error checking for that incorrect usage 262 263 .seealso: [](ch_matrices), `Mat`, `MATTRANSPOSEVIRTUAL`, `Mat`, `MatCreateHermitianTranspose()`, `MatCreateTranspose()` 264 M*/ 265 266 /*@ 267 MatCreateHermitianTranspose - Creates a new matrix object of `MatType` `MATHERMITIANTRANSPOSEVIRTUAL` that behaves like A'* 268 269 Collective 270 271 Input Parameter: 272 . A - the (possibly rectangular) matrix 273 274 Output Parameter: 275 . N - the matrix that represents A'* 276 277 Level: intermediate 278 279 Note: 280 The Hermitian transpose A' is NOT actually formed! Rather the new matrix 281 object performs the matrix-vector product, `MatMult()`, by using the `MatMultHermitianTranspose()` on 282 the original matrix 283 284 .seealso: [](ch_matrices), `Mat`, `MatCreateNormal()`, `MatMult()`, `MatMultHermitianTranspose()`, `MatCreate()`, 285 `MATTRANSPOSEVIRTUAL`, `MatCreateTranspose()`, `MatHermitianTransposeGetMat()`, `MATNORMAL`, `MATNORMALHERMITIAN` 286 @*/ 287 PetscErrorCode MatCreateHermitianTranspose(Mat A, Mat *N) 288 { 289 VecType vtype; 290 291 PetscFunctionBegin; 292 PetscCall(MatCreate(PetscObjectComm((PetscObject)A), N)); 293 PetscCall(PetscLayoutReference(A->rmap, &((*N)->cmap))); 294 PetscCall(PetscLayoutReference(A->cmap, &((*N)->rmap))); 295 PetscCall(MatSetType(*N, MATSHELL)); 296 PetscCall(MatShellSetContext(*N, A)); 297 PetscCall(PetscObjectReference((PetscObject)A)); 298 299 PetscCall(MatSetBlockSizes(*N, PetscAbs(A->cmap->bs), PetscAbs(A->rmap->bs))); 300 PetscCall(MatGetVecType(A, &vtype)); 301 PetscCall(MatSetVecType(*N, vtype)); 302 #if defined(PETSC_HAVE_DEVICE) 303 PetscCall(MatBindToCPU(*N, A->boundtocpu)); 304 #endif 305 PetscCall(MatSetUp(*N)); 306 307 PetscCall(MatShellSetOperation(*N, MATOP_DESTROY, (void (*)(void))MatDestroy_HT)); 308 PetscCall(MatShellSetOperation(*N, MATOP_MULT, (void (*)(void))MatMult_HT)); 309 PetscCall(MatShellSetOperation(*N, MATOP_MULT_HERMITIAN_TRANSPOSE, (void (*)(void))MatMultHermitianTranspose_HT)); 310 #if !defined(PETSC_USE_COMPLEX) 311 PetscCall(MatShellSetOperation(*N, MATOP_MULT_TRANSPOSE, (void (*)(void))MatMultHermitianTranspose_HT)); 312 #endif 313 PetscCall(MatShellSetOperation(*N, MATOP_DUPLICATE, (void (*)(void))MatDuplicate_HT)); 314 PetscCall(MatShellSetOperation(*N, MATOP_HAS_OPERATION, (void (*)(void))MatHasOperation_HT)); 315 PetscCall(MatShellSetOperation(*N, MATOP_GET_DIAGONAL, (void (*)(void))MatGetDiagonal_HT)); 316 PetscCall(MatShellSetOperation(*N, MATOP_CONVERT, (void (*)(void))MatConvert_HT)); 317 318 PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatHermitianTransposeGetMat_C", MatHermitianTransposeGetMat_HT)); 319 #if !defined(PETSC_USE_COMPLEX) 320 PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatTransposeGetMat_C", MatHermitianTransposeGetMat_HT)); 321 #endif 322 PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatProductSetFromOptions_anytype_C", MatProductSetFromOptions_HT)); 323 PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatShellSetContext_C", MatShellSetContext_HT)); 324 PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatShellSetContextDestroy_C", MatShellSetContextDestroy_HT)); 325 PetscCall(PetscObjectChangeTypeName((PetscObject)*N, MATHERMITIANTRANSPOSEVIRTUAL)); 326 PetscFunctionReturn(PETSC_SUCCESS); 327 } 328