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