xref: /petsc/src/mat/impls/aij/seq/fdaij.c (revision b0a32e0c6855ee6a6cd3495fa7da12ea9885bc5d)
1*b0a32e0cSBarry Smith /*$Id: fdaij.c,v 1.35 2000/10/24 20:25:32 bsmith Exp bsmith $*/
270c3da92SBarry Smith 
370c3da92SBarry Smith #include "src/mat/impls/aij/seq/aij.h"
470c3da92SBarry Smith #include "src/vec/vecimpl.h"
5639f9d9dSBarry Smith 
6ca44d042SBarry Smith EXTERN int MatGetColumnIJ_SeqAIJ(Mat,int,PetscTruth,int*,int**,int**,PetscTruth*);
7ca44d042SBarry Smith EXTERN int MatRestoreColumnIJ_SeqAIJ(Mat,int,PetscTruth,int*,int**,int**,PetscTruth*);
843a90d84SBarry Smith 
95615d1e5SSatish Balay #undef __FUNC__
10*b0a32e0cSBarry Smith #define __FUNC__ "MatFDColoringCreate_SeqAIJ"
11639f9d9dSBarry Smith int MatFDColoringCreate_SeqAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c)
12639f9d9dSBarry Smith {
13f1af5d2fSBarry Smith   int        i,*is,n,nrows,N = mat->N,j,k,m,*rows,ierr,*ci,*cj,ncols,col;
1430b34957SBarry Smith   int        nis = iscoloring->n,*rowhit,*columnsforrow,l;
15639f9d9dSBarry Smith   IS         *isa = iscoloring->is;
16f1af5d2fSBarry Smith   PetscTruth done,flg;
17639f9d9dSBarry Smith 
183a40ed3dSBarry Smith   PetscFunctionBegin;
19522c5e43SBarry Smith   if (!mat->assembled) {
2029bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Matrix must be assembled by calls to MatAssemblyBegin/End();");
21522c5e43SBarry Smith   }
22522c5e43SBarry Smith 
23005c665bSBarry Smith   c->M       = mat->M;  /* set total rows, columns and local rows */
24005c665bSBarry Smith   c->N       = mat->N;
25005c665bSBarry Smith   c->m       = mat->M;
26005c665bSBarry Smith   c->rstart  = 0;
27005c665bSBarry Smith 
28639f9d9dSBarry Smith   c->ncolors = nis;
29*b0a32e0cSBarry Smith   ierr       = PetscMalloc(nis*sizeof(int),&c->ncolumns);CHKERRQ(ierr);
30*b0a32e0cSBarry Smith   ierr       = PetscMalloc(nis*sizeof(int*),&c->columns);CHKERRQ(ierr);
31*b0a32e0cSBarry Smith   ierr       = PetscMalloc(nis*sizeof(int),&c->nrows);CHKERRQ(ierr);
32*b0a32e0cSBarry Smith   ierr       = PetscMalloc(nis*sizeof(int*),&c->rows);CHKERRQ(ierr);
33*b0a32e0cSBarry Smith   ierr       = PetscMalloc(nis*sizeof(int*),&c->columnsforrow);CHKERRQ(ierr);
3443a90d84SBarry Smith 
3543a90d84SBarry Smith   /*
3643a90d84SBarry Smith       Calls the _SeqAIJ() version of these routines to make sure it does not
3743a90d84SBarry Smith      get the reduced (by inodes) version of I and J
3843a90d84SBarry Smith   */
3943a90d84SBarry Smith   ierr = MatGetColumnIJ_SeqAIJ(mat,0,PETSC_FALSE,&ncols,&ci,&cj,&done);CHKERRQ(ierr);
4070c3da92SBarry Smith 
4170c3da92SBarry Smith   /*
42639f9d9dSBarry Smith      Temporary option to allow for debugging/testing
4370c3da92SBarry Smith   */
44*b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(PETSC_NULL,"-matfdcoloring_slow",&flg);CHKERRQ(ierr);
4570c3da92SBarry Smith 
46*b0a32e0cSBarry Smith   ierr = PetscMalloc((N+1)*sizeof(int),&rowhit);CHKERRQ(ierr);
47*b0a32e0cSBarry Smith   ierr = PetscMalloc((N+1)*sizeof(int),&columnsforrow);CHKERRQ(ierr);
4870c3da92SBarry Smith 
49639f9d9dSBarry Smith   for (i=0; i<nis; i++) {
50b9b97703SBarry Smith     ierr = ISGetLocalSize(isa[i],&n);CHKERRQ(ierr);
51639f9d9dSBarry Smith     ierr = ISGetIndices(isa[i],&is);CHKERRQ(ierr);
52639f9d9dSBarry Smith     c->ncolumns[i] = n;
53639f9d9dSBarry Smith     if (n) {
54*b0a32e0cSBarry Smith       ierr = PetscMalloc(n*sizeof(int),&c->columns[i]);CHKERRQ(ierr);
55549d3d68SSatish Balay       ierr = PetscMemcpy(c->columns[i],is,n*sizeof(int));CHKERRQ(ierr);
5670c3da92SBarry Smith     } else {
57639f9d9dSBarry Smith       c->columns[i]  = 0;
5870c3da92SBarry Smith     }
5970c3da92SBarry Smith 
60639f9d9dSBarry Smith     if (flg) { /* ------------------------------------------------------------------------------*/
61639f9d9dSBarry Smith       /* crude version requires O(N*N) work */
62549d3d68SSatish Balay       ierr = PetscMemzero(rowhit,N*sizeof(int));CHKERRQ(ierr);
63639f9d9dSBarry Smith       /* loop over columns*/
64639f9d9dSBarry Smith       for (j=0; j<n; j++) {
65639f9d9dSBarry Smith         col  = is[j];
66639f9d9dSBarry Smith         rows = cj + ci[col];
67639f9d9dSBarry Smith         m    = ci[col+1] - ci[col];
68639f9d9dSBarry Smith         /* loop over columns marking them in rowhit */
69639f9d9dSBarry Smith         for (k=0; k<m; k++) {
70639f9d9dSBarry Smith           rowhit[*rows++] = col + 1;
7170c3da92SBarry Smith         }
7270c3da92SBarry Smith       }
73639f9d9dSBarry Smith       /* count the number of hits */
74639f9d9dSBarry Smith       nrows = 0;
7570c3da92SBarry Smith       for (j=0; j<N; j++) {
76639f9d9dSBarry Smith         if (rowhit[j]) nrows++;
77639f9d9dSBarry Smith       }
78639f9d9dSBarry Smith       c->nrows[i] = nrows;
79*b0a32e0cSBarry Smith       ierr        = PetscMalloc(nrows*sizeof(int),&c->rows[i]);CHKERRQ(ierr);
80*b0a32e0cSBarry Smith       ierr        = PetscMalloc(nrows*sizeof(int),&c->columnsforrow[i]);CHKERRQ(ierr);
81639f9d9dSBarry Smith       nrows       = 0;
82639f9d9dSBarry Smith       for (j=0; j<N; j++) {
83639f9d9dSBarry Smith         if (rowhit[j]) {
84639f9d9dSBarry Smith           c->rows[i][nrows]           = j;
85639f9d9dSBarry Smith           c->columnsforrow[i][nrows] = rowhit[j] - 1;
86639f9d9dSBarry Smith           nrows++;
8770c3da92SBarry Smith         }
8870c3da92SBarry Smith       }
89639f9d9dSBarry Smith     } else {  /*-------------------------------------------------------------------------------*/
90639f9d9dSBarry Smith       /* efficient version, using rowhit as a linked list */
91639f9d9dSBarry Smith       int currentcol,fm,mfm;
92639f9d9dSBarry Smith       rowhit[N] = N;
93639f9d9dSBarry Smith       nrows     = 0;
94639f9d9dSBarry Smith       /* loop over columns */
95639f9d9dSBarry Smith       for (j=0; j<n; j++) {
96639f9d9dSBarry Smith         col   = is[j];
97639f9d9dSBarry Smith         rows  = cj + ci[col];
98639f9d9dSBarry Smith         m     = ci[col+1] - ci[col];
99639f9d9dSBarry Smith         /* loop over columns marking them in rowhit */
100639f9d9dSBarry Smith         fm    = N; /* fm points to first entry in linked list */
101639f9d9dSBarry Smith         for (k=0; k<m; k++) {
102639f9d9dSBarry Smith           currentcol = *rows++;
103639f9d9dSBarry Smith 	  /* is it already in the list? */
104639f9d9dSBarry Smith           do {
105639f9d9dSBarry Smith             mfm  = fm;
106639f9d9dSBarry Smith             fm   = rowhit[fm];
107639f9d9dSBarry Smith           } while (fm < currentcol);
108639f9d9dSBarry Smith           /* not in list so add it */
109639f9d9dSBarry Smith           if (fm != currentcol) {
110639f9d9dSBarry Smith             nrows++;
111639f9d9dSBarry Smith             columnsforrow[currentcol] = col;
112639f9d9dSBarry Smith             /* next three lines insert new entry into linked list */
113639f9d9dSBarry Smith             rowhit[mfm]               = currentcol;
114639f9d9dSBarry Smith             rowhit[currentcol]        = fm;
115639f9d9dSBarry Smith             fm                        = currentcol;
116639f9d9dSBarry Smith             /* fm points to present position in list since we know the columns are sorted */
11770c3da92SBarry Smith           } else {
11829bbc08cSBarry Smith             SETERRQ(PETSC_ERR_PLIB,"Detected invalid coloring");
11970c3da92SBarry Smith           }
120639f9d9dSBarry Smith 
121639f9d9dSBarry Smith         }
122639f9d9dSBarry Smith       }
123639f9d9dSBarry Smith       c->nrows[i] = nrows;
124*b0a32e0cSBarry Smith       ierr        = PetscMalloc((nrows+1)*sizeof(int),&c->rows[i]);CHKERRQ(ierr);
125*b0a32e0cSBarry Smith       ierr        = PetscMalloc((nrows+1)*sizeof(int),&c->columnsforrow[i]);CHKERRQ(ierr);
126639f9d9dSBarry Smith       /* now store the linked list of rows into c->rows[i] */
127639f9d9dSBarry Smith       nrows       = 0;
128639f9d9dSBarry Smith       fm          = rowhit[N];
129639f9d9dSBarry Smith       do {
130639f9d9dSBarry Smith         c->rows[i][nrows]            = fm;
131639f9d9dSBarry Smith         c->columnsforrow[i][nrows++] = columnsforrow[fm];
132639f9d9dSBarry Smith         fm                           = rowhit[fm];
133639f9d9dSBarry Smith       } while (fm < N);
134639f9d9dSBarry Smith     } /* ---------------------------------------------------------------------------------------*/
135639f9d9dSBarry Smith     ierr = ISRestoreIndices(isa[i],&is);CHKERRQ(ierr);
136639f9d9dSBarry Smith   }
13743a90d84SBarry Smith   ierr = MatRestoreColumnIJ_SeqAIJ(mat,0,PETSC_FALSE,&ncols,&ci,&cj,&done);CHKERRQ(ierr);
138639f9d9dSBarry Smith 
139606d414cSSatish Balay   ierr = PetscFree(rowhit);CHKERRQ(ierr);
140606d414cSSatish Balay   ierr = PetscFree(columnsforrow);CHKERRQ(ierr);
141639f9d9dSBarry Smith 
14230b34957SBarry Smith   /* Optimize by adding the vscale, and scaleforrow[][] fields */
14330b34957SBarry Smith   /*
14430b34957SBarry Smith        see the version for MPIAIJ
14530b34957SBarry Smith   */
14630b34957SBarry Smith   ierr = VecCreateGhost(mat->comm,mat->m,PETSC_DETERMINE,0,PETSC_NULL,&c->vscale);CHKERRQ(ierr)
147*b0a32e0cSBarry Smith   ierr = PetscMalloc(c->ncolors*sizeof(int*),&c->vscaleforrow);CHKERRQ(ierr);
14830b34957SBarry Smith   for (k=0; k<c->ncolors; k++) {
149*b0a32e0cSBarry Smith     ierr = PetscMalloc((c->nrows[k]+1)*sizeof(int),&c->vscaleforrow[k]);CHKERRQ(ierr);
15030b34957SBarry Smith     for (l=0; l<c->nrows[k]; l++) {
15130b34957SBarry Smith       col = c->columnsforrow[k][l];
15230b34957SBarry Smith       c->vscaleforrow[k][l] = col;
15330b34957SBarry Smith     }
15430b34957SBarry Smith   }
15530b34957SBarry Smith 
1563a40ed3dSBarry Smith   PetscFunctionReturn(0);
15770c3da92SBarry Smith }
15870c3da92SBarry Smith 
1595615d1e5SSatish Balay #undef __FUNC__
160*b0a32e0cSBarry Smith #define __FUNC__ "MatColoringPatch_SeqAIJ"
161639f9d9dSBarry Smith int MatColoringPatch_SeqAIJ(Mat mat,int ncolors,int *coloring,ISColoring *iscoloring)
16270c3da92SBarry Smith {
163273d9f13SBarry Smith   int        n = mat->n,*sizes,i,**ii,ierr,tag;
164639f9d9dSBarry Smith   IS         *is;
16570c3da92SBarry Smith 
1663a40ed3dSBarry Smith   PetscFunctionBegin;
167639f9d9dSBarry Smith   /* construct the index sets from the coloring array */
168*b0a32e0cSBarry Smith   ierr = PetscMalloc(ncolors*sizeof(int),&sizes);CHKERRQ(ierr);
169549d3d68SSatish Balay   ierr = PetscMemzero(sizes,ncolors*sizeof(int));CHKERRQ(ierr);
17070c3da92SBarry Smith   for (i=0; i<n; i++) {
171639f9d9dSBarry Smith     sizes[coloring[i]-1]++;
17270c3da92SBarry Smith   }
173*b0a32e0cSBarry Smith   ierr = PetscMalloc(ncolors*sizeof(int*),&ii);CHKERRQ(ierr);
174*b0a32e0cSBarry Smith   ierr = PetscMalloc(n*sizeof(int),&ii[0]);CHKERRQ(ierr);
175639f9d9dSBarry Smith   for (i=1; i<ncolors; i++) {
176639f9d9dSBarry Smith     ii[i] = ii[i-1] + sizes[i-1];
177639f9d9dSBarry Smith   }
178549d3d68SSatish Balay   ierr = PetscMemzero(sizes,ncolors*sizeof(int));CHKERRQ(ierr);
179639f9d9dSBarry Smith   for (i=0; i<n; i++) {
180639f9d9dSBarry Smith     ii[coloring[i]-1][sizes[coloring[i]-1]++] = i;
181639f9d9dSBarry Smith   }
182*b0a32e0cSBarry Smith   ierr = PetscMalloc(ncolors*sizeof(IS),&is);CHKERRQ(ierr);
183639f9d9dSBarry Smith   for (i=0; i<ncolors; i++) {
184029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,sizes[i],ii[i],is+i);CHKERRQ(ierr);
185639f9d9dSBarry Smith   }
186639f9d9dSBarry Smith 
187*b0a32e0cSBarry Smith   ierr                = PetscNew(struct _p_ISColoring,iscoloring);CHKERRQ(ierr);
188639f9d9dSBarry Smith   (*iscoloring)->n    = ncolors;
189639f9d9dSBarry Smith   (*iscoloring)->is   = is;
1904be1940cSBarry Smith   ierr = PetscCommDuplicate_Private(mat->comm,&(*iscoloring)->comm,&tag);CHKERRQ(ierr);
191606d414cSSatish Balay   ierr = PetscFree(sizes);CHKERRQ(ierr);
192606d414cSSatish Balay   ierr = PetscFree(ii[0]);CHKERRQ(ierr);
193606d414cSSatish Balay   ierr = PetscFree(ii);CHKERRQ(ierr);
1943a40ed3dSBarry Smith   PetscFunctionReturn(0);
19570c3da92SBarry Smith }
19670c3da92SBarry Smith 
197639f9d9dSBarry Smith /*
198639f9d9dSBarry Smith      Makes a longer coloring[] array and calls the usual code with that
199639f9d9dSBarry Smith */
2005615d1e5SSatish Balay #undef __FUNC__
201*b0a32e0cSBarry Smith #define __FUNC__ "MatColoringPatch_SeqAIJ_Inode"
202639f9d9dSBarry Smith int MatColoringPatch_SeqAIJ_Inode(Mat mat,int ncolors,int *coloring,ISColoring *iscoloring)
20370c3da92SBarry Smith {
204639f9d9dSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)mat->data;
205273d9f13SBarry Smith   int        n = mat->n,ierr,m = a->inode.node_count,j,*ns = a->inode.size,row;
206639f9d9dSBarry Smith   int        *colorused,i,*newcolor;
20770c3da92SBarry Smith 
2083a40ed3dSBarry Smith   PetscFunctionBegin;
209*b0a32e0cSBarry Smith   ierr = PetscMalloc((n+1)*sizeof(int),&newcolor);CHKERRQ(ierr);
21070c3da92SBarry Smith 
211639f9d9dSBarry Smith   /* loop over inodes, marking a color for each column*/
212639f9d9dSBarry Smith   row = 0;
21370c3da92SBarry Smith   for (i=0; i<m; i++){
214639f9d9dSBarry Smith     for (j=0; j<ns[i]; j++) {
215639f9d9dSBarry Smith       newcolor[row++] = coloring[i] + j*ncolors;
21670c3da92SBarry Smith     }
21770c3da92SBarry Smith   }
21870c3da92SBarry Smith 
219639f9d9dSBarry Smith   /* eliminate unneeded colors */
220*b0a32e0cSBarry Smith   ierr = PetscMalloc(5*ncolors*sizeof(int),&colorused);CHKERRQ(ierr);
221549d3d68SSatish Balay   ierr = PetscMemzero(colorused,5*ncolors*sizeof(int));CHKERRQ(ierr);
222639f9d9dSBarry Smith   for (i=0; i<n; i++) {
223639f9d9dSBarry Smith     colorused[newcolor[i]-1] = 1;
224639f9d9dSBarry Smith   }
22570c3da92SBarry Smith 
226639f9d9dSBarry Smith   for (i=1; i<5*ncolors; i++) {
227639f9d9dSBarry Smith     colorused[i] += colorused[i-1];
22870c3da92SBarry Smith   }
229639f9d9dSBarry Smith   ncolors = colorused[5*ncolors-1];
230639f9d9dSBarry Smith   for (i=0; i<n; i++) {
231639f9d9dSBarry Smith     newcolor[i] = colorused[newcolor[i]-1];
23270c3da92SBarry Smith   }
233606d414cSSatish Balay   ierr = PetscFree(colorused);CHKERRQ(ierr);
23470c3da92SBarry Smith 
235639f9d9dSBarry Smith   ierr = MatColoringPatch_SeqAIJ(mat,ncolors,newcolor,iscoloring);CHKERRQ(ierr);
236606d414cSSatish Balay   ierr = PetscFree(newcolor);CHKERRQ(ierr);
237639f9d9dSBarry Smith 
2383a40ed3dSBarry Smith   PetscFunctionReturn(0);
23970c3da92SBarry Smith }
24070c3da92SBarry Smith 
2413a40ed3dSBarry Smith 
2423a40ed3dSBarry Smith 
2433a40ed3dSBarry Smith 
2443a40ed3dSBarry Smith 
2453a40ed3dSBarry Smith 
246