Actual source code: mmbaij.c
1: /*$Id: mmbaij.c,v 1.46 2001/09/25 00:31:36 balay Exp $*/
3: /*
4: Support for the parallel BAIJ matrix vector multiply
5: */
6: #include src/mat/impls/baij/mpi/mpibaij.h
7: #include src/vec/vecimpl.h
9: EXTERN int MatSetValuesBlocked_SeqBAIJ(Mat,int,int*,int,int*,PetscScalar*,InsertMode);
11: #undef __FUNCT__
13: int MatSetUpMultiply_MPIBAIJ(Mat mat)
14: {
15: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
16: Mat_SeqBAIJ *B = (Mat_SeqBAIJ*)(baij->B->data);
17: int Nbs = baij->Nbs,i,j,*indices,*aj = B->j,ierr,ec = 0,*garray;
18: int bs = baij->bs,*stmp;
19: IS from,to;
20: Vec gvec;
21: #if defined (PETSC_USE_CTABLE)
22: PetscTable gid1_lid1;
23: PetscTablePosition tpos;
24: int gid,lid;
25: #endif
29: #if defined (PETSC_USE_CTABLE)
30: /* use a table - Mark Adams */
31: PetscTableCreate(B->mbs,&gid1_lid1);
32: for (i=0; i<B->mbs; i++) {
33: for (j=0; j<B->ilen[i]; j++) {
34: int data,gid1 = aj[B->i[i]+j] + 1;
35: PetscTableFind(gid1_lid1,gid1,&data) ;
36: if (!data) {
37: /* one based table */
38: PetscTableAdd(gid1_lid1,gid1,++ec);
39: }
40: }
41: }
42: /* form array of columns we need */
43: PetscMalloc((ec+1)*sizeof(int),&garray);
44: PetscTableGetHeadPosition(gid1_lid1,&tpos);
45: while (tpos) {
46: PetscTableGetNext(gid1_lid1,&tpos,&gid,&lid);
47: gid--; lid--;
48: garray[lid] = gid;
49: }
50: PetscSortInt(ec,garray);
51: /* qsort(garray, ec, sizeof(int), intcomparcarc); */
52: PetscTableRemoveAll(gid1_lid1);
53: for (i=0; i<ec; i++) {
54: PetscTableAdd(gid1_lid1,garray[i]+1,i+1);
55: }
56: /* compact out the extra columns in B */
57: for (i=0; i<B->mbs; i++) {
58: for (j=0; j<B->ilen[i]; j++) {
59: int gid1 = aj[B->i[i] + j] + 1;
60: PetscTableFind(gid1_lid1,gid1,&lid);
61: lid --;
62: aj[B->i[i]+j] = lid;
63: }
64: }
65: B->nbs = ec;
66: baij->B->n = ec*B->bs;
67: PetscTableDelete(gid1_lid1);
68: /* Mark Adams */
69: #else
70: /* Make an array as long as the number of columns */
71: /* mark those columns that are in baij->B */
72: PetscMalloc((Nbs+1)*sizeof(int),&indices);
73: PetscMemzero(indices,Nbs*sizeof(int));
74: for (i=0; i<B->mbs; i++) {
75: for (j=0; j<B->ilen[i]; j++) {
76: if (!indices[aj[B->i[i] + j]]) ec++;
77: indices[aj[B->i[i] + j]] = 1;
78: }
79: }
81: /* form array of columns we need */
82: PetscMalloc((ec+1)*sizeof(int),&garray);
83: ec = 0;
84: for (i=0; i<Nbs; i++) {
85: if (indices[i]) {
86: garray[ec++] = i;
87: }
88: }
90: /* make indices now point into garray */
91: for (i=0; i<ec; i++) {
92: indices[garray[i]] = i;
93: }
95: /* compact out the extra columns in B */
96: for (i=0; i<B->mbs; i++) {
97: for (j=0; j<B->ilen[i]; j++) {
98: aj[B->i[i] + j] = indices[aj[B->i[i] + j]];
99: }
100: }
101: B->nbs = ec;
102: baij->B->n = ec*B->bs;
103: PetscFree(indices);
104: #endif
106: /* create local vector that is used to scatter into */
107: VecCreateSeq(PETSC_COMM_SELF,ec*bs,&baij->lvec);
109: /* create two temporary index sets for building scatter-gather */
110: for (i=0; i<ec; i++) {
111: garray[i] = bs*garray[i];
112: }
113: ISCreateBlock(PETSC_COMM_SELF,bs,ec,garray,&from);
114: for (i=0; i<ec; i++) {
115: garray[i] = garray[i]/bs;
116: }
118: PetscMalloc((ec+1)*sizeof(int),&stmp);
119: for (i=0; i<ec; i++) { stmp[i] = bs*i; }
120: ISCreateBlock(PETSC_COMM_SELF,bs,ec,stmp,&to);
121: PetscFree(stmp);
123: /* create temporary global vector to generate scatter context */
124: /* this is inefficient, but otherwise we must do either
125: 1) save garray until the first actual scatter when the vector is known or
126: 2) have another way of generating a scatter context without a vector.*/
127: VecCreateMPI(mat->comm,mat->n,mat->N,&gvec);
129: /* gnerate the scatter context */
130: VecScatterCreate(gvec,from,baij->lvec,to,&baij->Mvctx);
132: /*
133: Post the receives for the first matrix vector product. We sync-chronize after
134: this on the chance that the user immediately calls MatMult() after assemblying
135: the matrix.
136: */
137: VecScatterPostRecvs(gvec,baij->lvec,INSERT_VALUES,SCATTER_FORWARD,baij->Mvctx);
138: MPI_Barrier(mat->comm);
140: PetscLogObjectParent(mat,baij->Mvctx);
141: PetscLogObjectParent(mat,baij->lvec);
142: PetscLogObjectParent(mat,from);
143: PetscLogObjectParent(mat,to);
144: baij->garray = garray;
145: PetscLogObjectMemory(mat,(ec+1)*sizeof(int));
146: ISDestroy(from);
147: ISDestroy(to);
148: VecDestroy(gvec);
149: return(0);
150: }
152: /*
153: Takes the local part of an already assembled MPIBAIJ matrix
154: and disassembles it. This is to allow new nonzeros into the matrix
155: that require more communication in the matrix vector multiply.
156: Thus certain data-structures must be rebuilt.
158: Kind of slow! But that's what application programmers get when
159: they are sloppy.
160: */
161: #undef __FUNCT__
163: int DisAssemble_MPIBAIJ(Mat A)
164: {
165: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)A->data;
166: Mat B = baij->B,Bnew;
167: Mat_SeqBAIJ *Bbaij = (Mat_SeqBAIJ*)B->data;
168: int ierr,i,j,mbs=Bbaij->mbs,n = A->N,col,*garray=baij->garray;
169: int bs2 = baij->bs2,*nz,ec,m = A->m;
170: MatScalar *a = Bbaij->a;
171: PetscScalar *atmp;
172: #if defined(PETSC_USE_MAT_SINGLE)
173: int k;
174: #endif
177: /* free stuff related to matrix-vec multiply */
178: VecGetSize(baij->lvec,&ec); /* needed for PetscLogObjectMemory below */
179: VecDestroy(baij->lvec); baij->lvec = 0;
180: VecScatterDestroy(baij->Mvctx); baij->Mvctx = 0;
181: if (baij->colmap) {
182: #if defined (PETSC_USE_CTABLE)
183: PetscTableDelete(baij->colmap); baij->colmap = 0;
184: #else
185: PetscFree(baij->colmap);
186: baij->colmap = 0;
187: PetscLogObjectMemory(A,-Bbaij->nbs*sizeof(int));
188: #endif
189: }
191: /* make sure that B is assembled so we can access its values */
192: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
193: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
195: /* invent new B and copy stuff over */
196: PetscMalloc(mbs*sizeof(int),&nz);
197: for (i=0; i<mbs; i++) {
198: nz[i] = Bbaij->i[i+1]-Bbaij->i[i];
199: }
200: MatCreateSeqBAIJ(PETSC_COMM_SELF,baij->bs,m,n,0,nz,&Bnew);
201: MatSetOption(Bnew,MAT_COLUMN_ORIENTED);
203: #if defined(PETSC_USE_MAT_SINGLE)
204: PetscMalloc(bs2*sizeof(PetscScalar),&atmp);
205: #endif
206: for (i=0; i<mbs; i++) {
207: for (j=Bbaij->i[i]; j<Bbaij->i[i+1]; j++) {
208: col = garray[Bbaij->j[j]];
209: #if defined(PETSC_USE_MAT_SINGLE)
210: for (k=0; k<bs2; k++) atmp[k] = a[j*bs2+k];
211: #else
212: atmp = a + j*bs2;
213: #endif
214: MatSetValuesBlocked_SeqBAIJ(Bnew,1,&i,1,&col,atmp,B->insertmode);
215: }
216: }
217: MatSetOption(Bnew,MAT_ROW_ORIENTED);
219: #if defined(PETSC_USE_MAT_SINGLE)
220: PetscFree(atmp);
221: #endif
223: PetscFree(nz);
224: PetscFree(baij->garray);
225: baij->garray = 0;
226: PetscLogObjectMemory(A,-ec*sizeof(int));
227: MatDestroy(B);
228: PetscLogObjectParent(A,Bnew);
229: baij->B = Bnew;
230: A->was_assembled = PETSC_FALSE;
231: return(0);
232: }
234: /* ugly stuff added for Glenn someday we should fix this up */
236: static int *uglyrmapd = 0,*uglyrmapo = 0; /* mapping from the local ordering to the "diagonal" and "off-diagonal"
237: parts of the local matrix */
238: static Vec uglydd = 0,uglyoo = 0; /* work vectors used to scale the two parts of the local matrix */
241: #undef __FUNCT__
243: int MatMPIBAIJDiagonalScaleLocalSetUp(Mat inA,Vec scale)
244: {
245: Mat_MPIBAIJ *ina = (Mat_MPIBAIJ*) inA->data; /*access private part of matrix */
246: Mat_SeqBAIJ *A = (Mat_SeqBAIJ*)ina->A->data;
247: Mat_SeqBAIJ *B = (Mat_SeqBAIJ*)ina->B->data;
248: int ierr,bs = A->bs,i,n,nt,j,cstart,cend,no,*garray = ina->garray,*lindices;
249: int *r_rmapd,*r_rmapo;
250:
252: MatGetOwnershipRange(inA,&cstart,&cend);
253: MatGetSize(ina->A,PETSC_NULL,&n);
254: PetscMalloc((inA->bmapping->n+1)*sizeof(int),&r_rmapd);
255: PetscMemzero(r_rmapd,inA->bmapping->n*sizeof(int));
256: nt = 0;
257: for (i=0; i<inA->bmapping->n; i++) {
258: if (inA->bmapping->indices[i]*bs >= cstart && inA->bmapping->indices[i]*bs < cend) {
259: nt++;
260: r_rmapd[i] = inA->bmapping->indices[i] + 1;
261: }
262: }
263: if (nt*bs != n) SETERRQ2(1,"Hmm nt*bs %d n %d",nt*bs,n);
264: PetscMalloc((n+1)*sizeof(int),&uglyrmapd);
265: for (i=0; i<inA->bmapping->n; i++) {
266: if (r_rmapd[i]){
267: for (j=0; j<bs; j++) {
268: uglyrmapd[(r_rmapd[i]-1)*bs+j-cstart] = i*bs + j;
269: }
270: }
271: }
272: PetscFree(r_rmapd);
273: VecCreateSeq(PETSC_COMM_SELF,n,&uglydd);
275: PetscMalloc((ina->Nbs+1)*sizeof(int),&lindices);
276: PetscMemzero(lindices,ina->Nbs*sizeof(int));
277: for (i=0; i<B->nbs; i++) {
278: lindices[garray[i]] = i+1;
279: }
280: no = inA->bmapping->n - nt;
281: PetscMalloc((inA->bmapping->n+1)*sizeof(int),&r_rmapo);
282: PetscMemzero(r_rmapo,inA->bmapping->n*sizeof(int));
283: nt = 0;
284: for (i=0; i<inA->bmapping->n; i++) {
285: if (lindices[inA->bmapping->indices[i]]) {
286: nt++;
287: r_rmapo[i] = lindices[inA->bmapping->indices[i]];
288: }
289: }
290: if (nt > no) SETERRQ2(1,"Hmm nt %d no %d",nt,n);
291: PetscFree(lindices);
292: PetscMalloc((nt*bs+1)*sizeof(int),&uglyrmapo);
293: for (i=0; i<inA->bmapping->n; i++) {
294: if (r_rmapo[i]){
295: for (j=0; j<bs; j++) {
296: uglyrmapo[(r_rmapo[i]-1)*bs+j] = i*bs + j;
297: }
298: }
299: }
300: PetscFree(r_rmapo);
301: VecCreateSeq(PETSC_COMM_SELF,nt*bs,&uglyoo);
303: return(0);
304: }
306: #undef __FUNCT__
308: int MatMPIBAIJDiagonalScaleLocal(Mat A,Vec scale)
309: {
310: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*) A->data; /*access private part of matrix */
311: int ierr,n,i;
312: PetscScalar *d,*o,*s;
313:
315: if (!uglyrmapd) {
316: MatMPIBAIJDiagonalScaleLocalSetUp(A,scale);
317: }
319: VecGetArray(scale,&s);
320:
321: VecGetLocalSize(uglydd,&n);
322: VecGetArray(uglydd,&d);
323: for (i=0; i<n; i++) {
324: d[i] = s[uglyrmapd[i]]; /* copy "diagonal" (true local) portion of scale into dd vector */
325: }
326: VecRestoreArray(uglydd,&d);
327: /* column scale "diagonal" portion of local matrix */
328: MatDiagonalScale(a->A,PETSC_NULL,uglydd);
330: VecGetLocalSize(uglyoo,&n);
331: VecGetArray(uglyoo,&o);
332: for (i=0; i<n; i++) {
333: o[i] = s[uglyrmapo[i]]; /* copy "off-diagonal" portion of scale into oo vector */
334: }
335: VecRestoreArray(scale,&s);
336: VecRestoreArray(uglyoo,&o);
337: /* column scale "off-diagonal" portion of local matrix */
338: MatDiagonalScale(a->B,PETSC_NULL,uglyoo);
340: return(0);
341: }