Actual source code: transm.c

petsc-master 2019-12-10
Report Typos and Errors

  2:  #include <petsc/private/matimpl.h>

  4: typedef struct {
  5:   Mat A;
  6: } Mat_Transpose;

  8: PetscErrorCode MatMult_Transpose(Mat N,Vec x,Vec y)
  9: {
 10:   Mat_Transpose  *Na = (Mat_Transpose*)N->data;

 14:   MatMultTranspose(Na->A,x,y);
 15:   return(0);
 16: }

 18: PetscErrorCode MatMultAdd_Transpose(Mat N,Vec v1,Vec v2,Vec v3)
 19: {
 20:   Mat_Transpose  *Na = (Mat_Transpose*)N->data;

 24:   MatMultTransposeAdd(Na->A,v1,v2,v3);
 25:   return(0);
 26: }

 28: PetscErrorCode MatMultTranspose_Transpose(Mat N,Vec x,Vec y)
 29: {
 30:   Mat_Transpose  *Na = (Mat_Transpose*)N->data;

 34:   MatMult(Na->A,x,y);
 35:   return(0);
 36: }

 38: PetscErrorCode MatMultTransposeAdd_Transpose(Mat N,Vec v1,Vec v2,Vec v3)
 39: {
 40:   Mat_Transpose  *Na = (Mat_Transpose*)N->data;

 44:   MatMultAdd(Na->A,v1,v2,v3);
 45:   return(0);
 46: }

 48: PetscErrorCode MatDestroy_Transpose(Mat N)
 49: {
 50:   Mat_Transpose  *Na = (Mat_Transpose*)N->data;

 54:   MatDestroy(&Na->A);
 55:   PetscObjectComposeFunction((PetscObject)N,"MatTransposeGetMat_C",NULL);
 56:   PetscFree(N->data);
 57:   return(0);
 58: }

 60: PetscErrorCode MatDuplicate_Transpose(Mat N, MatDuplicateOption op, Mat* m)
 61: {
 62:   Mat_Transpose  *Na = (Mat_Transpose*)N->data;

 66:   if (op == MAT_COPY_VALUES) {
 67:     MatTranspose(Na->A,MAT_INITIAL_MATRIX,m);
 68:   } else if (op == MAT_DO_NOT_COPY_VALUES) {
 69:     MatDuplicate(Na->A,MAT_DO_NOT_COPY_VALUES,m);
 70:     MatTranspose(*m,MAT_INPLACE_MATRIX,m);
 71:   } else SETERRQ(PetscObjectComm((PetscObject)N),PETSC_ERR_SUP,"MAT_SHARE_NONZERO_PATTERN not supported for this matrix type");
 72:   return(0);
 73: }

 75: PetscErrorCode MatCreateVecs_Transpose(Mat A,Vec *r, Vec *l)
 76: {
 77:   Mat_Transpose  *Aa = (Mat_Transpose*)A->data;

 81:   MatCreateVecs(Aa->A,l,r);
 82:   return(0);
 83: }

 85: PetscErrorCode MatAXPY_Transpose(Mat Y,PetscScalar a,Mat X,MatStructure str)
 86: {
 87:   Mat_Transpose  *Ya = (Mat_Transpose*)Y->data;
 88:   Mat_Transpose  *Xa = (Mat_Transpose*)X->data;
 89:   Mat              M = Ya->A;
 90:   Mat              N = Xa->A;

 94:   MatAXPY(M,a,N,str);
 95:   return(0);
 96: }

 98: PetscErrorCode MatHasOperation_Transpose(Mat mat,MatOperation op,PetscBool *has)
 99: {
100:   Mat_Transpose  *X = (Mat_Transpose*)mat->data;

104:   *has = PETSC_FALSE;
105:   if (op == MATOP_MAT_MULT || op == MATOP_TRANSPOSE_MAT_MULT) {
106:     MatHasOperation(X->A,op == MATOP_MAT_MULT ? MATOP_TRANSPOSE_MAT_MULT : MATOP_MAT_MULT,has);
107:   }
108:   else if (((void**)mat->ops)[op]) *has = PETSC_TRUE;
109:   return(0);
110: }

112: PetscErrorCode MatTransposeGetMat_Transpose(Mat A,Mat *M)
113: {
114:   Mat_Transpose  *Aa = (Mat_Transpose*)A->data;

117:   *M = Aa->A;
118:   return(0);
119: }

121: /*@
122:       MatTransposeGetMat - Gets the Mat object stored inside a MATTRANSPOSEMAT

124:    Logically collective on Mat

126:    Input Parameter:
127: .   A  - the MATTRANSPOSE matrix

129:    Output Parameter:
130: .   M - the matrix object stored inside A

132:    Level: intermediate

134: .seealso: MatCreateTranspose()

136: @*/
137: PetscErrorCode MatTransposeGetMat(Mat A,Mat *M)
138: {

145:   PetscUseMethod(A,"MatTransposeGetMat_C",(Mat,Mat*),(A,M));
146:   return(0);
147: }

149: /*@
150:       MatCreateTranspose - Creates a new matrix object that behaves like A'

152:    Collective on Mat

154:    Input Parameter:
155: .   A  - the (possibly rectangular) matrix

157:    Output Parameter:
158: .   N - the matrix that represents A'

160:    Level: intermediate

162:    Notes:
163:     The transpose A' is NOT actually formed! Rather the new matrix
164:           object performs the matrix-vector product by using the MatMultTranspose() on
165:           the original matrix

167: .seealso: MatCreateNormal(), MatMult(), MatMultTranspose(), MatCreate()

169: @*/
170: PetscErrorCode  MatCreateTranspose(Mat A,Mat *N)
171: {
173:   PetscInt       m,n;
174:   Mat_Transpose  *Na;

177:   MatGetLocalSize(A,&m,&n);
178:   MatCreate(PetscObjectComm((PetscObject)A),N);
179:   MatSetSizes(*N,n,m,PETSC_DECIDE,PETSC_DECIDE);
180:   PetscLayoutSetUp((*N)->rmap);
181:   PetscLayoutSetUp((*N)->cmap);
182:   PetscObjectChangeTypeName((PetscObject)*N,MATTRANSPOSEMAT);

184:   PetscNewLog(*N,&Na);
185:   (*N)->data = (void*) Na;
186:   PetscObjectReference((PetscObject)A);
187:   Na->A      = A;

189:   (*N)->ops->destroy          = MatDestroy_Transpose;
190:   (*N)->ops->mult             = MatMult_Transpose;
191:   (*N)->ops->multadd          = MatMultAdd_Transpose;
192:   (*N)->ops->multtranspose    = MatMultTranspose_Transpose;
193:   (*N)->ops->multtransposeadd = MatMultTransposeAdd_Transpose;
194:   (*N)->ops->duplicate        = MatDuplicate_Transpose;
195:   (*N)->ops->getvecs          = MatCreateVecs_Transpose;
196:   (*N)->ops->axpy             = MatAXPY_Transpose;
197:   (*N)->ops->hasoperation     = MatHasOperation_Transpose;
198:   (*N)->assembled             = PETSC_TRUE;

200:   PetscObjectComposeFunction((PetscObject)(*N),"MatTransposeGetMat_C",MatTransposeGetMat_Transpose);
201:   MatSetBlockSizes(*N,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));
202:   MatSetUp(*N);
203:   return(0);
204: }