Actual source code: fdmpiaij.c

  1: /*$Id: fdmpiaij.c,v 1.41 2001/06/21 21:16:31 bsmith Exp $*/

 3:  #include src/mat/impls/aij/mpi/mpiaij.h

  5: EXTERN int CreateColmap_MPIAIJ_Private(Mat);
  6: EXTERN int MatGetColumnIJ_SeqAIJ(Mat,int,PetscTruth,int*,int*[],int*[],PetscTruth*);
  7: EXTERN int MatRestoreColumnIJ_SeqAIJ(Mat,int,PetscTruth,int*,int*[],int*[],PetscTruth*);

 11: int MatFDColoringCreate_MPIAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c)
 12: {
 13:   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
 14:   int        i,*is,n,nrows,j,k,m,*rows = 0,ierr,*A_ci,*A_cj,ncols,col;
 15:   int        nis = iscoloring->n,*ncolsonproc,size,nctot,*cols,*disp,*B_ci,*B_cj;
 16:   int        *rowhit,M = mat->m,cstart = aij->cstart,cend = aij->cend,colb;
 17:   int        *columnsforrow,l;
 18:   IS         *isa;
 19:   PetscTruth done,flg;

 22:   if (!mat->assembled) {
 23:     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Matrix must be assembled first; MatAssemblyBegin/End();");
 24:   }

 26:   ISColoringGetIS(iscoloring,PETSC_IGNORE,&isa);
 27:   c->M             = mat->M;  /* set the global rows and columns and local rows */
 28:   c->N             = mat->N;
 29:   c->m             = mat->m;
 30:   c->rstart        = aij->rstart;

 32:   c->ncolors       = nis;
 33:   PetscMalloc(nis*sizeof(int),&c->ncolumns);
 34:   PetscMalloc(nis*sizeof(int*),&c->columns);
 35:   PetscMalloc(nis*sizeof(int),&c->nrows);
 36:   PetscMalloc(nis*sizeof(int*),&c->rows);
 37:   PetscMalloc(nis*sizeof(int*),&c->columnsforrow);
 38:   PetscLogObjectMemory(c,5*nis*sizeof(int));

 40:   /* Allow access to data structures of local part of matrix */
 41:   if (!aij->colmap) {
 42:     CreateColmap_MPIAIJ_Private(mat);
 43:   }
 44:   /*
 45:       Calls the _SeqAIJ() version of these routines to make sure it does not 
 46:      get the reduced (by inodes) version of I and J
 47:   */
 48:   MatGetColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);
 49:   MatGetColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);

 51:   MPI_Comm_size(mat->comm,&size);
 52:   PetscMalloc(2*size*sizeof(int*),&ncolsonproc);
 53:   disp = ncolsonproc + size;

 55:   PetscMalloc((M+1)*sizeof(int),&rowhit);
 56:   PetscMalloc((M+1)*sizeof(int),&columnsforrow);

 58:   /*
 59:      Temporary option to allow for debugging/testing
 60:   */
 61:   PetscOptionsHasName(PETSC_NULL,"-matfdcoloring_slow",&flg);

 63:   for (i=0; i<nis; i++) {
 64:     ISGetLocalSize(isa[i],&n);
 65:     ISGetIndices(isa[i],&is);
 66:     c->ncolumns[i] = n;
 67:     c->ncolumns[i] = n;
 68:     if (n) {
 69:       PetscMalloc(n*sizeof(int),&c->columns[i]);
 70:       PetscLogObjectMemory(c,n*sizeof(int));
 71:       PetscMemcpy(c->columns[i],is,n*sizeof(int));
 72:     } else {
 73:       c->columns[i]  = 0;
 74:     }

 76:     /* Determine the total (parallel) number of columns of this color */
 77:     MPI_Allgather(&n,1,MPI_INT,ncolsonproc,1,MPI_INT,mat->comm);
 78:     nctot = 0; for (j=0; j<size; j++) {nctot += ncolsonproc[j];}
 79:     if (!nctot) {
 80:       PetscLogInfo((PetscObject)mat,"MatFDColoringCreate_MPIAIJ: Coloring of matrix has some unneeded colors with no corresponding rows\n");
 81:     }

 83:     disp[0] = 0;
 84:     for (j=1; j<size; j++) {
 85:       disp[j] = disp[j-1] + ncolsonproc[j-1];
 86:     }
 87: 
 88:     /* Get complete list of columns for color on each processor */
 89:     PetscMalloc((nctot+1)*sizeof(int),&cols);
 90:     MPI_Allgatherv(is,n,MPI_INT,cols,ncolsonproc,disp,MPI_INT,mat->comm);

 92:     /*
 93:        Mark all rows affect by these columns
 94:     */
 95:     if (!flg) {/*-----------------------------------------------------------------------------*/
 96:       /* crude, fast version */
 97:       PetscMemzero(rowhit,M*sizeof(int));
 98:       /* loop over columns*/
 99:       for (j=0; j<nctot; j++) {
100:         col  = cols[j];
101:         if (col >= cstart && col < cend) {
102:           /* column is in diagonal block of matrix */
103:           rows = A_cj + A_ci[col-cstart];
104:           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
105:         } else {
106: #if defined (PETSC_USE_CTABLE)
107:           PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr)
108:           colb --;
109: #else
110:           colb = aij->colmap[col] - 1;
111: #endif
112:           if (colb == -1) {
113:             m = 0;
114:           } else {
115:             rows = B_cj + B_ci[colb];
116:             m    = B_ci[colb+1] - B_ci[colb];
117:           }
118:         }
119:         /* loop over columns marking them in rowhit */
120:         for (k=0; k<m; k++) {
121:           rowhit[*rows++] = col + 1;
122:         }
123:       }

125:       /* count the number of hits */
126:       nrows = 0;
127:       for (j=0; j<M; j++) {
128:         if (rowhit[j]) nrows++;
129:       }
130:       c->nrows[i]         = nrows;
131:       PetscMalloc((nrows+1)*sizeof(int),&c->rows[i]);
132:       PetscMalloc((nrows+1)*sizeof(int),&c->columnsforrow[i]);
133:       PetscLogObjectMemory(c,2*(nrows+1)*sizeof(int));
134:       nrows = 0;
135:       for (j=0; j<M; j++) {
136:         if (rowhit[j]) {
137:           c->rows[i][nrows]           = j;
138:           c->columnsforrow[i][nrows] = rowhit[j] - 1;
139:           nrows++;
140:         }
141:       }
142:     } else {/*-------------------------------------------------------------------------------*/
143:       /* slow version, using rowhit as a linked list */
144:       int currentcol,fm,mfm;
145:       rowhit[M] = M;
146:       nrows     = 0;
147:       /* loop over columns*/
148:       for (j=0; j<nctot; j++) {
149:         col  = cols[j];
150:         if (col >= cstart && col < cend) {
151:           /* column is in diagonal block of matrix */
152:           rows = A_cj + A_ci[col-cstart];
153:           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
154:         } else {
155: #if defined (PETSC_USE_CTABLE)
156:           PetscTableFind(aij->colmap,col+1,&colb);
157:           colb --;
158: #else
159:           colb = aij->colmap[col] - 1;
160: #endif
161:           if (colb == -1) {
162:             m = 0;
163:           } else {
164:             rows = B_cj + B_ci[colb];
165:             m    = B_ci[colb+1] - B_ci[colb];
166:           }
167:         }
168:         /* loop over columns marking them in rowhit */
169:         fm    = M; /* fm points to first entry in linked list */
170:         for (k=0; k<m; k++) {
171:           currentcol = *rows++;
172:           /* is it already in the list? */
173:           do {
174:             mfm  = fm;
175:             fm   = rowhit[fm];
176:           } while (fm < currentcol);
177:           /* not in list so add it */
178:           if (fm != currentcol) {
179:             nrows++;
180:             columnsforrow[currentcol] = col;
181:             /* next three lines insert new entry into linked list */
182:             rowhit[mfm]               = currentcol;
183:             rowhit[currentcol]        = fm;
184:             fm                        = currentcol;
185:             /* fm points to present position in list since we know the columns are sorted */
186:           } else {
187:             SETERRQ(PETSC_ERR_PLIB,"Invalid coloring of matrix detected");
188:           }
189:         }
190:       }
191:       c->nrows[i]         = nrows;
192:       PetscMalloc((nrows+1)*sizeof(int),&c->rows[i]);
193:       PetscMalloc((nrows+1)*sizeof(int),&c->columnsforrow[i]);
194:       PetscLogObjectMemory(c,(nrows+1)*sizeof(int));
195:       /* now store the linked list of rows into c->rows[i] */
196:       nrows = 0;
197:       fm    = rowhit[M];
198:       do {
199:         c->rows[i][nrows]            = fm;
200:         c->columnsforrow[i][nrows++] = columnsforrow[fm];
201:         fm                           = rowhit[fm];
202:       } while (fm < M);
203:     } /* ---------------------------------------------------------------------------------------*/
204:     PetscFree(cols);
205:   }

