xref: /petsc/src/mat/impls/aij/mpi/fdmpiaij.c (revision b1d57f1590ff1d2f11fead0759f3cc66a5edcec8)
1a64fbb32SBarry Smith 
26eaac0f3SBarry Smith #include "src/mat/impls/aij/mpi/mpiaij.h"
3a64fbb32SBarry Smith 
4dfbe8321SBarry Smith EXTERN PetscErrorCode CreateColmap_MPIAIJ_Private(Mat);
5*b1d57f15SBarry Smith EXTERN PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat,PetscInt,PetscTruth,PetscInt*,PetscInt*[],PetscInt*[],PetscTruth*);
6*b1d57f15SBarry Smith EXTERN PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat,PetscInt,PetscTruth,PetscInt*,PetscInt*[],PetscInt*[],PetscTruth*);
7a64fbb32SBarry Smith 
84a2ae208SSatish Balay #undef __FUNCT__
94a2ae208SSatish Balay #define __FUNCT__ "MatFDColoringCreate_MPIAIJ"
10dfbe8321SBarry Smith PetscErrorCode MatFDColoringCreate_MPIAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c)
11a64fbb32SBarry Smith {
126eaac0f3SBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
136849ba73SBarry Smith   PetscErrorCode ierr;
14*b1d57f15SBarry Smith   PetscMPIInt    size,*ncolsonproc,*disp,nn;
15*b1d57f15SBarry Smith   PetscInt       i,*is,n,nrows,j,k,m,*rows = 0,*A_ci,*A_cj,ncols,col;
16*b1d57f15SBarry Smith   PetscInt       nis = iscoloring->n,nctot,*cols,*B_ci,*B_cj;
17*b1d57f15SBarry Smith   PetscInt       *rowhit,M = mat->m,cstart = aij->cstart,cend = aij->cend,colb;
18*b1d57f15SBarry Smith   PetscInt       *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;
34*b1d57f15SBarry Smith   ierr             = PetscMalloc(nis*sizeof(PetscInt),&c->ncolumns);CHKERRQ(ierr);
35*b1d57f15SBarry Smith   ierr             = PetscMalloc(nis*sizeof(PetscInt*),&c->columns);CHKERRQ(ierr);
36*b1d57f15SBarry Smith   ierr             = PetscMalloc(nis*sizeof(PetscInt),&c->nrows);CHKERRQ(ierr);
37*b1d57f15SBarry Smith   ierr             = PetscMalloc(nis*sizeof(PetscInt*),&c->rows);CHKERRQ(ierr);
38*b1d57f15SBarry Smith   ierr             = PetscMalloc(nis*sizeof(PetscInt*),&c->columnsforrow);CHKERRQ(ierr);
39*b1d57f15SBarry Smith   PetscLogObjectMemory(c,5*nis*sizeof(PetscInt));
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);
53*b1d57f15SBarry Smith   ierr = PetscMalloc(2*size*sizeof(PetscInt*),&ncolsonproc);CHKERRQ(ierr);
546eaac0f3SBarry Smith   disp = ncolsonproc + size;
556eaac0f3SBarry Smith 
56*b1d57f15SBarry Smith   ierr = PetscMalloc((M+1)*sizeof(PetscInt),&rowhit);CHKERRQ(ierr);
57*b1d57f15SBarry Smith   ierr = PetscMalloc((M+1)*sizeof(PetscInt),&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) {
70*b1d57f15SBarry Smith       ierr = PetscMalloc(n*sizeof(PetscInt),&c->columns[i]);CHKERRQ(ierr);
71*b1d57f15SBarry Smith       PetscLogObjectMemory(c,n*sizeof(PetscInt));
72*b1d57f15SBarry Smith       ierr = PetscMemcpy(c->columns[i],is,n*sizeof(PetscInt));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 */
78*b1d57f15SBarry Smith     nn   = (PetscMPIInt)n;
79*b1d57f15SBarry Smith     ierr = MPI_Allgather(&nn,1,MPI_INT,ncolsonproc,1,MPI_INT,mat->comm);CHKERRQ(ierr);
806eaac0f3SBarry Smith     nctot = 0; for (j=0; j<size; j++) {nctot += ncolsonproc[j];}
813a7fca6bSBarry Smith     if (!nctot) {
823a7fca6bSBarry Smith       PetscLogInfo((PetscObject)mat,"MatFDColoringCreate_MPIAIJ: Coloring of matrix has some unneeded colors with no corresponding rows\n");
833a7fca6bSBarry Smith     }
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 */
91*b1d57f15SBarry Smith     ierr = PetscMalloc((nctot+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr);
92*b1d57f15SBarry Smith     ierr = MPI_Allgatherv(is,n,MPIU_INT,cols,ncolsonproc,disp,MPIU_INT,mat->comm);CHKERRQ(ierr);
936eaac0f3SBarry Smith 
946eaac0f3SBarry Smith     /*
956eaac0f3SBarry Smith        Mark all rows affect by these columns
966eaac0f3SBarry Smith     */
97f158e583SBarry Smith     if (!flg) {/*-----------------------------------------------------------------------------*/
98f158e583SBarry Smith       /* crude, fast version */
99*b1d57f15SBarry Smith       ierr = PetscMemzero(rowhit,M*sizeof(PetscInt));CHKERRQ(ierr);
100a64fbb32SBarry Smith       /* loop over columns*/
1016eaac0f3SBarry Smith       for (j=0; j<nctot; j++) {
1026eaac0f3SBarry Smith         col  = cols[j];
1036eaac0f3SBarry Smith         if (col >= cstart && col < cend) {
1046eaac0f3SBarry Smith           /* column is in diagonal block of matrix */
1056eaac0f3SBarry Smith           rows = A_cj + A_ci[col-cstart];
1066eaac0f3SBarry Smith           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
1076eaac0f3SBarry Smith         } else {
108aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
1090f5bd95cSBarry Smith           ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr)
110fa46199cSSatish Balay 	  colb --;
111b3d2dc96SSatish Balay #else
1126eaac0f3SBarry Smith           colb = aij->colmap[col] - 1;
113b3d2dc96SSatish Balay #endif
1146eaac0f3SBarry Smith           if (colb == -1) {
1156eaac0f3SBarry Smith             m = 0;
1166eaac0f3SBarry Smith           } else {
1176eaac0f3SBarry Smith             rows = B_cj + B_ci[colb];
1186eaac0f3SBarry Smith             m    = B_ci[colb+1] - B_ci[colb];
1196eaac0f3SBarry Smith           }
1206eaac0f3SBarry Smith         }
121a64fbb32SBarry Smith         /* loop over columns marking them in rowhit */
122a64fbb32SBarry Smith         for (k=0; k<m; k++) {
123a64fbb32SBarry Smith           rowhit[*rows++] = col + 1;
124a64fbb32SBarry Smith         }
125a64fbb32SBarry Smith       }
1266eaac0f3SBarry Smith 
127a64fbb32SBarry Smith       /* count the number of hits */
128a64fbb32SBarry Smith       nrows = 0;
1296eaac0f3SBarry Smith       for (j=0; j<M; j++) {
130a64fbb32SBarry Smith         if (rowhit[j]) nrows++;
131a64fbb32SBarry Smith       }
132a64fbb32SBarry Smith       c->nrows[i]         = nrows;
133*b1d57f15SBarry Smith       ierr                = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->rows[i]);CHKERRQ(ierr);
134*b1d57f15SBarry Smith       ierr                = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->columnsforrow[i]);CHKERRQ(ierr);
135*b1d57f15SBarry Smith       PetscLogObjectMemory(c,2*(nrows+1)*sizeof(PetscInt));
136a64fbb32SBarry Smith       nrows = 0;
1376eaac0f3SBarry Smith       for (j=0; j<M; j++) {
138a64fbb32SBarry Smith         if (rowhit[j]) {
139a64fbb32SBarry Smith           c->rows[i][nrows]           = j;
140a64fbb32SBarry Smith           c->columnsforrow[i][nrows] = rowhit[j] - 1;
141a64fbb32SBarry Smith           nrows++;
142a64fbb32SBarry Smith         }
143a64fbb32SBarry Smith       }
144a64fbb32SBarry Smith     } else {/*-------------------------------------------------------------------------------*/
145f158e583SBarry Smith       /* slow version, using rowhit as a linked list */
146*b1d57f15SBarry Smith       PetscInt currentcol,fm,mfm;
1476eaac0f3SBarry Smith       rowhit[M] = M;
148a64fbb32SBarry Smith       nrows     = 0;
149a64fbb32SBarry Smith       /* loop over columns*/
1506eaac0f3SBarry Smith       for (j=0; j<nctot; j++) {
1516eaac0f3SBarry Smith         col  = cols[j];
1526eaac0f3SBarry Smith         if (col >= cstart && col < cend) {
1536eaac0f3SBarry Smith           /* column is in diagonal block of matrix */
1546eaac0f3SBarry Smith           rows = A_cj + A_ci[col-cstart];
1556eaac0f3SBarry Smith           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
1566eaac0f3SBarry Smith         } else {
157aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
1580f5bd95cSBarry Smith 	  ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr);
159fa46199cSSatish Balay           colb --;
160b3d2dc96SSatish Balay #else
1616eaac0f3SBarry Smith           colb = aij->colmap[col] - 1;
162b3d2dc96SSatish Balay #endif
1636eaac0f3SBarry Smith           if (colb == -1) {
1646eaac0f3SBarry Smith             m = 0;
1656eaac0f3SBarry Smith           } else {
1666eaac0f3SBarry Smith             rows = B_cj + B_ci[colb];
1676eaac0f3SBarry Smith             m    = B_ci[colb+1] - B_ci[colb];
1686eaac0f3SBarry Smith           }
1696eaac0f3SBarry Smith         }
170a64fbb32SBarry Smith         /* loop over columns marking them in rowhit */
1716eaac0f3SBarry Smith         fm    = M; /* fm points to first entry in linked list */
172a64fbb32SBarry Smith         for (k=0; k<m; k++) {
173a64fbb32SBarry Smith           currentcol = *rows++;
174a64fbb32SBarry Smith 	  /* is it already in the list? */
175a64fbb32SBarry Smith           do {
176a64fbb32SBarry Smith             mfm  = fm;
177a64fbb32SBarry Smith             fm   = rowhit[fm];
178a64fbb32SBarry Smith           } while (fm < currentcol);
179a64fbb32SBarry Smith           /* not in list so add it */
180a64fbb32SBarry Smith           if (fm != currentcol) {
181a64fbb32SBarry Smith             nrows++;
182a64fbb32SBarry Smith             columnsforrow[currentcol] = col;
183a64fbb32SBarry Smith             /* next three lines insert new entry into linked list */
184a64fbb32SBarry Smith             rowhit[mfm]               = currentcol;
185a64fbb32SBarry Smith             rowhit[currentcol]        = fm;
186a64fbb32SBarry Smith             fm                        = currentcol;
187a64fbb32SBarry Smith             /* fm points to present position in list since we know the columns are sorted */
188a64fbb32SBarry Smith           } else {
18929bbc08cSBarry Smith             SETERRQ(PETSC_ERR_PLIB,"Invalid coloring of matrix detected");
190a64fbb32SBarry Smith           }
191a64fbb32SBarry Smith         }
192a64fbb32SBarry Smith       }
193a64fbb32SBarry Smith       c->nrows[i]         = nrows;
194*b1d57f15SBarry Smith       ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->rows[i]);CHKERRQ(ierr);
195*b1d57f15SBarry Smith       ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->columnsforrow[i]);CHKERRQ(ierr);
196*b1d57f15SBarry Smith       PetscLogObjectMemory(c,(nrows+1)*sizeof(PetscInt));
197a64fbb32SBarry Smith       /* now store the linked list of rows into c->rows[i] */
198a64fbb32SBarry Smith       nrows = 0;
1996eaac0f3SBarry Smith       fm    = rowhit[M];
200a64fbb32SBarry Smith       do {
201a64fbb32SBarry Smith         c->rows[i][nrows]            = fm;
202a64fbb32SBarry Smith         c->columnsforrow[i][nrows++] = columnsforrow[fm];
203a64fbb32SBarry Smith         fm                           = rowhit[fm];
2046eaac0f3SBarry Smith       } while (fm < M);
2056eaac0f3SBarry Smith     } /* ---------------------------------------------------------------------------------------*/
206606d414cSSatish Balay     ierr = PetscFree(cols);CHKERRQ(ierr);
2076eaac0f3SBarry Smith   }
20830b34957SBarry Smith 
20930b34957SBarry Smith   /* Optimize by adding the vscale, and scaleforrow[][] fields */
21030b34957SBarry Smith   /*
21130b34957SBarry Smith        vscale will contain the "diagonal" on processor scalings followed by the off processor
21230b34957SBarry Smith   */
21330b34957SBarry Smith   ierr = VecCreateGhost(mat->comm,aij->A->m,PETSC_DETERMINE,aij->B->n,aij->garray,&c->vscale);CHKERRQ(ierr)
214*b1d57f15SBarry Smith   ierr = PetscMalloc(c->ncolors*sizeof(PetscInt*),&c->vscaleforrow);CHKERRQ(ierr);
21530b34957SBarry Smith   for (k=0; k<c->ncolors; k++) {
216*b1d57f15SBarry Smith     ierr = PetscMalloc((c->nrows[k]+1)*sizeof(PetscInt),&c->vscaleforrow[k]);CHKERRQ(ierr);
21730b34957SBarry Smith     for (l=0; l<c->nrows[k]; l++) {
21830b34957SBarry Smith       col = c->columnsforrow[k][l];
21930b34957SBarry Smith       if (col >= cstart && col < cend) {
22030b34957SBarry Smith         /* column is in diagonal block of matrix */
22130b34957SBarry Smith         colb = col - cstart;
22230b34957SBarry Smith       } else {
22330b34957SBarry Smith         /* column  is in "off-processor" part */
22430b34957SBarry Smith #if defined (PETSC_USE_CTABLE)
22530b34957SBarry Smith         ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr);
22630b34957SBarry Smith         colb --;
22730b34957SBarry Smith #else
22830b34957SBarry Smith         colb = aij->colmap[col] - 1;
22930b34957SBarry Smith #endif
23030b34957SBarry Smith         colb += cend - cstart;
23130b34957SBarry Smith       }
23230b34957SBarry Smith       c->vscaleforrow[k][l] = colb;
23330b34957SBarry Smith     }
23430b34957SBarry Smith   }
235b9617806SBarry Smith   ierr = ISColoringRestoreIS(iscoloring,&isa);CHKERRQ(ierr);
23630b34957SBarry Smith 
237606d414cSSatish Balay   ierr = PetscFree(rowhit);CHKERRQ(ierr);
238606d414cSSatish Balay   ierr = PetscFree(columnsforrow);CHKERRQ(ierr);
239606d414cSSatish Balay   ierr = PetscFree(ncolsonproc);CHKERRQ(ierr);
24043a90d84SBarry Smith   ierr = MatRestoreColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr);
24143a90d84SBarry Smith   ierr = MatRestoreColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr);
2423a40ed3dSBarry Smith   PetscFunctionReturn(0);
243a64fbb32SBarry Smith }
244a64fbb32SBarry Smith 
245b9617806SBarry Smith 
246b9617806SBarry Smith 
247b9617806SBarry Smith 
248b9617806SBarry Smith 
249b9617806SBarry Smith 
250