170c3da92SBarry Smith 270c3da92SBarry Smith #include "src/mat/impls/aij/seq/aij.h" 3639f9d9dSBarry Smith 4*38baddfdSBarry Smith EXTERN PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat,PetscInt,PetscTruth,PetscInt*,PetscInt*[],PetscInt*[],PetscTruth*); 5*38baddfdSBarry Smith EXTERN PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat,PetscInt,PetscTruth,PetscInt*,PetscInt*[],PetscInt*[],PetscTruth*); 643a90d84SBarry Smith 74a2ae208SSatish Balay #undef __FUNCT__ 84a2ae208SSatish Balay #define __FUNCT__ "MatFDColoringCreate_SeqAIJ" 9dfbe8321SBarry Smith PetscErrorCode MatFDColoringCreate_SeqAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c) 10639f9d9dSBarry Smith { 116849ba73SBarry Smith PetscErrorCode ierr; 12*38baddfdSBarry Smith PetscInt i,*is,n,nrows,N = mat->N,j,k,m,*rows,*ci,*cj,ncols,col; 13*38baddfdSBarry Smith PetscInt nis = iscoloring->n,*rowhit,*columnsforrow,l; 14b9617806SBarry Smith IS *isa; 15f1af5d2fSBarry Smith PetscTruth done,flg; 16639f9d9dSBarry Smith 173a40ed3dSBarry Smith PetscFunctionBegin; 18522c5e43SBarry Smith if (!mat->assembled) { 1929bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Matrix must be assembled by calls to MatAssemblyBegin/End();"); 20522c5e43SBarry Smith } 21522c5e43SBarry Smith 22b9617806SBarry Smith ierr = ISColoringGetIS(iscoloring,PETSC_IGNORE,&isa);CHKERRQ(ierr); 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*38baddfdSBarry Smith ierr = PetscMalloc(nis*sizeof(PetscInt),&c->ncolumns);CHKERRQ(ierr); 30*38baddfdSBarry Smith ierr = PetscMalloc(nis*sizeof(PetscInt*),&c->columns);CHKERRQ(ierr); 31*38baddfdSBarry Smith ierr = PetscMalloc(nis*sizeof(PetscInt),&c->nrows);CHKERRQ(ierr); 32*38baddfdSBarry Smith ierr = PetscMalloc(nis*sizeof(PetscInt*),&c->rows);CHKERRQ(ierr); 33*38baddfdSBarry Smith ierr = PetscMalloc(nis*sizeof(PetscInt*),&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 */ 44b0a32e0cSBarry Smith ierr = PetscOptionsHasName(PETSC_NULL,"-matfdcoloring_slow",&flg);CHKERRQ(ierr); 4570c3da92SBarry Smith 46*38baddfdSBarry Smith ierr = PetscMalloc((N+1)*sizeof(PetscInt),&rowhit);CHKERRQ(ierr); 47*38baddfdSBarry Smith ierr = PetscMalloc((N+1)*sizeof(PetscInt),&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*38baddfdSBarry Smith ierr = PetscMalloc(n*sizeof(PetscInt),&c->columns[i]);CHKERRQ(ierr); 55*38baddfdSBarry Smith ierr = PetscMemcpy(c->columns[i],is,n*sizeof(PetscInt));CHKERRQ(ierr); 5670c3da92SBarry Smith } else { 57639f9d9dSBarry Smith c->columns[i] = 0; 5870c3da92SBarry Smith } 5970c3da92SBarry Smith 603a7fca6bSBarry Smith if (!flg) { /* ------------------------------------------------------------------------------*/ 613a7fca6bSBarry Smith /* fast, crude version requires O(N*N) work */ 62*38baddfdSBarry Smith ierr = PetscMemzero(rowhit,N*sizeof(PetscInt));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*38baddfdSBarry Smith ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->rows[i]);CHKERRQ(ierr); 80*38baddfdSBarry Smith ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&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 { /*-------------------------------------------------------------------------------*/ 903a7fca6bSBarry Smith /* slow version, using rowhit as a linked list */ 91*38baddfdSBarry Smith PetscInt 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*38baddfdSBarry Smith ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->rows[i]);CHKERRQ(ierr); 125*38baddfdSBarry Smith ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&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*38baddfdSBarry Smith ierr = PetscMalloc(c->ncolors*sizeof(PetscInt*),&c->vscaleforrow);CHKERRQ(ierr); 14830b34957SBarry Smith for (k=0; k<c->ncolors; k++) { 149*38baddfdSBarry Smith ierr = PetscMalloc((c->nrows[k]+1)*sizeof(PetscInt),&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 } 155b9617806SBarry Smith ierr = ISColoringRestoreIS(iscoloring,&isa);CHKERRQ(ierr); 1563a40ed3dSBarry Smith PetscFunctionReturn(0); 15770c3da92SBarry Smith } 15870c3da92SBarry Smith 159639f9d9dSBarry Smith /* 160639f9d9dSBarry Smith Makes a longer coloring[] array and calls the usual code with that 161639f9d9dSBarry Smith */ 1624a2ae208SSatish Balay #undef __FUNCT__ 1634a2ae208SSatish Balay #define __FUNCT__ "MatColoringPatch_SeqAIJ_Inode" 164*38baddfdSBarry Smith PetscErrorCode MatColoringPatch_SeqAIJ_Inode(Mat mat,PetscInt nin,PetscInt ncolors,ISColoringValue coloring[],ISColoring *iscoloring) 16570c3da92SBarry Smith { 166639f9d9dSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)mat->data; 1676849ba73SBarry Smith PetscErrorCode ierr; 168*38baddfdSBarry Smith PetscInt n = mat->n,m = a->inode.node_count,j,*ns = a->inode.size,row; 169*38baddfdSBarry Smith PetscInt *colorused,i; 17008b6dcc0SBarry Smith ISColoringValue *newcolor; 17170c3da92SBarry Smith 1723a40ed3dSBarry Smith PetscFunctionBegin; 173*38baddfdSBarry Smith ierr = PetscMalloc((n+1)*sizeof(PetscInt),&newcolor);CHKERRQ(ierr); 174639f9d9dSBarry Smith /* loop over inodes, marking a color for each column*/ 175639f9d9dSBarry Smith row = 0; 17670c3da92SBarry Smith for (i=0; i<m; i++){ 177639f9d9dSBarry Smith for (j=0; j<ns[i]; j++) { 178639f9d9dSBarry Smith newcolor[row++] = coloring[i] + j*ncolors; 17970c3da92SBarry Smith } 18070c3da92SBarry Smith } 18170c3da92SBarry Smith 182639f9d9dSBarry Smith /* eliminate unneeded colors */ 183*38baddfdSBarry Smith ierr = PetscMalloc(5*ncolors*sizeof(PetscInt),&colorused);CHKERRQ(ierr); 184*38baddfdSBarry Smith ierr = PetscMemzero(colorused,5*ncolors*sizeof(PetscInt));CHKERRQ(ierr); 185639f9d9dSBarry Smith for (i=0; i<n; i++) { 186b9617806SBarry Smith colorused[newcolor[i]] = 1; 187639f9d9dSBarry Smith } 18870c3da92SBarry Smith 189639f9d9dSBarry Smith for (i=1; i<5*ncolors; i++) { 190639f9d9dSBarry Smith colorused[i] += colorused[i-1]; 19170c3da92SBarry Smith } 192639f9d9dSBarry Smith ncolors = colorused[5*ncolors-1]; 193639f9d9dSBarry Smith for (i=0; i<n; i++) { 194b9617806SBarry Smith newcolor[i] = colorused[newcolor[i]]; 19570c3da92SBarry Smith } 196606d414cSSatish Balay ierr = PetscFree(colorused);CHKERRQ(ierr); 197b9617806SBarry Smith ierr = ISColoringCreate(mat->comm,n,newcolor,iscoloring);CHKERRQ(ierr); 19897f1f81fSBarry Smith ierr = PetscFree(coloring);CHKERRQ(ierr); 1993a40ed3dSBarry Smith PetscFunctionReturn(0); 20070c3da92SBarry Smith } 20170c3da92SBarry Smith 202