Actual source code: mmaij.c

petsc-3.3-p7 2013-05-11
  2: /*
  3:    Support for the parallel AIJ matrix vector multiply
  4: */
  5: #include <../src/mat/impls/aij/mpi/mpiaij.h>

  9: PetscErrorCode MatSetUpMultiply_MPIAIJ(Mat mat)
 10: {
 11:   Mat_MPIAIJ         *aij = (Mat_MPIAIJ*)mat->data;
 12:   Mat_SeqAIJ         *B = (Mat_SeqAIJ*)(aij->B->data);
 13:   PetscErrorCode     ierr;
 14:   PetscInt           i,j,*aj = B->j,ec = 0,*garray;
 15:   IS                 from,to;
 16:   Vec                gvec;
 17: #if defined (PETSC_USE_CTABLE)
 18:   PetscTable         gid1_lid1;
 19:   PetscTablePosition tpos;
 20:   PetscInt           gid,lid;
 21: #else
 22:   PetscInt           N = mat->cmap->N,*indices;
 23: #endif


 27: #if defined (PETSC_USE_CTABLE)
 28:   /* use a table */
 29:   PetscTableCreate(aij->B->rmap->n,mat->cmap->N+1,&gid1_lid1);
 30:   for (i=0; i<aij->B->rmap->n; i++) {
 31:     for (j=0; j<B->ilen[i]; j++) {
 32:       PetscInt data,gid1 = aj[B->i[i] + j] + 1;
 33:       PetscTableFind(gid1_lid1,gid1,&data);
 34:       if (!data) {
 35:         /* one based table */
 36:         PetscTableAdd(gid1_lid1,gid1,++ec,INSERT_VALUES);
 37:       }
 38:     }
 39:   }
 40:   /* form array of columns we need */
 41:   PetscMalloc((ec+1)*sizeof(PetscInt),&garray);
 42:   PetscTableGetHeadPosition(gid1_lid1,&tpos);
 43:   while (tpos) {
 44:     PetscTableGetNext(gid1_lid1,&tpos,&gid,&lid);
 45:     gid--;
 46:     lid--;
 47:     garray[lid] = gid;
 48:   }
 49:   PetscSortInt(ec,garray); /* sort, and rebuild */
 50:   PetscTableRemoveAll(gid1_lid1);
 51:   for (i=0; i<ec; i++) {
 52:     PetscTableAdd(gid1_lid1,garray[i]+1,i+1,INSERT_VALUES);
 53:   }
 54:   /* compact out the extra columns in B */
 55:   for (i=0; i<aij->B->rmap->n; i++) {
 56:     for (j=0; j<B->ilen[i]; j++) {
 57:       PetscInt gid1 = aj[B->i[i] + j] + 1;
 58:       PetscTableFind(gid1_lid1,gid1,&lid);
 59:       lid --;
 60:       aj[B->i[i] + j]  = lid;
 61:     }
 62:   }
 63:   aij->B->cmap->n = aij->B->cmap->N = ec;
 64:   PetscLayoutSetUp((aij->B->cmap));
 65:   PetscTableDestroy(&gid1_lid1);
 66: #else
 67:   /* Make an array as long as the number of columns */
 68:   /* mark those columns that are in aij->B */
 69:   PetscMalloc((N+1)*sizeof(PetscInt),&indices);
 70:   PetscMemzero(indices,N*sizeof(PetscInt));
 71:   for (i=0; i<aij->B->rmap->n; i++) {
 72:     for (j=0; j<B->ilen[i]; j++) {
 73:       if (!indices[aj[B->i[i] + j] ]) ec++;
 74:       indices[aj[B->i[i] + j] ] = 1;
 75:     }
 76:   }

 78:   /* form array of columns we need */
 79:   PetscMalloc((ec+1)*sizeof(PetscInt),&garray);
 80:   ec = 0;
 81:   for (i=0; i<N; i++) {
 82:     if (indices[i]) garray[ec++] = i;
 83:   }

 85:   /* make indices now point into garray */
 86:   for (i=0; i<ec; i++) {
 87:     indices[garray[i]] = i;
 88:   }

 90:   /* compact out the extra columns in B */
 91:   for (i=0; i<aij->B->rmap->n; i++) {
 92:     for (j=0; j<B->ilen[i]; j++) {
 93:       aj[B->i[i] + j] = indices[aj[B->i[i] + j]];
 94:     }
 95:   }
 96:   aij->B->cmap->n = aij->B->cmap->N = ec;
 97:   PetscLayoutSetUp((aij->B->cmap));
 98:   PetscFree(indices);
 99: #endif  
100:   /* create local vector that is used to scatter into */
101:   VecCreateSeq(PETSC_COMM_SELF,ec,&aij->lvec);

103:   /* create two temporary Index sets for build scatter gather */
104:   ISCreateGeneral(((PetscObject)mat)->comm,ec,garray,PETSC_COPY_VALUES,&from);

106:   ISCreateStride(PETSC_COMM_SELF,ec,0,1,&to);

108:   /* create temporary global vector to generate scatter context */
109:   /* This does not allocate the array's memory so is efficient */
110:   VecCreateMPIWithArray(((PetscObject)mat)->comm,1,mat->cmap->n,mat->cmap->N,PETSC_NULL,&gvec);

112:   /* generate the scatter context */
113:   VecScatterCreate(gvec,from,aij->lvec,to,&aij->Mvctx);
114:   PetscLogObjectParent(mat,aij->Mvctx);
115:   PetscLogObjectParent(mat,aij->lvec);
116:   PetscLogObjectParent(mat,from);
117:   PetscLogObjectParent(mat,to);
118:   aij->garray = garray;
119:   PetscLogObjectMemory(mat,(ec+1)*sizeof(PetscInt));
120:   ISDestroy(&from);
121:   ISDestroy(&to);
122:   VecDestroy(&gvec);
123:   return(0);
124: }


