1a1f56445SPierre Jolivet #include <../src/mat/impls/shell/shell.h> /*I "petscmat.h" I*/ 2d0de2241SAndrew Spott 3794904c8SPierre Jolivet typedef struct { 4794904c8SPierre Jolivet PetscErrorCode (*numeric)(Mat); 5*cc1eb50dSBarry Smith PetscCtxDestroyFn *destroy; 6794904c8SPierre Jolivet Mat B; 7794904c8SPierre Jolivet PetscScalar scale; 8794904c8SPierre Jolivet PetscBool conjugate; 9794904c8SPierre Jolivet PetscContainer container; 10*cc1eb50dSBarry Smith void *data; 11*cc1eb50dSBarry Smith } MatProductCtx_HT; 12794904c8SPierre Jolivet 13*cc1eb50dSBarry Smith static PetscErrorCode MatProductCtxDestroy_HT(void **ptr) 14794904c8SPierre Jolivet { 15*cc1eb50dSBarry Smith MatProductCtx_HT *data = *(MatProductCtx_HT **)ptr; 16794904c8SPierre Jolivet 17794904c8SPierre Jolivet PetscFunctionBegin; 18*cc1eb50dSBarry Smith if (data->data) PetscCall((*data->destroy)(&data->data)); 19794904c8SPierre Jolivet if (data->conjugate) PetscCall(MatDestroy(&data->B)); 20794904c8SPierre Jolivet PetscCall(PetscContainerDestroy(&data->container)); 21794904c8SPierre Jolivet PetscCall(PetscFree(data)); 22794904c8SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS); 23794904c8SPierre Jolivet } 24794904c8SPierre Jolivet 25794904c8SPierre Jolivet static PetscErrorCode MatProductNumeric_HT(Mat D) 26794904c8SPierre Jolivet { 27794904c8SPierre Jolivet Mat_Product *product; 28794904c8SPierre Jolivet Mat B; 29*cc1eb50dSBarry Smith MatProductCtx_HT *data; 30794904c8SPierre Jolivet PetscContainer container; 31794904c8SPierre Jolivet 32794904c8SPierre Jolivet PetscFunctionBegin; 33794904c8SPierre Jolivet MatCheckProduct(D, 1); 34794904c8SPierre Jolivet PetscCheck(D->product->data, PetscObjectComm((PetscObject)D), PETSC_ERR_PLIB, "Product data empty"); 35794904c8SPierre Jolivet product = D->product; 36*cc1eb50dSBarry Smith PetscCall(PetscObjectQuery((PetscObject)D, "MatProductCtx_HT", (PetscObject *)&container)); 37*cc1eb50dSBarry Smith PetscCheck(container, PetscObjectComm((PetscObject)D), PETSC_ERR_PLIB, "MatProductCtx_HT missing"); 38794904c8SPierre Jolivet PetscCall(PetscContainerGetPointer(container, (void **)&data)); 39794904c8SPierre Jolivet B = product->B; 40*cc1eb50dSBarry Smith data = (MatProductCtx_HT *)product->data; 41794904c8SPierre Jolivet if (data->conjugate) { 42794904c8SPierre Jolivet PetscCall(MatCopy(product->B, data->B, SAME_NONZERO_PATTERN)); 43794904c8SPierre Jolivet PetscCall(MatConjugate(data->B)); 44794904c8SPierre Jolivet product->B = data->B; 45794904c8SPierre Jolivet } 46*cc1eb50dSBarry Smith product->data = data->data; 47794904c8SPierre Jolivet PetscCall((*data->numeric)(D)); 48794904c8SPierre Jolivet if (data->conjugate) { 49794904c8SPierre Jolivet PetscCall(MatConjugate(D)); 50794904c8SPierre Jolivet product->B = B; 51794904c8SPierre Jolivet } 52794904c8SPierre Jolivet PetscCall(MatScale(D, data->scale)); 53794904c8SPierre Jolivet product->data = data; 54794904c8SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS); 55794904c8SPierre Jolivet } 56794904c8SPierre Jolivet 57794904c8SPierre Jolivet static PetscErrorCode MatProductSymbolic_HT(Mat D) 58794904c8SPierre Jolivet { 59794904c8SPierre Jolivet Mat_Product *product; 60794904c8SPierre Jolivet Mat B; 61*cc1eb50dSBarry Smith MatProductCtx_HT *data; 62794904c8SPierre Jolivet PetscContainer container; 63794904c8SPierre Jolivet 64794904c8SPierre Jolivet PetscFunctionBegin; 65794904c8SPierre Jolivet MatCheckProduct(D, 1); 66794904c8SPierre Jolivet product = D->product; 67794904c8SPierre Jolivet B = product->B; 68794904c8SPierre Jolivet if (D->ops->productsymbolic == MatProductSymbolic_HT) { 69794904c8SPierre Jolivet PetscCheck(!product->data, PetscObjectComm((PetscObject)D), PETSC_ERR_PLIB, "Product data not empty"); 70*cc1eb50dSBarry Smith PetscCall(PetscObjectQuery((PetscObject)D, "MatProductCtx_HT", (PetscObject *)&container)); 71*cc1eb50dSBarry Smith PetscCheck(container, PetscObjectComm((PetscObject)D), PETSC_ERR_PLIB, "MatProductCtx_HT missing"); 72794904c8SPierre Jolivet PetscCall(PetscContainerGetPointer(container, (void **)&data)); 73794904c8SPierre Jolivet PetscCall(MatProductSetFromOptions(D)); 74794904c8SPierre Jolivet if (data->conjugate) { 75794904c8SPierre Jolivet PetscCall(MatDuplicate(B, MAT_DO_NOT_COPY_VALUES, &data->B)); 76794904c8SPierre Jolivet product->B = data->B; 77794904c8SPierre Jolivet } 78794904c8SPierre Jolivet PetscCall(MatProductSymbolic(D)); 79794904c8SPierre Jolivet data->numeric = D->ops->productnumeric; 80794904c8SPierre Jolivet data->destroy = product->destroy; 81*cc1eb50dSBarry Smith data->data = product->data; 82794904c8SPierre Jolivet D->ops->productnumeric = MatProductNumeric_HT; 83*cc1eb50dSBarry Smith product->destroy = MatProductCtxDestroy_HT; 84794904c8SPierre Jolivet if (data->conjugate) product->B = B; 85794904c8SPierre Jolivet product->data = data; 86794904c8SPierre Jolivet } 87794904c8SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS); 88794904c8SPierre Jolivet } 89794904c8SPierre Jolivet 9094764886SPierre Jolivet static PetscErrorCode MatProductSetFromOptions_HT(Mat D) 91d71ae5a4SJacob Faibussowitsch { 92013e2dc7SBarry Smith Mat A, B, C, Ain, Bin, Cin; 93794904c8SPierre Jolivet PetscScalar scale = 1.0, vscale; 94794904c8SPierre Jolivet PetscBool Aistrans, Bistrans, Cistrans, conjugate = PETSC_FALSE; 95013e2dc7SBarry Smith PetscInt Atrans, Btrans, Ctrans; 96794904c8SPierre Jolivet PetscContainer container = NULL; 97*cc1eb50dSBarry Smith MatProductCtx_HT *data; 98013e2dc7SBarry Smith MatProductType ptype; 99013e2dc7SBarry Smith 100013e2dc7SBarry Smith PetscFunctionBegin; 101013e2dc7SBarry Smith MatCheckProduct(D, 1); 102013e2dc7SBarry Smith A = D->product->A; 103013e2dc7SBarry Smith B = D->product->B; 104013e2dc7SBarry Smith C = D->product->C; 105013e2dc7SBarry Smith PetscCall(PetscObjectTypeCompare((PetscObject)A, MATHERMITIANTRANSPOSEVIRTUAL, &Aistrans)); 106013e2dc7SBarry Smith PetscCall(PetscObjectTypeCompare((PetscObject)B, MATHERMITIANTRANSPOSEVIRTUAL, &Bistrans)); 107013e2dc7SBarry Smith PetscCall(PetscObjectTypeCompare((PetscObject)C, MATHERMITIANTRANSPOSEVIRTUAL, &Cistrans)); 108013e2dc7SBarry Smith PetscCheck(Aistrans || Bistrans || Cistrans, PetscObjectComm((PetscObject)D), PETSC_ERR_PLIB, "This should not happen"); 109013e2dc7SBarry Smith Atrans = 0; 110013e2dc7SBarry Smith Ain = A; 111013e2dc7SBarry Smith while (Aistrans) { 112013e2dc7SBarry Smith Atrans++; 113794904c8SPierre Jolivet PetscCall(MatShellGetScalingShifts(Ain, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, &vscale, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Mat *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED)); 114794904c8SPierre Jolivet conjugate = (PetscBool)!conjugate; 115794904c8SPierre Jolivet scale *= vscale; 116013e2dc7SBarry Smith PetscCall(MatHermitianTransposeGetMat(Ain, &Ain)); 117013e2dc7SBarry Smith PetscCall(PetscObjectTypeCompare((PetscObject)Ain, MATHERMITIANTRANSPOSEVIRTUAL, &Aistrans)); 118013e2dc7SBarry Smith } 119013e2dc7SBarry Smith Btrans = 0; 120013e2dc7SBarry Smith Bin = B; 121013e2dc7SBarry Smith while (Bistrans) { 122013e2dc7SBarry Smith Btrans++; 123794904c8SPierre Jolivet PetscCall(MatShellGetScalingShifts(Bin, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, &vscale, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Mat *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED)); 124794904c8SPierre Jolivet scale *= vscale; 125013e2dc7SBarry Smith PetscCall(MatHermitianTransposeGetMat(Bin, &Bin)); 126013e2dc7SBarry Smith PetscCall(PetscObjectTypeCompare((PetscObject)Bin, MATHERMITIANTRANSPOSEVIRTUAL, &Bistrans)); 127013e2dc7SBarry Smith } 128013e2dc7SBarry Smith Ctrans = 0; 129013e2dc7SBarry Smith Cin = C; 130013e2dc7SBarry Smith while (Cistrans) { 131013e2dc7SBarry Smith Ctrans++; 132794904c8SPierre Jolivet PetscCall(MatShellGetScalingShifts(Cin, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, &vscale, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Mat *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED)); 133794904c8SPierre Jolivet scale *= vscale; 134013e2dc7SBarry Smith PetscCall(MatHermitianTransposeGetMat(Cin, &Cin)); 135013e2dc7SBarry Smith PetscCall(PetscObjectTypeCompare((PetscObject)Cin, MATHERMITIANTRANSPOSEVIRTUAL, &Cistrans)); 136013e2dc7SBarry Smith } 137013e2dc7SBarry Smith Atrans = Atrans % 2; 138013e2dc7SBarry Smith Btrans = Btrans % 2; 139013e2dc7SBarry Smith Ctrans = Ctrans % 2; 140013e2dc7SBarry Smith ptype = D->product->type; /* same product type by default */ 141013e2dc7SBarry Smith if (Ain->symmetric == PETSC_BOOL3_TRUE) Atrans = 0; 142013e2dc7SBarry Smith if (Bin->symmetric == PETSC_BOOL3_TRUE) Btrans = 0; 143013e2dc7SBarry Smith if (Cin && Cin->symmetric == PETSC_BOOL3_TRUE) Ctrans = 0; 144013e2dc7SBarry Smith 145013e2dc7SBarry Smith if (Atrans || Btrans || Ctrans) { 146794904c8SPierre Jolivet PetscCheck(!PetscDefined(USE_COMPLEX) || (!Btrans && !Ctrans), PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "No support for complex Hermitian transpose matrices"); 147794904c8SPierre Jolivet if ((PetscDefined(USE_COMPLEX) && Atrans) || scale != 1.0) { 148*cc1eb50dSBarry Smith PetscCall(PetscObjectQuery((PetscObject)D, "MatProductCtx_HT", (PetscObject *)&container)); 149794904c8SPierre Jolivet if (!container) { 150794904c8SPierre Jolivet PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)D), &container)); 151794904c8SPierre Jolivet PetscCall(PetscNew(&data)); 152794904c8SPierre Jolivet data->scale = scale; 153794904c8SPierre Jolivet data->conjugate = (PetscBool)Atrans; 154794904c8SPierre Jolivet data->container = container; 155794904c8SPierre Jolivet PetscCall(PetscContainerSetPointer(container, data)); 156*cc1eb50dSBarry Smith PetscCall(PetscObjectCompose((PetscObject)D, "MatProductCtx_HT", (PetscObject)container)); 157794904c8SPierre Jolivet } 158794904c8SPierre Jolivet } 159013e2dc7SBarry Smith ptype = MATPRODUCT_UNSPECIFIED; 160013e2dc7SBarry Smith switch (D->product->type) { 161013e2dc7SBarry Smith case MATPRODUCT_AB: 162013e2dc7SBarry Smith if (Atrans && Btrans) { /* At * Bt we do not have support for this */ 163013e2dc7SBarry Smith /* TODO custom implementation ? */ 164013e2dc7SBarry Smith } else if (Atrans) { /* At * B */ 165013e2dc7SBarry Smith ptype = MATPRODUCT_AtB; 166013e2dc7SBarry Smith } else { /* A * Bt */ 167013e2dc7SBarry Smith ptype = MATPRODUCT_ABt; 168013e2dc7SBarry Smith } 169013e2dc7SBarry Smith break; 170013e2dc7SBarry Smith case MATPRODUCT_AtB: 171013e2dc7SBarry Smith if (Atrans && Btrans) { /* A * Bt */ 172013e2dc7SBarry Smith ptype = MATPRODUCT_ABt; 173013e2dc7SBarry Smith } else if (Atrans) { /* A * B */ 174013e2dc7SBarry Smith ptype = MATPRODUCT_AB; 175013e2dc7SBarry Smith } else { /* At * Bt we do not have support for this */ 176013e2dc7SBarry Smith /* TODO custom implementation ? */ 177013e2dc7SBarry Smith } 178013e2dc7SBarry Smith break; 179013e2dc7SBarry Smith case MATPRODUCT_ABt: 180013e2dc7SBarry Smith if (Atrans && Btrans) { /* At * B */ 181013e2dc7SBarry Smith ptype = MATPRODUCT_AtB; 182013e2dc7SBarry Smith } else if (Atrans) { /* At * Bt we do not have support for this */ 183013e2dc7SBarry Smith /* TODO custom implementation ? */ 184013e2dc7SBarry Smith } else { /* A * B */ 185013e2dc7SBarry Smith ptype = MATPRODUCT_AB; 186013e2dc7SBarry Smith } 187013e2dc7SBarry Smith break; 188013e2dc7SBarry Smith case MATPRODUCT_PtAP: 189013e2dc7SBarry Smith if (Atrans) { /* PtAtP */ 190013e2dc7SBarry Smith /* TODO custom implementation ? */ 191013e2dc7SBarry Smith } else { /* RARt */ 192013e2dc7SBarry Smith ptype = MATPRODUCT_RARt; 193013e2dc7SBarry Smith } 194013e2dc7SBarry Smith break; 195013e2dc7SBarry Smith case MATPRODUCT_RARt: 196013e2dc7SBarry Smith if (Atrans) { /* RAtRt */ 197013e2dc7SBarry Smith /* TODO custom implementation ? */ 198013e2dc7SBarry Smith } else { /* PtAP */ 199013e2dc7SBarry Smith ptype = MATPRODUCT_PtAP; 200013e2dc7SBarry Smith } 201013e2dc7SBarry Smith break; 202013e2dc7SBarry Smith case MATPRODUCT_ABC: 203013e2dc7SBarry Smith /* TODO custom implementation ? */ 204013e2dc7SBarry Smith break; 205d71ae5a4SJacob Faibussowitsch default: 206d71ae5a4SJacob Faibussowitsch SETERRQ(PetscObjectComm((PetscObject)D), PETSC_ERR_SUP, "ProductType %s is not supported", MatProductTypes[D->product->type]); 207013e2dc7SBarry Smith } 208013e2dc7SBarry Smith } 209013e2dc7SBarry Smith PetscCall(MatProductReplaceMats(Ain, Bin, Cin, D)); 210013e2dc7SBarry Smith PetscCall(MatProductSetType(D, ptype)); 211794904c8SPierre Jolivet if (container) D->ops->productsymbolic = MatProductSymbolic_HT; 212794904c8SPierre Jolivet else PetscCall(MatProductSetFromOptions(D)); 2133ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 214013e2dc7SBarry Smith } 215f1769f9dSJose E. Roman 21666976f2fSJacob Faibussowitsch static PetscErrorCode MatMult_HT(Mat N, Vec x, Vec y) 217d71ae5a4SJacob Faibussowitsch { 218bf477422SJose E. Roman Mat A; 219d0de2241SAndrew Spott 220d0de2241SAndrew Spott PetscFunctionBegin; 221bf477422SJose E. Roman PetscCall(MatShellGetContext(N, &A)); 222bf477422SJose E. Roman PetscCall(MatMultHermitianTranspose(A, x, y)); 2233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 224d0de2241SAndrew Spott } 225d0de2241SAndrew Spott 22666976f2fSJacob Faibussowitsch static PetscErrorCode MatMultHermitianTranspose_HT(Mat N, Vec x, Vec y) 227d71ae5a4SJacob Faibussowitsch { 228bf477422SJose E. Roman Mat A; 229d0de2241SAndrew Spott 230d0de2241SAndrew Spott PetscFunctionBegin; 231bf477422SJose E. Roman PetscCall(MatShellGetContext(N, &A)); 232bf477422SJose E. Roman PetscCall(MatMult(A, x, y)); 2333ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 234d0de2241SAndrew Spott } 235f1769f9dSJose E. Roman 236f1769f9dSJose E. Roman static PetscErrorCode MatSolve_HT_LU(Mat N, Vec b, Vec x) 237f1769f9dSJose E. Roman { 238f1769f9dSJose E. Roman Mat A; 239f1769f9dSJose E. Roman Vec w; 240f1769f9dSJose E. Roman 241f1769f9dSJose E. Roman PetscFunctionBegin; 242f1769f9dSJose E. Roman PetscCall(MatShellGetContext(N, &A)); 243f1769f9dSJose E. Roman PetscCall(VecDuplicate(b, &w)); 244f1769f9dSJose E. Roman PetscCall(VecCopy(b, w)); 245f1769f9dSJose E. Roman PetscCall(VecConjugate(w)); 246f1769f9dSJose E. Roman PetscCall(MatSolveTranspose(A, w, x)); 247f1769f9dSJose E. Roman PetscCall(VecConjugate(x)); 248f1769f9dSJose E. Roman PetscCall(VecDestroy(&w)); 249f1769f9dSJose E. Roman PetscFunctionReturn(PETSC_SUCCESS); 250f1769f9dSJose E. Roman } 251f1769f9dSJose E. Roman 252f1769f9dSJose E. Roman static PetscErrorCode MatSolveAdd_HT_LU(Mat N, Vec b, Vec y, Vec x) 253f1769f9dSJose E. Roman { 254f1769f9dSJose E. Roman Mat A; 255f1769f9dSJose E. Roman Vec v, w; 256f1769f9dSJose E. Roman 257f1769f9dSJose E. Roman PetscFunctionBegin; 258f1769f9dSJose E. Roman PetscCall(MatShellGetContext(N, &A)); 259f1769f9dSJose E. Roman PetscCall(VecDuplicate(b, &v)); 260f1769f9dSJose E. Roman PetscCall(VecDuplicate(b, &w)); 261f1769f9dSJose E. Roman PetscCall(VecCopy(y, v)); 262f1769f9dSJose E. Roman PetscCall(VecCopy(b, w)); 263f1769f9dSJose E. Roman PetscCall(VecConjugate(v)); 264f1769f9dSJose E. Roman PetscCall(VecConjugate(w)); 265f1769f9dSJose E. Roman PetscCall(MatSolveTransposeAdd(A, w, v, x)); 266f1769f9dSJose E. Roman PetscCall(VecConjugate(x)); 267f1769f9dSJose E. Roman PetscCall(VecDestroy(&v)); 268f1769f9dSJose E. Roman PetscCall(VecDestroy(&w)); 269f1769f9dSJose E. Roman PetscFunctionReturn(PETSC_SUCCESS); 270f1769f9dSJose E. Roman } 271f1769f9dSJose E. Roman 272f1769f9dSJose E. Roman static PetscErrorCode MatMatSolve_HT_LU(Mat N, Mat B, Mat X) 273f1769f9dSJose E. Roman { 274f1769f9dSJose E. Roman Mat A, W; 275f1769f9dSJose E. Roman 276f1769f9dSJose E. Roman PetscFunctionBegin; 277f1769f9dSJose E. Roman PetscCall(MatShellGetContext(N, &A)); 278f1769f9dSJose E. Roman PetscCall(MatDuplicate(B, MAT_COPY_VALUES, &W)); 279f1769f9dSJose E. Roman PetscCall(MatConjugate(W)); 280f1769f9dSJose E. Roman PetscCall(MatMatSolveTranspose(A, W, X)); 281f1769f9dSJose E. Roman PetscCall(MatConjugate(X)); 282f1769f9dSJose E. Roman PetscCall(MatDestroy(&W)); 283f1769f9dSJose E. Roman PetscFunctionReturn(PETSC_SUCCESS); 284f1769f9dSJose E. Roman } 285f1769f9dSJose E. Roman 286f1769f9dSJose E. Roman static PetscErrorCode MatLUFactor_HT(Mat N, IS row, IS col, const MatFactorInfo *minfo) 287f1769f9dSJose E. Roman { 288f1769f9dSJose E. Roman Mat A; 289f1769f9dSJose E. Roman 290f1769f9dSJose E. Roman PetscFunctionBegin; 291f1769f9dSJose E. Roman PetscCall(MatShellGetContext(N, &A)); 292f1769f9dSJose E. Roman PetscCall(MatLUFactor(A, col, row, minfo)); 29357d50842SBarry Smith PetscCall(MatShellSetOperation(N, MATOP_SOLVE, (PetscErrorCodeFn *)MatSolve_HT_LU)); 29457d50842SBarry Smith PetscCall(MatShellSetOperation(N, MATOP_SOLVE_ADD, (PetscErrorCodeFn *)MatSolveAdd_HT_LU)); 29557d50842SBarry Smith PetscCall(MatShellSetOperation(N, MATOP_MAT_SOLVE, (PetscErrorCodeFn *)MatMatSolve_HT_LU)); 296f1769f9dSJose E. Roman PetscFunctionReturn(PETSC_SUCCESS); 297f1769f9dSJose E. Roman } 298f1769f9dSJose E. Roman 299f1769f9dSJose E. Roman static PetscErrorCode MatSolve_HT_Cholesky(Mat N, Vec b, Vec x) 300f1769f9dSJose E. Roman { 301f1769f9dSJose E. Roman Mat A; 302f1769f9dSJose E. Roman 303f1769f9dSJose E. Roman PetscFunctionBegin; 304f1769f9dSJose E. Roman PetscCall(MatShellGetContext(N, &A)); 305f1769f9dSJose E. Roman PetscCall(MatSolve(A, b, x)); 306f1769f9dSJose E. Roman PetscFunctionReturn(PETSC_SUCCESS); 307f1769f9dSJose E. Roman } 308f1769f9dSJose E. Roman 309f1769f9dSJose E. Roman static PetscErrorCode MatSolveAdd_HT_Cholesky(Mat N, Vec b, Vec y, Vec x) 310f1769f9dSJose E. Roman { 311f1769f9dSJose E. Roman Mat A; 312f1769f9dSJose E. Roman Vec v, w; 313f1769f9dSJose E. Roman 314f1769f9dSJose E. Roman PetscFunctionBegin; 315f1769f9dSJose E. Roman PetscCall(MatShellGetContext(N, &A)); 316f1769f9dSJose E. Roman PetscCall(VecDuplicate(b, &v)); 317f1769f9dSJose E. Roman PetscCall(VecDuplicate(b, &w)); 318f1769f9dSJose E. Roman PetscCall(VecCopy(y, v)); 319f1769f9dSJose E. Roman PetscCall(VecCopy(b, w)); 320f1769f9dSJose E. Roman PetscCall(VecConjugate(v)); 321f1769f9dSJose E. Roman PetscCall(VecConjugate(w)); 322f1769f9dSJose E. Roman PetscCall(MatSolveTransposeAdd(A, w, v, x)); 323f1769f9dSJose E. Roman PetscCall(VecConjugate(x)); 324f1769f9dSJose E. Roman PetscCall(VecDestroy(&v)); 325f1769f9dSJose E. Roman PetscCall(VecDestroy(&w)); 326f1769f9dSJose E. Roman PetscFunctionReturn(PETSC_SUCCESS); 327f1769f9dSJose E. Roman } 328f1769f9dSJose E. Roman 329f1769f9dSJose E. Roman static PetscErrorCode MatMatSolve_HT_Cholesky(Mat N, Mat B, Mat X) 330f1769f9dSJose E. Roman { 331f1769f9dSJose E. Roman Mat A, W; 332f1769f9dSJose E. Roman 333f1769f9dSJose E. Roman PetscFunctionBegin; 334f1769f9dSJose E. Roman PetscCall(MatShellGetContext(N, &A)); 335f1769f9dSJose E. Roman PetscCall(MatDuplicate(B, MAT_COPY_VALUES, &W)); 336f1769f9dSJose E. Roman PetscCall(MatConjugate(W)); 337f1769f9dSJose E. Roman PetscCall(MatMatSolveTranspose(A, W, X)); 338f1769f9dSJose E. Roman PetscCall(MatConjugate(X)); 339f1769f9dSJose E. Roman PetscCall(MatDestroy(&W)); 340f1769f9dSJose E. Roman PetscFunctionReturn(PETSC_SUCCESS); 341f1769f9dSJose E. Roman } 342f1769f9dSJose E. Roman 343f1769f9dSJose E. Roman static PetscErrorCode MatCholeskyFactor_HT(Mat N, IS perm, const MatFactorInfo *minfo) 344f1769f9dSJose E. Roman { 345f1769f9dSJose E. Roman Mat A; 346f1769f9dSJose E. Roman 347f1769f9dSJose E. Roman PetscFunctionBegin; 348f1769f9dSJose E. Roman PetscCall(MatShellGetContext(N, &A)); 349f1769f9dSJose E. Roman PetscCheck(!PetscDefined(USE_COMPLEX) || A->hermitian == PETSC_BOOL3_TRUE, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Cholesky supported only if original matrix is Hermitian"); 350f1769f9dSJose E. Roman PetscCall(MatCholeskyFactor(A, perm, minfo)); 35157d50842SBarry Smith PetscCall(MatShellSetOperation(N, MATOP_SOLVE, (PetscErrorCodeFn *)MatSolve_HT_Cholesky)); 35257d50842SBarry Smith PetscCall(MatShellSetOperation(N, MATOP_SOLVE_ADD, (PetscErrorCodeFn *)MatSolveAdd_HT_Cholesky)); 35357d50842SBarry Smith PetscCall(MatShellSetOperation(N, MATOP_MAT_SOLVE, (PetscErrorCodeFn *)MatMatSolve_HT_Cholesky)); 354f1769f9dSJose E. Roman PetscFunctionReturn(PETSC_SUCCESS); 355f1769f9dSJose E. Roman } 356f1769f9dSJose E. Roman 357f5bab676SJose E. Roman static PetscErrorCode MatLUFactorNumeric_HT(Mat F, Mat N, const MatFactorInfo *info) 358f5bab676SJose E. Roman { 359f5bab676SJose E. Roman Mat A, FA; 360f5bab676SJose E. Roman 361f5bab676SJose E. Roman PetscFunctionBegin; 362f5bab676SJose E. Roman PetscCall(MatShellGetContext(N, &A)); 363f5bab676SJose E. Roman PetscCall(MatShellGetContext(F, &FA)); 364f5bab676SJose E. Roman PetscCall(MatLUFactorNumeric(FA, A, info)); 36557d50842SBarry Smith PetscCall(MatShellSetOperation(F, MATOP_SOLVE, (PetscErrorCodeFn *)MatSolve_HT_LU)); 36657d50842SBarry Smith PetscCall(MatShellSetOperation(F, MATOP_SOLVE_ADD, (PetscErrorCodeFn *)MatSolveAdd_HT_LU)); 36757d50842SBarry Smith PetscCall(MatShellSetOperation(F, MATOP_MAT_SOLVE, (PetscErrorCodeFn *)MatMatSolve_HT_LU)); 368f5bab676SJose E. Roman PetscFunctionReturn(PETSC_SUCCESS); 369f5bab676SJose E. Roman } 370f5bab676SJose E. Roman 371f5bab676SJose E. Roman static PetscErrorCode MatLUFactorSymbolic_HT(Mat F, Mat N, IS row, IS col, const MatFactorInfo *info) 372f5bab676SJose E. Roman { 373f5bab676SJose E. Roman Mat A, FA; 374f5bab676SJose E. Roman 375f5bab676SJose E. Roman PetscFunctionBegin; 376f5bab676SJose E. Roman PetscCall(MatShellGetContext(N, &A)); 377f5bab676SJose E. Roman PetscCall(MatShellGetContext(F, &FA)); 378f5bab676SJose E. Roman PetscCall(MatLUFactorSymbolic(FA, A, row, col, info)); 37957d50842SBarry Smith PetscCall(MatShellSetOperation(F, MATOP_LUFACTOR_NUMERIC, (PetscErrorCodeFn *)MatLUFactorNumeric_HT)); 380f5bab676SJose E. Roman PetscFunctionReturn(PETSC_SUCCESS); 381f5bab676SJose E. Roman } 382f5bab676SJose E. Roman 383f5bab676SJose E. Roman static PetscErrorCode MatCholeskyFactorNumeric_HT(Mat F, Mat N, const MatFactorInfo *info) 384f5bab676SJose E. Roman { 385f5bab676SJose E. Roman Mat A, FA; 386f5bab676SJose E. Roman 387f5bab676SJose E. Roman PetscFunctionBegin; 388f5bab676SJose E. Roman PetscCall(MatShellGetContext(N, &A)); 389f5bab676SJose E. Roman PetscCall(MatShellGetContext(F, &FA)); 390f5bab676SJose E. Roman PetscCall(MatCholeskyFactorNumeric(FA, A, info)); 39157d50842SBarry Smith PetscCall(MatShellSetOperation(F, MATOP_SOLVE, (PetscErrorCodeFn *)MatSolve_HT_Cholesky)); 39257d50842SBarry Smith PetscCall(MatShellSetOperation(F, MATOP_SOLVE_ADD, (PetscErrorCodeFn *)MatSolveAdd_HT_Cholesky)); 39357d50842SBarry Smith PetscCall(MatShellSetOperation(F, MATOP_MAT_SOLVE, (PetscErrorCodeFn *)MatMatSolve_HT_Cholesky)); 394f5bab676SJose E. Roman PetscFunctionReturn(PETSC_SUCCESS); 395f5bab676SJose E. Roman } 396f5bab676SJose E. Roman 397f5bab676SJose E. Roman static PetscErrorCode MatCholeskyFactorSymbolic_HT(Mat F, Mat N, IS perm, const MatFactorInfo *info) 398f5bab676SJose E. Roman { 399f5bab676SJose E. Roman Mat A, FA; 400f5bab676SJose E. Roman 401f5bab676SJose E. Roman PetscFunctionBegin; 402f5bab676SJose E. Roman PetscCall(MatShellGetContext(N, &A)); 403f5bab676SJose E. Roman PetscCall(MatShellGetContext(F, &FA)); 404f5bab676SJose E. Roman PetscCall(MatCholeskyFactorSymbolic(FA, A, perm, info)); 40557d50842SBarry Smith PetscCall(MatShellSetOperation(F, MATOP_CHOLESKY_FACTOR_NUMERIC, (PetscErrorCodeFn *)MatCholeskyFactorNumeric_HT)); 406f5bab676SJose E. Roman PetscFunctionReturn(PETSC_SUCCESS); 407f5bab676SJose E. Roman } 408f5bab676SJose E. Roman 409f5bab676SJose E. Roman static PetscErrorCode MatGetFactor_HT(Mat N, MatSolverType type, MatFactorType ftype, Mat *F) 410f5bab676SJose E. Roman { 411f5bab676SJose E. Roman Mat A, FA; 412f5bab676SJose E. Roman 413f5bab676SJose E. Roman PetscFunctionBegin; 414f5bab676SJose E. Roman PetscCall(MatShellGetContext(N, &A)); 415f5bab676SJose E. Roman PetscCall(MatGetFactor(A, type, ftype, &FA)); 416f5bab676SJose E. Roman PetscCall(MatCreateTranspose(FA, F)); 41757d50842SBarry Smith if (ftype == MAT_FACTOR_LU) PetscCall(MatShellSetOperation(*F, MATOP_LUFACTOR_SYMBOLIC, (PetscErrorCodeFn *)MatLUFactorSymbolic_HT)); 418f5bab676SJose E. Roman else if (ftype == MAT_FACTOR_CHOLESKY) { 419f5bab676SJose E. Roman PetscCheck(!PetscDefined(USE_COMPLEX) || A->hermitian == PETSC_BOOL3_TRUE, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Cholesky supported only if original matrix is Hermitian"); 420f5bab676SJose E. Roman PetscCall(MatPropagateSymmetryOptions(A, FA)); 42157d50842SBarry Smith PetscCall(MatShellSetOperation(*F, MATOP_CHOLESKY_FACTOR_SYMBOLIC, (PetscErrorCodeFn *)MatCholeskyFactorSymbolic_HT)); 422f5bab676SJose E. Roman } else SETERRQ(PetscObjectComm((PetscObject)N), PETSC_ERR_SUP, "Support for factor type %s not implemented in MATTRANSPOSEVIRTUAL", MatFactorTypes[ftype]); 4231a1fb624SJose E. Roman (*F)->factortype = ftype; 424f5bab676SJose E. Roman PetscCall(MatDestroy(&FA)); 425f5bab676SJose E. Roman PetscFunctionReturn(PETSC_SUCCESS); 426f5bab676SJose E. Roman } 427f5bab676SJose E. Roman 42866976f2fSJacob Faibussowitsch static PetscErrorCode MatDestroy_HT(Mat N) 429d71ae5a4SJacob Faibussowitsch { 430bf477422SJose E. Roman Mat A; 431d0de2241SAndrew Spott 432d0de2241SAndrew Spott PetscFunctionBegin; 433bf477422SJose E. Roman PetscCall(MatShellGetContext(N, &A)); 434bf477422SJose E. Roman PetscCall(MatDestroy(&A)); 4359566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatHermitianTransposeGetMat_C", NULL)); 436204606b3SStefano Zampini #if !defined(PETSC_USE_COMPLEX) 4379566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatTransposeGetMat_C", NULL)); 438204606b3SStefano Zampini #endif 439bf477422SJose E. Roman PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatProductSetFromOptions_anytype_C", NULL)); 440543844c4SJose E. Roman PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatShellSetContext_C", NULL)); 4411a1fb624SJose E. Roman PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatFactorGetSolverType_C", NULL)); 4421a1fb624SJose E. Roman PetscFunctionReturn(PETSC_SUCCESS); 4431a1fb624SJose E. Roman } 4441a1fb624SJose E. Roman 4451a1fb624SJose E. Roman static PetscErrorCode MatGetInfo_HT(Mat N, MatInfoType flag, MatInfo *info) 4461a1fb624SJose E. Roman { 4471a1fb624SJose E. Roman Mat A; 4481a1fb624SJose E. Roman 4491a1fb624SJose E. Roman PetscFunctionBegin; 4501a1fb624SJose E. Roman PetscCall(MatShellGetContext(N, &A)); 4511a1fb624SJose E. Roman PetscCall(MatGetInfo(A, flag, info)); 4521a1fb624SJose E. Roman PetscFunctionReturn(PETSC_SUCCESS); 4531a1fb624SJose E. Roman } 4541a1fb624SJose E. Roman 4551a1fb624SJose E. Roman static PetscErrorCode MatFactorGetSolverType_HT(Mat N, MatSolverType *type) 4561a1fb624SJose E. Roman { 4571a1fb624SJose E. Roman Mat A; 4581a1fb624SJose E. Roman 4591a1fb624SJose E. Roman PetscFunctionBegin; 4601a1fb624SJose E. Roman PetscCall(MatShellGetContext(N, &A)); 4611a1fb624SJose E. Roman PetscCall(MatFactorGetSolverType(A, type)); 4623ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 463d0de2241SAndrew Spott } 464d0de2241SAndrew Spott 46566976f2fSJacob Faibussowitsch static PetscErrorCode MatDuplicate_HT(Mat N, MatDuplicateOption op, Mat *m) 466d71ae5a4SJacob Faibussowitsch { 467a1f56445SPierre Jolivet Mat A, C; 468d0de2241SAndrew Spott 469d0de2241SAndrew Spott PetscFunctionBegin; 470bf477422SJose E. Roman PetscCall(MatShellGetContext(N, &A)); 471a1f56445SPierre Jolivet PetscCall(MatDuplicate(A, op, &C)); 472a1f56445SPierre Jolivet PetscCall(MatCreateHermitianTranspose(C, m)); 473c956ced0SPierre Jolivet if (op == MAT_COPY_VALUES) PetscCall(MatCopy(N, *m, SAME_NONZERO_PATTERN)); 474a1f56445SPierre Jolivet PetscCall(MatDestroy(&C)); 4753ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 476d0de2241SAndrew Spott } 477d0de2241SAndrew Spott 4782543984dSJose E. Roman static PetscErrorCode MatHasOperation_HT(Mat mat, MatOperation op, PetscBool *has) 4792543984dSJose E. Roman { 4802543984dSJose E. Roman Mat A; 4812543984dSJose E. Roman 4822543984dSJose E. Roman PetscFunctionBegin; 4832543984dSJose E. Roman PetscCall(MatShellGetContext(mat, &A)); 4842543984dSJose E. Roman *has = PETSC_FALSE; 4852543984dSJose E. Roman if (op == MATOP_MULT || op == MATOP_MULT_ADD) { 4862543984dSJose E. Roman PetscCall(MatHasOperation(A, MATOP_MULT_HERMITIAN_TRANSPOSE, has)); 4872543984dSJose E. Roman if (!*has) PetscCall(MatHasOperation(A, MATOP_MULT_TRANSPOSE, has)); 4882543984dSJose E. Roman } else if (op == MATOP_MULT_HERMITIAN_TRANSPOSE || op == MATOP_MULT_HERMITIAN_TRANS_ADD || op == MATOP_MULT_TRANSPOSE || op == MATOP_MULT_TRANSPOSE_ADD) { 4892543984dSJose E. Roman PetscCall(MatHasOperation(A, MATOP_MULT, has)); 4902543984dSJose E. Roman } else if (((void **)mat->ops)[op]) *has = PETSC_TRUE; 4912543984dSJose E. Roman PetscFunctionReturn(PETSC_SUCCESS); 4922543984dSJose E. Roman } 4932543984dSJose E. Roman 49466976f2fSJacob Faibussowitsch static PetscErrorCode MatHermitianTransposeGetMat_HT(Mat N, Mat *M) 495d71ae5a4SJacob Faibussowitsch { 49606511a5cSPierre Jolivet PetscFunctionBegin; 497bf477422SJose E. Roman PetscCall(MatShellGetContext(N, M)); 4983ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 49906511a5cSPierre Jolivet } 50006511a5cSPierre Jolivet 50106511a5cSPierre Jolivet /*@ 502013e2dc7SBarry Smith MatHermitianTransposeGetMat - Gets the `Mat` object stored inside a `MATHERMITIANTRANSPOSEVIRTUAL` 50306511a5cSPierre Jolivet 50420f4b53cSBarry Smith Logically Collective 50506511a5cSPierre Jolivet 50606511a5cSPierre Jolivet Input Parameter: 507013e2dc7SBarry Smith . A - the `MATHERMITIANTRANSPOSEVIRTUAL` matrix 50806511a5cSPierre Jolivet 50906511a5cSPierre Jolivet Output Parameter: 51006511a5cSPierre Jolivet . M - the matrix object stored inside A 51106511a5cSPierre Jolivet 51206511a5cSPierre Jolivet Level: intermediate 51306511a5cSPierre Jolivet 5141cc06b55SBarry Smith .seealso: [](ch_matrices), `Mat`, `MATHERMITIANTRANSPOSEVIRTUAL`, `MatCreateHermitianTranspose()` 51506511a5cSPierre Jolivet @*/ 516d71ae5a4SJacob Faibussowitsch PetscErrorCode MatHermitianTransposeGetMat(Mat A, Mat *M) 517d71ae5a4SJacob Faibussowitsch { 51806511a5cSPierre Jolivet PetscFunctionBegin; 51906511a5cSPierre Jolivet PetscValidHeaderSpecific(A, MAT_CLASSID, 1); 52006511a5cSPierre Jolivet PetscValidType(A, 1); 5214f572ea9SToby Isaac PetscAssertPointer(M, 2); 522cac4c232SBarry Smith PetscUseMethod(A, "MatHermitianTransposeGetMat_C", (Mat, Mat *), (A, M)); 5233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 52406511a5cSPierre Jolivet } 52506511a5cSPierre Jolivet 526bf477422SJose E. Roman static PetscErrorCode MatGetDiagonal_HT(Mat N, Vec v) 527d71ae5a4SJacob Faibussowitsch { 528bf477422SJose E. Roman Mat A; 529a0eea678SPierre Jolivet 530a0eea678SPierre Jolivet PetscFunctionBegin; 531bf477422SJose E. Roman PetscCall(MatShellGetContext(N, &A)); 532bf477422SJose E. Roman PetscCall(MatGetDiagonal(A, v)); 5339566063dSJacob Faibussowitsch PetscCall(VecConjugate(v)); 5343ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 535a0eea678SPierre Jolivet } 536a0eea678SPierre Jolivet 537a1f56445SPierre Jolivet static PetscErrorCode MatCopy_HT(Mat A, Mat B, MatStructure str) 538a1f56445SPierre Jolivet { 539a1f56445SPierre Jolivet Mat a, b; 540a1f56445SPierre Jolivet 541a1f56445SPierre Jolivet PetscFunctionBegin; 542a1f56445SPierre Jolivet PetscCall(MatShellGetContext(A, &a)); 543a1f56445SPierre Jolivet PetscCall(MatShellGetContext(B, &b)); 544a1f56445SPierre Jolivet PetscCall(MatCopy(a, b, str)); 545a1f56445SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS); 546a1f56445SPierre Jolivet } 547a1f56445SPierre Jolivet 548bf477422SJose E. Roman static PetscErrorCode MatConvert_HT(Mat N, MatType newtype, MatReuse reuse, Mat *newmat) 549d71ae5a4SJacob Faibussowitsch { 550bf477422SJose E. Roman Mat A; 551b22c5e46SPierre Jolivet PetscScalar vscale = 1.0, vshift = 0.0; 5526a4403aaSStefano Zampini PetscBool flg; 553a0eea678SPierre Jolivet 554a0eea678SPierre Jolivet PetscFunctionBegin; 555bf477422SJose E. Roman PetscCall(MatShellGetContext(N, &A)); 556bf477422SJose E. Roman PetscCall(MatHasOperation(A, MATOP_HERMITIAN_TRANSPOSE, &flg)); 557b22c5e46SPierre Jolivet if (flg || N->ops->getrow) { /* if this condition is false, MatConvert_Shell() will be called in MatConvert_Basic(), so the following checks are not needed */ 558b9c875b8SPierre Jolivet PetscCall(MatShellGetScalingShifts(N, &vshift, &vscale, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Mat *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED)); 559b22c5e46SPierre Jolivet } 5606a4403aaSStefano Zampini if (flg) { 5616a4403aaSStefano Zampini Mat B; 5626a4403aaSStefano Zampini 563bf477422SJose E. Roman PetscCall(MatHermitianTranspose(A, MAT_INITIAL_MATRIX, &B)); 564ff83db7bSPierre Jolivet if (reuse != MAT_INPLACE_MATRIX) { 5659566063dSJacob Faibussowitsch PetscCall(MatConvert(B, newtype, reuse, newmat)); 5669566063dSJacob Faibussowitsch PetscCall(MatDestroy(&B)); 567ff83db7bSPierre Jolivet } else { 5689566063dSJacob Faibussowitsch PetscCall(MatConvert(B, newtype, MAT_INPLACE_MATRIX, &B)); 569bf477422SJose E. Roman PetscCall(MatHeaderReplace(N, &B)); 570ff83db7bSPierre Jolivet } 5716a4403aaSStefano Zampini } else { /* use basic converter as fallback */ 572b22c5e46SPierre Jolivet flg = (PetscBool)(N->ops->getrow != NULL); 573bf477422SJose E. Roman PetscCall(MatConvert_Basic(N, newtype, reuse, newmat)); 5746a4403aaSStefano Zampini } 575b22c5e46SPierre Jolivet if (flg) { 576b22c5e46SPierre Jolivet PetscCall(MatScale(*newmat, vscale)); 577b22c5e46SPierre Jolivet PetscCall(MatShift(*newmat, vshift)); 578b22c5e46SPierre Jolivet } 5793ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 580a0eea678SPierre Jolivet } 581a0eea678SPierre Jolivet 58211a5261eSBarry Smith /*MC 583013e2dc7SBarry Smith MATHERMITIANTRANSPOSEVIRTUAL - "hermitiantranspose" - A matrix type that represents a virtual transpose of a matrix 584d0de2241SAndrew Spott 58511a5261eSBarry Smith Level: advanced 58611a5261eSBarry Smith 587a1f56445SPierre Jolivet Developer Notes: 588543844c4SJose E. Roman This is implemented on top of `MATSHELL` to get support for scaling and shifting without requiring duplicate code 589543844c4SJose E. Roman 590543844c4SJose E. Roman Users can not call `MatShellSetOperation()` operations on this class, there is some error checking for that incorrect usage 591543844c4SJose E. Roman 5921cc06b55SBarry Smith .seealso: [](ch_matrices), `Mat`, `MATTRANSPOSEVIRTUAL`, `Mat`, `MatCreateHermitianTranspose()`, `MatCreateTranspose()` 59311a5261eSBarry Smith M*/ 59411a5261eSBarry Smith 59511a5261eSBarry Smith /*@ 596013e2dc7SBarry Smith MatCreateHermitianTranspose - Creates a new matrix object of `MatType` `MATHERMITIANTRANSPOSEVIRTUAL` that behaves like A'* 59711a5261eSBarry Smith 598c3339decSBarry Smith Collective 599d0de2241SAndrew Spott 600d0de2241SAndrew Spott Input Parameter: 601d0de2241SAndrew Spott . A - the (possibly rectangular) matrix 602d0de2241SAndrew Spott 603d0de2241SAndrew Spott Output Parameter: 604d0de2241SAndrew Spott . N - the matrix that represents A'* 605d0de2241SAndrew Spott 606d0de2241SAndrew Spott Level: intermediate 607d0de2241SAndrew Spott 60811a5261eSBarry Smith Note: 60911a5261eSBarry Smith The Hermitian transpose A' is NOT actually formed! Rather the new matrix 61011a5261eSBarry Smith object performs the matrix-vector product, `MatMult()`, by using the `MatMultHermitianTranspose()` on 611d0de2241SAndrew Spott the original matrix 612d0de2241SAndrew Spott 6131cc06b55SBarry Smith .seealso: [](ch_matrices), `Mat`, `MatCreateNormal()`, `MatMult()`, `MatMultHermitianTranspose()`, `MatCreate()`, 6142ef1f0ffSBarry Smith `MATTRANSPOSEVIRTUAL`, `MatCreateTranspose()`, `MatHermitianTransposeGetMat()`, `MATNORMAL`, `MATNORMALHERMITIAN` 615d0de2241SAndrew Spott @*/ 616d71ae5a4SJacob Faibussowitsch PetscErrorCode MatCreateHermitianTranspose(Mat A, Mat *N) 617d71ae5a4SJacob Faibussowitsch { 618487d878eSStefano Zampini VecType vtype; 619d0de2241SAndrew Spott 620d0de2241SAndrew Spott PetscFunctionBegin; 6219566063dSJacob Faibussowitsch PetscCall(MatCreate(PetscObjectComm((PetscObject)A), N)); 62287971105SStefano Zampini PetscCall(PetscLayoutReference(A->rmap, &((*N)->cmap))); 62387971105SStefano Zampini PetscCall(PetscLayoutReference(A->cmap, &((*N)->rmap))); 624bf477422SJose E. Roman PetscCall(MatSetType(*N, MATSHELL)); 625bf477422SJose E. Roman PetscCall(MatShellSetContext(*N, A)); 6269566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)A)); 627d0de2241SAndrew Spott 62858b7e2c1SStefano Zampini PetscCall(MatSetBlockSizes(*N, A->cmap->bs, A->rmap->bs)); 6299566063dSJacob Faibussowitsch PetscCall(MatGetVecType(A, &vtype)); 6309566063dSJacob Faibussowitsch PetscCall(MatSetVecType(*N, vtype)); 6312487f3f2SStefano Zampini #if defined(PETSC_HAVE_DEVICE) 6329566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(*N, A->boundtocpu)); 6332487f3f2SStefano Zampini #endif 6349566063dSJacob Faibussowitsch PetscCall(MatSetUp(*N)); 635bf477422SJose E. Roman 63657d50842SBarry Smith PetscCall(MatShellSetOperation(*N, MATOP_DESTROY, (PetscErrorCodeFn *)MatDestroy_HT)); 63757d50842SBarry Smith PetscCall(MatShellSetOperation(*N, MATOP_MULT, (PetscErrorCodeFn *)MatMult_HT)); 63857d50842SBarry Smith PetscCall(MatShellSetOperation(*N, MATOP_MULT_HERMITIAN_TRANSPOSE, (PetscErrorCodeFn *)MatMultHermitianTranspose_HT)); 639bf477422SJose E. Roman #if !defined(PETSC_USE_COMPLEX) 64057d50842SBarry Smith PetscCall(MatShellSetOperation(*N, MATOP_MULT_TRANSPOSE, (PetscErrorCodeFn *)MatMultHermitianTranspose_HT)); 641bf477422SJose E. Roman #endif 64257d50842SBarry Smith PetscCall(MatShellSetOperation(*N, MATOP_LUFACTOR, (PetscErrorCodeFn *)MatLUFactor_HT)); 64357d50842SBarry Smith PetscCall(MatShellSetOperation(*N, MATOP_CHOLESKYFACTOR, (PetscErrorCodeFn *)MatCholeskyFactor_HT)); 64457d50842SBarry Smith PetscCall(MatShellSetOperation(*N, MATOP_GET_FACTOR, (PetscErrorCodeFn *)MatGetFactor_HT)); 64557d50842SBarry Smith PetscCall(MatShellSetOperation(*N, MATOP_GETINFO, (PetscErrorCodeFn *)MatGetInfo_HT)); 64657d50842SBarry Smith PetscCall(MatShellSetOperation(*N, MATOP_DUPLICATE, (PetscErrorCodeFn *)MatDuplicate_HT)); 64757d50842SBarry Smith PetscCall(MatShellSetOperation(*N, MATOP_HAS_OPERATION, (PetscErrorCodeFn *)MatHasOperation_HT)); 64857d50842SBarry Smith PetscCall(MatShellSetOperation(*N, MATOP_GET_DIAGONAL, (PetscErrorCodeFn *)MatGetDiagonal_HT)); 64957d50842SBarry Smith PetscCall(MatShellSetOperation(*N, MATOP_COPY, (PetscErrorCodeFn *)MatCopy_HT)); 65057d50842SBarry Smith PetscCall(MatShellSetOperation(*N, MATOP_CONVERT, (PetscErrorCodeFn *)MatConvert_HT)); 651bf477422SJose E. Roman 652543844c4SJose E. Roman PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatHermitianTransposeGetMat_C", MatHermitianTransposeGetMat_HT)); 653bf477422SJose E. Roman #if !defined(PETSC_USE_COMPLEX) 654543844c4SJose E. Roman PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatTransposeGetMat_C", MatHermitianTransposeGetMat_HT)); 655bf477422SJose E. Roman #endif 656543844c4SJose E. Roman PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatProductSetFromOptions_anytype_C", MatProductSetFromOptions_HT)); 6571a1fb624SJose E. Roman PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatFactorGetSolverType_C", MatFactorGetSolverType_HT)); 658a1f56445SPierre Jolivet PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatShellSetContext_C", MatShellSetContext_Immutable)); 659a1f56445SPierre Jolivet PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatShellSetContextDestroy_C", MatShellSetContextDestroy_Immutable)); 660a1f56445SPierre Jolivet PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatShellSetManageScalingShifts_C", MatShellSetManageScalingShifts_Immutable)); 661bf477422SJose E. Roman PetscCall(PetscObjectChangeTypeName((PetscObject)*N, MATHERMITIANTRANSPOSEVIRTUAL)); 6623ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 663d0de2241SAndrew Spott } 664