207:   /* Optimize by adding the vscale, and scaleforrow[][] fields */
208:   /*
209:        vscale will contain the "diagonal" on processor scalings followed by the off processor
210:   */
211:   VecCreateGhost(mat->comm,aij->A->m,PETSC_DETERMINE,aij->B->n,aij->garray,&c->vscale);CHKERRQ(ierr)
212:   PetscMalloc(c->ncolors*sizeof(int*),&c->vscaleforrow);
213:   for (k=0; k<c->ncolors; k++) {
214:     PetscMalloc((c->nrows[k]+1)*sizeof(int),&c->vscaleforrow[k]);
215:     for (l=0; l<c->nrows[k]; l++) {
216:       col = c->columnsforrow[k][l];
217:       if (col >= cstart && col < cend) {
218:         /* column is in diagonal block of matrix */
219:         colb = col - cstart;
220:       } else {
221:         /* column  is in "off-processor" part */
222: #if defined (PETSC_USE_CTABLE)
223:         PetscTableFind(aij->colmap,col+1,&colb);
224:         colb --;
225: #else
226:         colb = aij->colmap[col] - 1;
227: #endif
228:         colb += cend - cstart;
229:       }
230:       c->vscaleforrow[k][l] = colb;
231:     }
232:   }
233:   ISColoringRestoreIS(iscoloring,&isa);

235:   PetscFree(rowhit);
236:   PetscFree(columnsforrow);
237:   PetscFree(ncolsonproc);
238:   MatRestoreColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);
239:   MatRestoreColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);
240:   return(0);
241: }