129: /*
130:      Takes the local part of an already assembled MPIAIJ matrix
131:    and disassembles it. This is to allow new nonzeros into the matrix
132:    that require more communication in the matrix vector multiply. 
133:    Thus certain data-structures must be rebuilt.

135:    Kind of slow! But that's what application programmers get when 
136:    they are sloppy.
137: */
138: PetscErrorCode MatDisAssemble_MPIAIJ(Mat A)
139: {
140:   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)A->data;
141:   Mat            B = aij->B,Bnew;
142:   Mat_SeqAIJ     *Baij = (Mat_SeqAIJ*)B->data;
144:   PetscInt       i,j,m = B->rmap->n,n = A->cmap->N,col,ct = 0,*garray = aij->garray,*nz,ec;
145:   PetscScalar    v;

148:   /* free stuff related to matrix-vec multiply */
149:   VecGetSize(aij->lvec,&ec); /* needed for PetscLogObjectMemory below */
150:   VecDestroy(&aij->lvec);
151:   VecScatterDestroy(&aij->Mvctx);
152:   if (aij->colmap) {
153: #if defined (PETSC_USE_CTABLE)
154:     PetscTableDestroy(&aij->colmap);
155: #else
156:     PetscFree(aij->colmap);
157:     PetscLogObjectMemory(A,-aij->B->cmap->n*sizeof(PetscInt));
158: #endif
159:   }

161:   /* make sure that B is assembled so we can access its values */
162:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
163:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

165:   /* invent new B and copy stuff over */
166:   PetscMalloc((m+1)*sizeof(PetscInt),&nz);
167:   for (i=0; i<m; i++) {
168:     nz[i] = Baij->i[i+1] - Baij->i[i];
169:   }
170:   MatCreate(PETSC_COMM_SELF,&Bnew);
171:   MatSetSizes(Bnew,m,n,m,n);
172:   MatSetBlockSizes(Bnew,A->rmap->bs,A->cmap->bs);
173:   MatSetType(Bnew,((PetscObject)B)->type_name);
174:   MatSeqAIJSetPreallocation(Bnew,0,nz);
175:   ((Mat_SeqAIJ*)Bnew->data)->nonew = Baij->nonew; /* Inherit insertion error options. */
176:   PetscFree(nz);
177:   for (i=0; i<m; i++) {
178:     for (j=Baij->i[i]; j<Baij->i[i+1]; j++) {
179:       col  = garray[Baij->j[ct]];
180:       v    = Baij->a[ct++];
181:       MatSetValues(Bnew,1,&i,1,&col,&v,B->insertmode);
182:     }
183:   }
184:   PetscFree(aij->garray);
185:   PetscLogObjectMemory(A,-ec*sizeof(PetscInt));
186:   MatDestroy(&B);
187:   PetscLogObjectParent(A,Bnew);
188:   aij->B = Bnew;
189:   A->was_assembled = PETSC_FALSE;
190:   return(0);
191: }

193: /*      ugly stuff added for Glenn someday we should fix this up */

195: static PetscInt *auglyrmapd = 0,*auglyrmapo = 0;  /* mapping from the local ordering to the "diagonal" and "off-diagonal"
196:                                       parts of the local matrix */
197: static Vec auglydd = 0,auglyoo = 0;   /* work vectors used to scale the two parts of the local matrix */


