xref: /petsc/src/mat/impls/aij/mpi/fdmpiaij.c (revision 43a90d8472dff9fb76015431d971d6cf7e7ec88a)
1a64fbb32SBarry Smith 
2a64fbb32SBarry Smith 
3a64fbb32SBarry Smith #ifndef lint
4*43a90d84SBarry Smith static char vcid[] = "$Id: fdmpiaij.c,v 1.2 1996/10/28 14:59:58 bsmith Exp bsmith $";
5a64fbb32SBarry Smith #endif
6a64fbb32SBarry Smith 
76eaac0f3SBarry Smith #include "src/mat/impls/aij/mpi/mpiaij.h"
8a64fbb32SBarry Smith #include "src/vec/vecimpl.h"
9a64fbb32SBarry Smith #include "petsc.h"
10a64fbb32SBarry Smith 
116eaac0f3SBarry Smith extern int CreateColmap_MPIAIJ_Private(Mat);
12*43a90d84SBarry Smith extern int MatGetColumnIJ_SeqAIJ(Mat,int,PetscTruth,int*,int**,int**,PetscTruth*);
13*43a90d84SBarry Smith extern int MatRestoreColumnIJ_SeqAIJ(Mat,int,PetscTruth,int*,int**,int**,PetscTruth*);
14a64fbb32SBarry Smith 
156eaac0f3SBarry Smith int MatFDColoringCreate_MPIAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c)
16a64fbb32SBarry Smith {
176eaac0f3SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
186eaac0f3SBarry Smith   int        i,*is,n,nrows,j,k,m,*rows = 0,ierr,*A_ci,*A_cj,ncols,col,flg;
196eaac0f3SBarry Smith   int        nis = iscoloring->n,*ncolsonproc,size,nctot,*cols,*disp,*B_ci,*B_cj;
206eaac0f3SBarry Smith   int        *rowhit, M = mat->m,cstart = aij->cstart, cend = aij->cend,colb;
216eaac0f3SBarry Smith   int        *columnsforrow;
22a64fbb32SBarry Smith   IS         *isa = iscoloring->is;
23a64fbb32SBarry Smith   PetscTruth done;
24a64fbb32SBarry Smith 
25a64fbb32SBarry Smith   c->ncolors       = nis;
26a64fbb32SBarry Smith   c->ncolumns      = (int *) PetscMalloc( nis*sizeof(int) );   CHKPTRQ(c->ncolumns);
27a64fbb32SBarry Smith   c->columns       = (int **) PetscMalloc( nis*sizeof(int *)); CHKPTRQ(c->columns);
28a64fbb32SBarry Smith   c->nrows         = (int *) PetscMalloc( nis*sizeof(int) );   CHKPTRQ(c->nrows);
29a64fbb32SBarry Smith   c->rows          = (int **) PetscMalloc( nis*sizeof(int *)); CHKPTRQ(c->rows);
30a64fbb32SBarry Smith   c->columnsforrow = (int **) PetscMalloc( nis*sizeof(int *)); CHKPTRQ(c->columnsforrow);
316eaac0f3SBarry Smith 
326eaac0f3SBarry Smith   /* Allow access to data structures of local part of matrix */
336eaac0f3SBarry Smith   if (!aij->colmap) {
346eaac0f3SBarry Smith     ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
356eaac0f3SBarry Smith   }
36*43a90d84SBarry Smith   /*
37*43a90d84SBarry Smith       Calls the _SeqAIJ() version of these routines to make sure it does not
38*43a90d84SBarry Smith      get the reduced (by inodes) version of I and J
39*43a90d84SBarry Smith   */
40*43a90d84SBarry Smith   ierr = MatGetColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done); CHKERRQ(ierr);
41*43a90d84SBarry Smith   ierr = MatGetColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done); CHKERRQ(ierr);
426eaac0f3SBarry Smith 
436eaac0f3SBarry Smith   MPI_Comm_size(mat->comm,&size);
446eaac0f3SBarry Smith   ncolsonproc = (int *) PetscMalloc( 2*size*sizeof(int *) ); CHKPTRQ(ncolsonproc);
456eaac0f3SBarry Smith   disp        = ncolsonproc + size;
466eaac0f3SBarry Smith 
476eaac0f3SBarry Smith   rowhit        = (int *) PetscMalloc( (M+1)*sizeof(int) ); CHKPTRQ(rowhit);
486eaac0f3SBarry Smith   columnsforrow = (int *) PetscMalloc( (M+1)*sizeof(int) );CHKPTRQ(columnsforrow);
496eaac0f3SBarry Smith 
50a64fbb32SBarry Smith   /*
51a64fbb32SBarry Smith      Temporary option to allow for debugging/testing
52a64fbb32SBarry Smith   */
53a64fbb32SBarry Smith   ierr = OptionsHasName(0,"-matfdcoloring_slow",&flg);
546eaac0f3SBarry Smith 
55a64fbb32SBarry Smith   for ( i=0; i<nis; i++ ) {
56a64fbb32SBarry Smith     ierr = ISGetSize(isa[i],&n); CHKERRQ(ierr);
57a64fbb32SBarry Smith     ierr = ISGetIndices(isa[i],&is); CHKERRQ(ierr);
58a64fbb32SBarry Smith     c->ncolumns[i] = n;
596eaac0f3SBarry Smith     c->ncolumns[i] = n;
60a64fbb32SBarry Smith     if (n) {
61a64fbb32SBarry Smith       c->columns[i]  = (int *) PetscMalloc( n*sizeof(int) ); CHKPTRQ(c->columns[i]);
62a64fbb32SBarry Smith       PetscMemcpy(c->columns[i],is,n*sizeof(int));
63a64fbb32SBarry Smith     } else {
64a64fbb32SBarry Smith       c->columns[i]  = 0;
65a64fbb32SBarry Smith     }
66a64fbb32SBarry Smith 
676eaac0f3SBarry Smith     /* Determine the total (parallel) number of columns of this color */
686eaac0f3SBarry Smith     MPI_Allgather(&n,1,MPI_INT,ncolsonproc,1,MPI_INT,mat->comm);
696eaac0f3SBarry Smith     nctot = 0; for ( j=0; j<size; j++ ) {nctot += ncolsonproc[j];}
706eaac0f3SBarry Smith     if (!nctot) SETERRQ(1,"MatFDColoringCreate_MPIAIJ:Invalid coloring");
716eaac0f3SBarry Smith 
726eaac0f3SBarry Smith     disp[0] = 0;
736eaac0f3SBarry Smith     for ( j=1; j<size; j++ ) {
746eaac0f3SBarry Smith       disp[j] = disp[j-1] + ncolsonproc[j-1];
756eaac0f3SBarry Smith     }
766eaac0f3SBarry Smith 
776eaac0f3SBarry Smith     /* Get complete list of columns for color on each processor */
786eaac0f3SBarry Smith     cols = (int *) PetscMalloc( nctot*sizeof(int) ); CHKPTRQ(cols);
796eaac0f3SBarry Smith     MPI_Allgatherv(is,n,MPI_INT,cols,ncolsonproc,disp,MPI_INT,mat->comm);
806eaac0f3SBarry Smith 
816eaac0f3SBarry Smith /*
826eaac0f3SBarry Smith for ( j=0; j<nctot; j++ ) {
836eaac0f3SBarry Smith   printf("color %d %d col %d\n",i,j,cols[j]);
846eaac0f3SBarry Smith }
856eaac0f3SBarry Smith */
866eaac0f3SBarry Smith 
876eaac0f3SBarry Smith     /*
886eaac0f3SBarry Smith        Mark all rows affect by these columns
896eaac0f3SBarry Smith     */
906eaac0f3SBarry Smith     if (flg) {/*-----------------------------------------------------------------------------*/
916eaac0f3SBarry Smith       /* crude, slow version */
926eaac0f3SBarry Smith       PetscMemzero(rowhit,M*sizeof(int));
93a64fbb32SBarry Smith       /* loop over columns*/
946eaac0f3SBarry Smith       for ( j=0; j<nctot; j++ ) {
956eaac0f3SBarry Smith         col  = cols[j];
966eaac0f3SBarry Smith         if (col >= cstart && col < cend) {
976eaac0f3SBarry Smith           /* column is in diagonal block of matrix */
986eaac0f3SBarry Smith           rows = A_cj + A_ci[col-cstart];
996eaac0f3SBarry Smith           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
1006eaac0f3SBarry Smith         } else {
1016eaac0f3SBarry Smith           colb = aij->colmap[col] - 1;
1026eaac0f3SBarry Smith           if (colb == -1) {
1036eaac0f3SBarry Smith             m = 0;
1046eaac0f3SBarry Smith           } else {
1056eaac0f3SBarry Smith             rows = B_cj + B_ci[colb];
1066eaac0f3SBarry Smith             m    = B_ci[colb+1] - B_ci[colb];
1076eaac0f3SBarry Smith           }
1086eaac0f3SBarry Smith         }
109a64fbb32SBarry Smith         /* loop over columns marking them in rowhit */
110a64fbb32SBarry Smith         for ( k=0; k<m; k++ ) {
111a64fbb32SBarry Smith           rowhit[*rows++] = col + 1;
112a64fbb32SBarry Smith         }
113a64fbb32SBarry Smith       }
1146eaac0f3SBarry Smith 
1156eaac0f3SBarry Smith /*
1166eaac0f3SBarry Smith printf("for col %d found rows \n",i);
1176eaac0f3SBarry Smith for ( j=0; j<M; j++ ) printf("rhow hit %d %d\n",j,rowhit[j]);
1186eaac0f3SBarry Smith */
1196eaac0f3SBarry Smith 
120a64fbb32SBarry Smith       /* count the number of hits */
121a64fbb32SBarry Smith       nrows = 0;
1226eaac0f3SBarry Smith       for ( j=0; j<M; j++ ) {
123a64fbb32SBarry Smith         if (rowhit[j]) nrows++;
124a64fbb32SBarry Smith       }
125a64fbb32SBarry Smith       c->nrows[i]         = nrows;
1266eaac0f3SBarry Smith       c->rows[i]          = (int *) PetscMalloc((nrows+1)*sizeof(int)); CHKPTRQ(c->rows[i]);
1276eaac0f3SBarry Smith       c->columnsforrow[i] = (int *) PetscMalloc((nrows+1)*sizeof(int)); CHKPTRQ(c->columnsforrow[i]);
128a64fbb32SBarry Smith       nrows = 0;
1296eaac0f3SBarry Smith       for ( j=0; j<M; j++ ) {
130a64fbb32SBarry Smith         if (rowhit[j]) {
131a64fbb32SBarry Smith           c->rows[i][nrows]           = j;
132a64fbb32SBarry Smith           c->columnsforrow[i][nrows] = rowhit[j] - 1;
133a64fbb32SBarry Smith           nrows++;
134a64fbb32SBarry Smith         }
135a64fbb32SBarry Smith       }
136a64fbb32SBarry Smith     } else {/*-------------------------------------------------------------------------------*/
137a64fbb32SBarry Smith       /* efficient version, using rowhit as a linked list */
1386eaac0f3SBarry Smith       int currentcol,fm,mfm;
1396eaac0f3SBarry Smith       rowhit[M] = M;
140a64fbb32SBarry Smith       nrows     = 0;
141a64fbb32SBarry Smith       /* loop over columns*/
1426eaac0f3SBarry Smith       for ( j=0; j<nctot; j++ ) {
1436eaac0f3SBarry Smith         col  = cols[j];
1446eaac0f3SBarry Smith         if (col >= cstart && col < cend) {
1456eaac0f3SBarry Smith           /* column is in diagonal block of matrix */
1466eaac0f3SBarry Smith           rows = A_cj + A_ci[col-cstart];
1476eaac0f3SBarry Smith           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
1486eaac0f3SBarry Smith         } else {
1496eaac0f3SBarry Smith           colb = aij->colmap[col] - 1;
1506eaac0f3SBarry Smith           if (colb == -1) {
1516eaac0f3SBarry Smith             m = 0;
1526eaac0f3SBarry Smith           } else {
1536eaac0f3SBarry Smith             rows = B_cj + B_ci[colb];
1546eaac0f3SBarry Smith             m    = B_ci[colb+1] - B_ci[colb];
1556eaac0f3SBarry Smith           }
1566eaac0f3SBarry Smith         }
157a64fbb32SBarry Smith         /* loop over columns marking them in rowhit */
1586eaac0f3SBarry Smith         fm    = M; /* fm points to first entry in linked list */
159a64fbb32SBarry Smith         for ( k=0; k<m; k++ ) {
160a64fbb32SBarry Smith           currentcol = *rows++;
161a64fbb32SBarry Smith 	  /* is it already in the list? */
162a64fbb32SBarry Smith           do {
163a64fbb32SBarry Smith             mfm  = fm;
164a64fbb32SBarry Smith             fm   = rowhit[fm];
165a64fbb32SBarry Smith           } while (fm < currentcol);
166a64fbb32SBarry Smith           /* not in list so add it */
167a64fbb32SBarry Smith           if (fm != currentcol) {
168a64fbb32SBarry Smith             nrows++;
169a64fbb32SBarry Smith             columnsforrow[currentcol] = col;
170a64fbb32SBarry Smith             /* next three lines insert new entry into linked list */
171a64fbb32SBarry Smith             rowhit[mfm]               = currentcol;
172a64fbb32SBarry Smith             rowhit[currentcol]        = fm;
173a64fbb32SBarry Smith             fm                        = currentcol;
174a64fbb32SBarry Smith             /* fm points to present position in list since we know the columns are sorted */
175a64fbb32SBarry Smith           } else {
1766eaac0f3SBarry Smith             SETERRQ(1,"MatFDColoringCreate_MPIAIJ:Invalid coloring");
177a64fbb32SBarry Smith           }
178a64fbb32SBarry Smith         }
179a64fbb32SBarry Smith       }
180a64fbb32SBarry Smith       c->nrows[i]         = nrows;
1816eaac0f3SBarry Smith       c->rows[i]          = (int *)PetscMalloc((nrows+1)*sizeof(int));CHKPTRQ(c->rows[i]);
1826eaac0f3SBarry Smith       c->columnsforrow[i] = (int *)PetscMalloc((nrows+1)*sizeof(int));CHKPTRQ(c->columnsforrow[i]);
183a64fbb32SBarry Smith       /* now store the linked list of rows into c->rows[i] */
184a64fbb32SBarry Smith       nrows = 0;
1856eaac0f3SBarry Smith       fm    = rowhit[M];
186a64fbb32SBarry Smith       do {
187a64fbb32SBarry Smith         c->rows[i][nrows]            = fm;
188a64fbb32SBarry Smith         c->columnsforrow[i][nrows++] = columnsforrow[fm];
189a64fbb32SBarry Smith         fm                           = rowhit[fm];
1906eaac0f3SBarry Smith       } while (fm < M);
1916eaac0f3SBarry Smith     } /* ---------------------------------------------------------------------------------------*/
1926eaac0f3SBarry Smith     PetscFree(cols);
1936eaac0f3SBarry Smith   }
194a64fbb32SBarry Smith   PetscFree(rowhit);
195a64fbb32SBarry Smith   PetscFree(columnsforrow);
1966eaac0f3SBarry Smith   PetscFree(ncolsonproc);
197*43a90d84SBarry Smith   ierr = MatRestoreColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done); CHKERRQ(ierr);
198*43a90d84SBarry Smith   ierr = MatRestoreColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done); CHKERRQ(ierr);
199a64fbb32SBarry Smith 
2006eaac0f3SBarry Smith   c->scale  = (Scalar *) PetscMalloc( 2*mat->N*sizeof(Scalar) ); CHKPTRQ(c->scale);
2016eaac0f3SBarry Smith   c->wscale = c->scale + mat->N;
202a64fbb32SBarry Smith   return 0;
203a64fbb32SBarry Smith }
204a64fbb32SBarry Smith 
205