Date: 2022-08-02T03:13:51+00:00

Actual source code: baijsolvtran2.c

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

      4: PetscErrorCode MatSolveTranspose_SeqBAIJ_2_inplace(Mat A,Vec bb,Vec xx)
      5: {
      6:   Mat_SeqBAIJ       *a   =(Mat_SeqBAIJ*)A->data;
      7:   IS                iscol=a->col,isrow=a->row;
      8:   const PetscInt    *r,*c,*rout,*cout;
      9:   const PetscInt    *diag=a->diag,n=a->mbs,*vi,*ai=a->i,*aj=a->j;
     10:   PetscInt          i,nz,idx,idt,ii,ic,ir,oidx;
     11:   const MatScalar   *aa=a->a,*v;
     12:   PetscScalar       s1,s2,x1,x2,*x,*t;
     13:   const PetscScalar *b;

     15:   VecGetArrayRead(bb,&b);
     16:   VecGetArray(xx,&x);
     17:   t    = a->solve_work;

     19:   ISGetIndices(isrow,&rout); r = rout;
     20:   ISGetIndices(iscol,&cout); c = cout;

     22:   /* copy the b into temp work space according to permutation */
     23:   ii = 0;
     24:   for (i=0; i<n; i++) {
     25:     ic      = 2*c[i];
     26:     t[ii]   = b[ic];
     27:     t[ii+1] = b[ic+1];
     28:     ii     += 2;
     29:   }

     31:   /* forward solve the U^T */
     32:   idx = 0;
     33:   for (i=0; i<n; i++) {

     35:     v = aa + 4*diag[i];
     36:     /* multiply by the inverse of the block diagonal */
     37:     x1 = t[idx];   x2 = t[1+idx];
     38:     s1 = v[0]*x1  +  v[1]*x2;
     39:     s2 = v[2]*x1  +  v[3]*x2;
     40:     v += 4;

     42:     vi = aj + diag[i] + 1;
     43:     nz = ai[i+1] - diag[i] - 1;
     44:     while (nz--) {
     45:       oidx       = 2*(*vi++);
     46:       t[oidx]   -= v[0]*s1  +  v[1]*s2;
     47:       t[oidx+1] -= v[2]*s1  +  v[3]*s2;
     48:       v         += 4;
     49:     }
     50:     t[idx] = s1;t[1+idx] = s2;
     51:     idx   += 2;
     52:   }
     53:   /* backward solve the L^T */
     54:   for (i=n-1; i>=0; i--) {
     55:     v   = aa + 4*diag[i] - 4;
     56:     vi  = aj + diag[i] - 1;
     57:     nz  = diag[i] - ai[i];
     58:     idt = 2*i;
     59:     s1  = t[idt];  s2 = t[1+idt];
     60:     while (nz--) {
     61:       idx       = 2*(*vi--);
     62:       t[idx]   -=  v[0]*s1 +  v[1]*s2;
     63:       t[idx+1] -=  v[2]*s1 +  v[3]*s2;
     64:       v        -= 4;
     65:     }
     66:   }

     68:   /* copy t into x according to permutation */
     69:   ii = 0;
     70:   for (i=0; i<n; i++) {
     71:     ir      = 2*r[i];
     72:     x[ir]   = t[ii];
     73:     x[ir+1] = t[ii+1];
     74:     ii     += 2;
     75:   }

     77:   ISRestoreIndices(isrow,&rout);
     78:   ISRestoreIndices(iscol,&cout);
     79:   VecRestoreArrayRead(bb,&b);
     80:   VecRestoreArray(xx,&x);
     81:   PetscLogFlops(2.0*4*(a->nz) - 2.0*A->cmap->n);
     82:   return 0;
     83: }

     85: PetscErrorCode MatSolveTranspose_SeqBAIJ_2(Mat A,Vec bb,Vec xx)
     86: {
     87:   Mat_SeqBAIJ       *a=(Mat_SeqBAIJ*)A->data;
     88:   IS                iscol=a->col,isrow=a->row;
     89:   const PetscInt    n    =a->mbs,*vi,*ai=a->i,*aj=a->j,*diag=a->diag;
     90:   const PetscInt    *r,*c,*rout,*cout;
     91:   PetscInt          nz,idx,idt,j,i,oidx,ii,ic,ir;
     92:   const PetscInt    bs =A->rmap->bs,bs2=a->bs2;
     93:   const MatScalar   *aa=a->a,*v;
     94:   PetscScalar       s1,s2,x1,x2,*x,*t;
     95:   const PetscScalar *b;

     97:   VecGetArrayRead(bb,&b);
     98:   VecGetArray(xx,&x);
     99:   t    = a->solve_work;

    101:   ISGetIndices(isrow,&rout); r = rout;
    102:   ISGetIndices(iscol,&cout); c = cout;

    104:   /* copy b into temp work space according to permutation */
    105:   for (i=0; i<n; i++) {
    106:     ii    = bs*i; ic = bs*c[i];
    107:     t[ii] = b[ic]; t[ii+1] = b[ic+1];
    108:   }

    110:   /* forward solve the U^T */
    111:   idx = 0;
    112:   for (i=0; i<n; i++) {
    113:     v = aa + bs2*diag[i];
    114:     /* multiply by the inverse of the block diagonal */
    115:     x1 = t[idx];   x2 = t[1+idx];
    116:     s1 = v[0]*x1  +  v[1]*x2;
    117:     s2 = v[2]*x1  +  v[3]*x2;
    118:     v -= bs2;

    120:     vi = aj + diag[i] - 1;
    121:     nz = diag[i] - diag[i+1] - 1;
    122:     for (j=0; j>-nz; j--) {
    123:       oidx       = bs*vi[j];
    124:       t[oidx]   -= v[0]*s1  +  v[1]*s2;
    125:       t[oidx+1] -= v[2]*s1  +  v[3]*s2;
    126:       v         -= bs2;
    127:     }
    128:     t[idx] = s1;t[1+idx] = s2;
    129:     idx   += bs;
    130:   }
    131:   /* backward solve the L^T */
    132:   for (i=n-1; i>=0; i--) {
    133:     v   = aa + bs2*ai[i];
    134:     vi  = aj + ai[i];
    135:     nz  = ai[i+1] - ai[i];
    136:     idt = bs*i;
    137:     s1  = t[idt];  s2 = t[1+idt];
    138:     for (j=0; j<nz; j++) {
    139:       idx       = bs*vi[j];
    140:       t[idx]   -=  v[0]*s1 +  v[1]*s2;
    141:       t[idx+1] -=  v[2]*s1 +  v[3]*s2;
    142:       v        += bs2;
    143:     }
    144:   }

    146:   /* copy t into x according to permutation */
    147:   for (i=0; i<n; i++) {
    148:     ii    = bs*i;  ir = bs*r[i];
    149:     x[ir] = t[ii];  x[ir+1] = t[ii+1];
    150:   }

    152:   ISRestoreIndices(isrow,&rout);
    153:   ISRestoreIndices(iscol,&cout);
    154:   VecRestoreArrayRead(bb,&b);
    155:   VecRestoreArray(xx,&x);
    156:   PetscLogFlops(2.0*bs2*(a->nz) - bs*A->cmap->n);
    157:   return 0;
    158: }