Actual source code: mmbaij.c

  1: #define PETSCMAT_DLL

  3: /*
  4:    Support for the parallel BAIJ matrix vector multiply
  5: */
 6:  #include ../src/mat/impls/baij/mpi/mpibaij.h

  8: EXTERN PetscErrorCode MatSetValuesBlocked_SeqBAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[],const PetscScalar[],InsertMode);

 12: PetscErrorCode MatSetUpMultiply_MPIBAIJ(Mat mat)
 13: {
 14:   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
 15:   Mat_SeqBAIJ    *B = (Mat_SeqBAIJ*)(baij->B->data);
 17:   PetscInt       i,j,*aj = B->j,ec = 0,*garray;
 18:   PetscInt       bs = mat->rmap->bs,*stmp;
 19:   IS             from,to;
 20:   Vec            gvec;
 21: #if defined (PETSC_USE_CTABLE)
 22:   PetscTable     gid1_lid1;
 23:   PetscTablePosition tpos;
 24:   PetscInt       gid,lid;
 25: #else
 26:   PetscInt       Nbs = baij->Nbs,*indices;
 27: #endif  


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

 83:   /* form array of columns we need */
 84:   PetscMalloc((ec+1)*sizeof(PetscInt),&garray);
 85:   ec = 0;
 86:   for (i=0; i<Nbs; i++) {
 87:     if (indices[i]) {
 88:       garray[ec++] = i;
 89:     }
 90:   }

 92:   /* make indices now point into garray */
 93:   for (i=0; i<ec; i++) {
 94:     indices[garray[i]] = i;
 95:   }

 97:   /* compact out the extra columns in B */
 98:   for (i=0; i<B->mbs; i++) {
 99:     for (j=0; j<B->ilen[i]; j++) {
100:       aj[B->i[i] + j] = indices[aj[B->i[i] + j]];
101:     }
102:   }
103:   B->nbs       = ec;
104:   baij->B->cmap->n =baij->B->cmap->N  = ec*mat->rmap->bs;
105:   PetscLayoutSetUp((baij->B->cmap));
106:   PetscFree(indices);
107: #endif  

109:   /* create local vector that is used to scatter into */
110:   VecCreateSeq(PETSC_COMM_SELF,ec*bs,&baij->lvec);

112:   /* create two temporary index sets for building scatter-gather */
113:   for (i=0; i<ec; i++) {
114:     garray[i] = bs*garray[i];
115:   }
116:   ISCreateBlock(PETSC_COMM_SELF,bs,ec,garray,&from);
117:   for (i=0; i<ec; i++) {
118:     garray[i] = garray[i]/bs;
119:   }

121:   PetscMalloc((ec+1)*sizeof(PetscInt),&stmp);
122:   for (i=0; i<ec; i++) { stmp[i] = bs*i; }
123:   ISCreateBlock(PETSC_COMM_SELF,bs,ec,stmp,&to);
124:   PetscFree(stmp);

126:   /* create temporary global vector to generate scatter context */
127:   VecCreateMPIWithArray(((PetscObject)mat)->comm,mat->cmap->n,mat->cmap->N,PETSC_NULL,&gvec);

129:   VecScatterCreate(gvec,from,baij->lvec,to,&baij->Mvctx);

131:   PetscLogObjectParent(mat,baij->Mvctx);
132:   PetscLogObjectParent(mat,baij->lvec);
133:   PetscLogObjectParent(mat,from);
134:   PetscLogObjectParent(mat,to);
135:   baij->garray = garray;
136:   PetscLogObjectMemory(mat,(ec+1)*sizeof(PetscInt));
137:   ISDestroy(from);
138:   ISDestroy(to);
139:   VecDestroy(gvec);
140:   return(0);
141: }

143: /*
144:      Takes the local part of an already assembled MPIBAIJ matrix
145:    and disassembles it. This is to allow new nonzeros into the matrix
146:    that require more communication in the matrix vector multiply. 
147:    Thus certain data-structures must be rebuilt.

149:    Kind of slow! But that's what application programmers get when 
150:    they are sloppy.
151: */
154: PetscErrorCode DisAssemble_MPIBAIJ(Mat A)
155: {
156:   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)A->data;
157:   Mat            B = baij->B,Bnew;
158:   Mat_SeqBAIJ    *Bbaij = (Mat_SeqBAIJ*)B->data;
160:   PetscInt       i,j,mbs=Bbaij->mbs,n = A->cmap->N,col,*garray=baij->garray;
161:   PetscInt       bs2 = baij->bs2,*nz,ec,m = A->rmap->n;
162:   MatScalar      *a = Bbaij->a;
163:   MatScalar      *atmp;


