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: PetscMapSetUp(&(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: PetscMapSetUp(&(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: PetscScalar *atmp;
164: #if defined(PETSC_USE_MAT_SINGLE)
165: PetscInt k;
166: #endif
169: /* free stuff related to matrix-vec multiply */
170: VecGetSize(baij->lvec,&ec); /* needed for PetscLogObjectMemory below */
171: VecDestroy(baij->lvec); baij->lvec = 0;
172: VecScatterDestroy(baij->Mvctx); baij->Mvctx = 0;
173: if (baij->colmap) {
174: #if defined (PETSC_USE_CTABLE)
175: PetscTableDestroy(baij->colmap); baij->colmap = 0;
176: #else
177: PetscFree(baij->colmap);
178: baij->colmap = 0;
179: PetscLogObjectMemory(A,-Bbaij->nbs*sizeof(PetscInt));
180: #endif
181: }
183: /* make sure that B is assembled so we can access its values */
184: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
185: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
187: /* invent new B and copy stuff over */
188: PetscMalloc(mbs*sizeof(PetscInt),&nz);
189: for (i=0; i<mbs; i++) {
190: nz[i] = Bbaij->i[i+1]-Bbaij->i[i];
191: }
192: MatCreate(((PetscObject)B)->comm,&Bnew);
193: MatSetSizes(Bnew,m,n,m,n);
194: MatSetType(Bnew,((PetscObject)B)->type_name);
195: MatSeqBAIJSetPreallocation(Bnew,B->rmap.bs,0,nz);
196: MatSetOption(Bnew,MAT_ROW_ORIENTED,PETSC_FALSE);
198: #if defined(PETSC_USE_MAT_SINGLE)
199: PetscMalloc(bs2*sizeof(PetscScalar),&atmp);
200: #endif
201: for (i=0; i<mbs; i++) {
202: for (j=Bbaij->i[i]; j<Bbaij->i[i+1]; j++) {
203: col = garray[Bbaij->j[j]];
204: #if defined(PETSC_USE_MAT_SINGLE)
205: for (k=0; k<bs2; k++) atmp[k] = a[j*bs2+k];
206: #else
207: atmp = a + j*bs2;
208: #endif
209: MatSetValuesBlocked_SeqBAIJ(Bnew,1,&i,1,&col,atmp,B->insertmode);
210: }
211: }
212: MatSetOption(Bnew,MAT_ROW_ORIENTED,PETSC_TRUE);
214: #if defined(PETSC_USE_MAT_SINGLE)
215: PetscFree(atmp);
216: #endif
218: PetscFree(nz);
219: PetscFree(baij->garray);
220: baij->garray = 0;
221: PetscLogObjectMemory(A,-ec*sizeof(PetscInt));
222: MatDestroy(B);
223: PetscLogObjectParent(A,Bnew);
224: baij->B = Bnew;
225: A->was_assembled = PETSC_FALSE;
226: return(0);
227: }
229: /* ugly stuff added for Glenn someday we should fix this up */
231: static PetscInt *uglyrmapd = 0,*uglyrmapo = 0; /* mapping from the local ordering to the "diagonal" and "off-diagonal"
232: parts of the local matrix */
233: static Vec uglydd = 0,uglyoo = 0; /* work vectors used to scale the two parts of the local matrix */
238: PetscErrorCode MatMPIBAIJDiagonalScaleLocalSetUp(Mat inA,Vec scale)
239: {
240: Mat_MPIBAIJ *ina = (Mat_MPIBAIJ*) inA->data; /*access private part of matrix */
241: Mat_SeqBAIJ *B = (Mat_SeqBAIJ*)ina->B->data;
243: PetscInt bs = inA->rmap.bs,i,n,nt,j,cstart,cend,no,*garray = ina->garray,*lindices;
244: PetscInt *r_rmapd,*r_rmapo;
245:
247: MatGetOwnershipRange(inA,&cstart,&cend);
248: MatGetSize(ina->A,PETSC_NULL,&n);
249: PetscMalloc((inA->bmapping->n+1)*sizeof(PetscInt),&r_rmapd);
250: PetscMemzero(r_rmapd,inA->bmapping->n*sizeof(PetscInt));
251: nt = 0;
252: for (i=0; i<inA->bmapping->n; i++) {
253: if (inA->bmapping->indices[i]*bs >= cstart && inA->bmapping->indices[i]*bs < cend) {
254: nt++;
255: r_rmapd[i] = inA->bmapping->indices[i] + 1;
256: }
257: }
258: if (nt*bs != n) SETERRQ2(PETSC_ERR_PLIB,"Hmm nt*bs %D n %D",nt*bs,n);
259: PetscMalloc((n+1)*sizeof(PetscInt),&uglyrmapd);
260: for (i=0; i<inA->bmapping->n; i++) {
261: if (r_rmapd[i]){
262: for (j=0; j<bs; j++) {
263: uglyrmapd[(r_rmapd[i]-1)*bs+j-cstart] = i*bs + j;
264: }
265: }
266: }
267: PetscFree(r_rmapd);
268: VecCreateSeq(PETSC_COMM_SELF,n,&uglydd);
270: PetscMalloc((ina->Nbs+1)*sizeof(PetscInt),&lindices);
271: PetscMemzero(lindices,ina->Nbs*sizeof(PetscInt));
272: for (i=0; i<B->nbs; i++) {
273: lindices[garray[i]] = i+1;
274: }
275: no = inA->bmapping->n - nt;
276: PetscMalloc((inA->bmapping->n+1)*sizeof(PetscInt),&r_rmapo);
277: PetscMemzero(r_rmapo,inA->bmapping->n*sizeof(PetscInt));
278: nt = 0;
279: for (i=0; i<inA->bmapping->n; i++) {
280: if (lindices[inA->bmapping->indices[i]]) {
281: nt++;
282: r_rmapo[i] = lindices[inA->bmapping->indices[i]];
283: }
284: }
285: if (nt > no) SETERRQ2(PETSC_ERR_PLIB,"Hmm nt %D no %D",nt,n);
286: PetscFree(lindices);
287: PetscMalloc((nt*bs+1)*sizeof(PetscInt),&uglyrmapo);
288: for (i=0; i<inA->bmapping->n; i++) {
289: if (r_rmapo[i]){
290: for (j=0; j<bs; j++) {
291: uglyrmapo[(r_rmapo[i]-1)*bs+j] = i*bs + j;
292: }
293: }
294: }
295: PetscFree(r_rmapo);
296: VecCreateSeq(PETSC_COMM_SELF,nt*bs,&uglyoo);
298: return(0);
299: }
303: PetscErrorCode MatMPIBAIJDiagonalScaleLocal(Mat A,Vec scale)
304: {
305: /* This routine should really be abandoned as it duplicates MatDiagonalScaleLocal */
306: PetscErrorCode ierr,(*f)(Mat,Vec);
309: PetscObjectQueryFunction((PetscObject)A,"MatDiagonalScaleLocal_C",(void (**)(void))&f);
310: if (f) {
311: (*f)(A,scale);
312: }
313: return(0);
314: }
319: PetscErrorCode MatDiagonalScaleLocal_MPIBAIJ(Mat A,Vec scale)
320: {
321: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*) A->data; /*access private part of matrix */
323: PetscInt n,i;
324: PetscScalar *d,*o,*s;
325:
327: if (!uglyrmapd) {
328: MatMPIBAIJDiagonalScaleLocalSetUp(A,scale);
329: }
331: VecGetArray(scale,&s);
332:
333: VecGetLocalSize(uglydd,&n);
334: VecGetArray(uglydd,&d);
335: for (i=0; i<n; i++) {
336: d[i] = s[uglyrmapd[i]]; /* copy "diagonal" (true local) portion of scale into dd vector */
337: }
338: VecRestoreArray(uglydd,&d);
339: /* column scale "diagonal" portion of local matrix */
340: MatDiagonalScale(a->A,PETSC_NULL,uglydd);
342: VecGetLocalSize(uglyoo,&n);
343: VecGetArray(uglyoo,&o);
344: for (i=0; i<n; i++) {
345: o[i] = s[uglyrmapo[i]]; /* copy "off-diagonal" portion of scale into oo vector */
346: }
347: VecRestoreArray(scale,&s);
348: VecRestoreArray(uglyoo,&o);
349: /* column scale "off-diagonal" portion of local matrix */
350: MatDiagonalScale(a->B,PETSC_NULL,uglyoo);
352: return(0);
353: }