xref: /petsc/src/mat/impls/aij/mpi/fdmpiaij.c (revision 7eb4e439e85a62817f0f2ee27461d73cf6c9615d)
1 
2 #ifdef PETSC_RCS_HEADER
3 static char vcid[] = "$Id: fdmpiaij.c,v 1.16 1997/12/01 01:54:32 bsmith 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 	  colb = TableFind( aij->colmap, col + 1 ) - 1;
116 #else
117           colb = aij->colmap[col] - 1;
118 #endif
119           if (colb == -1) {
120             m = 0;
121           } else {
122             rows = B_cj + B_ci[colb];
123             m    = B_ci[colb+1] - B_ci[colb];
124           }
125         }
126         /* loop over columns marking them in rowhit */
127         for ( k=0; k<m; k++ ) {
128           rowhit[*rows++] = col + 1;
129         }
130       }
131 
132 /*
133 printf("for col %d found rows \n",i);
134 for ( j=0; j<M; j++ ) printf("rhow hit %d %d\n",j,rowhit[j]);
135 */
136 
137       /* count the number of hits */
138       nrows = 0;
139       for ( j=0; j<M; j++ ) {
140         if (rowhit[j]) nrows++;
141       }
142       c->nrows[i]         = nrows;
143       c->rows[i]          = (int *) PetscMalloc((nrows+1)*sizeof(int)); CHKPTRQ(c->rows[i]);
144       c->columnsforrow[i] = (int *) PetscMalloc((nrows+1)*sizeof(int)); CHKPTRQ(c->columnsforrow[i]);
145       PLogObjectMemory(c,2*(nrows+1)*sizeof(int));
146       nrows = 0;
147       for ( j=0; j<M; j++ ) {
148         if (rowhit[j]) {
149           c->rows[i][nrows]           = j;
150           c->columnsforrow[i][nrows] = rowhit[j] - 1;
151           nrows++;
152         }
153       }
154     } else {/*-------------------------------------------------------------------------------*/
155       /* efficient version, using rowhit as a linked list */
156       int currentcol,fm,mfm;
157       rowhit[M] = M;
158       nrows     = 0;
159       /* loop over columns*/
160       for ( j=0; j<nctot; j++ ) {
161         col  = cols[j];
162         if (col >= cstart && col < cend) {
163           /* column is in diagonal block of matrix */
164           rows = A_cj + A_ci[col-cstart];
165           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
166         } else {
167 #if defined (USE_CTABLE)
168 	  colb = TableFind( aij->colmap, col + 1 ) - 1;
169 #else
170           colb = aij->colmap[col] - 1;
171 #endif
172           if (colb == -1) {
173             m = 0;
174           } else {
175             rows = B_cj + B_ci[colb];
176             m    = B_ci[colb+1] - B_ci[colb];
177           }
178         }
179         /* loop over columns marking them in rowhit */
180         fm    = M; /* fm points to first entry in linked list */
181         for ( k=0; k<m; k++ ) {
182           currentcol = *rows++;
183 	  /* is it already in the list? */
184           do {
185             mfm  = fm;
186             fm   = rowhit[fm];
187           } while (fm < currentcol);
188           /* not in list so add it */
189           if (fm != currentcol) {
190             nrows++;
191             columnsforrow[currentcol] = col;
192             /* next three lines insert new entry into linked list */
193             rowhit[mfm]               = currentcol;
194             rowhit[currentcol]        = fm;
195             fm                        = currentcol;
196             /* fm points to present position in list since we know the columns are sorted */
197           } else {
198             SETERRQ(PETSC_ERR_PLIB,0,"Invalid coloring of matrix detected");
199           }
200         }
201       }
202       c->nrows[i]         = nrows;
203       c->rows[i]          = (int *)PetscMalloc((nrows+1)*sizeof(int));CHKPTRQ(c->rows[i]);
204       c->columnsforrow[i] = (int *)PetscMalloc((nrows+1)*sizeof(int));CHKPTRQ(c->columnsforrow[i]);
205       PLogObjectMemory(c,(nrows+1)*sizeof(int));
206       /* now store the linked list of rows into c->rows[i] */
207       nrows = 0;
208       fm    = rowhit[M];
209       do {
210         c->rows[i][nrows]            = fm;
211         c->columnsforrow[i][nrows++] = columnsforrow[fm];
212         fm                           = rowhit[fm];
213       } while (fm < M);
214     } /* ---------------------------------------------------------------------------------------*/
215     PetscFree(cols);
216   }
217   PetscFree(rowhit);
218   PetscFree(columnsforrow);
219   PetscFree(ncolsonproc);
220   ierr = MatRestoreColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done); CHKERRQ(ierr);
221   ierr = MatRestoreColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done); CHKERRQ(ierr);
222 
223   c->scale  = (Scalar *) PetscMalloc( 2*mat->N*sizeof(Scalar) ); CHKPTRQ(c->scale);
224   PLogObjectMemory(c,2*mat->N*sizeof(Scalar));
225   c->wscale = c->scale + mat->N;
226   PetscFunctionReturn(0);
227 }
228 
229