Actual source code: mscatter.c

  1: #define PETSCMAT_DLL

  3: /*
  4:    This provides a matrix that applies a VecScatter to a vector.
  5: */

 7:  #include private/matimpl.h
 8:  #include private/vecimpl.h

 10: typedef struct {
 11:   VecScatter scatter;
 12: } Mat_Scatter;

 16: /*@
 17:     MatScatterGetVecScatter - Returns the user-provided scatter set with MatScatterSetVecScatter()

 19:     Not Collective, but not cannot use scatter if not used collectively on Mat

 21:     Input Parameter:
 22: .   mat - the matrix, should have been created with MatCreateScatter() or have type MATSCATTER

 24:     Output Parameter:
 25: .   scatter - the scatter context

 27:     Level: intermediate

 29: .keywords: matrix, scatter, get

 31: .seealso: MatCreateScatter(), MatScatterSetVecScatter(), MATSCATTER
 32: @*/
 33: PetscErrorCode  MatScatterGetVecScatter(Mat mat,VecScatter *scatter)
 34: {
 35:   Mat_Scatter    *mscatter;

 40:   mscatter = (Mat_Scatter*)mat->data;
 41:   *scatter = mscatter->scatter;
 42:   return(0);
 43: }

 47: PetscErrorCode MatDestroy_Scatter(Mat mat)
 48: {
 50:   Mat_Scatter    *scatter = (Mat_Scatter*)mat->data;

 53:   if (scatter->scatter) {VecScatterDestroy(scatter->scatter);}
 54:   PetscFree(scatter);
 55:   return(0);
 56: }

 60: PetscErrorCode MatMult_Scatter(Mat A,Vec x,Vec y)
 61: {
 62:   Mat_Scatter    *scatter = (Mat_Scatter*)A->data;

 66:   if (!scatter->scatter) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Need to first call MatScatterSetScatter()");
 67:   VecScatterBegin(scatter->scatter,x,y,INSERT_VALUES,SCATTER_FORWARD);
 68:   VecScatterEnd(scatter->scatter,x,y,INSERT_VALUES,SCATTER_FORWARD);
 69:   return(0);
 70: }

 74: PetscErrorCode MatMultAdd_Scatter(Mat A,Vec x,Vec y,Vec z)
 75: {
 76:   Mat_Scatter    *scatter = (Mat_Scatter*)A->data;

 80:   if (!scatter->scatter) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Need to first call MatScatterSetScatter()");
 81:   if (z != y) {VecCopy(y,z);}
 82:   VecScatterBegin(scatter->scatter,x,z,ADD_VALUES,SCATTER_FORWARD);
 83:   VecScatterEnd(scatter->scatter,x,z,ADD_VALUES,SCATTER_FORWARD);
 84:   return(0);
 85: }

 89: PetscErrorCode MatMultTranspose_Scatter(Mat A,Vec x,Vec y)
 90: {
 91:   Mat_Scatter    *scatter = (Mat_Scatter*)A->data;

 95:   if (!scatter->scatter) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Need to first call MatScatterSetScatter()");
 96:   VecScatterBegin(scatter->scatter,x,y,INSERT_VALUES,SCATTER_REVERSE);
 97:   VecScatterEnd(scatter->scatter,x,y,INSERT_VALUES,SCATTER_REVERSE);
 98:   return(0);
 99: }

103: PetscErrorCode MatMultTransposeAdd_Scatter(Mat A,Vec x,Vec y,Vec z)
104: {
105:   Mat_Scatter    *scatter = (Mat_Scatter*)A->data;

109:   if (!scatter->scatter) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Need to first call MatScatterSetScatter()");
110:   if (z != y) {VecCopy(y,z);}
111:   VecScatterBegin(scatter->scatter,x,z,ADD_VALUES,SCATTER_REVERSE);
112:   VecScatterEnd(scatter->scatter,x,z,ADD_VALUES,SCATTER_REVERSE);
113:   return(0);
114: }

