xref: /petsc/src/mat/impls/aij/mpi/fdmpiaij.c (revision 026e39d04bcf2a445c0a9332c845c324154b2e8f)
1 
2 
3 #ifndef lint
4 static char vcid[] = "$Id: fdmpiaij.c,v 1.3 1996/11/27 22:53:12 bsmith Exp balay $";
5 #endif
6 
7 #include "src/mat/impls/aij/mpi/mpiaij.h"
8 #include "src/vec/vecimpl.h"
9 #include "petsc.h"
10 
11 extern int CreateColmap_MPIAIJ_Private(Mat);
12 extern int MatGetColumnIJ_SeqAIJ(Mat,int,PetscTruth,int*,int**,int**,PetscTruth*);
13 extern int MatRestoreColumnIJ_SeqAIJ(Mat,int,PetscTruth,int*,int**,int**,PetscTruth*);
14 
15 #undef __FUNCTION__
16 #define __FUNCTION__ "MatFDColoringCreate_MPIAIJ"
17 int MatFDColoringCreate_MPIAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c)
18 {
19   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
20   int        i,*is,n,nrows,j,k,m,*rows = 0,ierr,*A_ci,*A_cj,ncols,col,flg;
21   int        nis = iscoloring->n,*ncolsonproc,size,nctot,*cols,*disp,*B_ci,*B_cj;
22   int        *rowhit, M = mat->m,cstart = aij->cstart, cend = aij->cend,colb;
23   int        *columnsforrow;
24   IS         *isa = iscoloring->is;
25   PetscTruth done;
26 
27   c->ncolors       = nis;
28   c->ncolumns      = (int *) PetscMalloc( nis*sizeof(int) );   CHKPTRQ(c->ncolumns);
29   c->columns       = (int **) PetscMalloc( nis*sizeof(int *)); CHKPTRQ(c->columns);
30   c->nrows         = (int *) PetscMalloc( nis*sizeof(int) );   CHKPTRQ(c->nrows);
31   c->rows          = (int **) PetscMalloc( nis*sizeof(int *)); CHKPTRQ(c->rows);
32   c->columnsforrow = (int **) PetscMalloc( nis*sizeof(int *)); CHKPTRQ(c->columnsforrow);
33 
34   /* Allow access to data structures of local part of matrix */
35   if (!aij->colmap) {
36     ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
37   }
38   /*
39       Calls the _SeqAIJ() version of these routines to make sure it does not
40      get the reduced (by inodes) version of I and J
41   */
42   ierr = MatGetColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done); CHKERRQ(ierr);
43   ierr = MatGetColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done); CHKERRQ(ierr);
44 
45   MPI_Comm_size(mat->comm,&size);
46   ncolsonproc = (int *) PetscMalloc( 2*size*sizeof(int *) ); CHKPTRQ(ncolsonproc);
47   disp        = ncolsonproc + size;
48 
49   rowhit        = (int *) PetscMalloc( (M+1)*sizeof(int) ); CHKPTRQ(rowhit);
50   columnsforrow = (int *) PetscMalloc( (M+1)*sizeof(int) );CHKPTRQ(columnsforrow);
51 
52   /*
53      Temporary option to allow for debugging/testing
54   */
55   ierr = OptionsHasName(0,"-matfdcoloring_slow",&flg);
56 
57   for ( i=0; i<nis; i++ ) {
58     ierr = ISGetSize(isa[i],&n); CHKERRQ(ierr);
59     ierr = ISGetIndices(isa[i],&is); CHKERRQ(ierr);
60     c->ncolumns[i] = n;
61     c->ncolumns[i] = n;
62     if (n) {
63       c->columns[i]  = (int *) PetscMalloc( n*sizeof(int) ); CHKPTRQ(c->columns[i]);
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,"MatFDColoringCreate_MPIAIJ: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       nrows = 0;
131       for ( j=0; j<M; j++ ) {
132         if (rowhit[j]) {
133           c->rows[i][nrows]           = j;
134           c->columnsforrow[i][nrows] = rowhit[j] - 1;
135           nrows++;
136         }
137       }
138     } else {/*-------------------------------------------------------------------------------*/
139       /* efficient version, using rowhit as a linked list */
140       int currentcol,fm,mfm;
141       rowhit[M] = M;
142       nrows     = 0;
143       /* loop over columns*/
144       for ( j=0; j<nctot; j++ ) {
145         col  = cols[j];
146         if (col >= cstart && col < cend) {
147           /* column is in diagonal block of matrix */
148           rows = A_cj + A_ci[col-cstart];
149           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
150         } else {
151           colb = aij->colmap[col] - 1;
152           if (colb == -1) {
153             m = 0;
154           } else {
155             rows = B_cj + B_ci[colb];
156             m    = B_ci[colb+1] - B_ci[colb];
157           }
158         }
159         /* loop over columns marking them in rowhit */
160         fm    = M; /* fm points to first entry in linked list */
161         for ( k=0; k<m; k++ ) {
162           currentcol = *rows++;
163 	  /* is it already in the list? */
164           do {
165             mfm  = fm;
166             fm   = rowhit[fm];
167           } while (fm < currentcol);
168           /* not in list so add it */
169           if (fm != currentcol) {
170             nrows++;
171             columnsforrow[currentcol] = col;
172             /* next three lines insert new entry into linked list */
173             rowhit[mfm]               = currentcol;
174             rowhit[currentcol]        = fm;
175             fm                        = currentcol;
176             /* fm points to present position in list since we know the columns are sorted */
177           } else {
178             SETERRQ(1,"MatFDColoringCreate_MPIAIJ:Invalid coloring");
179           }
180         }
181       }
182       c->nrows[i]         = nrows;
183       c->rows[i]          = (int *)PetscMalloc((nrows+1)*sizeof(int));CHKPTRQ(c->rows[i]);
184       c->columnsforrow[i] = (int *)PetscMalloc((nrows+1)*sizeof(int));CHKPTRQ(c->columnsforrow[i]);
185       /* now store the linked list of rows into c->rows[i] */
186       nrows = 0;
187       fm    = rowhit[M];
188       do {
189         c->rows[i][nrows]            = fm;
190         c->columnsforrow[i][nrows++] = columnsforrow[fm];
191         fm                           = rowhit[fm];
192       } while (fm < M);
193     } /* ---------------------------------------------------------------------------------------*/
194     PetscFree(cols);
195   }
196   PetscFree(rowhit);
197   PetscFree(columnsforrow);
198   PetscFree(ncolsonproc);
199   ierr = MatRestoreColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done); CHKERRQ(ierr);
200   ierr = MatRestoreColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done); CHKERRQ(ierr);
201 
202   c->scale  = (Scalar *) PetscMalloc( 2*mat->N*sizeof(Scalar) ); CHKPTRQ(c->scale);
203   c->wscale = c->scale + mat->N;
204   return 0;
205 }
206 
207