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