xref: /petsc/src/mat/impls/aij/mpi/fdmpiaij.c (revision 6849ba73f22fecb8f92ef896a42e4e8bd4cd6965)
1 
2 #include "src/mat/impls/aij/mpi/mpiaij.h"
3 
4 EXTERN PetscErrorCode CreateColmap_MPIAIJ_Private(Mat);
5 EXTERN PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat,int,PetscTruth,int*,int*[],int*[],PetscTruth*);
6 EXTERN PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat,int,PetscTruth,int*,int*[],int*[],PetscTruth*);
7 
8 #undef __FUNCT__
9 #define __FUNCT__ "MatFDColoringCreate_MPIAIJ"
10 PetscErrorCode MatFDColoringCreate_MPIAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c)
11 {
12   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
13   PetscErrorCode ierr;
14   int        i,*is,n,nrows,j,k,m,*rows = 0,*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;
20 
21   PetscFunctionBegin;
22   if (!mat->assembled) {
23     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Matrix must be assembled first; MatAssemblyBegin/End();");
24   }
25 
26   ierr = ISColoringGetIS(iscoloring,PETSC_IGNORE,&isa);CHKERRQ(ierr);
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;
31 
32   c->ncolors       = nis;
33   ierr             = PetscMalloc(nis*sizeof(int),&c->ncolumns);CHKERRQ(ierr);
34   ierr             = PetscMalloc(nis*sizeof(int*),&c->columns);CHKERRQ(ierr);
35   ierr             = PetscMalloc(nis*sizeof(int),&c->nrows);CHKERRQ(ierr);
36   ierr             = PetscMalloc(nis*sizeof(int*),&c->rows);CHKERRQ(ierr);
37   ierr             = PetscMalloc(nis*sizeof(int*),&c->columnsforrow);CHKERRQ(ierr);
38   PetscLogObjectMemory(c,5*nis*sizeof(int));
39 
40   /* Allow access to data structures of local part of matrix */
41   if (!aij->colmap) {
42     ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
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   ierr = MatGetColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr);
49   ierr = MatGetColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr);
50 
51   ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr);
52   ierr = PetscMalloc(2*size*sizeof(int*),&ncolsonproc);CHKERRQ(ierr);
53   disp = ncolsonproc + size;
54 
55   ierr = PetscMalloc((M+1)*sizeof(int),&rowhit);CHKERRQ(ierr);
56   ierr = PetscMalloc((M+1)*sizeof(int),&columnsforrow);CHKERRQ(ierr);
57 
58   /*
59      Temporary option to allow for debugging/testing
60   */
61   ierr = PetscOptionsHasName(PETSC_NULL,"-matfdcoloring_slow",&flg);CHKERRQ(ierr);
62 
63   for (i=0; i<nis; i++) {
64     ierr = ISGetLocalSize(isa[i],&n);CHKERRQ(ierr);
65     ierr = ISGetIndices(isa[i],&is);CHKERRQ(ierr);
66     c->ncolumns[i] = n;
67     c->ncolumns[i] = n;
68     if (n) {
69       ierr = PetscMalloc(n*sizeof(int),&c->columns[i]);CHKERRQ(ierr);
70       PetscLogObjectMemory(c,n*sizeof(int));
71       ierr = PetscMemcpy(c->columns[i],is,n*sizeof(int));CHKERRQ(ierr);
72     } else {
73       c->columns[i]  = 0;
74     }
75 
76     /* Determine the total (parallel) number of columns of this color */
77     ierr = MPI_Allgather(&n,1,MPI_INT,ncolsonproc,1,MPI_INT,mat->comm);CHKERRQ(ierr);
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     }
82 
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     ierr = PetscMalloc((nctot+1)*sizeof(int),&cols);CHKERRQ(ierr);
90     ierr = MPI_Allgatherv(is,n,MPI_INT,cols,ncolsonproc,disp,MPI_INT,mat->comm);CHKERRQ(ierr);
91 
92     /*
93        Mark all rows affect by these columns
94     */
95     if (!flg) {/*-----------------------------------------------------------------------------*/
96       /* crude, fast version */
97       ierr = PetscMemzero(rowhit,M*sizeof(int));CHKERRQ(ierr);
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           ierr = 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       }
124 
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       ierr                = PetscMalloc((nrows+1)*sizeof(int),&c->rows[i]);CHKERRQ(ierr);
132       ierr                = PetscMalloc((nrows+1)*sizeof(int),&c->columnsforrow[i]);CHKERRQ(ierr);
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 	  ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr);
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       ierr = PetscMalloc((nrows+1)*sizeof(int),&c->rows[i]);CHKERRQ(ierr);
193       ierr = PetscMalloc((nrows+1)*sizeof(int),&c->columnsforrow[i]);CHKERRQ(ierr);
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     ierr = PetscFree(cols);CHKERRQ(ierr);
205   }
206 
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   ierr = VecCreateGhost(mat->comm,aij->A->m,PETSC_DETERMINE,aij->B->n,aij->garray,&c->vscale);CHKERRQ(ierr)
212   ierr = PetscMalloc(c->ncolors*sizeof(int*),&c->vscaleforrow);CHKERRQ(ierr);
213   for (k=0; k<c->ncolors; k++) {
214     ierr = PetscMalloc((c->nrows[k]+1)*sizeof(int),&c->vscaleforrow[k]);CHKERRQ(ierr);
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         ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr);
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   ierr = ISColoringRestoreIS(iscoloring,&isa);CHKERRQ(ierr);
234 
235   ierr = PetscFree(rowhit);CHKERRQ(ierr);
236   ierr = PetscFree(columnsforrow);CHKERRQ(ierr);
237   ierr = PetscFree(ncolsonproc);CHKERRQ(ierr);
238   ierr = MatRestoreColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr);
239   ierr = MatRestoreColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr);
240   PetscFunctionReturn(0);
241 }
242 
243 
244 
245 
246 
247 
248