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