xref: /petsc/src/mat/impls/aij/mpi/fdmpiaij.c (revision 6831982abb6453c2f3e25efecb5d0951d333371e)
1 
2 #ifdef PETSC_RCS_HEADER
3 static char vcid[] = "$Id: fdmpiaij.c,v 1.25 1999/10/13 20:37:20 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   ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr);
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(PETSC_NULL,"-matfdcoloring_slow",&flg);CHKERRQ(ierr);
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       ierr = PetscMemcpy(c->columns[i],is,n*sizeof(int));CHKERRQ(ierr);
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        Mark all rows affect by these columns
96     */
97     if (flg) {/*-----------------------------------------------------------------------------*/
98       /* crude, slow version */
99       ierr = PetscMemzero(rowhit,M*sizeof(int));CHKERRQ(ierr);
100       /* loop over columns*/
101       for ( j=0; j<nctot; j++ ) {
102         col  = cols[j];
103         if (col >= cstart && col < cend) {
104           /* column is in diagonal block of matrix */
105           rows = A_cj + A_ci[col-cstart];
106           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
107         } else {
108 #if defined (PETSC_USE_CTABLE)
109           ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr)
110 	  colb --;
111 #else
112           colb = aij->colmap[col] - 1;
113 #endif
114           if (colb == -1) {
115             m = 0;
116           } else {
117             rows = B_cj + B_ci[colb];
118             m    = B_ci[colb+1] - B_ci[colb];
119           }
120         }
121         /* loop over columns marking them in rowhit */
122         for ( k=0; k<m; k++ ) {
123           rowhit[*rows++] = col + 1;
124         }
125       }
126 
127       /* count the number of hits */
128       nrows = 0;
129       for ( j=0; j<M; j++ ) {
130         if (rowhit[j]) nrows++;
131       }
132       c->nrows[i]         = nrows;
133       c->rows[i]          = (int *) PetscMalloc((nrows+1)*sizeof(int));CHKPTRQ(c->rows[i]);
134       c->columnsforrow[i] = (int *) PetscMalloc((nrows+1)*sizeof(int));CHKPTRQ(c->columnsforrow[i]);
135       PLogObjectMemory(c,2*(nrows+1)*sizeof(int));
136       nrows = 0;
137       for ( j=0; j<M; j++ ) {
138         if (rowhit[j]) {
139           c->rows[i][nrows]           = j;
140           c->columnsforrow[i][nrows] = rowhit[j] - 1;
141           nrows++;
142         }
143       }
144     } else {/*-------------------------------------------------------------------------------*/
145       /* efficient version, using rowhit as a linked list */
146       int currentcol,fm,mfm;
147       rowhit[M] = M;
148       nrows     = 0;
149       /* loop over columns*/
150       for ( j=0; j<nctot; j++ ) {
151         col  = cols[j];
152         if (col >= cstart && col < cend) {
153           /* column is in diagonal block of matrix */
154           rows = A_cj + A_ci[col-cstart];
155           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
156         } else {
157 #if defined (PETSC_USE_CTABLE)
158 	  ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr);
159           colb --;
160 #else
161           colb = aij->colmap[col] - 1;
162 #endif
163           if (colb == -1) {
164             m = 0;
165           } else {
166             rows = B_cj + B_ci[colb];
167             m    = B_ci[colb+1] - B_ci[colb];
168           }
169         }
170         /* loop over columns marking them in rowhit */
171         fm    = M; /* fm points to first entry in linked list */
172         for ( k=0; k<m; k++ ) {
173           currentcol = *rows++;
174 	  /* is it already in the list? */
175           do {
176             mfm  = fm;
177             fm   = rowhit[fm];
178           } while (fm < currentcol);
179           /* not in list so add it */
180           if (fm != currentcol) {
181             nrows++;
182             columnsforrow[currentcol] = col;
183             /* next three lines insert new entry into linked list */
184             rowhit[mfm]               = currentcol;
185             rowhit[currentcol]        = fm;
186             fm                        = currentcol;
187             /* fm points to present position in list since we know the columns are sorted */
188           } else {
189             SETERRQ(PETSC_ERR_PLIB,0,"Invalid coloring of matrix detected");
190           }
191         }
192       }
193       c->nrows[i]         = nrows;
194       c->rows[i]          = (int *)PetscMalloc((nrows+1)*sizeof(int));CHKPTRQ(c->rows[i]);
195       c->columnsforrow[i] = (int *)PetscMalloc((nrows+1)*sizeof(int));CHKPTRQ(c->columnsforrow[i]);
196       PLogObjectMemory(c,(nrows+1)*sizeof(int));
197       /* now store the linked list of rows into c->rows[i] */
198       nrows = 0;
199       fm    = rowhit[M];
200       do {
201         c->rows[i][nrows]            = fm;
202         c->columnsforrow[i][nrows++] = columnsforrow[fm];
203         fm                           = rowhit[fm];
204       } while (fm < M);
205     } /* ---------------------------------------------------------------------------------------*/
206     ierr = PetscFree(cols);CHKERRQ(ierr);
207   }
208   ierr = PetscFree(rowhit);CHKERRQ(ierr);
209   ierr = PetscFree(columnsforrow);CHKERRQ(ierr);
210   ierr = PetscFree(ncolsonproc);CHKERRQ(ierr);
211   ierr = MatRestoreColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr);
212   ierr = MatRestoreColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr);
213 
214   c->scale  = (Scalar *) PetscMalloc( 2*mat->N*sizeof(Scalar) );CHKPTRQ(c->scale);
215   PLogObjectMemory(c,2*mat->N*sizeof(Scalar));
216   c->wscale = c->scale + mat->N;
217   PetscFunctionReturn(0);
218 }
219 
220