1c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/aij.h> 2525d23c0SHong Zhang #include <../src/mat/impls/baij/seq/baij.h> 3d4002b98SHong Zhang #include <../src/mat/impls/sell/seq/sell.h> 4af0996ceSBarry Smith #include <petsc/private/isimpl.h> 5525d23c0SHong Zhang 6040ebd07SHong Zhang /* 7040ebd07SHong Zhang This routine is shared by SeqAIJ and SeqBAIJ matrices, 8040ebd07SHong Zhang since it operators only on the nonzero structure of the elements or blocks. 9040ebd07SHong Zhang */ 1093dfae19SHong Zhang PetscErrorCode MatFDColoringCreate_SeqXAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c) 1193dfae19SHong Zhang { 1293dfae19SHong Zhang PetscErrorCode ierr; 13a8971b87SHong Zhang PetscInt bs,nis=iscoloring->n,m=mat->rmap->n; 14d4002b98SHong Zhang PetscBool isBAIJ,isSELL; 1593dfae19SHong Zhang 1693dfae19SHong Zhang PetscFunctionBegin; 17531e53bdSHong Zhang /* set default brows and bcols for speedup inserting the dense matrix into sparse Jacobian */ 1893dfae19SHong Zhang ierr = MatGetBlockSize(mat,&bs);CHKERRQ(ierr); 19b9e7e5c1SBarry Smith ierr = PetscObjectBaseTypeCompare((PetscObject)mat,MATSEQBAIJ,&isBAIJ);CHKERRQ(ierr); 20d4002b98SHong Zhang ierr = PetscObjectTypeCompare((PetscObject)mat,MATSEQSELL,&isSELL);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 */ 26531e53bdSHong Zhang PetscReal mem; 27a8971b87SHong Zhang PetscInt nz,brows,bcols; 28d4002b98SHong Zhang if (isSELL) { 29d4002b98SHong Zhang Mat_SeqSELL *spA = (Mat_SeqSELL*)mat->data; 30f4b713efSHong Zhang nz = spA->nz; 31f4b713efSHong Zhang } else { 32f4b713efSHong Zhang Mat_SeqAIJ *spA = (Mat_SeqAIJ*)mat->data; 33f4b713efSHong Zhang nz = spA->nz; 34f4b713efSHong Zhang } 35531e53bdSHong Zhang 3693dfae19SHong Zhang bs = 1; /* only bs=1 is supported for SeqAIJ matrix */ 37531e53bdSHong Zhang mem = nz*(sizeof(PetscScalar) + sizeof(PetscInt)) + 3*m*sizeof(PetscInt); 38531e53bdSHong Zhang bcols = (PetscInt)(0.5*mem /(m*sizeof(PetscScalar))); 39531e53bdSHong Zhang brows = 1000/bcols; 40531e53bdSHong Zhang if (bcols > nis) bcols = nis; 41531e53bdSHong Zhang if (brows == 0 || brows > m) brows = m; 42531e53bdSHong Zhang c->brows = brows; 43531e53bdSHong Zhang c->bcols = bcols; 4493dfae19SHong Zhang } 45531e53bdSHong Zhang 4693dfae19SHong Zhang c->M = mat->rmap->N/bs; /* set total rows, columns and local rows */ 4793dfae19SHong Zhang c->N = mat->cmap->N/bs; 4893dfae19SHong Zhang c->m = mat->rmap->N/bs; 4993dfae19SHong Zhang c->rstart = 0; 5093dfae19SHong Zhang c->ncolors = nis; 5191e7fa0fSBarry Smith c->ctype = iscoloring->ctype; 5293dfae19SHong Zhang PetscFunctionReturn(0); 5393dfae19SHong Zhang } 5493dfae19SHong Zhang 55b3e1f37bSHong Zhang /* 560df34763SHong Zhang Reorder Jentry such that blocked brows*bols of entries from dense matrix are inserted into Jacobian for improved cache performance 57b3e1f37bSHong Zhang Input Parameters: 58b3e1f37bSHong Zhang + mat - the matrix containing the nonzero structure of the Jacobian 59b3e1f37bSHong Zhang . color - the coloring context 60b3e1f37bSHong Zhang - nz - number of local non-zeros in mat 61b3e1f37bSHong Zhang */ 62a8971b87SHong Zhang PetscErrorCode MatFDColoringSetUpBlocked_AIJ_Private(Mat mat,MatFDColoring c,PetscInt nz) 6379c1e64dSHong Zhang { 6479c1e64dSHong Zhang PetscErrorCode ierr; 65b3e1f37bSHong Zhang PetscInt i,j,nrows,nbcols,brows=c->brows,bcols=c->bcols,mbs=c->m,nis=c->ncolors; 66b3e1f37bSHong Zhang PetscInt *color_start,*row_start,*nrows_new,nz_new,row_end; 6779c1e64dSHong Zhang 6879c1e64dSHong Zhang PetscFunctionBegin; 69a8971b87SHong Zhang if (brows < 1 || brows > mbs) brows = mbs; 70dcca6d9dSJed Brown ierr = PetscMalloc2(bcols+1,&color_start,bcols,&row_start);CHKERRQ(ierr); 715bdb020cSBarry Smith ierr = PetscCalloc1(nis,&nrows_new);CHKERRQ(ierr); 72785e854fSJed Brown ierr = PetscMalloc1(bcols*mat->rmap->n,&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; 77054951adSHong Zhang color_start[bcols] = 0; 780df34763SHong Zhang 790df34763SHong Zhang if (c->htype[0] == 'd') { /* ---- c->htype == 'ds', use MatEntry --------*/ 800df34763SHong Zhang MatEntry *Jentry_new,*Jentry=c->matentry; 81*e2cf4d64SStefano Zampini 82785e854fSJed Brown ierr = PetscMalloc1(nz,&Jentry_new);CHKERRQ(ierr); 83e2c857f8SHong Zhang for (i=0; i<nis; i+=bcols) { /* loop over colors */ 84054951adSHong Zhang if (i + bcols > nis) { 85054951adSHong Zhang color_start[nis - i] = color_start[bcols]; 86054951adSHong Zhang bcols = nis - i; 87054951adSHong Zhang } 88054951adSHong Zhang 89054951adSHong Zhang color_start[0] = color_start[bcols]; 90054951adSHong Zhang for (j=0; j<bcols; j++) { 91054951adSHong Zhang color_start[j+1] = c->nrows[i+j] + color_start[j]; 92054951adSHong Zhang row_start[j] = 0; 93054951adSHong Zhang } 94e2c857f8SHong Zhang 95e2c857f8SHong Zhang row_end = brows; 96c8a9c622SHong Zhang if (row_end > mbs) row_end = mbs; 97054951adSHong Zhang 98c8a9c622SHong Zhang while (row_end <= mbs) { /* loop over block rows */ 99e2c857f8SHong Zhang for (j=0; j<bcols; j++) { /* loop over block columns */ 100e2c857f8SHong Zhang nrows = c->nrows[i+j]; 101054951adSHong Zhang nz = color_start[j]; 102c8a9c622SHong Zhang while (row_start[j] < nrows) { 103e2c857f8SHong Zhang if (Jentry[nz].row >= row_end) { 104054951adSHong Zhang color_start[j] = nz; 105e2c857f8SHong Zhang break; 106c8a9c622SHong Zhang } else { /* copy Jentry[nz] to Jentry_new[nz_new] */ 107c8a9c622SHong Zhang Jentry_new[nz_new].row = Jentry[nz].row + j*mbs; /* index in dy-array */ 108d880da65SHong Zhang Jentry_new[nz_new].col = Jentry[nz].col; 109e2c857f8SHong Zhang Jentry_new[nz_new].valaddr = Jentry[nz].valaddr; 110c8a9c622SHong Zhang nz_new++; nz++; row_start[j]++; 111e2c857f8SHong Zhang } 112e2c857f8SHong Zhang } 113e2c857f8SHong Zhang } 114c8a9c622SHong Zhang if (row_end == mbs) break; 115e2c857f8SHong Zhang row_end += brows; 116c8a9c622SHong Zhang if (row_end > mbs) row_end = mbs; 117e2c857f8SHong Zhang } 1186a509798SHong Zhang nrows_new[nbcols++] = nz_new; 119e2c857f8SHong Zhang } 1200df34763SHong Zhang ierr = PetscFree(Jentry);CHKERRQ(ierr); 1210df34763SHong Zhang c->matentry = Jentry_new; 1220df34763SHong Zhang } else { /* --------- c->htype == 'wp', use MatEntry2 ------------------*/ 1230df34763SHong Zhang MatEntry2 *Jentry2_new,*Jentry2=c->matentry2; 124*e2cf4d64SStefano Zampini 125785e854fSJed Brown ierr = PetscMalloc1(nz,&Jentry2_new);CHKERRQ(ierr); 1260df34763SHong Zhang for (i=0; i<nis; i+=bcols) { /* loop over colors */ 1270df34763SHong Zhang if (i + bcols > nis) { 1280df34763SHong Zhang color_start[nis - i] = color_start[bcols]; 1290df34763SHong Zhang bcols = nis - i; 1300df34763SHong Zhang } 1310df34763SHong Zhang 1320df34763SHong Zhang color_start[0] = color_start[bcols]; 1330df34763SHong Zhang for (j=0; j<bcols; j++) { 1340df34763SHong Zhang color_start[j+1] = c->nrows[i+j] + color_start[j]; 1350df34763SHong Zhang row_start[j] = 0; 1360df34763SHong Zhang } 1370df34763SHong Zhang 1380df34763SHong Zhang row_end = brows; 1390df34763SHong Zhang if (row_end > mbs) row_end = mbs; 1400df34763SHong Zhang 1410df34763SHong Zhang while (row_end <= mbs) { /* loop over block rows */ 1420df34763SHong Zhang for (j=0; j<bcols; j++) { /* loop over block columns */ 1430df34763SHong Zhang nrows = c->nrows[i+j]; 1440df34763SHong Zhang nz = color_start[j]; 1450df34763SHong Zhang while (row_start[j] < nrows) { 1460df34763SHong Zhang if (Jentry2[nz].row >= row_end) { 1470df34763SHong Zhang color_start[j] = nz; 1480df34763SHong Zhang break; 1490df34763SHong Zhang } else { /* copy Jentry2[nz] to Jentry2_new[nz_new] */ 1500df34763SHong Zhang Jentry2_new[nz_new].row = Jentry2[nz].row + j*mbs; /* index in dy-array */ 1510df34763SHong Zhang Jentry2_new[nz_new].valaddr = Jentry2[nz].valaddr; 1520df34763SHong Zhang nz_new++; nz++; row_start[j]++; 1530df34763SHong Zhang } 1540df34763SHong Zhang } 1550df34763SHong Zhang } 1560df34763SHong Zhang if (row_end == mbs) break; 1570df34763SHong Zhang row_end += brows; 1580df34763SHong Zhang if (row_end > mbs) row_end = mbs; 1590df34763SHong Zhang } 1600df34763SHong Zhang nrows_new[nbcols++] = nz_new; 1610df34763SHong Zhang } 1620df34763SHong Zhang ierr = PetscFree(Jentry2);CHKERRQ(ierr); 1630df34763SHong Zhang c->matentry2 = Jentry2_new; 1640df34763SHong Zhang } /* ---------------------------------------------*/ 1650df34763SHong Zhang 166b3e1f37bSHong Zhang ierr = PetscFree2(color_start,row_start);CHKERRQ(ierr); 167b3e1f37bSHong Zhang 1686a509798SHong Zhang for (i=nbcols-1; i>0; i--) nrows_new[i] -= nrows_new[i-1]; 1696a509798SHong Zhang ierr = PetscFree(c->nrows);CHKERRQ(ierr); 170b3e1f37bSHong Zhang c->nrows = nrows_new; 171a8971b87SHong Zhang PetscFunctionReturn(0); 172e2c857f8SHong Zhang } 173a8971b87SHong Zhang 174a8971b87SHong Zhang PetscErrorCode MatFDColoringSetUp_SeqXAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c) 175a8971b87SHong Zhang { 176a8971b87SHong Zhang PetscErrorCode ierr; 177071fcb05SBarry Smith PetscInt i,n,nrows,mbs=c->m,j,k,m,ncols,col,nis=iscoloring->n,*rowhit,bs,bs2,*spidx,nz,tmp; 178a8971b87SHong Zhang const PetscInt *is,*row,*ci,*cj; 179d4002b98SHong Zhang PetscBool isBAIJ,isSELL; 180071fcb05SBarry Smith const PetscScalar *A_val; 181071fcb05SBarry Smith PetscScalar **valaddrhit; 182a8971b87SHong Zhang MatEntry *Jentry; 1830df34763SHong Zhang MatEntry2 *Jentry2; 184a8971b87SHong Zhang 185a8971b87SHong Zhang PetscFunctionBegin; 186071fcb05SBarry Smith ierr = ISColoringGetIS(iscoloring,PETSC_OWN_POINTER,PETSC_IGNORE,&c->isa);CHKERRQ(ierr); 187a8971b87SHong Zhang 188a8971b87SHong Zhang ierr = MatGetBlockSize(mat,&bs);CHKERRQ(ierr); 189b9e7e5c1SBarry Smith ierr = PetscObjectBaseTypeCompare((PetscObject)mat,MATSEQBAIJ,&isBAIJ);CHKERRQ(ierr); 190d4002b98SHong Zhang ierr = PetscObjectTypeCompare((PetscObject)mat,MATSEQSELL,&isSELL);CHKERRQ(ierr); 191a8971b87SHong Zhang if (isBAIJ) { 192a8971b87SHong Zhang Mat_SeqBAIJ *spA = (Mat_SeqBAIJ*)mat->data; 193*e2cf4d64SStefano Zampini 194a8971b87SHong Zhang A_val = spA->a; 195a8971b87SHong Zhang nz = spA->nz; 196d4002b98SHong Zhang } else if (isSELL) { 197d4002b98SHong Zhang Mat_SeqSELL *spA = (Mat_SeqSELL*)mat->data; 198*e2cf4d64SStefano Zampini 199f4b713efSHong Zhang A_val = spA->val; 200f4b713efSHong Zhang nz = spA->nz; 201d4002b98SHong Zhang bs = 1; /* only bs=1 is supported for SeqSELL matrix */ 202a8971b87SHong Zhang } else { 203a8971b87SHong Zhang Mat_SeqAIJ *spA = (Mat_SeqAIJ*)mat->data; 204*e2cf4d64SStefano Zampini 205a8971b87SHong Zhang A_val = spA->a; 206a8971b87SHong Zhang nz = spA->nz; 207a8971b87SHong Zhang bs = 1; /* only bs=1 is supported for SeqAIJ matrix */ 208a8971b87SHong Zhang } 209a8971b87SHong Zhang 210071fcb05SBarry Smith ierr = PetscMalloc2(nis,&c->ncolumns,nis,&c->columns);CHKERRQ(ierr); 211071fcb05SBarry Smith ierr = PetscMalloc1(nis,&c->nrows);CHKERRQ(ierr); /* nrows is freeed seperately from ncolumns and columns */ 212a8971b87SHong Zhang ierr = PetscLogObjectMemory((PetscObject)c,3*nis*sizeof(PetscInt));CHKERRQ(ierr); 213a8971b87SHong Zhang 2140df34763SHong Zhang if (c->htype[0] == 'd') { 215785e854fSJed Brown ierr = PetscMalloc1(nz,&Jentry);CHKERRQ(ierr); 216a8971b87SHong Zhang ierr = PetscLogObjectMemory((PetscObject)c,nz*sizeof(MatEntry));CHKERRQ(ierr); 217a8971b87SHong Zhang c->matentry = Jentry; 2180df34763SHong Zhang } else if (c->htype[0] == 'w') { 219785e854fSJed Brown ierr = PetscMalloc1(nz,&Jentry2);CHKERRQ(ierr); 2200df34763SHong Zhang ierr = PetscLogObjectMemory((PetscObject)c,nz*sizeof(MatEntry2));CHKERRQ(ierr); 2210df34763SHong Zhang c->matentry2 = Jentry2; 2220df34763SHong Zhang } else SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"htype is not supported"); 223a8971b87SHong Zhang 224a8971b87SHong Zhang if (isBAIJ) { 225a8971b87SHong Zhang ierr = MatGetColumnIJ_SeqBAIJ_Color(mat,0,PETSC_FALSE,PETSC_FALSE,&ncols,&ci,&cj,&spidx,NULL);CHKERRQ(ierr); 226d4002b98SHong Zhang } else if (isSELL) { 227d4002b98SHong Zhang ierr = MatGetColumnIJ_SeqSELL_Color(mat,0,PETSC_FALSE,PETSC_FALSE,&ncols,&ci,&cj,&spidx,NULL);CHKERRQ(ierr); 228a8971b87SHong Zhang } else { 229a8971b87SHong Zhang ierr = MatGetColumnIJ_SeqAIJ_Color(mat,0,PETSC_FALSE,PETSC_FALSE,&ncols,&ci,&cj,&spidx,NULL);CHKERRQ(ierr); 230a8971b87SHong Zhang } 231a8971b87SHong Zhang 232857a15f1SBarry Smith ierr = PetscCalloc1(c->m,&rowhit);CHKERRQ(ierr); 233857a15f1SBarry Smith ierr = PetscMalloc1(c->m,&valaddrhit);CHKERRQ(ierr); 234a8971b87SHong Zhang 235a8971b87SHong Zhang nz = 0; 236a8971b87SHong Zhang for (i=0; i<nis; i++) { /* loop over colors */ 237071fcb05SBarry Smith ierr = ISGetLocalSize(c->isa[i],&n);CHKERRQ(ierr); 238071fcb05SBarry Smith ierr = ISGetIndices(c->isa[i],&is);CHKERRQ(ierr); 239a8971b87SHong Zhang 240a8971b87SHong Zhang c->ncolumns[i] = n; 241071fcb05SBarry Smith c->columns[i] = (PetscInt*)is; 242071fcb05SBarry Smith /* note: we know that c->isa is going to be around as long at the c->columns values */ 243071fcb05SBarry Smith ierr = ISRestoreIndices(c->isa[i],&is);CHKERRQ(ierr); 244a8971b87SHong Zhang 245a8971b87SHong Zhang /* fast, crude version requires O(N*N) work */ 246a8971b87SHong Zhang bs2 = bs*bs; 247a8971b87SHong Zhang nrows = 0; 248a8971b87SHong Zhang for (j=0; j<n; j++) { /* loop over columns */ 249a8971b87SHong Zhang col = is[j]; 250071fcb05SBarry Smith tmp = ci[col]; 251071fcb05SBarry Smith row = cj + tmp; 252071fcb05SBarry Smith m = ci[col+1] - tmp; 253a8971b87SHong Zhang nrows += m; 254a8971b87SHong Zhang for (k=0; k<m; k++) { /* loop over columns marking them in rowhit */ 255a8971b87SHong Zhang rowhit[*row] = col + 1; 256071fcb05SBarry Smith valaddrhit[*row++] = (PetscScalar*)&A_val[bs2*spidx[tmp + k]]; 257a8971b87SHong Zhang } 258a8971b87SHong Zhang } 259a8971b87SHong Zhang c->nrows[i] = nrows; /* total num of rows for this color */ 260a8971b87SHong Zhang 2610df34763SHong Zhang if (c->htype[0] == 'd') { 262a8971b87SHong Zhang for (j=0; j<mbs; j++) { /* loop over rows */ 263a8971b87SHong Zhang if (rowhit[j]) { 264a8971b87SHong Zhang Jentry[nz].row = j; /* local row index */ 265a8971b87SHong Zhang Jentry[nz].col = rowhit[j] - 1; /* local column index */ 266a8971b87SHong Zhang Jentry[nz].valaddr = valaddrhit[j]; /* address of mat value for this entry */ 267a8971b87SHong Zhang nz++; 268a8971b87SHong Zhang rowhit[j] = 0.0; /* zero rowhit for reuse */ 269a8971b87SHong Zhang } 270a8971b87SHong Zhang } 2710df34763SHong Zhang } else { /* c->htype == 'wp' */ 2720df34763SHong Zhang for (j=0; j<mbs; j++) { /* loop over rows */ 2730df34763SHong Zhang if (rowhit[j]) { 2740df34763SHong Zhang Jentry2[nz].row = j; /* local row index */ 2750df34763SHong Zhang Jentry2[nz].valaddr = valaddrhit[j]; /* address of mat value for this entry */ 2760df34763SHong Zhang nz++; 2770df34763SHong Zhang rowhit[j] = 0.0; /* zero rowhit for reuse */ 2780df34763SHong Zhang } 2790df34763SHong Zhang } 2800df34763SHong Zhang } 281a8971b87SHong Zhang } 282a8971b87SHong Zhang 283a8971b87SHong Zhang if (c->bcols > 1) { /* reorder Jentry for faster MatFDColoringApply() */ 284a8971b87SHong Zhang ierr = MatFDColoringSetUpBlocked_AIJ_Private(mat,c,nz);CHKERRQ(ierr); 285a8971b87SHong Zhang } 28685740eacSHong Zhang 287525d23c0SHong Zhang if (isBAIJ) { 288525d23c0SHong Zhang ierr = MatRestoreColumnIJ_SeqBAIJ_Color(mat,0,PETSC_FALSE,PETSC_FALSE,&ncols,&ci,&cj,&spidx,NULL);CHKERRQ(ierr); 289785e854fSJed Brown ierr = PetscMalloc1(bs*mat->rmap->n,&c->dy);CHKERRQ(ierr); 290b3e1f37bSHong Zhang ierr = PetscLogObjectMemory((PetscObject)c,bs*mat->rmap->n*sizeof(PetscScalar));CHKERRQ(ierr); 291d4002b98SHong Zhang } else if (isSELL) { 292d4002b98SHong Zhang ierr = MatRestoreColumnIJ_SeqSELL_Color(mat,0,PETSC_FALSE,PETSC_FALSE,&ncols,&ci,&cj,&spidx,NULL);CHKERRQ(ierr); 293525d23c0SHong Zhang } else { 2946378f32dSHong Zhang ierr = MatRestoreColumnIJ_SeqAIJ_Color(mat,0,PETSC_FALSE,PETSC_FALSE,&ncols,&ci,&cj,&spidx,NULL);CHKERRQ(ierr); 295525d23c0SHong Zhang } 296857a15f1SBarry Smith ierr = PetscFree(rowhit);CHKERRQ(ierr); 297857a15f1SBarry Smith ierr = PetscFree(valaddrhit);CHKERRQ(ierr); 298071fcb05SBarry Smith ierr = ISColoringRestoreIS(iscoloring,PETSC_OWN_POINTER,&c->isa);CHKERRQ(ierr); 299476e0d0aSHong Zhang 30052011a10SHong Zhang ierr = VecCreateGhost(PetscObjectComm((PetscObject)mat),mat->rmap->n,PETSC_DETERMINE,0,NULL,&c->vscale);CHKERRQ(ierr); 301b3e1f37bSHong Zhang ierr = PetscInfo3(c,"ncolors %D, brows %D and bcols %D are used.\n",c->ncolors,c->brows,c->bcols);CHKERRQ(ierr); 30279c1e64dSHong Zhang PetscFunctionReturn(0); 30379c1e64dSHong Zhang } 304