xref: /petsc/src/mat/impls/aij/seq/fdaij.c (revision 054951ad227a3627c7f2e3d3160721959c51d9d8)
170c3da92SBarry Smith 
2c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/aij.h>
3525d23c0SHong Zhang #include <../src/mat/impls/baij/seq/baij.h>
4525d23c0SHong Zhang 
5040ebd07SHong Zhang /*
6040ebd07SHong Zhang     This routine is shared by SeqAIJ and SeqBAIJ matrices,
7040ebd07SHong Zhang     since it operators only on the nonzero structure of the elements or blocks.
8040ebd07SHong Zhang */
9b582cc96SHong Zhang #undef __FUNCT__
1093dfae19SHong Zhang #define __FUNCT__ "MatFDColoringCreate_SeqXAIJ"
1193dfae19SHong Zhang PetscErrorCode MatFDColoringCreate_SeqXAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c)
1293dfae19SHong Zhang {
1393dfae19SHong Zhang   PetscErrorCode ierr;
14a8971b87SHong Zhang   PetscInt       bs,nis=iscoloring->n,m=mat->rmap->n;
1593dfae19SHong Zhang   PetscBool      isBAIJ;
1693dfae19SHong Zhang 
1793dfae19SHong Zhang   PetscFunctionBegin;
18531e53bdSHong Zhang   /* set default brows and bcols for speedup inserting the dense matrix into sparse Jacobian */
1993dfae19SHong Zhang   ierr = MatGetBlockSize(mat,&bs);CHKERRQ(ierr);
2093dfae19SHong Zhang   ierr = PetscObjectTypeCompare((PetscObject)mat,MATSEQBAIJ,&isBAIJ);CHKERRQ(ierr);
21a8971b87SHong Zhang   if (isBAIJ) {
22a8971b87SHong Zhang     c->brows = m;
23a8971b87SHong Zhang     c->bcols = 1;
24531e53bdSHong Zhang   } else { /* seqaij matrix */
25531e53bdSHong Zhang     /* bcols is chosen s.t. dy-array takes 50% of memory space as mat */
2693dfae19SHong Zhang     Mat_SeqAIJ *spA = (Mat_SeqAIJ*)mat->data;
27531e53bdSHong Zhang     PetscReal  mem;
28a8971b87SHong Zhang     PetscInt   nz,brows,bcols;
29531e53bdSHong Zhang 
3093dfae19SHong Zhang     bs    = 1; /* only bs=1 is supported for SeqAIJ matrix */
31531e53bdSHong Zhang 
32531e53bdSHong Zhang     nz    = spA->nz;
33531e53bdSHong Zhang     mem   = nz*(sizeof(PetscScalar) + sizeof(PetscInt)) + 3*m*sizeof(PetscInt);
34531e53bdSHong Zhang     bcols = (PetscInt)(0.5*mem /(m*sizeof(PetscScalar)));
35531e53bdSHong Zhang     brows = 1000/bcols;
36531e53bdSHong Zhang     if (bcols > nis) bcols = nis;
37531e53bdSHong Zhang     if (brows == 0 || brows > m) brows = m;
38531e53bdSHong Zhang     c->brows = brows;
39531e53bdSHong Zhang     c->bcols = bcols;
4093dfae19SHong Zhang   }
41531e53bdSHong Zhang 
4293dfae19SHong Zhang   c->M       = mat->rmap->N/bs;   /* set total rows, columns and local rows */
4393dfae19SHong Zhang   c->N       = mat->cmap->N/bs;
4493dfae19SHong Zhang   c->m       = mat->rmap->N/bs;
4593dfae19SHong Zhang   c->rstart  = 0;
4693dfae19SHong Zhang   c->ncolors = nis;
4793dfae19SHong Zhang   c->ctype   = IS_COLORING_GHOSTED;
4893dfae19SHong Zhang   PetscFunctionReturn(0);
4993dfae19SHong Zhang }
5093dfae19SHong Zhang 
51b3e1f37bSHong Zhang /*
52b3e1f37bSHong Zhang  Reorder Jentry such that blocked brows*bols of entries from dense matrix are inserted into sparse Jacobian
53b3e1f37bSHong Zhang    Input Parameters:
54b3e1f37bSHong Zhang +  mat - the matrix containing the nonzero structure of the Jacobian
55b3e1f37bSHong Zhang .  color - the coloring context
56b3e1f37bSHong Zhang -  nz - number of local non-zeros in mat
57b3e1f37bSHong Zhang */
5893dfae19SHong Zhang #undef __FUNCT__
59a8971b87SHong Zhang #define __FUNCT__ "MatFDColoringSetUpBlocked_AIJ_Private"
60a8971b87SHong Zhang PetscErrorCode MatFDColoringSetUpBlocked_AIJ_Private(Mat mat,MatFDColoring c,PetscInt nz)
6179c1e64dSHong Zhang {
6279c1e64dSHong Zhang   PetscErrorCode ierr;
63b3e1f37bSHong Zhang   PetscInt       i,j,nrows,nbcols,brows=c->brows,bcols=c->bcols,mbs=c->m,nis=c->ncolors;
64b3e1f37bSHong Zhang   PetscInt       *color_start,*row_start,*nrows_new,nz_new,row_end;
65a8971b87SHong Zhang   MatEntry       *Jentry_new,*Jentry=c->matentry;
6679c1e64dSHong Zhang 
6779c1e64dSHong Zhang   PetscFunctionBegin;
68a8971b87SHong Zhang   if (brows < 1 || brows > mbs) brows = mbs;
69*054951adSHong Zhang   ierr = PetscMalloc2(bcols+1,PetscInt,&color_start,bcols,PetscInt,&row_start);CHKERRQ(ierr);
706a509798SHong Zhang   ierr = PetscMalloc(nz*sizeof(MatEntry),&Jentry_new);CHKERRQ(ierr);
716a509798SHong Zhang   ierr = PetscMalloc(nis*sizeof(PetscInt),&nrows_new);CHKERRQ(ierr);
72b3e1f37bSHong Zhang   ierr = PetscMalloc(bcols*mat->rmap->n*sizeof(PetscScalar),&c->dy);CHKERRQ(ierr);
73b3e1f37bSHong Zhang   ierr = PetscLogObjectMemory((PetscObject)c,bcols*mat->rmap->n*sizeof(PetscScalar));CHKERRQ(ierr);
746a509798SHong Zhang 
75e2c857f8SHong Zhang   nz_new = 0;
76b3e1f37bSHong Zhang   nbcols = 0;
77*054951adSHong Zhang   color_start[bcols] = 0;
78e2c857f8SHong Zhang   for (i=0; i<nis; i+=bcols) { /* loop over colors */
79*054951adSHong Zhang     if (i + bcols > nis) {
80*054951adSHong Zhang       color_start[nis - i] = color_start[bcols];
81*054951adSHong Zhang       bcols                = nis - i;
82*054951adSHong Zhang     }
83*054951adSHong Zhang 
84*054951adSHong Zhang     color_start[0] = color_start[bcols];
85*054951adSHong Zhang     for (j=0; j<bcols; j++) {
86*054951adSHong Zhang       color_start[j+1] = c->nrows[i+j] + color_start[j];
87*054951adSHong Zhang       row_start[j]     = 0;
88*054951adSHong Zhang     }
89e2c857f8SHong Zhang 
90e2c857f8SHong Zhang     row_end = brows;
91c8a9c622SHong Zhang     if (row_end > mbs) row_end = mbs;
92*054951adSHong Zhang 
93c8a9c622SHong Zhang     while (row_end <= mbs) {   /* loop over block rows */
94e2c857f8SHong Zhang       for (j=0; j<bcols; j++) {       /* loop over block columns */
95e2c857f8SHong Zhang         nrows = c->nrows[i+j];
96*054951adSHong Zhang         nz    = color_start[j];
97c8a9c622SHong Zhang         while (row_start[j] < nrows) {
98e2c857f8SHong Zhang           if (Jentry[nz].row >= row_end) {
99*054951adSHong Zhang             color_start[j] = nz;
100e2c857f8SHong Zhang             break;
101c8a9c622SHong Zhang           } else { /* copy Jentry[nz] to Jentry_new[nz_new] */
102c8a9c622SHong Zhang             Jentry_new[nz_new].row     = Jentry[nz].row + j*mbs; /* index in dy-array */
103d880da65SHong Zhang             Jentry_new[nz_new].col     = Jentry[nz].col;
104e2c857f8SHong Zhang             Jentry_new[nz_new].valaddr = Jentry[nz].valaddr;
105c8a9c622SHong Zhang             nz_new++; nz++; row_start[j]++;
106e2c857f8SHong Zhang           }
107e2c857f8SHong Zhang         }
108e2c857f8SHong Zhang       }
109c8a9c622SHong Zhang       if (row_end == mbs) break;
110e2c857f8SHong Zhang       row_end += brows;
111c8a9c622SHong Zhang       if (row_end > mbs) row_end = mbs;
112e2c857f8SHong Zhang     }
1136a509798SHong Zhang     nrows_new[nbcols++] = nz_new;
114e2c857f8SHong Zhang   }
115b3e1f37bSHong Zhang   ierr = PetscFree2(color_start,row_start);CHKERRQ(ierr);
116b3e1f37bSHong Zhang 
1176a509798SHong Zhang   for (i=nbcols-1; i>0; i--) nrows_new[i] -= nrows_new[i-1];
1186a509798SHong Zhang   ierr = PetscFree(c->nrows);CHKERRQ(ierr);
1196a509798SHong Zhang   ierr = PetscFree(Jentry);CHKERRQ(ierr);
120b3e1f37bSHong Zhang   c->nrows    = nrows_new;
1216a509798SHong Zhang   c->matentry = Jentry_new;
122a8971b87SHong Zhang   PetscFunctionReturn(0);
123e2c857f8SHong Zhang }
124a8971b87SHong Zhang 
125a8971b87SHong Zhang #undef __FUNCT__
126a8971b87SHong Zhang #define __FUNCT__ "MatFDColoringSetUp_SeqXAIJ"
127a8971b87SHong Zhang PetscErrorCode MatFDColoringSetUp_SeqXAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c)
128a8971b87SHong Zhang {
129a8971b87SHong Zhang   PetscErrorCode ierr;
130a8971b87SHong Zhang   PetscInt       i,n,nrows,mbs=c->m,j,k,m,ncols,col,nis=iscoloring->n,*rowhit,bs,bs2,*spidx,nz;
131a8971b87SHong Zhang   const PetscInt *is,*row,*ci,*cj;
132a8971b87SHong Zhang   IS             *isa;
133a8971b87SHong Zhang   PetscBool      isBAIJ;
134a8971b87SHong Zhang   PetscScalar    *A_val,**valaddrhit;
135a8971b87SHong Zhang   MatEntry       *Jentry;
136a8971b87SHong Zhang 
137a8971b87SHong Zhang   PetscFunctionBegin;
138a8971b87SHong Zhang   ierr = ISColoringGetIS(iscoloring,PETSC_IGNORE,&isa);CHKERRQ(ierr);
139a8971b87SHong Zhang 
140a8971b87SHong Zhang   ierr = MatGetBlockSize(mat,&bs);CHKERRQ(ierr);
141a8971b87SHong Zhang   ierr = PetscObjectTypeCompare((PetscObject)mat,MATSEQBAIJ,&isBAIJ);CHKERRQ(ierr);
142a8971b87SHong Zhang   if (isBAIJ) {
143a8971b87SHong Zhang     Mat_SeqBAIJ *spA = (Mat_SeqBAIJ*)mat->data;
144a8971b87SHong Zhang     A_val = spA->a;
145a8971b87SHong Zhang     nz    = spA->nz;
146a8971b87SHong Zhang   } else {
147a8971b87SHong Zhang     Mat_SeqAIJ  *spA = (Mat_SeqAIJ*)mat->data;
148a8971b87SHong Zhang     A_val = spA->a;
149a8971b87SHong Zhang     nz    = spA->nz;
150a8971b87SHong Zhang     bs    = 1; /* only bs=1 is supported for SeqAIJ matrix */
151a8971b87SHong Zhang   }
152a8971b87SHong Zhang 
153a8971b87SHong Zhang   ierr       = PetscMalloc(nis*sizeof(PetscInt),&c->ncolumns);CHKERRQ(ierr);
154a8971b87SHong Zhang   ierr       = PetscMalloc(nis*sizeof(PetscInt*),&c->columns);CHKERRQ(ierr);
155a8971b87SHong Zhang   ierr       = PetscMalloc(nis*sizeof(PetscInt),&c->nrows);CHKERRQ(ierr);
156a8971b87SHong Zhang   ierr       = PetscLogObjectMemory((PetscObject)c,3*nis*sizeof(PetscInt));CHKERRQ(ierr);
157a8971b87SHong Zhang 
158a8971b87SHong Zhang   ierr       = PetscMalloc(nz*sizeof(MatEntry),&Jentry);CHKERRQ(ierr);
159a8971b87SHong Zhang   ierr       = PetscLogObjectMemory((PetscObject)c,nz*sizeof(MatEntry));CHKERRQ(ierr);
160a8971b87SHong Zhang   c->matentry = Jentry;
161a8971b87SHong Zhang 
162a8971b87SHong Zhang   if (isBAIJ) {
163a8971b87SHong Zhang     ierr = MatGetColumnIJ_SeqBAIJ_Color(mat,0,PETSC_FALSE,PETSC_FALSE,&ncols,&ci,&cj,&spidx,NULL);CHKERRQ(ierr);
164a8971b87SHong Zhang   } else {
165a8971b87SHong Zhang     ierr = MatGetColumnIJ_SeqAIJ_Color(mat,0,PETSC_FALSE,PETSC_FALSE,&ncols,&ci,&cj,&spidx,NULL);CHKERRQ(ierr);
166a8971b87SHong Zhang   }
167a8971b87SHong Zhang 
168a8971b87SHong Zhang   ierr = PetscMalloc2(c->m,PetscInt,&rowhit,c->m,PetscScalar*,&valaddrhit);CHKERRQ(ierr);
169a8971b87SHong Zhang   ierr = PetscMemzero(rowhit,c->m*sizeof(PetscInt));CHKERRQ(ierr);
170a8971b87SHong Zhang 
171a8971b87SHong Zhang   nz = 0;
172a8971b87SHong Zhang   for (i=0; i<nis; i++) { /* loop over colors */
173a8971b87SHong Zhang     ierr = ISGetLocalSize(isa[i],&n);CHKERRQ(ierr);
174a8971b87SHong Zhang     ierr = ISGetIndices(isa[i],&is);CHKERRQ(ierr);
175a8971b87SHong Zhang 
176a8971b87SHong Zhang     c->ncolumns[i] = n;
177a8971b87SHong Zhang     if (n) {
178a8971b87SHong Zhang       ierr = PetscMalloc(n*sizeof(PetscInt),&c->columns[i]);CHKERRQ(ierr);
179a8971b87SHong Zhang       ierr = PetscLogObjectMemory((PetscObject)c,n*sizeof(PetscInt));CHKERRQ(ierr);
180a8971b87SHong Zhang       ierr = PetscMemcpy(c->columns[i],is,n*sizeof(PetscInt));CHKERRQ(ierr);
181a8971b87SHong Zhang     } else {
182a8971b87SHong Zhang       c->columns[i] = 0;
183a8971b87SHong Zhang     }
184a8971b87SHong Zhang 
185a8971b87SHong Zhang     /* fast, crude version requires O(N*N) work */
186a8971b87SHong Zhang     bs2   = bs*bs;
187a8971b87SHong Zhang     nrows = 0;
188a8971b87SHong Zhang     for (j=0; j<n; j++) {  /* loop over columns */
189a8971b87SHong Zhang       col    = is[j];
190a8971b87SHong Zhang       row    = cj + ci[col];
191a8971b87SHong Zhang       m      = ci[col+1] - ci[col];
192a8971b87SHong Zhang       nrows += m;
193a8971b87SHong Zhang       for (k=0; k<m; k++) {  /* loop over columns marking them in rowhit */
194a8971b87SHong Zhang         rowhit[*row]       = col + 1;
195a8971b87SHong Zhang         valaddrhit[*row++] = &A_val[bs2*spidx[ci[col] + k]];
196a8971b87SHong Zhang       }
197a8971b87SHong Zhang     }
198a8971b87SHong Zhang     c->nrows[i] = nrows; /* total num of rows for this color */
199a8971b87SHong Zhang 
200a8971b87SHong Zhang     for (j=0; j<mbs; j++) { /* loop over rows */
201a8971b87SHong Zhang       if (rowhit[j]) {
202a8971b87SHong Zhang         Jentry[nz].row     = j;              /* local row index */
203a8971b87SHong Zhang         Jentry[nz].col     = rowhit[j] - 1;  /* local column index */
204a8971b87SHong Zhang         Jentry[nz].valaddr = valaddrhit[j];  /* address of mat value for this entry */
205a8971b87SHong Zhang         nz++;
206a8971b87SHong Zhang         rowhit[j] = 0.0;                     /* zero rowhit for reuse */
207a8971b87SHong Zhang       }
208a8971b87SHong Zhang     }
209a8971b87SHong Zhang     ierr = ISRestoreIndices(isa[i],&is);CHKERRQ(ierr);
210a8971b87SHong Zhang   }
211a8971b87SHong Zhang 
212a8971b87SHong Zhang   if (c->bcols > 1) {  /* reorder Jentry for faster MatFDColoringApply() */
213a8971b87SHong Zhang     ierr = MatFDColoringSetUpBlocked_AIJ_Private(mat,c,nz);CHKERRQ(ierr);
214a8971b87SHong Zhang   }
21585740eacSHong Zhang 
216525d23c0SHong Zhang   if (isBAIJ) {
217525d23c0SHong Zhang     ierr = MatRestoreColumnIJ_SeqBAIJ_Color(mat,0,PETSC_FALSE,PETSC_FALSE,&ncols,&ci,&cj,&spidx,NULL);CHKERRQ(ierr);
218e3225e9dSHong Zhang     ierr = PetscMalloc(bs*mat->rmap->n*sizeof(PetscScalar),&c->dy);CHKERRQ(ierr);
219b3e1f37bSHong Zhang     ierr = PetscLogObjectMemory((PetscObject)c,bs*mat->rmap->n*sizeof(PetscScalar));CHKERRQ(ierr);
220525d23c0SHong Zhang   } else {
2216378f32dSHong Zhang     ierr = MatRestoreColumnIJ_SeqAIJ_Color(mat,0,PETSC_FALSE,PETSC_FALSE,&ncols,&ci,&cj,&spidx,NULL);CHKERRQ(ierr);
222525d23c0SHong Zhang   }
223a8971b87SHong Zhang   ierr = PetscFree2(rowhit,valaddrhit);CHKERRQ(ierr);
22479c1e64dSHong Zhang   ierr = ISColoringRestoreIS(iscoloring,&isa);CHKERRQ(ierr);
225476e0d0aSHong Zhang 
22652011a10SHong Zhang   ierr = VecCreateGhost(PetscObjectComm((PetscObject)mat),mat->rmap->n,PETSC_DETERMINE,0,NULL,&c->vscale);CHKERRQ(ierr);
227b3e1f37bSHong Zhang #if defined(PETSC_USE_INFO)
228b3e1f37bSHong Zhang   ierr = PetscInfo3(c,"ncolors %D, brows %D and bcols %D are used.\n",c->ncolors,c->brows,c->bcols);CHKERRQ(ierr);
229b3e1f37bSHong Zhang #endif
23079c1e64dSHong Zhang   PetscFunctionReturn(0);
23179c1e64dSHong Zhang }
232