116: static struct _MatOps MatOps_Values = {0,
117:        0,
118:        0,
119:        MatMult_Scatter,
120: /* 4*/ MatMultAdd_Scatter,
121:        MatMultTranspose_Scatter,
122:        MatMultTransposeAdd_Scatter,
123:        0,
124:        0,
125:        0,
126: /*10*/ 0,
127:        0,
128:        0,
129:        0,
130:        0,
131: /*15*/ 0,
132:        0,
133:        0,
134:        0,
135:        0,
136: /*20*/ 0,
137:        0,
138:        0,
139:        0,
140:        0,
141: /*25*/ 0,
142:        0,
143:        0,
144:        0,
145:        0,
146: /*30*/ 0,
147:        0,
148:        0,
149:        0,
150:        0,
151: /*35*/ 0,
152:        0,
153:        0,
154:        0,
155:        0,
156: /*40*/ 0,
157:        0,
158:        0,
159:        0,
160:        0,
161: /*45*/ 0,
162:        0,
163:        0,
164:        0,
165:        0,
166: /*50*/ 0,
167:        0,
168:        0,
169:        0,
170:        0,
171: /*55*/ 0,
172:        0,
173:        0,
174:        0,
175:        0,
176: /*60*/ 0,
177:        MatDestroy_Scatter,
178:        0,
179:        0,
180:        0,
181: /*65*/ 0,
182:        0,
183:        0,
184:        0,
185:        0,
186: /*70*/ 0,
187:        0,
188:        0,
189:        0,
190:        0,
191: /*75*/ 0,
192:        0,
193:        0,
194:        0,
195:        0,
196: /*80*/ 0,
197:        0,
198:        0,
199:        0,
200:        0,
201: /*85*/ 0,
202:        0,
203:        0,
204:        0,
205:        0,
206: /*90*/ 0,
207:        0,
208:        0,
209:        0,
210:        0,
211: /*95*/ 0,
212:        0,
213:        0,
214:        0};

216: /*MC
217:    MATSCATTER - MATSCATTER = "scatter" - A matrix type that simply applies a VecScatterBegin/End()

219:   Level: advanced

221: .seealso: MatCreateScatter(), MatScatterSetVecScatter(), MatScatterGetVecScatter()

223: M*/

228: PetscErrorCode  MatCreate_Scatter(Mat A)
229: {
230:   Mat_Scatter    *b;

234:   PetscMemcpy(A->ops,&MatOps_Values,sizeof(struct _MatOps));
235:   PetscNewLog(A,Mat_Scatter,&b);

237:   A->data = (void*)b;

239:   PetscMapSetBlockSize(A->rmap,1);
240:   PetscMapSetBlockSize(A->cmap,1);
241:   PetscMapSetUp(A->rmap);
242:   PetscMapSetUp(A->cmap);

244:   A->assembled     = PETSC_TRUE;
245:   A->preallocated  = PETSC_FALSE;
246:   return(0);
247: }

252: /*@C
253:    MatCreateScatter - Creates a new matrix based on a VecScatter

255:   Collective on MPI_Comm

257:    Input Parameters:
258: +  comm - MPI communicator
259: -  scatter - a VecScatterContext

261:    Output Parameter:
262: .  A - the matrix

264:    Level: intermediate

266:    PETSc requires that matrices and vectors being used for certain
267:    operations are partitioned accordingly.  For example, when
268:    creating a scatter matrix, A, that supports parallel matrix-vector
269:    products using MatMult(A,x,y) the user should set the number
270:    of local matrix rows to be the number of local elements of the
271:    corresponding result vector, y. Note that this is information is
272:    required for use of the matrix interface routines, even though
273:    the scatter matrix may not actually be physically partitioned.
274:    For example,

276: .keywords: matrix, scatter, create

278: .seealso: MatScatterSetVecScatter(), MatScatterGetVecScatter(), MATSCATTER
279: @*/
280: PetscErrorCode  MatCreateScatter(MPI_Comm comm,VecScatter scatter,Mat *A)
281: {

285:   MatCreate(comm,A);
286:   MatSetSizes(*A,scatter->to_n,scatter->from_n,PETSC_DETERMINE,PETSC_DETERMINE);
287:   MatSetType(*A,MATSCATTER);
288:   MatScatterSetVecScatter(*A,scatter);
289:   return(0);
290: }

294: /*@
295:     MatScatterSetVecScatter - sets that scatter that the matrix is to apply as its linear operator

297:    Collective on Mat

299:     Input Parameters:
300: +   mat - the scatter matrix
301: -   scatter - the scatter context create with VecScatterCreate()

303:    Level: advanced


306: .seealso: MatCreateScatter(), MATSCATTER
307: @*/
308: PetscErrorCode  MatScatterSetVecScatter(Mat mat,VecScatter scatter)
309: {
310:   Mat_Scatter    *mscatter = (Mat_Scatter*)mat->data;

317:   if (mat->rmap->n != scatter->to_n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Number of local rows in matrix %D not equal local scatter size %D",mat->rmap->n,scatter->to_n);
318:   if (mat->cmap->n != scatter->from_n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Number of local columns in matrix %D not equal local scatter size %D",mat->cmap->n,scatter->from_n);

320:   PetscObjectReference((PetscObject)scatter);
321:   if (mscatter->scatter) {VecScatterDestroy(mscatter->scatter);}
322:   mscatter->scatter = scatter;
323:   return(0);
324: }