xref: /petsc/src/mat/impls/aij/seq/fdaij.c (revision d4bb536f0e426e9a0292bbfd5743770a9b03f0d5)
1005c665bSBarry Smith 
2a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
3*d4bb536fSBarry Smith static char vcid[] = "$Id: fdaij.c,v 1.13 1997/08/13 22:24:02 bsmith Exp bsmith $";
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__
14*d4bb536fSBarry 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 
22005c665bSBarry Smith   c->M             = mat->M;  /* set total rows, columns and local rows */
23005c665bSBarry Smith   c->N             = mat->N;
24005c665bSBarry Smith   c->m             = mat->M;
25005c665bSBarry Smith   c->rstart        = 0;
26005c665bSBarry Smith 
27639f9d9dSBarry Smith   c->ncolors       = nis;
28639f9d9dSBarry Smith   c->ncolumns      = (int *) PetscMalloc( nis*sizeof(int) );   CHKPTRQ(c->ncolumns);
29639f9d9dSBarry Smith   c->columns       = (int **) PetscMalloc( nis*sizeof(int *)); CHKPTRQ(c->columns);
30639f9d9dSBarry Smith   c->nrows         = (int *) PetscMalloc( nis*sizeof(int) );   CHKPTRQ(c->nrows);
31639f9d9dSBarry Smith   c->rows          = (int **) PetscMalloc( nis*sizeof(int *)); CHKPTRQ(c->rows);
32639f9d9dSBarry Smith   c->columnsforrow = (int **) PetscMalloc( nis*sizeof(int *)); CHKPTRQ(c->columnsforrow);
3343a90d84SBarry Smith 
3443a90d84SBarry Smith   /*
3543a90d84SBarry Smith       Calls the _SeqAIJ() version of these routines to make sure it does not
3643a90d84SBarry Smith      get the reduced (by inodes) version of I and J
3743a90d84SBarry Smith   */
3843a90d84SBarry Smith   ierr = MatGetColumnIJ_SeqAIJ(mat,0,PETSC_FALSE,&ncols,&ci,&cj,&done); CHKERRQ(ierr);
3970c3da92SBarry Smith 
4070c3da92SBarry Smith   /*
41639f9d9dSBarry Smith      Temporary option to allow for debugging/testing
4270c3da92SBarry Smith   */
43639f9d9dSBarry Smith   ierr = OptionsHasName(0,"-matfdcoloring_slow",&flg);
4470c3da92SBarry Smith 
45639f9d9dSBarry Smith   rowhit        = (int *) PetscMalloc( (N+1)*sizeof(int) ); CHKPTRQ(rowhit);
46639f9d9dSBarry Smith   columnsforrow = (int *) PetscMalloc( (N+1)*sizeof(int) ); CHKPTRQ(columnsforrow);
4770c3da92SBarry Smith 
48639f9d9dSBarry Smith   for ( i=0; i<nis; i++ ) {
49639f9d9dSBarry Smith     ierr = ISGetSize(isa[i],&n); CHKERRQ(ierr);
50639f9d9dSBarry Smith     ierr = ISGetIndices(isa[i],&is); CHKERRQ(ierr);
51639f9d9dSBarry Smith     c->ncolumns[i] = n;
52639f9d9dSBarry Smith     if (n) {
53639f9d9dSBarry Smith       c->columns[i]  = (int *) PetscMalloc( n*sizeof(int) ); CHKPTRQ(c->columns[i]);
54639f9d9dSBarry Smith       PetscMemcpy(c->columns[i],is,n*sizeof(int));
5570c3da92SBarry Smith     } else {
56639f9d9dSBarry Smith       c->columns[i]  = 0;
5770c3da92SBarry Smith     }
5870c3da92SBarry Smith 
59639f9d9dSBarry Smith     if (flg) { /* ------------------------------------------------------------------------------*/
60639f9d9dSBarry Smith       /* crude version requires O(N*N) work */
61639f9d9dSBarry Smith       PetscMemzero(rowhit,N*sizeof(int));
62639f9d9dSBarry Smith       /* loop over columns*/
63639f9d9dSBarry Smith       for ( j=0; j<n; j++ ) {
64639f9d9dSBarry Smith         col  = is[j];
65639f9d9dSBarry Smith         rows = cj + ci[col];
66639f9d9dSBarry Smith         m    = ci[col+1] - ci[col];
67639f9d9dSBarry Smith         /* loop over columns marking them in rowhit */
68639f9d9dSBarry Smith         for ( k=0; k<m; k++ ) {
69639f9d9dSBarry Smith           rowhit[*rows++] = col + 1;
7070c3da92SBarry Smith         }
7170c3da92SBarry Smith       }
72639f9d9dSBarry Smith       /* count the number of hits */
73639f9d9dSBarry Smith       nrows = 0;
7470c3da92SBarry Smith       for ( j=0; j<N; j++ ) {
75639f9d9dSBarry Smith         if (rowhit[j]) nrows++;
76639f9d9dSBarry Smith       }
77639f9d9dSBarry Smith       c->nrows[i]         = nrows;
78639f9d9dSBarry Smith       c->rows[i]          = (int *) PetscMalloc(nrows*sizeof(int)); CHKPTRQ(c->rows[i]);
79639f9d9dSBarry Smith       c->columnsforrow[i] = (int *) PetscMalloc(nrows*sizeof(int)); CHKPTRQ(c->columnsforrow[i]);
80639f9d9dSBarry Smith       nrows = 0;
81639f9d9dSBarry Smith       for ( j=0; j<N; j++ ) {
82639f9d9dSBarry Smith         if (rowhit[j]) {
83639f9d9dSBarry Smith           c->rows[i][nrows]           = j;
84639f9d9dSBarry Smith           c->columnsforrow[i][nrows] = rowhit[j] - 1;
85639f9d9dSBarry Smith           nrows++;
8670c3da92SBarry Smith         }
8770c3da92SBarry Smith       }
88639f9d9dSBarry Smith     } else {  /*-------------------------------------------------------------------------------*/
89639f9d9dSBarry Smith       /* efficient version, using rowhit as a linked list */
90639f9d9dSBarry Smith       int currentcol,fm,mfm;
91639f9d9dSBarry Smith       rowhit[N] = N;
92639f9d9dSBarry Smith       nrows     = 0;
93639f9d9dSBarry Smith       /* loop over columns */
94639f9d9dSBarry Smith       for ( j=0; j<n; j++ ) {
95639f9d9dSBarry Smith         col   = is[j];
96639f9d9dSBarry Smith         rows  = cj + ci[col];
97639f9d9dSBarry Smith         m     = ci[col+1] - ci[col];
98639f9d9dSBarry Smith         /* loop over columns marking them in rowhit */
99639f9d9dSBarry Smith         fm    = N; /* fm points to first entry in linked list */
100639f9d9dSBarry Smith         for ( k=0; k<m; k++ ) {
101639f9d9dSBarry Smith           currentcol = *rows++;
102639f9d9dSBarry Smith 	  /* is it already in the list? */
103639f9d9dSBarry Smith           do {
104639f9d9dSBarry Smith             mfm  = fm;
105639f9d9dSBarry Smith             fm   = rowhit[fm];
106639f9d9dSBarry Smith           } while (fm < currentcol);
107639f9d9dSBarry Smith           /* not in list so add it */
108639f9d9dSBarry Smith           if (fm != currentcol) {
109639f9d9dSBarry Smith             nrows++;
110639f9d9dSBarry Smith             columnsforrow[currentcol] = col;
111639f9d9dSBarry Smith             /* next three lines insert new entry into linked list */
112639f9d9dSBarry Smith             rowhit[mfm]               = currentcol;
113639f9d9dSBarry Smith             rowhit[currentcol]        = fm;
114639f9d9dSBarry Smith             fm                        = currentcol;
115639f9d9dSBarry Smith             /* fm points to present position in list since we know the columns are sorted */
11670c3da92SBarry Smith           } else {
117e3372554SBarry Smith             SETERRQ(1,0,"Invalid coloring");
11870c3da92SBarry Smith           }
119639f9d9dSBarry Smith 
120639f9d9dSBarry Smith         }
121639f9d9dSBarry Smith       }
122639f9d9dSBarry Smith       c->nrows[i]         = nrows;
123639f9d9dSBarry Smith       c->rows[i]          = (int *)PetscMalloc((nrows+1)*sizeof(int));CHKPTRQ(c->rows[i]);
124639f9d9dSBarry Smith       c->columnsforrow[i] = (int *)PetscMalloc((nrows+1)*sizeof(int));CHKPTRQ(c->columnsforrow[i]);
125639f9d9dSBarry Smith       /* now store the linked list of rows into c->rows[i] */
126639f9d9dSBarry Smith       nrows = 0;
127639f9d9dSBarry Smith       fm    = rowhit[N];
128639f9d9dSBarry Smith       do {
129639f9d9dSBarry Smith         c->rows[i][nrows]            = fm;
130639f9d9dSBarry Smith         c->columnsforrow[i][nrows++] = columnsforrow[fm];
131639f9d9dSBarry Smith         fm                           = rowhit[fm];
132639f9d9dSBarry Smith       } while (fm < N);
133639f9d9dSBarry Smith     } /* ---------------------------------------------------------------------------------------*/
134639f9d9dSBarry Smith     ierr = ISRestoreIndices(isa[i],&is); CHKERRQ(ierr);
135639f9d9dSBarry Smith   }
13643a90d84SBarry Smith   ierr = MatRestoreColumnIJ_SeqAIJ(mat,0,PETSC_FALSE,&ncols,&ci,&cj,&done); CHKERRQ(ierr);
137639f9d9dSBarry Smith 
138639f9d9dSBarry Smith   PetscFree(rowhit);
139639f9d9dSBarry Smith   PetscFree(columnsforrow);
140639f9d9dSBarry Smith 
141639f9d9dSBarry Smith   c->scale  = (Scalar *) PetscMalloc( 2*N*sizeof(Scalar) ); CHKPTRQ(c->scale);
142639f9d9dSBarry Smith   c->wscale = c->scale + N;
143639f9d9dSBarry Smith 
14470c3da92SBarry Smith   return 0;
14570c3da92SBarry Smith }
14670c3da92SBarry Smith 
1475615d1e5SSatish Balay #undef __FUNC__
148*d4bb536fSBarry Smith #define __FUNC__ "MatColoringPatch_SeqAIJ"
149639f9d9dSBarry Smith int MatColoringPatch_SeqAIJ(Mat mat,int ncolors,int *coloring,ISColoring *iscoloring)
15070c3da92SBarry Smith {
151639f9d9dSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) mat->data;
152639f9d9dSBarry Smith   int        n = a->n,*sizes,i,**ii,ierr,tag;
153639f9d9dSBarry Smith   IS         *is;
15470c3da92SBarry Smith 
155639f9d9dSBarry Smith   /* construct the index sets from the coloring array */
156639f9d9dSBarry Smith   sizes = (int *) PetscMalloc( ncolors*sizeof(int) ); CHKPTRQ(sizes);
157639f9d9dSBarry Smith   PetscMemzero(sizes,ncolors*sizeof(int));
15870c3da92SBarry Smith   for ( i=0; i<n; i++ ) {
159639f9d9dSBarry Smith     sizes[coloring[i]-1]++;
16070c3da92SBarry Smith   }
161639f9d9dSBarry Smith   ii    = (int **) PetscMalloc( ncolors*sizeof(int*) ); CHKPTRQ(ii);
162639f9d9dSBarry Smith   ii[0] = (int *) PetscMalloc( n*sizeof(int) ); CHKPTRQ(ii[0]);
163639f9d9dSBarry Smith   for ( i=1; i<ncolors; i++ ) {
164639f9d9dSBarry Smith     ii[i] = ii[i-1] + sizes[i-1];
165639f9d9dSBarry Smith   }
166639f9d9dSBarry Smith   PetscMemzero(sizes,ncolors*sizeof(int));
167639f9d9dSBarry Smith   for ( i=0; i<n; i++ ) {
168639f9d9dSBarry Smith     ii[coloring[i]-1][sizes[coloring[i]-1]++] = i;
169639f9d9dSBarry Smith   }
170775f6a71SSatish Balay   is  = (IS *) PetscMalloc( ncolors*sizeof(IS) ); CHKPTRQ(is);
171639f9d9dSBarry Smith   for ( i=0; i<ncolors; i++ ) {
172029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,sizes[i],ii[i],is+i); CHKERRQ(ierr);
173639f9d9dSBarry Smith   }
174639f9d9dSBarry Smith 
175f09e8eb9SSatish Balay   *iscoloring         = (ISColoring) PetscMalloc(sizeof(struct _p_ISColoring));CHKPTRQ(*iscoloring);
176639f9d9dSBarry Smith   (*iscoloring)->n    = ncolors;
177639f9d9dSBarry Smith   (*iscoloring)->is   = is;
178639f9d9dSBarry Smith   PetscCommDup_Private(mat->comm,&(*iscoloring)->comm,&tag);
179639f9d9dSBarry Smith   PetscFree(sizes);
180639f9d9dSBarry Smith   PetscFree(ii[0]);
181639f9d9dSBarry Smith   PetscFree(ii);
18270c3da92SBarry Smith   return 0;
18370c3da92SBarry Smith }
18470c3da92SBarry Smith 
185639f9d9dSBarry Smith /*
186639f9d9dSBarry Smith      Makes a longer coloring[] array and calls the usual code with that
187639f9d9dSBarry Smith */
1885615d1e5SSatish Balay #undef __FUNC__
189*d4bb536fSBarry Smith #define __FUNC__ "MatColoringPatch_SeqAIJ_Inode"
190639f9d9dSBarry Smith int MatColoringPatch_SeqAIJ_Inode(Mat mat,int ncolors,int *coloring,ISColoring *iscoloring)
19170c3da92SBarry Smith {
192639f9d9dSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) mat->data;
193639f9d9dSBarry Smith   int        n = a->n,ierr, m = a->inode.node_count,j,*ns = a->inode.size,row;
194639f9d9dSBarry Smith   int        *colorused,i,*newcolor;
19570c3da92SBarry Smith 
196639f9d9dSBarry Smith   newcolor = (int *) PetscMalloc((n+1)*sizeof(int)); CHKPTRQ(newcolor);
19770c3da92SBarry Smith 
198639f9d9dSBarry Smith   /* loop over inodes, marking a color for each column*/
199639f9d9dSBarry Smith   row = 0;
20070c3da92SBarry Smith   for ( i=0; i<m; i++){
201639f9d9dSBarry Smith     for ( j=0; j<ns[i]; j++) {
202639f9d9dSBarry Smith       newcolor[row++] = coloring[i] + j*ncolors;
20370c3da92SBarry Smith     }
20470c3da92SBarry Smith   }
20570c3da92SBarry Smith 
206639f9d9dSBarry Smith   /* eliminate unneeded colors */
207639f9d9dSBarry Smith   colorused = (int *) PetscMalloc( 5*ncolors*sizeof(int) ); CHKPTRQ(colorused);
208639f9d9dSBarry Smith   PetscMemzero(colorused,5*ncolors*sizeof(int));
209639f9d9dSBarry Smith   for ( i=0; i<n; i++ ) {
210639f9d9dSBarry Smith     colorused[newcolor[i]-1] = 1;
211639f9d9dSBarry Smith   }
21270c3da92SBarry Smith 
213639f9d9dSBarry Smith   for ( i=1; i<5*ncolors; i++ ) {
214639f9d9dSBarry Smith     colorused[i] += colorused[i-1];
21570c3da92SBarry Smith   }
216639f9d9dSBarry Smith   ncolors = colorused[5*ncolors-1];
217639f9d9dSBarry Smith   for ( i=0; i<n; i++ ) {
218639f9d9dSBarry Smith     newcolor[i] = colorused[newcolor[i]-1];
21970c3da92SBarry Smith   }
220639f9d9dSBarry Smith   PetscFree(colorused);
22170c3da92SBarry Smith 
222639f9d9dSBarry Smith   ierr = MatColoringPatch_SeqAIJ(mat,ncolors,newcolor,iscoloring); CHKERRQ(ierr);
223639f9d9dSBarry Smith   PetscFree(newcolor);
224639f9d9dSBarry Smith 
22570c3da92SBarry Smith   return 0;
22670c3da92SBarry Smith }
22770c3da92SBarry Smith 
228