xref: /petsc/src/mat/impls/aij/mpi/fdmpiaij.c (revision 80fe4e4924c5f322a48ab2dc2966efa1d877fe84)
1 /*$Id: fdmpiaij.c,v 1.39 2001/04/10 19:35:25 bsmith Exp bsmith $*/
2 
3 #include "src/mat/impls/aij/mpi/mpiaij.h"
4 #include "src/vec/vecimpl.h"
5 
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*);
9 
10 #undef __FUNCT__
11 #define __FUNCT__ "MatFDColoringCreate_MPIAIJ"
12 int MatFDColoringCreate_MPIAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c)
13 {
14   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
15   int        i,*is,n,nrows,j,k,m,*rows = 0,ierr,*A_ci,*A_cj,ncols,col;
16   int        nis = iscoloring->n,*ncolsonproc,size,nctot,*cols,*disp,*B_ci,*B_cj;
17   int        *rowhit,M = mat->m,cstart = aij->cstart,cend = aij->cend,colb;
18   int        *columnsforrow,l;
19   IS         *isa;
20   PetscTruth done,flg;
21 
22   PetscFunctionBegin;
23   if (!mat->assembled) {
24     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Matrix must be assembled first; MatAssemblyBegin/End();");
25   }
26 
27   ierr = ISColoringGetIS(iscoloring,PETSC_IGNORE,&isa);CHKERRQ(ierr);
28   c->M             = mat->M;  /* set the global rows and columns and local rows */
29   c->N             = mat->N;
30   c->m             = mat->m;
31   c->rstart        = aij->rstart;
32 
33   c->ncolors       = nis;
34   ierr             = PetscMalloc(nis*sizeof(int),&c->ncolumns);CHKERRQ(ierr);
35   ierr             = PetscMalloc(nis*sizeof(int*),&c->columns);CHKERRQ(ierr);
36   ierr             = PetscMalloc(nis*sizeof(int),&c->nrows);CHKERRQ(ierr);
37   ierr             = PetscMalloc(nis*sizeof(int*),&c->rows);CHKERRQ(ierr);
38   ierr             = PetscMalloc(nis*sizeof(int*),&c->columnsforrow);CHKERRQ(ierr);
39   PetscLogObjectMemory(c,5*nis*sizeof(int));
40 
41   /* Allow access to data structures of local part of matrix */
42   if (!aij->colmap) {
43     ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
44   }
45   /*
46       Calls the _SeqAIJ() version of these routines to make sure it does not
47      get the reduced (by inodes) version of I and J
48   */
49   ierr = MatGetColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr);
50   ierr = MatGetColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr);
51 
52   ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr);
53   ierr = PetscMalloc(2*size*sizeof(int*),&ncolsonproc);CHKERRQ(ierr);
54   disp = ncolsonproc + size;
55 
56   ierr = PetscMalloc((M+1)*sizeof(int),&rowhit);CHKERRQ(ierr);
57   ierr = PetscMalloc((M+1)*sizeof(int),&columnsforrow);CHKERRQ(ierr);
58 
59   /*
60      Temporary option to allow for debugging/testing
61   */
62   ierr = PetscOptionsHasName(PETSC_NULL,"-matfdcoloring_slow",&flg);CHKERRQ(ierr);
63 
64   for (i=0; i<nis; i++) {
65     ierr = ISGetLocalSize(isa[i],&n);CHKERRQ(ierr);
66     ierr = ISGetIndices(isa[i],&is);CHKERRQ(ierr);
67     c->ncolumns[i] = n;
68     c->ncolumns[i] = n;
69     if (n) {
70       ierr = PetscMalloc(n*sizeof(int),&c->columns[i]);CHKERRQ(ierr);
71       PetscLogObjectMemory(c,n*sizeof(int));
72       ierr = PetscMemcpy(c->columns[i],is,n*sizeof(int));CHKERRQ(ierr);
73     } else {
74       c->columns[i]  = 0;
75     }
76 
77     /* Determine the total (parallel) number of columns of this color */
78     ierr = MPI_Allgather(&n,1,MPI_INT,ncolsonproc,1,MPI_INT,mat->comm);CHKERRQ(ierr);
79     nctot = 0; for (j=0; j<size; j++) {nctot += ncolsonproc[j];}
80     if (!nctot) SETERRQ(PETSC_ERR_PLIB,"Invalid coloring of matrix detected");
81 
82     disp[0] = 0;
83     for (j=1; j<size; j++) {
84       disp[j] = disp[j-1] + ncolsonproc[j-1];
85     }
86 
87     /* Get complete list of columns for color on each processor */
88     ierr = PetscMalloc(nctot*sizeof(int),&cols);CHKERRQ(ierr);
89     ierr = MPI_Allgatherv(is,n,MPI_INT,cols,ncolsonproc,disp,MPI_INT,mat->comm);CHKERRQ(ierr);
90 
91     /*
92        Mark all rows affect by these columns
93     */
94     if (!flg) {/*-----------------------------------------------------------------------------*/
95       /* crude, fast version */
96       ierr = PetscMemzero(rowhit,M*sizeof(int));CHKERRQ(ierr);
97       /* loop over columns*/
98       for (j=0; j<nctot; j++) {
99         col  = cols[j];
100         if (col >= cstart && col < cend) {
101           /* column is in diagonal block of matrix */
102           rows = A_cj + A_ci[col-cstart];
103           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
104         } else {
105 #if defined (PETSC_USE_CTABLE)
106           ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr)
107 	  colb --;
108 #else
109           colb = aij->colmap[col] - 1;
110 #endif
111           if (colb == -1) {
112             m = 0;
113           } else {
114             rows = B_cj + B_ci[colb];
115             m    = B_ci[colb+1] - B_ci[colb];
116           }
117         }
118         /* loop over columns marking them in rowhit */
119         for (k=0; k<m; k++) {
120           rowhit[*rows++] = col + 1;
121         }
122       }
123 
124       /* count the number of hits */
125       nrows = 0;
126       for (j=0; j<M; j++) {
127         if (rowhit[j]) nrows++;
128       }
129       c->nrows[i]         = nrows;
130       ierr                = PetscMalloc((nrows+1)*sizeof(int),&c->rows[i]);CHKERRQ(ierr);
131       ierr                = PetscMalloc((nrows+1)*sizeof(int),&c->columnsforrow[i]);CHKERRQ(ierr);
132       PetscLogObjectMemory(c,2*(nrows+1)*sizeof(int));
133       nrows = 0;
134       for (j=0; j<M; j++) {
135         if (rowhit[j]) {
136           c->rows[i][nrows]           = j;
137           c->columnsforrow[i][nrows] = rowhit[j] - 1;
138           nrows++;
139         }
140       }
141     } else {/*-------------------------------------------------------------------------------*/
142       /* slow version, using rowhit as a linked list */
143       int currentcol,fm,mfm;
144       rowhit[M] = M;
145       nrows     = 0;
146       /* loop over columns*/
147       for (j=0; j<nctot; j++) {
148         col  = cols[j];
149         if (col >= cstart && col < cend) {
150           /* column is in diagonal block of matrix */
151           rows = A_cj + A_ci[col-cstart];
152           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
153         } else {
154 #if defined (PETSC_USE_CTABLE)
155 	  ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr);
156           colb --;
157 #else
158           colb = aij->colmap[col] - 1;
159 #endif
160           if (colb == -1) {
161             m = 0;
162           } else {
163             rows = B_cj + B_ci[colb];
164             m    = B_ci[colb+1] - B_ci[colb];
165           }
166         }
167         /* loop over columns marking them in rowhit */
168         fm    = M; /* fm points to first entry in linked list */
169         for (k=0; k<m; k++) {
170           currentcol = *rows++;
171 	  /* is it already in the list? */
172           do {
173             mfm  = fm;
174             fm   = rowhit[fm];
175           } while (fm < currentcol);
176           /* not in list so add it */
177           if (fm != currentcol) {
178             nrows++;
179             columnsforrow[currentcol] = col;
180             /* next three lines insert new entry into linked list */
181             rowhit[mfm]               = currentcol;
182             rowhit[currentcol]        = fm;
183             fm                        = currentcol;
184             /* fm points to present position in list since we know the columns are sorted */
185           } else {
186             SETERRQ(PETSC_ERR_PLIB,"Invalid coloring of matrix detected");
187           }
188         }
189       }
190       c->nrows[i]         = nrows;
191       ierr = PetscMalloc((nrows+1)*sizeof(int),&c->rows[i]);CHKERRQ(ierr);
192       ierr = PetscMalloc((nrows+1)*sizeof(int),&c->columnsforrow[i]);CHKERRQ(ierr);
193       PetscLogObjectMemory(c,(nrows+1)*sizeof(int));
194       /* now store the linked list of rows into c->rows[i] */
195       nrows = 0;
196       fm    = rowhit[M];
197       do {
198         c->rows[i][nrows]            = fm;
199         c->columnsforrow[i][nrows++] = columnsforrow[fm];
200         fm                           = rowhit[fm];
201       } while (fm < M);
202     } /* ---------------------------------------------------------------------------------------*/
203     ierr = PetscFree(cols);CHKERRQ(ierr);
204   }
205 
206   /* Optimize by adding the vscale, and scaleforrow[][] fields */
207   /*
208        vscale will contain the "diagonal" on processor scalings followed by the off processor
209   */
210   ierr = VecCreateGhost(mat->comm,aij->A->m,PETSC_DETERMINE,aij->B->n,aij->garray,&c->vscale);CHKERRQ(ierr)
211   ierr = PetscMalloc(c->ncolors*sizeof(int*),&c->vscaleforrow);CHKERRQ(ierr);
212   for (k=0; k<c->ncolors; k++) {
213     ierr = PetscMalloc((c->nrows[k]+1)*sizeof(int),&c->vscaleforrow[k]);CHKERRQ(ierr);
214     for (l=0; l<c->nrows[k]; l++) {
215       col = c->columnsforrow[k][l];
216       if (col >= cstart && col < cend) {
217         /* column is in diagonal block of matrix */
218         colb = col - cstart;
219       } else {
220         /* column  is in "off-processor" part */
221 #if defined (PETSC_USE_CTABLE)
222         ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr);
223         colb --;
224 #else
225         colb = aij->colmap[col] - 1;
226 #endif
227         colb += cend - cstart;
228       }
229       c->vscaleforrow[k][l] = colb;
230     }
231   }
232   ierr = ISColoringRestoreIS(iscoloring,&isa);CHKERRQ(ierr);
233 
234   ierr = PetscFree(rowhit);CHKERRQ(ierr);
235   ierr = PetscFree(columnsforrow);CHKERRQ(ierr);
236   ierr = PetscFree(ncolsonproc);CHKERRQ(ierr);
237   ierr = MatRestoreColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr);
238   ierr = MatRestoreColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr);
239   PetscFunctionReturn(0);
240 }
241 
242 
243 
244 
245 
246 
247