xref: /petsc/src/mat/impls/aij/mpi/fdmpiaij.c (revision 3a7fca6b988f1faea172e46abdf950477dbfaf76)
1*3a7fca6bSBarry Smith /*$Id: fdmpiaij.c,v 1.40 2001/05/29 19:26:27 bsmith Exp bsmith $*/
2a64fbb32SBarry Smith 
36eaac0f3SBarry Smith #include "src/mat/impls/aij/mpi/mpiaij.h"
4a64fbb32SBarry Smith #include "src/vec/vecimpl.h"
5a64fbb32SBarry Smith 
6ca44d042SBarry Smith EXTERN int CreateColmap_MPIAIJ_Private(Mat);
7ca44d042SBarry Smith EXTERN int MatGetColumnIJ_SeqAIJ(Mat,int,PetscTruth,int*,int**,int**,PetscTruth*);
8ca44d042SBarry Smith EXTERN int MatRestoreColumnIJ_SeqAIJ(Mat,int,PetscTruth,int*,int**,int**,PetscTruth*);
9a64fbb32SBarry Smith 
104a2ae208SSatish Balay #undef __FUNCT__
114a2ae208SSatish Balay #define __FUNCT__ "MatFDColoringCreate_MPIAIJ"
126eaac0f3SBarry Smith int MatFDColoringCreate_MPIAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c)
13a64fbb32SBarry Smith {
146eaac0f3SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
15f1af5d2fSBarry Smith   int        i,*is,n,nrows,j,k,m,*rows = 0,ierr,*A_ci,*A_cj,ncols,col;
166eaac0f3SBarry Smith   int        nis = iscoloring->n,*ncolsonproc,size,nctot,*cols,*disp,*B_ci,*B_cj;
176eaac0f3SBarry Smith   int        *rowhit,M = mat->m,cstart = aij->cstart,cend = aij->cend,colb;
1830b34957SBarry Smith   int        *columnsforrow,l;
19b9617806SBarry Smith   IS         *isa;
20f1af5d2fSBarry Smith   PetscTruth done,flg;
21a64fbb32SBarry Smith 
223a40ed3dSBarry Smith   PetscFunctionBegin;
23522c5e43SBarry Smith   if (!mat->assembled) {
2429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Matrix must be assembled first; MatAssemblyBegin/End();");
25522c5e43SBarry Smith   }
26522c5e43SBarry Smith 
27b9617806SBarry Smith   ierr = ISColoringGetIS(iscoloring,PETSC_IGNORE,&isa);CHKERRQ(ierr);
28005c665bSBarry Smith   c->M             = mat->M;  /* set the global rows and columns and local rows */
29005c665bSBarry Smith   c->N             = mat->N;
30005c665bSBarry Smith   c->m             = mat->m;
31005c665bSBarry Smith   c->rstart        = aij->rstart;
32005c665bSBarry Smith 
33a64fbb32SBarry Smith   c->ncolors       = nis;
34b0a32e0cSBarry Smith   ierr             = PetscMalloc(nis*sizeof(int),&c->ncolumns);CHKERRQ(ierr);
35b0a32e0cSBarry Smith   ierr             = PetscMalloc(nis*sizeof(int*),&c->columns);CHKERRQ(ierr);
36b0a32e0cSBarry Smith   ierr             = PetscMalloc(nis*sizeof(int),&c->nrows);CHKERRQ(ierr);
37b0a32e0cSBarry Smith   ierr             = PetscMalloc(nis*sizeof(int*),&c->rows);CHKERRQ(ierr);
38b0a32e0cSBarry Smith   ierr             = PetscMalloc(nis*sizeof(int*),&c->columnsforrow);CHKERRQ(ierr);
39b0a32e0cSBarry Smith   PetscLogObjectMemory(c,5*nis*sizeof(int));
406eaac0f3SBarry Smith 
416eaac0f3SBarry Smith   /* Allow access to data structures of local part of matrix */
426eaac0f3SBarry Smith   if (!aij->colmap) {
436eaac0f3SBarry Smith     ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
446eaac0f3SBarry Smith   }
4543a90d84SBarry Smith   /*
4643a90d84SBarry Smith       Calls the _SeqAIJ() version of these routines to make sure it does not
4743a90d84SBarry Smith      get the reduced (by inodes) version of I and J
4843a90d84SBarry Smith   */
4943a90d84SBarry Smith   ierr = MatGetColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr);
5043a90d84SBarry Smith   ierr = MatGetColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr);
516eaac0f3SBarry Smith 
521dab6e02SBarry Smith   ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr);
53b0a32e0cSBarry Smith   ierr = PetscMalloc(2*size*sizeof(int*),&ncolsonproc);CHKERRQ(ierr);
546eaac0f3SBarry Smith   disp = ncolsonproc + size;
556eaac0f3SBarry Smith 
56b0a32e0cSBarry Smith   ierr = PetscMalloc((M+1)*sizeof(int),&rowhit);CHKERRQ(ierr);
57b0a32e0cSBarry Smith   ierr = PetscMalloc((M+1)*sizeof(int),&columnsforrow);CHKERRQ(ierr);
586eaac0f3SBarry Smith 
59a64fbb32SBarry Smith   /*
60a64fbb32SBarry Smith      Temporary option to allow for debugging/testing
61a64fbb32SBarry Smith   */
62b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(PETSC_NULL,"-matfdcoloring_slow",&flg);CHKERRQ(ierr);
636eaac0f3SBarry Smith 
64a64fbb32SBarry Smith   for (i=0; i<nis; i++) {
65b9b97703SBarry Smith     ierr = ISGetLocalSize(isa[i],&n);CHKERRQ(ierr);
66a64fbb32SBarry Smith     ierr = ISGetIndices(isa[i],&is);CHKERRQ(ierr);
67a64fbb32SBarry Smith     c->ncolumns[i] = n;
686eaac0f3SBarry Smith     c->ncolumns[i] = n;
69a64fbb32SBarry Smith     if (n) {
70b0a32e0cSBarry Smith       ierr = PetscMalloc(n*sizeof(int),&c->columns[i]);CHKERRQ(ierr);
71b0a32e0cSBarry Smith       PetscLogObjectMemory(c,n*sizeof(int));
72549d3d68SSatish Balay       ierr = PetscMemcpy(c->columns[i],is,n*sizeof(int));CHKERRQ(ierr);
73a64fbb32SBarry Smith     } else {
74a64fbb32SBarry Smith       c->columns[i]  = 0;
75a64fbb32SBarry Smith     }
76a64fbb32SBarry Smith 
776eaac0f3SBarry Smith     /* Determine the total (parallel) number of columns of this color */
78ca161407SBarry Smith     ierr = MPI_Allgather(&n,1,MPI_INT,ncolsonproc,1,MPI_INT,mat->comm);CHKERRQ(ierr);
796eaac0f3SBarry Smith     nctot = 0; for (j=0; j<size; j++) {nctot += ncolsonproc[j];}
80*3a7fca6bSBarry Smith     if (!nctot) {
81*3a7fca6bSBarry Smith       PetscLogInfo((PetscObject)mat,"MatFDColoringCreate_MPIAIJ: Coloring of matrix has some unneeded colors with no corresponding rows\n");
82*3a7fca6bSBarry Smith     }
836eaac0f3SBarry Smith 
846eaac0f3SBarry Smith     disp[0] = 0;
856eaac0f3SBarry Smith     for (j=1; j<size; j++) {
866eaac0f3SBarry Smith       disp[j] = disp[j-1] + ncolsonproc[j-1];
876eaac0f3SBarry Smith     }
886eaac0f3SBarry Smith 
896eaac0f3SBarry Smith     /* Get complete list of columns for color on each processor */
90*3a7fca6bSBarry Smith     ierr = PetscMalloc((nctot+1)*sizeof(int),&cols);CHKERRQ(ierr);
91ca161407SBarry Smith     ierr = MPI_Allgatherv(is,n,MPI_INT,cols,ncolsonproc,disp,MPI_INT,mat->comm);CHKERRQ(ierr);
926eaac0f3SBarry Smith 
936eaac0f3SBarry Smith     /*
946eaac0f3SBarry Smith        Mark all rows affect by these columns
956eaac0f3SBarry Smith     */
96f158e583SBarry Smith     if (!flg) {/*-----------------------------------------------------------------------------*/
97f158e583SBarry Smith       /* crude, fast version */
98549d3d68SSatish Balay       ierr = PetscMemzero(rowhit,M*sizeof(int));CHKERRQ(ierr);
99a64fbb32SBarry Smith       /* loop over columns*/
1006eaac0f3SBarry Smith       for (j=0; j<nctot; j++) {
1016eaac0f3SBarry Smith         col  = cols[j];
1026eaac0f3SBarry Smith         if (col >= cstart && col < cend) {
1036eaac0f3SBarry Smith           /* column is in diagonal block of matrix */
1046eaac0f3SBarry Smith           rows = A_cj + A_ci[col-cstart];
1056eaac0f3SBarry Smith           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
1066eaac0f3SBarry Smith         } else {
107aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
1080f5bd95cSBarry Smith           ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr)
109fa46199cSSatish Balay 	  colb --;
110b3d2dc96SSatish Balay #else
1116eaac0f3SBarry Smith           colb = aij->colmap[col] - 1;
112b3d2dc96SSatish Balay #endif
1136eaac0f3SBarry Smith           if (colb == -1) {
1146eaac0f3SBarry Smith             m = 0;
1156eaac0f3SBarry Smith           } else {
1166eaac0f3SBarry Smith             rows = B_cj + B_ci[colb];
1176eaac0f3SBarry Smith             m    = B_ci[colb+1] - B_ci[colb];
1186eaac0f3SBarry Smith           }
1196eaac0f3SBarry Smith         }
120a64fbb32SBarry Smith         /* loop over columns marking them in rowhit */
121a64fbb32SBarry Smith         for (k=0; k<m; k++) {
122a64fbb32SBarry Smith           rowhit[*rows++] = col + 1;
123a64fbb32SBarry Smith         }
124a64fbb32SBarry Smith       }
1256eaac0f3SBarry Smith 
126a64fbb32SBarry Smith       /* count the number of hits */
127a64fbb32SBarry Smith       nrows = 0;
1286eaac0f3SBarry Smith       for (j=0; j<M; j++) {
129a64fbb32SBarry Smith         if (rowhit[j]) nrows++;
130a64fbb32SBarry Smith       }
131a64fbb32SBarry Smith       c->nrows[i]         = nrows;
132b0a32e0cSBarry Smith       ierr                = PetscMalloc((nrows+1)*sizeof(int),&c->rows[i]);CHKERRQ(ierr);
133b0a32e0cSBarry Smith       ierr                = PetscMalloc((nrows+1)*sizeof(int),&c->columnsforrow[i]);CHKERRQ(ierr);
134b0a32e0cSBarry Smith       PetscLogObjectMemory(c,2*(nrows+1)*sizeof(int));
135a64fbb32SBarry Smith       nrows = 0;
1366eaac0f3SBarry Smith       for (j=0; j<M; j++) {
137a64fbb32SBarry Smith         if (rowhit[j]) {
138a64fbb32SBarry Smith           c->rows[i][nrows]           = j;
139a64fbb32SBarry Smith           c->columnsforrow[i][nrows] = rowhit[j] - 1;
140a64fbb32SBarry Smith           nrows++;
141a64fbb32SBarry Smith         }
142a64fbb32SBarry Smith       }
143a64fbb32SBarry Smith     } else {/*-------------------------------------------------------------------------------*/
144f158e583SBarry Smith       /* slow version, using rowhit as a linked list */
1456eaac0f3SBarry Smith       int currentcol,fm,mfm;
1466eaac0f3SBarry Smith       rowhit[M] = M;
147a64fbb32SBarry Smith       nrows     = 0;
148a64fbb32SBarry Smith       /* loop over columns*/
1496eaac0f3SBarry Smith       for (j=0; j<nctot; j++) {
1506eaac0f3SBarry Smith         col  = cols[j];
1516eaac0f3SBarry Smith         if (col >= cstart && col < cend) {
1526eaac0f3SBarry Smith           /* column is in diagonal block of matrix */
1536eaac0f3SBarry Smith           rows = A_cj + A_ci[col-cstart];
1546eaac0f3SBarry Smith           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
1556eaac0f3SBarry Smith         } else {
156aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
1570f5bd95cSBarry Smith 	  ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr);
158fa46199cSSatish Balay           colb --;
159b3d2dc96SSatish Balay #else
1606eaac0f3SBarry Smith           colb = aij->colmap[col] - 1;
161b3d2dc96SSatish Balay #endif
1626eaac0f3SBarry Smith           if (colb == -1) {
1636eaac0f3SBarry Smith             m = 0;
1646eaac0f3SBarry Smith           } else {
1656eaac0f3SBarry Smith             rows = B_cj + B_ci[colb];
1666eaac0f3SBarry Smith             m    = B_ci[colb+1] - B_ci[colb];
1676eaac0f3SBarry Smith           }
1686eaac0f3SBarry Smith         }
169a64fbb32SBarry Smith         /* loop over columns marking them in rowhit */
1706eaac0f3SBarry Smith         fm    = M; /* fm points to first entry in linked list */
171a64fbb32SBarry Smith         for (k=0; k<m; k++) {
172a64fbb32SBarry Smith           currentcol = *rows++;
173a64fbb32SBarry Smith 	  /* is it already in the list? */
174a64fbb32SBarry Smith           do {
175a64fbb32SBarry Smith             mfm  = fm;
176a64fbb32SBarry Smith             fm   = rowhit[fm];
177a64fbb32SBarry Smith           } while (fm < currentcol);
178a64fbb32SBarry Smith           /* not in list so add it */
179a64fbb32SBarry Smith           if (fm != currentcol) {
180a64fbb32SBarry Smith             nrows++;
181a64fbb32SBarry Smith             columnsforrow[currentcol] = col;
182a64fbb32SBarry Smith             /* next three lines insert new entry into linked list */
183a64fbb32SBarry Smith             rowhit[mfm]               = currentcol;
184a64fbb32SBarry Smith             rowhit[currentcol]        = fm;
185a64fbb32SBarry Smith             fm                        = currentcol;
186a64fbb32SBarry Smith             /* fm points to present position in list since we know the columns are sorted */
187a64fbb32SBarry Smith           } else {
18829bbc08cSBarry Smith             SETERRQ(PETSC_ERR_PLIB,"Invalid coloring of matrix detected");
189a64fbb32SBarry Smith           }
190a64fbb32SBarry Smith         }
191a64fbb32SBarry Smith       }
192a64fbb32SBarry Smith       c->nrows[i]         = nrows;
193b0a32e0cSBarry Smith       ierr = PetscMalloc((nrows+1)*sizeof(int),&c->rows[i]);CHKERRQ(ierr);
194b0a32e0cSBarry Smith       ierr = PetscMalloc((nrows+1)*sizeof(int),&c->columnsforrow[i]);CHKERRQ(ierr);
195b0a32e0cSBarry Smith       PetscLogObjectMemory(c,(nrows+1)*sizeof(int));
196a64fbb32SBarry Smith       /* now store the linked list of rows into c->rows[i] */
197a64fbb32SBarry Smith       nrows = 0;
1986eaac0f3SBarry Smith       fm    = rowhit[M];
199a64fbb32SBarry Smith       do {
200a64fbb32SBarry Smith         c->rows[i][nrows]            = fm;
201a64fbb32SBarry Smith         c->columnsforrow[i][nrows++] = columnsforrow[fm];
202a64fbb32SBarry Smith         fm                           = rowhit[fm];
2036eaac0f3SBarry Smith       } while (fm < M);
2046eaac0f3SBarry Smith     } /* ---------------------------------------------------------------------------------------*/
205606d414cSSatish Balay     ierr = PetscFree(cols);CHKERRQ(ierr);
2066eaac0f3SBarry Smith   }
20730b34957SBarry Smith 
20830b34957SBarry Smith   /* Optimize by adding the vscale, and scaleforrow[][] fields */
20930b34957SBarry Smith   /*
21030b34957SBarry Smith        vscale will contain the "diagonal" on processor scalings followed by the off processor
21130b34957SBarry Smith   */
21230b34957SBarry Smith   ierr = VecCreateGhost(mat->comm,aij->A->m,PETSC_DETERMINE,aij->B->n,aij->garray,&c->vscale);CHKERRQ(ierr)
213b0a32e0cSBarry Smith   ierr = PetscMalloc(c->ncolors*sizeof(int*),&c->vscaleforrow);CHKERRQ(ierr);
21430b34957SBarry Smith   for (k=0; k<c->ncolors; k++) {
215b0a32e0cSBarry Smith     ierr = PetscMalloc((c->nrows[k]+1)*sizeof(int),&c->vscaleforrow[k]);CHKERRQ(ierr);
21630b34957SBarry Smith     for (l=0; l<c->nrows[k]; l++) {
21730b34957SBarry Smith       col = c->columnsforrow[k][l];
21830b34957SBarry Smith       if (col >= cstart && col < cend) {
21930b34957SBarry Smith         /* column is in diagonal block of matrix */
22030b34957SBarry Smith         colb = col - cstart;
22130b34957SBarry Smith       } else {
22230b34957SBarry Smith         /* column  is in "off-processor" part */
22330b34957SBarry Smith #if defined (PETSC_USE_CTABLE)
22430b34957SBarry Smith         ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr);
22530b34957SBarry Smith         colb --;
22630b34957SBarry Smith #else
22730b34957SBarry Smith         colb = aij->colmap[col] - 1;
22830b34957SBarry Smith #endif
22930b34957SBarry Smith         colb += cend - cstart;
23030b34957SBarry Smith       }
23130b34957SBarry Smith       c->vscaleforrow[k][l] = colb;
23230b34957SBarry Smith     }
23330b34957SBarry Smith   }
234b9617806SBarry Smith   ierr = ISColoringRestoreIS(iscoloring,&isa);CHKERRQ(ierr);
23530b34957SBarry Smith 
236606d414cSSatish Balay   ierr = PetscFree(rowhit);CHKERRQ(ierr);
237606d414cSSatish Balay   ierr = PetscFree(columnsforrow);CHKERRQ(ierr);
238606d414cSSatish Balay   ierr = PetscFree(ncolsonproc);CHKERRQ(ierr);
23943a90d84SBarry Smith   ierr = MatRestoreColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr);
24043a90d84SBarry Smith   ierr = MatRestoreColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr);
2413a40ed3dSBarry Smith   PetscFunctionReturn(0);
242a64fbb32SBarry Smith }
243a64fbb32SBarry Smith 
244b9617806SBarry Smith 
245b9617806SBarry Smith 
246b9617806SBarry Smith 
247b9617806SBarry Smith 
248b9617806SBarry Smith 
249