xref: /petsc/src/mat/impls/aij/seq/fdaij.c (revision 606d414c6e034e02e67059b83ebaefc3ebe99698)
1005c665bSBarry Smith 
2a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
3*606d414cSSatish Balay static char vcid[] = "$Id: fdaij.c,v 1.21 1999/06/08 22:55:44 balay Exp balay $";
470c3da92SBarry Smith #endif
570c3da92SBarry Smith 
670c3da92SBarry Smith #include "src/mat/impls/aij/seq/aij.h"
770c3da92SBarry Smith #include "src/vec/vecimpl.h"
870c3da92SBarry Smith #include "petsc.h"
9639f9d9dSBarry Smith 
1043a90d84SBarry Smith extern int MatGetColumnIJ_SeqAIJ(Mat,int,PetscTruth,int*,int**,int**,PetscTruth*);
1143a90d84SBarry Smith extern int MatRestoreColumnIJ_SeqAIJ(Mat,int,PetscTruth,int*,int**,int**,PetscTruth*);
1243a90d84SBarry Smith 
135615d1e5SSatish Balay #undef __FUNC__
14d4bb536fSBarry Smith #define __FUNC__ "MatFDColoringCreate_SeqAIJ"
15639f9d9dSBarry Smith int MatFDColoringCreate_SeqAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c)
16639f9d9dSBarry Smith {
17639f9d9dSBarry Smith   int        i,*is,n,nrows,N = mat->N,j,k,m,*rows,ierr,*ci,*cj,ncols,col,flg;
18639f9d9dSBarry Smith   int        nis = iscoloring->n,*rowhit,*columnsforrow;
19639f9d9dSBarry Smith   IS         *isa = iscoloring->is;
20639f9d9dSBarry Smith   PetscTruth done;
21639f9d9dSBarry Smith 
223a40ed3dSBarry Smith   PetscFunctionBegin;
23522c5e43SBarry Smith   if (!mat->assembled) {
24a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,1,"Matrix must be assembled by calls to MatAssemblyBegin/End();");
25522c5e43SBarry Smith   }
26522c5e43SBarry Smith 
27005c665bSBarry Smith   c->M             = mat->M;  /* set total rows, columns and local rows */
28005c665bSBarry Smith   c->N             = mat->N;
29005c665bSBarry Smith   c->m             = mat->M;
30005c665bSBarry Smith   c->rstart        = 0;
31005c665bSBarry Smith 
32639f9d9dSBarry Smith   c->ncolors       = nis;
33639f9d9dSBarry Smith   c->ncolumns      = (int *) PetscMalloc( nis*sizeof(int) );CHKPTRQ(c->ncolumns);
34639f9d9dSBarry Smith   c->columns       = (int **) PetscMalloc( nis*sizeof(int *));CHKPTRQ(c->columns);
35639f9d9dSBarry Smith   c->nrows         = (int *) PetscMalloc( nis*sizeof(int) );CHKPTRQ(c->nrows);
36639f9d9dSBarry Smith   c->rows          = (int **) PetscMalloc( nis*sizeof(int *));CHKPTRQ(c->rows);
37639f9d9dSBarry Smith   c->columnsforrow = (int **) PetscMalloc( nis*sizeof(int *));CHKPTRQ(c->columnsforrow);
3843a90d84SBarry Smith 
3943a90d84SBarry Smith   /*
4043a90d84SBarry Smith       Calls the _SeqAIJ() version of these routines to make sure it does not
4143a90d84SBarry Smith      get the reduced (by inodes) version of I and J
4243a90d84SBarry Smith   */
4343a90d84SBarry Smith   ierr = MatGetColumnIJ_SeqAIJ(mat,0,PETSC_FALSE,&ncols,&ci,&cj,&done);CHKERRQ(ierr);
4470c3da92SBarry Smith 
4570c3da92SBarry Smith   /*
46639f9d9dSBarry Smith      Temporary option to allow for debugging/testing
4770c3da92SBarry Smith   */
48888f2ed8SSatish Balay   ierr = OptionsHasName(0,"-matfdcoloring_slow",&flg);CHKERRQ(ierr);
4970c3da92SBarry Smith 
50639f9d9dSBarry Smith   rowhit        = (int *) PetscMalloc( (N+1)*sizeof(int) );CHKPTRQ(rowhit);
51639f9d9dSBarry Smith   columnsforrow = (int *) PetscMalloc( (N+1)*sizeof(int) );CHKPTRQ(columnsforrow);
5270c3da92SBarry Smith 
53639f9d9dSBarry Smith   for ( i=0; i<nis; i++ ) {
54639f9d9dSBarry Smith     ierr = ISGetSize(isa[i],&n);CHKERRQ(ierr);
55639f9d9dSBarry Smith     ierr = ISGetIndices(isa[i],&is);CHKERRQ(ierr);
56639f9d9dSBarry Smith     c->ncolumns[i] = n;
57639f9d9dSBarry Smith     if (n) {
58639f9d9dSBarry Smith       c->columns[i]  = (int *) PetscMalloc( n*sizeof(int) );CHKPTRQ(c->columns[i]);
59549d3d68SSatish Balay       ierr = PetscMemcpy(c->columns[i],is,n*sizeof(int));CHKERRQ(ierr);
6070c3da92SBarry Smith     } else {
61639f9d9dSBarry Smith       c->columns[i]  = 0;
6270c3da92SBarry Smith     }
6370c3da92SBarry Smith 
64639f9d9dSBarry Smith     if (flg) { /* ------------------------------------------------------------------------------*/
65639f9d9dSBarry Smith       /* crude version requires O(N*N) work */
66549d3d68SSatish Balay       ierr = PetscMemzero(rowhit,N*sizeof(int));CHKERRQ(ierr);
67639f9d9dSBarry Smith       /* loop over columns*/
68639f9d9dSBarry Smith       for ( j=0; j<n; j++ ) {
69639f9d9dSBarry Smith         col  = is[j];
70639f9d9dSBarry Smith         rows = cj + ci[col];
71639f9d9dSBarry Smith         m    = ci[col+1] - ci[col];
72639f9d9dSBarry Smith         /* loop over columns marking them in rowhit */
73639f9d9dSBarry Smith         for ( k=0; k<m; k++ ) {
74639f9d9dSBarry Smith           rowhit[*rows++] = col + 1;
7570c3da92SBarry Smith         }
7670c3da92SBarry Smith       }
77639f9d9dSBarry Smith       /* count the number of hits */
78639f9d9dSBarry Smith       nrows = 0;
7970c3da92SBarry Smith       for ( j=0; j<N; j++ ) {
80639f9d9dSBarry Smith         if (rowhit[j]) nrows++;
81639f9d9dSBarry Smith       }
82639f9d9dSBarry Smith       c->nrows[i]         = nrows;
83639f9d9dSBarry Smith       c->rows[i]          = (int *) PetscMalloc(nrows*sizeof(int));CHKPTRQ(c->rows[i]);
84639f9d9dSBarry Smith       c->columnsforrow[i] = (int *) PetscMalloc(nrows*sizeof(int));CHKPTRQ(c->columnsforrow[i]);
85639f9d9dSBarry Smith       nrows = 0;
86639f9d9dSBarry Smith       for ( j=0; j<N; j++ ) {
87639f9d9dSBarry Smith         if (rowhit[j]) {
88639f9d9dSBarry Smith           c->rows[i][nrows]           = j;
89639f9d9dSBarry Smith           c->columnsforrow[i][nrows] = rowhit[j] - 1;
90639f9d9dSBarry Smith           nrows++;
9170c3da92SBarry Smith         }
9270c3da92SBarry Smith       }
93639f9d9dSBarry Smith     } else {  /*-------------------------------------------------------------------------------*/
94639f9d9dSBarry Smith       /* efficient version, using rowhit as a linked list */
95639f9d9dSBarry Smith       int currentcol,fm,mfm;
96639f9d9dSBarry Smith       rowhit[N] = N;
97639f9d9dSBarry Smith       nrows     = 0;
98639f9d9dSBarry Smith       /* loop over columns */
99639f9d9dSBarry Smith       for ( j=0; j<n; j++ ) {
100639f9d9dSBarry Smith         col   = is[j];
101639f9d9dSBarry Smith         rows  = cj + ci[col];
102639f9d9dSBarry Smith         m     = ci[col+1] - ci[col];
103639f9d9dSBarry Smith         /* loop over columns marking them in rowhit */
104639f9d9dSBarry Smith         fm    = N; /* fm points to first entry in linked list */
105639f9d9dSBarry Smith         for ( k=0; k<m; k++ ) {
106639f9d9dSBarry Smith           currentcol = *rows++;
107639f9d9dSBarry Smith 	  /* is it already in the list? */
108639f9d9dSBarry Smith           do {
109639f9d9dSBarry Smith             mfm  = fm;
110639f9d9dSBarry Smith             fm   = rowhit[fm];
111639f9d9dSBarry Smith           } while (fm < currentcol);
112639f9d9dSBarry Smith           /* not in list so add it */
113639f9d9dSBarry Smith           if (fm != currentcol) {
114639f9d9dSBarry Smith             nrows++;
115639f9d9dSBarry Smith             columnsforrow[currentcol] = col;
116639f9d9dSBarry Smith             /* next three lines insert new entry into linked list */
117639f9d9dSBarry Smith             rowhit[mfm]               = currentcol;
118639f9d9dSBarry Smith             rowhit[currentcol]        = fm;
119639f9d9dSBarry Smith             fm                        = currentcol;
120639f9d9dSBarry Smith             /* fm points to present position in list since we know the columns are sorted */
12170c3da92SBarry Smith           } else {
122a8c6a408SBarry Smith             SETERRQ(PETSC_ERR_PLIB,0,"Detected invalid coloring");
12370c3da92SBarry Smith           }
124639f9d9dSBarry Smith 
125639f9d9dSBarry Smith         }
126639f9d9dSBarry Smith       }
127639f9d9dSBarry Smith       c->nrows[i]         = nrows;
128639f9d9dSBarry Smith       c->rows[i]          = (int *)PetscMalloc((nrows+1)*sizeof(int));CHKPTRQ(c->rows[i]);
129639f9d9dSBarry Smith       c->columnsforrow[i] = (int *)PetscMalloc((nrows+1)*sizeof(int));CHKPTRQ(c->columnsforrow[i]);
130639f9d9dSBarry Smith       /* now store the linked list of rows into c->rows[i] */
131639f9d9dSBarry Smith       nrows = 0;
132639f9d9dSBarry Smith       fm    = rowhit[N];
133639f9d9dSBarry Smith       do {
134639f9d9dSBarry Smith         c->rows[i][nrows]            = fm;
135639f9d9dSBarry Smith         c->columnsforrow[i][nrows++] = columnsforrow[fm];
136639f9d9dSBarry Smith         fm                           = rowhit[fm];
137639f9d9dSBarry Smith       } while (fm < N);
138639f9d9dSBarry Smith     } /* ---------------------------------------------------------------------------------------*/
139639f9d9dSBarry Smith     ierr = ISRestoreIndices(isa[i],&is);CHKERRQ(ierr);
140639f9d9dSBarry Smith   }
14143a90d84SBarry Smith   ierr = MatRestoreColumnIJ_SeqAIJ(mat,0,PETSC_FALSE,&ncols,&ci,&cj,&done);CHKERRQ(ierr);
142639f9d9dSBarry Smith 
143*606d414cSSatish Balay   ierr = PetscFree(rowhit);CHKERRQ(ierr);
144*606d414cSSatish Balay   ierr = PetscFree(columnsforrow);CHKERRQ(ierr);
145639f9d9dSBarry Smith 
146639f9d9dSBarry Smith   c->scale  = (Scalar *) PetscMalloc( 2*N*sizeof(Scalar) );CHKPTRQ(c->scale);
147639f9d9dSBarry Smith   c->wscale = c->scale + N;
148639f9d9dSBarry Smith 
1493a40ed3dSBarry Smith   PetscFunctionReturn(0);
15070c3da92SBarry Smith }
15170c3da92SBarry Smith 
1525615d1e5SSatish Balay #undef __FUNC__
153d4bb536fSBarry Smith #define __FUNC__ "MatColoringPatch_SeqAIJ"
154639f9d9dSBarry Smith int MatColoringPatch_SeqAIJ(Mat mat,int ncolors,int *coloring,ISColoring *iscoloring)
15570c3da92SBarry Smith {
156639f9d9dSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) mat->data;
157639f9d9dSBarry Smith   int        n = a->n,*sizes,i,**ii,ierr,tag;
158639f9d9dSBarry Smith   IS         *is;
15970c3da92SBarry Smith 
1603a40ed3dSBarry Smith   PetscFunctionBegin;
161639f9d9dSBarry Smith   /* construct the index sets from the coloring array */
162639f9d9dSBarry Smith   sizes = (int *) PetscMalloc( ncolors*sizeof(int) );CHKPTRQ(sizes);
163549d3d68SSatish Balay   ierr = PetscMemzero(sizes,ncolors*sizeof(int));CHKERRQ(ierr);
16470c3da92SBarry Smith   for ( i=0; i<n; i++ ) {
165639f9d9dSBarry Smith     sizes[coloring[i]-1]++;
16670c3da92SBarry Smith   }
167639f9d9dSBarry Smith   ii    = (int **) PetscMalloc( ncolors*sizeof(int*) );CHKPTRQ(ii);
168639f9d9dSBarry Smith   ii[0] = (int *) PetscMalloc( n*sizeof(int) );CHKPTRQ(ii[0]);
169639f9d9dSBarry Smith   for ( i=1; i<ncolors; i++ ) {
170639f9d9dSBarry Smith     ii[i] = ii[i-1] + sizes[i-1];
171639f9d9dSBarry Smith   }
172549d3d68SSatish Balay   ierr = PetscMemzero(sizes,ncolors*sizeof(int));CHKERRQ(ierr);
173639f9d9dSBarry Smith   for ( i=0; i<n; i++ ) {
174639f9d9dSBarry Smith     ii[coloring[i]-1][sizes[coloring[i]-1]++] = i;
175639f9d9dSBarry Smith   }
176775f6a71SSatish Balay   is  = (IS *) PetscMalloc( ncolors*sizeof(IS) );CHKPTRQ(is);
177639f9d9dSBarry Smith   for ( i=0; i<ncolors; i++ ) {
178029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,sizes[i],ii[i],is+i);CHKERRQ(ierr);
179639f9d9dSBarry Smith   }
180639f9d9dSBarry Smith 
181f09e8eb9SSatish Balay   *iscoloring         = (ISColoring) PetscMalloc(sizeof(struct _p_ISColoring));CHKPTRQ(*iscoloring);
182639f9d9dSBarry Smith   (*iscoloring)->n    = ncolors;
183639f9d9dSBarry Smith   (*iscoloring)->is   = is;
1844be1940cSBarry Smith   ierr = PetscCommDuplicate_Private(mat->comm,&(*iscoloring)->comm,&tag);CHKERRQ(ierr);
185*606d414cSSatish Balay   ierr = PetscFree(sizes);CHKERRQ(ierr);
186*606d414cSSatish Balay   ierr = PetscFree(ii[0]);CHKERRQ(ierr);
187*606d414cSSatish Balay   ierr = PetscFree(ii);CHKERRQ(ierr);
1883a40ed3dSBarry Smith   PetscFunctionReturn(0);
18970c3da92SBarry Smith }
19070c3da92SBarry Smith 
191639f9d9dSBarry Smith /*
192639f9d9dSBarry Smith      Makes a longer coloring[] array and calls the usual code with that
193639f9d9dSBarry Smith */
1945615d1e5SSatish Balay #undef __FUNC__
195d4bb536fSBarry Smith #define __FUNC__ "MatColoringPatch_SeqAIJ_Inode"
196639f9d9dSBarry Smith int MatColoringPatch_SeqAIJ_Inode(Mat mat,int ncolors,int *coloring,ISColoring *iscoloring)
19770c3da92SBarry Smith {
198639f9d9dSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) mat->data;
199639f9d9dSBarry Smith   int        n = a->n,ierr, m = a->inode.node_count,j,*ns = a->inode.size,row;
200639f9d9dSBarry Smith   int        *colorused,i,*newcolor;
20170c3da92SBarry Smith 
2023a40ed3dSBarry Smith   PetscFunctionBegin;
203639f9d9dSBarry Smith   newcolor = (int *) PetscMalloc((n+1)*sizeof(int));CHKPTRQ(newcolor);
20470c3da92SBarry Smith 
205639f9d9dSBarry Smith   /* loop over inodes, marking a color for each column*/
206639f9d9dSBarry Smith   row = 0;
20770c3da92SBarry Smith   for ( i=0; i<m; i++){
208639f9d9dSBarry Smith     for ( j=0; j<ns[i]; j++) {
209639f9d9dSBarry Smith       newcolor[row++] = coloring[i] + j*ncolors;
21070c3da92SBarry Smith     }
21170c3da92SBarry Smith   }
21270c3da92SBarry Smith 
213639f9d9dSBarry Smith   /* eliminate unneeded colors */
214639f9d9dSBarry Smith   colorused = (int *) PetscMalloc( 5*ncolors*sizeof(int) );CHKPTRQ(colorused);
215549d3d68SSatish Balay   ierr      = PetscMemzero(colorused,5*ncolors*sizeof(int));CHKERRQ(ierr);
216639f9d9dSBarry Smith   for ( i=0; i<n; i++ ) {
217639f9d9dSBarry Smith     colorused[newcolor[i]-1] = 1;
218639f9d9dSBarry Smith   }
21970c3da92SBarry Smith 
220639f9d9dSBarry Smith   for ( i=1; i<5*ncolors; i++ ) {
221639f9d9dSBarry Smith     colorused[i] += colorused[i-1];
22270c3da92SBarry Smith   }
223639f9d9dSBarry Smith   ncolors = colorused[5*ncolors-1];
224639f9d9dSBarry Smith   for ( i=0; i<n; i++ ) {
225639f9d9dSBarry Smith     newcolor[i] = colorused[newcolor[i]-1];
22670c3da92SBarry Smith   }
227*606d414cSSatish Balay   ierr = PetscFree(colorused);CHKERRQ(ierr);
22870c3da92SBarry Smith 
229639f9d9dSBarry Smith   ierr = MatColoringPatch_SeqAIJ(mat,ncolors,newcolor,iscoloring);CHKERRQ(ierr);
230*606d414cSSatish Balay   ierr = PetscFree(newcolor);CHKERRQ(ierr);
231639f9d9dSBarry Smith 
2323a40ed3dSBarry Smith   PetscFunctionReturn(0);
23370c3da92SBarry Smith }
23470c3da92SBarry Smith 
2353a40ed3dSBarry Smith 
2363a40ed3dSBarry Smith 
2373a40ed3dSBarry Smith 
2383a40ed3dSBarry Smith 
2393a40ed3dSBarry Smith 
240