167:   /* free stuff related to matrix-vec multiply */
168:   VecGetSize(baij->lvec,&ec); /* needed for PetscLogObjectMemory below */
169:   VecDestroy(baij->lvec); baij->lvec = 0;
170:   VecScatterDestroy(baij->Mvctx); baij->Mvctx = 0;
171:   if (baij->colmap) {
172: #if defined (PETSC_USE_CTABLE)
173:     PetscTableDestroy(baij->colmap); baij->colmap = 0;
174: #else
175:     PetscFree(baij->colmap);
176:     baij->colmap = 0;
177:     PetscLogObjectMemory(A,-Bbaij->nbs*sizeof(PetscInt));
178: #endif
179:   }

181:   /* make sure that B is assembled so we can access its values */
182:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
183:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

185:   /* invent new B and copy stuff over */
186:   PetscMalloc(mbs*sizeof(PetscInt),&nz);
187:   for (i=0; i<mbs; i++) {
188:     nz[i] = Bbaij->i[i+1]-Bbaij->i[i];
189:   }
190:   MatCreate(((PetscObject)B)->comm,&Bnew);
191:   MatSetSizes(Bnew,m,n,m,n);
192:   MatSetType(Bnew,((PetscObject)B)->type_name);
193:   MatSeqBAIJSetPreallocation(Bnew,B->rmap->bs,0,nz);
194:   MatSetOption(Bnew,MAT_ROW_ORIENTED,PETSC_FALSE);

196:   for (i=0; i<mbs; i++) {
197:     for (j=Bbaij->i[i]; j<Bbaij->i[i+1]; j++) {
198:       col  = garray[Bbaij->j[j]];
199:       atmp = a + j*bs2;
200:       MatSetValuesBlocked_SeqBAIJ(Bnew,1,&i,1,&col,atmp,B->insertmode);
201:     }
202:   }
203:   MatSetOption(Bnew,MAT_ROW_ORIENTED,PETSC_TRUE);

205:   PetscFree(nz);
206:   PetscFree(baij->garray);
207:   baij->garray = 0;
208:   PetscLogObjectMemory(A,-ec*sizeof(PetscInt));
209:   MatDestroy(B);
210:   PetscLogObjectParent(A,Bnew);
211:   baij->B = Bnew;
212:   A->was_assembled = PETSC_FALSE;
213:   return(0);
214: }

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

218: static PetscInt *uglyrmapd = 0,*uglyrmapo = 0;  /* mapping from the local ordering to the "diagonal" and "off-diagonal"
219:                                       parts of the local matrix */
220: static Vec uglydd = 0,uglyoo = 0;   /* work vectors used to scale the two parts of the local matrix */