202: PetscErrorCode MatMPIAIJDiagonalScaleLocalSetUp(Mat inA,Vec scale)
203: {
204:   Mat_MPIAIJ     *ina = (Mat_MPIAIJ*) inA->data; /*access private part of matrix */
206:   PetscInt       i,n,nt,cstart,cend,no,*garray = ina->garray,*lindices;
207:   PetscInt       *r_rmapd,*r_rmapo;
208: 
210:   MatGetOwnershipRange(inA,&cstart,&cend);
211:   MatGetSize(ina->A,PETSC_NULL,&n);
212:   PetscMalloc((inA->rmap->mapping->n+1)*sizeof(PetscInt),&r_rmapd);
213:   PetscMemzero(r_rmapd,inA->rmap->mapping->n*sizeof(PetscInt));
214:   nt   = 0;
215:   for (i=0; i<inA->rmap->mapping->n; i++) {
216:     if (inA->rmap->mapping->indices[i] >= cstart && inA->rmap->mapping->indices[i] < cend) {
217:       nt++;
218:       r_rmapd[i] = inA->rmap->mapping->indices[i] + 1;
219:     }
220:   }
221:   if (nt != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Hmm nt %D n %D",nt,n);
222:   PetscMalloc((n+1)*sizeof(PetscInt),&auglyrmapd);
223:   for (i=0; i<inA->rmap->mapping->n; i++) {
224:     if (r_rmapd[i]){
225:       auglyrmapd[(r_rmapd[i]-1)-cstart] = i;
226:     }
227:   }
228:   PetscFree(r_rmapd);
229:   VecCreateSeq(PETSC_COMM_SELF,n,&auglydd);

231:   PetscMalloc((inA->cmap->N+1)*sizeof(PetscInt),&lindices);
232:   PetscMemzero(lindices,inA->cmap->N*sizeof(PetscInt));
233:   for (i=0; i<ina->B->cmap->n; i++) {
234:     lindices[garray[i]] = i+1;
235:   }
236:   no   = inA->rmap->mapping->n - nt;
237:   PetscMalloc((inA->rmap->mapping->n+1)*sizeof(PetscInt),&r_rmapo);
238:   PetscMemzero(r_rmapo,inA->rmap->mapping->n*sizeof(PetscInt));
239:   nt   = 0;
240:   for (i=0; i<inA->rmap->mapping->n; i++) {
241:     if (lindices[inA->rmap->mapping->indices[i]]) {
242:       nt++;
243:       r_rmapo[i] = lindices[inA->rmap->mapping->indices[i]];
244:     }
245:   }
246:   if (nt > no) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Hmm nt %D no %D",nt,n);
247:   PetscFree(lindices);
248:   PetscMalloc((nt+1)*sizeof(PetscInt),&auglyrmapo);
249:   for (i=0; i<inA->rmap->mapping->n; i++) {
250:     if (r_rmapo[i]){
251:       auglyrmapo[(r_rmapo[i]-1)] = i;
252:     }
253:   }
254:   PetscFree(r_rmapo);
255:   VecCreateSeq(PETSC_COMM_SELF,nt,&auglyoo);

257:   return(0);
258: }

262: PetscErrorCode MatMPIAIJDiagonalScaleLocal(Mat A,Vec scale)
263: {
264:   /* This routine should really be abandoned as it duplicates MatDiagonalScaleLocal */

268:   PetscTryMethod(A,"MatDiagonalScaleLocal_C",(Mat,Vec),(A,scale));
269:   return(0);
270: }

272: EXTERN_C_BEGIN
275: PetscErrorCode  MatDiagonalScaleLocal_MPIAIJ(Mat A,Vec scale)
276: {
277:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*) A->data; /*access private part of matrix */
279:   PetscInt       n,i;
280:   PetscScalar    *d,*o,*s;
281: 
283:   if (!auglyrmapd) {
284:     MatMPIAIJDiagonalScaleLocalSetUp(A,scale);
285:   }

287:   VecGetArray(scale,&s);
288: 
289:   VecGetLocalSize(auglydd,&n);
290:   VecGetArray(auglydd,&d);
291:   for (i=0; i<n; i++) {
292:     d[i] = s[auglyrmapd[i]]; /* copy "diagonal" (true local) portion of scale into dd vector */
293:   }
294:   VecRestoreArray(auglydd,&d);
295:   /* column scale "diagonal" portion of local matrix */
296:   MatDiagonalScale(a->A,PETSC_NULL,auglydd);

298:   VecGetLocalSize(auglyoo,&n);
299:   VecGetArray(auglyoo,&o);
300:   for (i=0; i<n; i++) {
301:     o[i] = s[auglyrmapo[i]]; /* copy "off-diagonal" portion of scale into oo vector */
302:   }
303:   VecRestoreArray(scale,&s);
304:   VecRestoreArray(auglyoo,&o);
305:   /* column scale "off-diagonal" portion of local matrix */
306:   MatDiagonalScale(a->B,PETSC_NULL,auglyoo);

308:   return(0);
309: }
310: EXTERN_C_END