Actual source code: sbaijfact4.c
petsc-3.7.1 2016-05-15
Report Typos and Errors

      2: #include <../src/mat/impls/sbaij/seq/sbaij.h>
      3: #include <petsc/private/kernels/blockinvert.h>

      5: /*
      6:       Version for when blocks are 3 by 3 Using natural ordering
      7: */
     10: PetscErrorCode MatCholeskyFactorNumeric_SeqSBAIJ_3_NaturalOrdering(Mat C,Mat A,const MatFactorInfo *info)
     11: {
     12:   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data,*b = (Mat_SeqSBAIJ*)C->data;
     14:   PetscInt       i,j,mbs=a->mbs,*bi=b->i,*bj=b->j;
     15:   PetscInt       *ai,*aj,k,k1,jmin,jmax,*jl,*il,vj,nexti,ili;
     16:   MatScalar      *ba = b->a,*aa,*ap,*dk,*uik;
     17:   MatScalar      *u,*diag,*rtmp,*rtmp_ptr;
     18:   PetscReal      shift = info->shiftamount;
     19:   PetscBool      allowzeropivot,zeropivotdetected;

     22:   /* initialization */
     23:   allowzeropivot = PetscNot(A->erroriffailure);
     24:   PetscCalloc1(9*mbs,&rtmp);
     25:   PetscMalloc2(mbs,&il,mbs,&jl);
     26:   for (i=0; i<mbs; i++) {
     27:     jl[i] = mbs; il[0] = 0;
     28:   }
     29:   PetscMalloc2(9,&dk,9,&uik);
     30:   ai   = a->i; aj = a->j; aa = a->a;

     32:   /* for each row k */
     33:   for (k = 0; k<mbs; k++) {

     35:     /*initialize k-th row with elements nonzero in row k of A */
     36:     jmin = ai[k]; jmax = ai[k+1];
     37:     if (jmin < jmax) {
     38:       ap = aa + jmin*9;
     39:       for (j = jmin; j < jmax; j++) {
     40:         vj       = aj[j];   /* block col. index */
     41:         rtmp_ptr = rtmp + vj*9;
     42:         for (i=0; i<9; i++) *rtmp_ptr++ = *ap++;
     43:       }
     44:     }

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

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

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

     56:       /* uik = -inv(Di)*U_bar(i,k) */
     57:       diag = ba + i*9;
     58:       u    = ba + ili*9;

     60:       uik[0] = -(diag[0]*u[0] + diag[3]*u[1] + diag[6]*u[2]);
     61:       uik[1] = -(diag[1]*u[0] + diag[4]*u[1] + diag[7]*u[2]);
     62:       uik[2] = -(diag[2]*u[0] + diag[5]*u[1] + diag[8]*u[2]);

     64:       uik[3] = -(diag[0]*u[3] + diag[3]*u[4] + diag[6]*u[5]);
     65:       uik[4] = -(diag[1]*u[3] + diag[4]*u[4] + diag[7]*u[5]);
     66:       uik[5] = -(diag[2]*u[3] + diag[5]*u[4] + diag[8]*u[5]);

     68:       uik[6] = -(diag[0]*u[6] + diag[3]*u[7] + diag[6]*u[8]);
     69:       uik[7] = -(diag[1]*u[6] + diag[4]*u[7] + diag[7]*u[8]);
     70:       uik[8] = -(diag[2]*u[6] + diag[5]*u[7] + diag[8]*u[8]);

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

     77:       dk[3] += uik[0]*u[3] + uik[1]*u[4] + uik[2]*u[5];
     78:       dk[4] += uik[3]*u[3] + uik[4]*u[4] + uik[5]*u[5];
     79:       dk[5] += uik[6]*u[3] + uik[7]*u[4] + uik[8]*u[5];

     81:       dk[6] += uik[0]*u[6] + uik[1]*u[7] + uik[2]*u[8];
     82:       dk[7] += uik[3]*u[6] + uik[4]*u[7] + uik[5]*u[8];
     83:       dk[8] += uik[6]*u[6] + uik[7]*u[7] + uik[8]*u[8];

     85:       PetscLogFlops(27.0*4.0);

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

     90:       /* add multiple of row i to k-th row ... */
     91:       jmin = ili + 1; jmax = bi[i+1];
     92:       if (jmin < jmax) {
     93:         for (j=jmin; j<jmax; j++) {
     94:           /* rtmp += -U(i,k)^T * U_bar(i,j) */
     95:           rtmp_ptr     = rtmp + bj[j]*9;
     96:           u            = ba + j*9;
     97:           rtmp_ptr[0] += uik[0]*u[0] + uik[1]*u[1] + uik[2]*u[2];
     98:           rtmp_ptr[1] += uik[3]*u[0] + uik[4]*u[1] + uik[5]*u[2];
     99:           rtmp_ptr[2] += uik[6]*u[0] + uik[7]*u[1] + uik[8]*u[2];

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

    105:           rtmp_ptr[6] += uik[0]*u[6] + uik[1]*u[7] + uik[2]*u[8];
    106:           rtmp_ptr[7] += uik[3]*u[6] + uik[4]*u[7] + uik[5]*u[8];
    107:           rtmp_ptr[8] += uik[6]*u[6] + uik[7]*u[7] + uik[8]*u[8];
    108:         }
    109:         PetscLogFlops(2.0*27.0*(jmax-jmin));

    111:         /* ... add i to row list for next nonzero entry */
    112:         il[i] = jmin;             /* update il(i) in column k+1, ... mbs-1 */
    113:         j     = bj[jmin];
    114:         jl[i] = jl[j]; jl[j] = i; /* update jl */
    115:       }
    116:       i = nexti;
    117:     }

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

    121:     /* invert diagonal block */
    122:     diag = ba+k*9;
    123:     PetscMemcpy(diag,dk,9*sizeof(MatScalar));
    124:     PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);
    125:     if (zeropivotdetected) C->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;

    127:     jmin = bi[k]; jmax = bi[k+1];
    128:     if (jmin < jmax) {
    129:       for (j=jmin; j<jmax; j++) {
    130:         vj       = bj[j];      /* block col. index of U */
    131:         u        = ba + j*9;
    132:         rtmp_ptr = rtmp + vj*9;
    133:         for (k1=0; k1<9; k1++) {
    134:           *u++        = *rtmp_ptr;
    135:           *rtmp_ptr++ = 0.0;
    136:         }
    137:       }

    139:       /* ... add k to row list for first nonzero entry in k-th row */
    140:       il[k] = jmin;
    141:       i     = bj[jmin];
    142:       jl[k] = jl[i]; jl[i] = k;
    143:     }
    144:   }

    146:   PetscFree(rtmp);
    147:   PetscFree2(il,jl);
    148:   PetscFree2(dk,uik);

    150:   C->ops->solve          = MatSolve_SeqSBAIJ_3_NaturalOrdering_inplace;
    151:   C->ops->solvetranspose = MatSolve_SeqSBAIJ_3_NaturalOrdering_inplace;
    152:   C->ops->forwardsolve   = MatForwardSolve_SeqSBAIJ_3_NaturalOrdering_inplace;
    153:   C->ops->backwardsolve  = MatBackwardSolve_SeqSBAIJ_3_NaturalOrdering_inplace;

    155:   C->assembled    = PETSC_TRUE;
    156:   C->preallocated = PETSC_TRUE;

    158:   PetscLogFlops(1.3333*27*b->mbs); /* from inverting diagonal blocks */
    159:   return(0);
    160: }