xref: /petsc/src/mat/impls/aij/mpi/fdmpiaij.c (revision fa46199cd6736f835a9dfd5fb06fcbb3763bab4e) !
1005c665bSBarry Smith 
2a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
3*fa46199cSSatish Balay static char vcid[] = "$Id: fdmpiaij.c,v 1.17 1999/01/08 21:47:51 balay Exp balay $";
4a64fbb32SBarry Smith #endif
5a64fbb32SBarry Smith 
66eaac0f3SBarry Smith #include "src/mat/impls/aij/mpi/mpiaij.h"
7a64fbb32SBarry Smith #include "src/vec/vecimpl.h"
8a64fbb32SBarry Smith #include "petsc.h"
9a64fbb32SBarry Smith 
106eaac0f3SBarry Smith extern int CreateColmap_MPIAIJ_Private(Mat);
1143a90d84SBarry Smith extern int MatGetColumnIJ_SeqAIJ(Mat,int,PetscTruth,int*,int**,int**,PetscTruth*);
1243a90d84SBarry Smith extern int MatRestoreColumnIJ_SeqAIJ(Mat,int,PetscTruth,int*,int**,int**,PetscTruth*);
13a64fbb32SBarry Smith 
145615d1e5SSatish Balay #undef __FUNC__
15d4bb536fSBarry Smith #define __FUNC__ "MatFDColoringCreate_MPIAIJ"
166eaac0f3SBarry Smith int MatFDColoringCreate_MPIAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c)
17a64fbb32SBarry Smith {
186eaac0f3SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
196eaac0f3SBarry Smith   int        i,*is,n,nrows,j,k,m,*rows = 0,ierr,*A_ci,*A_cj,ncols,col,flg;
206eaac0f3SBarry Smith   int        nis = iscoloring->n,*ncolsonproc,size,nctot,*cols,*disp,*B_ci,*B_cj;
216eaac0f3SBarry Smith   int        *rowhit, M = mat->m,cstart = aij->cstart, cend = aij->cend,colb;
226eaac0f3SBarry Smith   int        *columnsforrow;
23a64fbb32SBarry Smith   IS         *isa = iscoloring->is;
24a64fbb32SBarry Smith   PetscTruth done;
25a64fbb32SBarry Smith 
263a40ed3dSBarry Smith   PetscFunctionBegin;
27522c5e43SBarry Smith   if (!mat->assembled) {
28a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,1,"Matrix must be assembled first; MatAssemblyBegin/End();");
29522c5e43SBarry Smith   }
30522c5e43SBarry Smith 
31005c665bSBarry Smith   c->M             = mat->M;  /* set the global rows and columns and local rows */
32005c665bSBarry Smith   c->N             = mat->N;
33005c665bSBarry Smith   c->m             = mat->m;
34005c665bSBarry Smith   c->rstart        = aij->rstart;
35005c665bSBarry Smith 
36a64fbb32SBarry Smith   c->ncolors       = nis;
37a64fbb32SBarry Smith   c->ncolumns      = (int *) PetscMalloc( nis*sizeof(int) );   CHKPTRQ(c->ncolumns);
38a64fbb32SBarry Smith   c->columns       = (int **) PetscMalloc( nis*sizeof(int *)); CHKPTRQ(c->columns);
39a64fbb32SBarry Smith   c->nrows         = (int *) PetscMalloc( nis*sizeof(int) );   CHKPTRQ(c->nrows);
40a64fbb32SBarry Smith   c->rows          = (int **) PetscMalloc( nis*sizeof(int *)); CHKPTRQ(c->rows);
41a64fbb32SBarry Smith   c->columnsforrow = (int **) PetscMalloc( nis*sizeof(int *)); CHKPTRQ(c->columnsforrow);
42eed86810SBarry Smith   PLogObjectMemory(c,5*nis*sizeof(int));
436eaac0f3SBarry Smith 
446eaac0f3SBarry Smith   /* Allow access to data structures of local part of matrix */
456eaac0f3SBarry Smith   if (!aij->colmap) {
466eaac0f3SBarry Smith     ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
476eaac0f3SBarry Smith   }
4843a90d84SBarry Smith   /*
4943a90d84SBarry Smith       Calls the _SeqAIJ() version of these routines to make sure it does not
5043a90d84SBarry Smith      get the reduced (by inodes) version of I and J
5143a90d84SBarry Smith   */
5243a90d84SBarry Smith   ierr = MatGetColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done); CHKERRQ(ierr);
5343a90d84SBarry Smith   ierr = MatGetColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done); CHKERRQ(ierr);
546eaac0f3SBarry Smith 
556eaac0f3SBarry Smith   MPI_Comm_size(mat->comm,&size);
566eaac0f3SBarry Smith   ncolsonproc = (int *) PetscMalloc( 2*size*sizeof(int *) ); CHKPTRQ(ncolsonproc);
576eaac0f3SBarry Smith   disp        = ncolsonproc + size;
586eaac0f3SBarry Smith 
596eaac0f3SBarry Smith   rowhit        = (int *) PetscMalloc( (M+1)*sizeof(int) ); CHKPTRQ(rowhit);
606eaac0f3SBarry Smith   columnsforrow = (int *) PetscMalloc( (M+1)*sizeof(int) );CHKPTRQ(columnsforrow);
616eaac0f3SBarry Smith 
62a64fbb32SBarry Smith   /*
63a64fbb32SBarry Smith      Temporary option to allow for debugging/testing
64a64fbb32SBarry Smith   */
65a64fbb32SBarry Smith   ierr = OptionsHasName(0,"-matfdcoloring_slow",&flg);
666eaac0f3SBarry Smith 
67a64fbb32SBarry Smith   for ( i=0; i<nis; i++ ) {
68a64fbb32SBarry Smith     ierr = ISGetSize(isa[i],&n); CHKERRQ(ierr);
69a64fbb32SBarry Smith     ierr = ISGetIndices(isa[i],&is); CHKERRQ(ierr);
70a64fbb32SBarry Smith     c->ncolumns[i] = n;
716eaac0f3SBarry Smith     c->ncolumns[i] = n;
72a64fbb32SBarry Smith     if (n) {
73a64fbb32SBarry Smith       c->columns[i]  = (int *) PetscMalloc( n*sizeof(int) ); CHKPTRQ(c->columns[i]);
74eed86810SBarry Smith       PLogObjectMemory(c,n*sizeof(int));
75a64fbb32SBarry Smith       PetscMemcpy(c->columns[i],is,n*sizeof(int));
76a64fbb32SBarry Smith     } else {
77a64fbb32SBarry Smith       c->columns[i]  = 0;
78a64fbb32SBarry Smith     }
79a64fbb32SBarry Smith 
806eaac0f3SBarry Smith     /* Determine the total (parallel) number of columns of this color */
81ca161407SBarry Smith     ierr = MPI_Allgather(&n,1,MPI_INT,ncolsonproc,1,MPI_INT,mat->comm);CHKERRQ(ierr);
826eaac0f3SBarry Smith     nctot = 0; for ( j=0; j<size; j++ ) {nctot += ncolsonproc[j];}
83a8c6a408SBarry Smith     if (!nctot) SETERRQ(PETSC_ERR_PLIB,0,"Invalid coloring of matrix detected");
846eaac0f3SBarry Smith 
856eaac0f3SBarry Smith     disp[0] = 0;
866eaac0f3SBarry Smith     for ( j=1; j<size; j++ ) {
876eaac0f3SBarry Smith       disp[j] = disp[j-1] + ncolsonproc[j-1];
886eaac0f3SBarry Smith     }
896eaac0f3SBarry Smith 
906eaac0f3SBarry Smith     /* Get complete list of columns for color on each processor */
916eaac0f3SBarry Smith     cols = (int *) PetscMalloc( nctot*sizeof(int) ); CHKPTRQ(cols);
92ca161407SBarry Smith     ierr = MPI_Allgatherv(is,n,MPI_INT,cols,ncolsonproc,disp,MPI_INT,mat->comm);CHKERRQ(ierr);
936eaac0f3SBarry Smith 
946eaac0f3SBarry Smith /*
956eaac0f3SBarry Smith for ( j=0; j<nctot; j++ ) {
966eaac0f3SBarry Smith   printf("color %d %d col %d\n",i,j,cols[j]);
976eaac0f3SBarry Smith }
986eaac0f3SBarry Smith */
996eaac0f3SBarry Smith 
1006eaac0f3SBarry Smith     /*
1016eaac0f3SBarry Smith        Mark all rows affect by these columns
1026eaac0f3SBarry Smith     */
1036eaac0f3SBarry Smith     if (flg) {/*-----------------------------------------------------------------------------*/
1046eaac0f3SBarry Smith       /* crude, slow version */
1056eaac0f3SBarry Smith       PetscMemzero(rowhit,M*sizeof(int));
106a64fbb32SBarry Smith       /* loop over columns*/
1076eaac0f3SBarry Smith       for ( j=0; j<nctot; j++ ) {
1086eaac0f3SBarry Smith         col  = cols[j];
1096eaac0f3SBarry Smith         if (col >= cstart && col < cend) {
1106eaac0f3SBarry Smith           /* column is in diagonal block of matrix */
1116eaac0f3SBarry Smith           rows = A_cj + A_ci[col-cstart];
1126eaac0f3SBarry Smith           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
1136eaac0f3SBarry Smith         } else {
114b3d2dc96SSatish Balay #if defined (USE_CTABLE)
115*fa46199cSSatish Balay           ierr = TableFind(aij->colmap,col+1,&colb); CHKERRQ(ierr)
116*fa46199cSSatish Balay 	  colb --;
117b3d2dc96SSatish Balay #else
1186eaac0f3SBarry Smith           colb = aij->colmap[col] - 1;
119b3d2dc96SSatish Balay #endif
1206eaac0f3SBarry Smith           if (colb == -1) {
1216eaac0f3SBarry Smith             m = 0;
1226eaac0f3SBarry Smith           } else {
1236eaac0f3SBarry Smith             rows = B_cj + B_ci[colb];
1246eaac0f3SBarry Smith             m    = B_ci[colb+1] - B_ci[colb];
1256eaac0f3SBarry Smith           }
1266eaac0f3SBarry Smith         }
127a64fbb32SBarry Smith         /* loop over columns marking them in rowhit */
128a64fbb32SBarry Smith         for ( k=0; k<m; k++ ) {
129a64fbb32SBarry Smith           rowhit[*rows++] = col + 1;
130a64fbb32SBarry Smith         }
131a64fbb32SBarry Smith       }
1326eaac0f3SBarry Smith 
1336eaac0f3SBarry Smith /*
1346eaac0f3SBarry Smith printf("for col %d found rows \n",i);
1356eaac0f3SBarry Smith for ( j=0; j<M; j++ ) printf("rhow hit %d %d\n",j,rowhit[j]);
1366eaac0f3SBarry Smith */
1376eaac0f3SBarry Smith 
138a64fbb32SBarry Smith       /* count the number of hits */
139a64fbb32SBarry Smith       nrows = 0;
1406eaac0f3SBarry Smith       for ( j=0; j<M; j++ ) {
141a64fbb32SBarry Smith         if (rowhit[j]) nrows++;
142a64fbb32SBarry Smith       }
143a64fbb32SBarry Smith       c->nrows[i]         = nrows;
1446eaac0f3SBarry Smith       c->rows[i]          = (int *) PetscMalloc((nrows+1)*sizeof(int)); CHKPTRQ(c->rows[i]);
1456eaac0f3SBarry Smith       c->columnsforrow[i] = (int *) PetscMalloc((nrows+1)*sizeof(int)); CHKPTRQ(c->columnsforrow[i]);
146eed86810SBarry Smith       PLogObjectMemory(c,2*(nrows+1)*sizeof(int));
147a64fbb32SBarry Smith       nrows = 0;
1486eaac0f3SBarry Smith       for ( j=0; j<M; j++ ) {
149a64fbb32SBarry Smith         if (rowhit[j]) {
150a64fbb32SBarry Smith           c->rows[i][nrows]           = j;
151a64fbb32SBarry Smith           c->columnsforrow[i][nrows] = rowhit[j] - 1;
152a64fbb32SBarry Smith           nrows++;
153a64fbb32SBarry Smith         }
154a64fbb32SBarry Smith       }
155a64fbb32SBarry Smith     } else {/*-------------------------------------------------------------------------------*/
156a64fbb32SBarry Smith       /* efficient version, using rowhit as a linked list */
1576eaac0f3SBarry Smith       int currentcol,fm,mfm;
1586eaac0f3SBarry Smith       rowhit[M] = M;
159a64fbb32SBarry Smith       nrows     = 0;
160a64fbb32SBarry Smith       /* loop over columns*/
1616eaac0f3SBarry Smith       for ( j=0; j<nctot; j++ ) {
1626eaac0f3SBarry Smith         col  = cols[j];
1636eaac0f3SBarry Smith         if (col >= cstart && col < cend) {
1646eaac0f3SBarry Smith           /* column is in diagonal block of matrix */
1656eaac0f3SBarry Smith           rows = A_cj + A_ci[col-cstart];
1666eaac0f3SBarry Smith           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
1676eaac0f3SBarry Smith         } else {
168b3d2dc96SSatish Balay #if defined (USE_CTABLE)
169*fa46199cSSatish Balay 	  ierr = TableFind(aij->colmap,col+1,&colb); CHKERRQ(ierr);
170*fa46199cSSatish Balay           colb --;
171b3d2dc96SSatish Balay #else
1726eaac0f3SBarry Smith           colb = aij->colmap[col] - 1;
173b3d2dc96SSatish Balay #endif
1746eaac0f3SBarry Smith           if (colb == -1) {
1756eaac0f3SBarry Smith             m = 0;
1766eaac0f3SBarry Smith           } else {
1776eaac0f3SBarry Smith             rows = B_cj + B_ci[colb];
1786eaac0f3SBarry Smith             m    = B_ci[colb+1] - B_ci[colb];
1796eaac0f3SBarry Smith           }
1806eaac0f3SBarry Smith         }
181a64fbb32SBarry Smith         /* loop over columns marking them in rowhit */
1826eaac0f3SBarry Smith         fm    = M; /* fm points to first entry in linked list */
183a64fbb32SBarry Smith         for ( k=0; k<m; k++ ) {
184a64fbb32SBarry Smith           currentcol = *rows++;
185a64fbb32SBarry Smith 	  /* is it already in the list? */
186a64fbb32SBarry Smith           do {
187a64fbb32SBarry Smith             mfm  = fm;
188a64fbb32SBarry Smith             fm   = rowhit[fm];
189a64fbb32SBarry Smith           } while (fm < currentcol);
190a64fbb32SBarry Smith           /* not in list so add it */
191a64fbb32SBarry Smith           if (fm != currentcol) {
192a64fbb32SBarry Smith             nrows++;
193a64fbb32SBarry Smith             columnsforrow[currentcol] = col;
194a64fbb32SBarry Smith             /* next three lines insert new entry into linked list */
195a64fbb32SBarry Smith             rowhit[mfm]               = currentcol;
196a64fbb32SBarry Smith             rowhit[currentcol]        = fm;
197a64fbb32SBarry Smith             fm                        = currentcol;
198a64fbb32SBarry Smith             /* fm points to present position in list since we know the columns are sorted */
199a64fbb32SBarry Smith           } else {
200a8c6a408SBarry Smith             SETERRQ(PETSC_ERR_PLIB,0,"Invalid coloring of matrix detected");
201a64fbb32SBarry Smith           }
202a64fbb32SBarry Smith         }
203a64fbb32SBarry Smith       }
204a64fbb32SBarry Smith       c->nrows[i]         = nrows;
2056eaac0f3SBarry Smith       c->rows[i]          = (int *)PetscMalloc((nrows+1)*sizeof(int));CHKPTRQ(c->rows[i]);
2066eaac0f3SBarry Smith       c->columnsforrow[i] = (int *)PetscMalloc((nrows+1)*sizeof(int));CHKPTRQ(c->columnsforrow[i]);
207eed86810SBarry Smith       PLogObjectMemory(c,(nrows+1)*sizeof(int));
208a64fbb32SBarry Smith       /* now store the linked list of rows into c->rows[i] */
209a64fbb32SBarry Smith       nrows = 0;
2106eaac0f3SBarry Smith       fm    = rowhit[M];
211a64fbb32SBarry Smith       do {
212a64fbb32SBarry Smith         c->rows[i][nrows]            = fm;
213a64fbb32SBarry Smith         c->columnsforrow[i][nrows++] = columnsforrow[fm];
214a64fbb32SBarry Smith         fm                           = rowhit[fm];
2156eaac0f3SBarry Smith       } while (fm < M);
2166eaac0f3SBarry Smith     } /* ---------------------------------------------------------------------------------------*/
2176eaac0f3SBarry Smith     PetscFree(cols);
2186eaac0f3SBarry Smith   }
219a64fbb32SBarry Smith   PetscFree(rowhit);
220a64fbb32SBarry Smith   PetscFree(columnsforrow);
2216eaac0f3SBarry Smith   PetscFree(ncolsonproc);
22243a90d84SBarry Smith   ierr = MatRestoreColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done); CHKERRQ(ierr);
22343a90d84SBarry Smith   ierr = MatRestoreColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done); CHKERRQ(ierr);
224a64fbb32SBarry Smith 
2256eaac0f3SBarry Smith   c->scale  = (Scalar *) PetscMalloc( 2*mat->N*sizeof(Scalar) ); CHKPTRQ(c->scale);
226eed86810SBarry Smith   PLogObjectMemory(c,2*mat->N*sizeof(Scalar));
2276eaac0f3SBarry Smith   c->wscale = c->scale + mat->N;
2283a40ed3dSBarry Smith   PetscFunctionReturn(0);
229a64fbb32SBarry Smith }
230a64fbb32SBarry Smith 
231