Actual source code: sbaijfact3.c

      1: #define PETSCMAT_DLL

     3:  #include src/mat/impls/sbaij/seq/sbaij.h
     4:  #include src/inline/ilu.h

      6: /* Version for when blocks are 3 by 3  */
      9: PetscErrorCode MatCholeskyFactorNumeric_SeqSBAIJ_3(Mat A,MatFactorInfo *info,Mat *B)
     10: {
     11:   Mat            C = *B;
     12:   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data,*b = (Mat_SeqSBAIJ *)C->data;
     13:   IS             perm = b->row;
     15:   PetscInt       *perm_ptr,i,j,mbs=a->mbs,*bi=b->i,*bj=b->j;
     16:   PetscInt       *ai,*aj,*a2anew,k,k1,jmin,jmax,*jl,*il,vj,nexti,ili;
     17:   MatScalar      *ba = b->a,*aa,*ap,*dk,*uik;
     18:   MatScalar      *u,*diag,*rtmp,*rtmp_ptr;

     21:
     22:   /* initialization */
     23:   PetscMalloc(9*mbs*sizeof(MatScalar),&rtmp);
     24:   PetscMemzero(rtmp,9*mbs*sizeof(MatScalar));
     25:   PetscMalloc(2*mbs*sizeof(PetscInt),&il);
     26:   jl   = il + mbs;
     27:   for (i=0; i<mbs; i++) {
     28:     jl[i] = mbs; il[0] = 0;
     29:   }
     30:   PetscMalloc(18*sizeof(MatScalar),&dk);
     31:   uik  = dk + 9;
     32:   ISGetIndices(perm,&perm_ptr);

     34:   /* check permutation */
     35:   if (!a->permute){
     36:     ai = a->i; aj = a->j; aa = a->a;
     37:   } else {
     38:     ai   = a->inew; aj = a->jnew;
     39:     PetscMalloc(9*ai[mbs]*sizeof(MatScalar),&aa);
     40:     PetscMemcpy(aa,a->a,9*ai[mbs]*sizeof(MatScalar));
     41:     PetscMalloc(ai[mbs]*sizeof(PetscInt),&a2anew);
     42:     PetscMemcpy(a2anew,a->a2anew,(ai[mbs])*sizeof(PetscInt));

     44:     for (i=0; i<mbs; i++){
     45:       jmin = ai[i]; jmax = ai[i+1];
     46:       for (j=jmin; j<jmax; j++){
     47:         while (a2anew[j] != j){
     48:           k = a2anew[j]; a2anew[j] = a2anew[k]; a2anew[k] = k;
     49:           for (k1=0; k1<9; k1++){
     50:             dk[k1]       = aa[k*9+k1];
     51:             aa[k*9+k1] = aa[j*9+k1];
     52:             aa[j*9+k1] = dk[k1];
     53:           }
     54:         }
     55:         /* transform columnoriented blocks that lie in the lower triangle to roworiented blocks */
     56:         if (i > aj[j]){
     57:           /* printf("change orientation, row: %d, col: %d\n",i,aj[j]); */
     58:           ap = aa + j*9;                     /* ptr to the beginning of j-th block of aa */
     59:           for (k=0; k<9; k++) dk[k] = ap[k]; /* dk <- j-th block of aa */
     60:           for (k=0; k<3; k++){               /* j-th block of aa <- dk^T */
     61:             for (k1=0; k1<3; k1++) *ap++ = dk[k + 3*k1];
     62:           }
     63:         }
     64:       }
     65:     }
     66:     PetscFree(a2anew);
     67:   }
     68:
     69:   /* for each row k */
     70:   for (k = 0; k<mbs; k++){

     72:     /*initialize k-th row with elements nonzero in row perm(k) of A */
     73:     jmin = ai[perm_ptr[k]]; jmax = ai[perm_ptr[k]+1];
     74:     if (jmin < jmax) {
     75:       ap = aa + jmin*9;
     76:       for (j = jmin; j < jmax; j++){
     77:         vj = perm_ptr[aj[j]];         /* block col. index */
     78:         rtmp_ptr = rtmp + vj*9;
     79:         for (i=0; i<9; i++) *rtmp_ptr++ = *ap++;
     80:       }
     81:     }

     83:     /* modify k-th row by adding in those rows i with U(i,k) != 0 */
     84:     PetscMemcpy(dk,rtmp+k*9,9*sizeof(MatScalar));
     85:     i = jl[k]; /* first row to be added to k_th row  */

     87:     while (i < mbs){
     88:       nexti = jl[i]; /* next row to be added to k_th row */

     90:       /* compute multiplier */
     91:       ili = il[i];  /* index of first nonzero element in U(i,k:bms-1) */

     93:       /* uik = -inv(Di)*U_bar(i,k) */
     94:       diag = ba + i*9;
     95:       u    = ba + ili*9;

     97:       uik[0] = -(diag[0]*u[0] + diag[3]*u[1] + diag[6]*u[2]);
     98:       uik[1] = -(diag[1]*u[0] + diag[4]*u[1] + diag[7]*u[2]);
     99:       uik[2] = -(diag[2]*u[0] + diag[5]*u[1] + diag[8]*u[2]);

    101:       uik[3] = -(diag[0]*u[3] + diag[3]*u[4] + diag[6]*u[5]);
    102:       uik[4] = -(diag[1]*u[3] + diag[4]*u[4] + diag[7]*u[5]);
    103:       uik[5] = -(diag[2]*u[3] + diag[5]*u[4] + diag[8]*u[5]);

    105:       uik[6] = -(diag[0]*u[6] + diag[3]*u[7] + diag[6]*u[8]);
    106:       uik[7] = -(diag[1]*u[6] + diag[4]*u[7] + diag[7]*u[8]);
    107:       uik[8] = -(diag[2]*u[6] + diag[5]*u[7] + diag[8]*u[8]);

    109:       /* update D(k) += -U(i,k)^T * U_bar(i,k) */
    110:       dk[0] += uik[0]*u[0] + uik[1]*u[1] + uik[2]*u[2];
    111:       dk[1] += uik[3]*u[0] + uik[4]*u[1] + uik[5]*u[2];
    112:       dk[2] += uik[6]*u[0] + uik[7]*u[1] + uik[8]*u[2];

    114:       dk[3] += uik[0]*u[3] + uik[1]*u[4] + uik[2]*u[5];
    115:       dk[4] += uik[3]*u[3] + uik[4]*u[4] + uik[5]*u[5];
    116:       dk[5] += uik[6]*u[3] + uik[7]*u[4] + uik[8]*u[5];

    118:       dk[6] += uik[0]*u[6] + uik[1]*u[7] + uik[2]*u[8];
    119:       dk[7] += uik[3]*u[6] + uik[4]*u[7] + uik[5]*u[8];
    120:       dk[8] += uik[6]*u[6] + uik[7]*u[7] + uik[8]*u[8];

    122:       /* update -U(i,k) */
    123:       PetscMemcpy(ba+ili*9,uik,9*sizeof(MatScalar));

    125:       /* add multiple of row i to k-th row ... */
    126:       jmin = ili + 1; jmax = bi[i+1];
    127:       if (jmin < jmax){
    128:         for (j=jmin; j<jmax; j++) {
    129:           /* rtmp += -U(i,k)^T * U_bar(i,j) */
    130:           rtmp_ptr = rtmp + bj[j]*9;
    131:           u = ba + j*9;
    132:           rtmp_ptr[0] += uik[0]*u[0] + uik[1]*u[1] + uik[2]*u[2];
    133:           rtmp_ptr[1] += uik[3]*u[0] + uik[4]*u[1] + uik[5]*u[2];
    134:           rtmp_ptr[2] += uik[6]*u[0] + uik[7]*u[1] + uik[8]*u[2];

    136:           rtmp_ptr[3] += uik[0]*u[3] + uik[1]*u[4] + uik[2]*u[5];
    137:           rtmp_ptr[4] += uik[3]*u[3] + uik[4]*u[4] + uik[5]*u[5];
    138:           rtmp_ptr[5] += uik[6]*u[3] + uik[7]*u[4] + uik[8]*u[5];

    140:           rtmp_ptr[6] += uik[0]*u[6] + uik[1]*u[7] + uik[2]*u[8];
    141:           rtmp_ptr[7] += uik[3]*u[6] + uik[4]*u[7] + uik[5]*u[8];
    142:           rtmp_ptr[8] += uik[6]*u[6] + uik[7]*u[7] + uik[8]*u[8];
    143:         }
    144:
    145:         /* ... add i to row list for next nonzero entry */
    146:         il[i] = jmin;             /* update il(i) in column k+1, ... mbs-1 */
    147:         j     = bj[jmin];
    148:         jl[i] = jl[j]; jl[j] = i; /* update jl */
    149:       }
    150:       i = nexti;
    151:     }

    153:     /* save nonzero entries in k-th row of U ... */

    155:     /* invert diagonal block */
    156:     diag = ba+k*9;
    157:     PetscMemcpy(diag,dk,9*sizeof(MatScalar));
    158:     Kernel_A_gets_inverse_A_3(diag);
    159:
    160:     jmin = bi[k]; jmax = bi[k+1];
    161:     if (jmin < jmax) {
    162:       for (j=jmin; j<jmax; j++){
    163:          vj = bj[j];           /* block col. index of U */
    164:          u   = ba + j*9;
    165:          rtmp_ptr = rtmp + vj*9;
    166:          for (k1=0; k1<9; k1++){
    167:            *u++        = *rtmp_ptr;
    168:            *rtmp_ptr++ = 0.0;
    169:          }
    170:       }
    171:
    172:       /* ... add k to row list for first nonzero entry in k-th row */
    173:       il[k] = jmin;
    174:       i     = bj[jmin];
    175:       jl[k] = jl[i]; jl[i] = k;
    176:     }
    177:   }

    179:   PetscFree(rtmp);
    180:   PetscFree(il);
    181:   PetscFree(dk);
    182:   if (a->permute){
    183:     PetscFree(aa);
    184:   }

    186:   ISRestoreIndices(perm,&perm_ptr);
    187:   C->factor    = FACTOR_CHOLESKY;
    188:   C->assembled = PETSC_TRUE;
    189:   C->preallocated = PETSC_TRUE;
    190:   PetscLogFlops(1.3333*27*b->mbs); /* from inverting diagonal blocks */
    191:   return(0);
    192: }