225: PetscErrorCode MatMPIBAIJDiagonalScaleLocalSetUp(Mat inA,Vec scale)
226: {
227:   Mat_MPIBAIJ    *ina = (Mat_MPIBAIJ*) inA->data; /*access private part of matrix */
228:   Mat_SeqBAIJ    *B = (Mat_SeqBAIJ*)ina->B->data;
230:   PetscInt       bs = inA->rmap->bs,i,n,nt,j,cstart,cend,no,*garray = ina->garray,*lindices;
231:   PetscInt       *r_rmapd,*r_rmapo;
232: 
234:   MatGetOwnershipRange(inA,&cstart,&cend);
235:   MatGetSize(ina->A,PETSC_NULL,&n);
236:   PetscMalloc((inA->bmapping->n+1)*sizeof(PetscInt),&r_rmapd);
237:   PetscMemzero(r_rmapd,inA->bmapping->n*sizeof(PetscInt));
238:   nt   = 0;
239:   for (i=0; i<inA->bmapping->n; i++) {
240:     if (inA->bmapping->indices[i]*bs >= cstart && inA->bmapping->indices[i]*bs < cend) {
241:       nt++;
242:       r_rmapd[i] = inA->bmapping->indices[i] + 1;
243:     }
244:   }
245:   if (nt*bs != n) SETERRQ2(PETSC_ERR_PLIB,"Hmm nt*bs %D n %D",nt*bs,n);
246:   PetscMalloc((n+1)*sizeof(PetscInt),&uglyrmapd);
247:   for (i=0; i<inA->bmapping->n; i++) {
248:     if (r_rmapd[i]){
249:       for (j=0; j<bs; j++) {
250:         uglyrmapd[(r_rmapd[i]-1)*bs+j-cstart] = i*bs + j;
251:       }
252:     }
253:   }
254:   PetscFree(r_rmapd);
255:   VecCreateSeq(PETSC_COMM_SELF,n,&uglydd);

257:   PetscMalloc((ina->Nbs+1)*sizeof(PetscInt),&lindices);
258:   PetscMemzero(lindices,ina->Nbs*sizeof(PetscInt));
259:   for (i=0; i<B->nbs; i++) {
260:     lindices[garray[i]] = i+1;
261:   }
262:   no   = inA->bmapping->n - nt;
263:   PetscMalloc((inA->bmapping->n+1)*sizeof(PetscInt),&r_rmapo);
264:   PetscMemzero(r_rmapo,inA->bmapping->n*sizeof(PetscInt));
265:   nt   = 0;
266:   for (i=0; i<inA->bmapping->n; i++) {
267:     if (lindices[inA->bmapping->indices[i]]) {
268:       nt++;
269:       r_rmapo[i] = lindices[inA->bmapping->indices[i]];
270:     }
271:   }
272:   if (nt > no) SETERRQ2(PETSC_ERR_PLIB,"Hmm nt %D no %D",nt,n);
273:   PetscFree(lindices);
274:   PetscMalloc((nt*bs+1)*sizeof(PetscInt),&uglyrmapo);
275:   for (i=0; i<inA->bmapping->n; i++) {
276:     if (r_rmapo[i]){
277:       for (j=0; j<bs; j++) {
278:         uglyrmapo[(r_rmapo[i]-1)*bs+j] = i*bs + j;
279:       }
280:     }
281:   }
282:   PetscFree(r_rmapo);
283:   VecCreateSeq(PETSC_COMM_SELF,nt*bs,&uglyoo);

285:   return(0);
286: }

290: PetscErrorCode  MatMPIBAIJDiagonalScaleLocal(Mat A,Vec scale)
291: {
292:   /* This routine should really be abandoned as it duplicates MatDiagonalScaleLocal */
293:   PetscErrorCode ierr,(*f)(Mat,Vec);

296:   PetscObjectQueryFunction((PetscObject)A,"MatDiagonalScaleLocal_C",(void (**)(void))&f);
297:   if (f) {
298:     (*f)(A,scale);
299:   }
300:   return(0);
301: }

306: PetscErrorCode  MatDiagonalScaleLocal_MPIBAIJ(Mat A,Vec scale)
307: {
308:   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*) A->data; /*access private part of matrix */
310:   PetscInt       n,i;
311:   PetscScalar    *d,*o,*s;
312: 
314:   if (!uglyrmapd) {
315:     MatMPIBAIJDiagonalScaleLocalSetUp(A,scale);
316:   }

318:   VecGetArray(scale,&s);
319: 
320:   VecGetLocalSize(uglydd,&n);
321:   VecGetArray(uglydd,&d);
322:   for (i=0; i<n; i++) {
323:     d[i] = s[uglyrmapd[i]]; /* copy "diagonal" (true local) portion of scale into dd vector */
324:   }
325:   VecRestoreArray(uglydd,&d);
326:   /* column scale "diagonal" portion of local matrix */
327:   MatDiagonalScale(a->A,PETSC_NULL,uglydd);

329:   VecGetLocalSize(uglyoo,&n);
330:   VecGetArray(uglyoo,&o);
331:   for (i=0; i<n; i++) {
332:     o[i] = s[uglyrmapo[i]]; /* copy "off-diagonal" portion of scale into oo vector */
333:   }
334:   VecRestoreArray(scale,&s);
335:   VecRestoreArray(uglyoo,&o);
336:   /* column scale "off-diagonal" portion of local matrix */
337:   MatDiagonalScale(a->B,PETSC_NULL,uglyoo);

339:   return(0);
340: }