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