Actual source code: fdmpiaij.c

  1: /*$Id: fdmpiaij.c,v 1.39 2001/04/10 19:35:25 bsmith Exp $*/

  3: #include "src/mat/impls/aij/mpi/mpiaij.h"
  4: #include "src/vec/vecimpl.h"

  6: EXTERN int CreateColmap_MPIAIJ_Private(Mat);
  7: EXTERN int MatGetColumnIJ_SeqAIJ(Mat,int,PetscTruth,int*,int**,int**,PetscTruth*);
  8: EXTERN int MatRestoreColumnIJ_SeqAIJ(Mat,int,PetscTruth,int*,int**,int**,PetscTruth*);

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

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

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

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

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

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

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

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

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

 75:     /* Determine the total (parallel) number of columns of this color */
 76:     MPI_Allgather(&n,1,MPI_INT,ncolsonproc,1,MPI_INT,mat->comm);
 77:     nctot = 0; for (j=0; j<size; j++) {nctot += ncolsonproc[j];}
 78:     if (!nctot) SETERRQ(PETSC_ERR_PLIB,"Invalid coloring of matrix detected");

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

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

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

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

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