Date: 2022-03-04T17:37:44+00:00

Actual source code: crl.c

      2: /*
      3:   Defines a matrix-vector product for the MATSEQAIJCRL matrix class.
      4:   This class is derived from the MATSEQAIJ class and retains the
      5:   compressed row storage (aka Yale sparse matrix format) but augments
      6:   it with a column oriented storage that is more efficient for
      7:   matrix vector products on Vector machines.

      9:   CRL stands for constant row length (that is the same number of columns
     10:   is kept (padded with zeros) for each row of the sparse matrix.
     11: */
     12: #include <../src/mat/impls/aij/seq/crl/crl.h>

     14: PetscErrorCode MatDestroy_SeqAIJCRL(Mat A)
     15: {
     17:   Mat_AIJCRL     *aijcrl = (Mat_AIJCRL*) A->spptr;

     20:   /* Free everything in the Mat_AIJCRL data structure. */
     21:   if (aijcrl) {
     22:     PetscFree2(aijcrl->acols,aijcrl->icols);
     23:   }
     24:   PetscFree(A->spptr);
     25:   PetscObjectChangeTypeName((PetscObject)A, MATSEQAIJ);
     26:   MatDestroy_SeqAIJ(A);
     27:   return(0);
     28: }

     30: PetscErrorCode MatDuplicate_AIJCRL(Mat A, MatDuplicateOption op, Mat *M)
     31: {
     32:   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot duplicate AIJCRL matrices yet");
     33: }

     35: PetscErrorCode MatSeqAIJCRL_create_aijcrl(Mat A)
     36: {
     37:   Mat_SeqAIJ     *a      = (Mat_SeqAIJ*)(A)->data;
     38:   Mat_AIJCRL     *aijcrl = (Mat_AIJCRL*) A->spptr;
     39:   PetscInt       m       = A->rmap->n; /* Number of rows in the matrix. */
     40:   PetscInt       *aj     = a->j; /* From the CSR representation; points to the beginning  of each row. */
     41:   PetscInt       i, j,rmax = a->rmax,*icols, *ilen = a->ilen;
     42:   MatScalar      *aa = a->a;
     43:   PetscScalar    *acols;

     47:   aijcrl->nz   = a->nz;
     48:   aijcrl->m    = A->rmap->n;
     49:   aijcrl->rmax = rmax;

     51:   PetscFree2(aijcrl->acols,aijcrl->icols);
     52:   PetscMalloc2(rmax*m,&aijcrl->acols,rmax*m,&aijcrl->icols);
     53:   acols = aijcrl->acols;
     54:   icols = aijcrl->icols;
     55:   for (i=0; i<m; i++) {
     56:     for (j=0; j<ilen[i]; j++) {
     57:       acols[j*m+i] = *aa++;
     58:       icols[j*m+i] = *aj++;
     59:     }
     60:     for (; j<rmax; j++) { /* empty column entries */
     61:       acols[j*m+i] = 0.0;
     62:       icols[j*m+i] = (j) ? icols[(j-1)*m+i] : 0;  /* handle case where row is EMPTY */
     63:     }
     64:   }
     65:   PetscInfo2(A,"Percentage of 0's introduced for vectorized multiply %g. Rmax= %D\n",1.0-((double)a->nz)/((double)(rmax*m)),rmax);
     66:   return(0);
     67: }

     69: PetscErrorCode MatAssemblyEnd_SeqAIJCRL(Mat A, MatAssemblyType mode)
     70: {
     72:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

     75:   a->inode.use = PETSC_FALSE;

     77:   MatAssemblyEnd_SeqAIJ(A,mode);
     78:   if (mode == MAT_FLUSH_ASSEMBLY) return(0);

     80:   /* Now calculate the permutation and grouping information. */
     81:   MatSeqAIJCRL_create_aijcrl(A);
     82:   return(0);
     83: }

     85: #include <../src/mat/impls/aij/seq/crl/ftn-kernels/fmultcrl.h>

     87: /*
     88:     Shared by both sequential and parallel versions of CRL matrix: MATMPIAIJCRL and MATSEQAIJCRL
     89:     - the scatter is used only in the parallel version

     91: */
     92: PetscErrorCode MatMult_AIJCRL(Mat A,Vec xx,Vec yy)
     93: {
     94:   Mat_AIJCRL        *aijcrl = (Mat_AIJCRL*) A->spptr;
     95:   PetscInt          m       = aijcrl->m; /* Number of rows in the matrix. */
     96:   PetscInt          rmax    = aijcrl->rmax,*icols = aijcrl->icols;
     97:   PetscScalar       *acols  = aijcrl->acols;
     98:   PetscErrorCode    ierr;
     99:   PetscScalar       *y;
    100:   const PetscScalar *x;
    101: #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTCRL)
    102:   PetscInt          i,j,ii;
    103: #endif

    105: #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
    106: #pragma disjoint(*x,*y,*aa)
    107: #endif

    110:   if (aijcrl->xscat) {
    111:     VecCopy(xx,aijcrl->xwork);
    112:     /* get remote values needed for local part of multiply */
    113:     VecScatterBegin(aijcrl->xscat,xx,aijcrl->fwork,INSERT_VALUES,SCATTER_FORWARD);
    114:     VecScatterEnd(aijcrl->xscat,xx,aijcrl->fwork,INSERT_VALUES,SCATTER_FORWARD);
    115:     xx   = aijcrl->xwork;
    116:   }

    118:   VecGetArrayRead(xx,&x);
    119:   VecGetArray(yy,&y);

    121: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTCRL)
    122:   fortranmultcrl_(&m,&rmax,x,y,icols,acols);
    123: #else

    125:   /* first column */
    126:   for (j=0; j<m; j++) y[j] = acols[j]*x[icols[j]];

    128:   /* other columns */
    129: #if defined(PETSC_HAVE_CRAY_VECTOR)
    130: #pragma _CRI preferstream
    131: #endif
    132:   for (i=1; i<rmax; i++) {
    133:     ii = i*m;
    134: #if defined(PETSC_HAVE_CRAY_VECTOR)
    135: #pragma _CRI prefervector
    136: #endif
    137:     for (j=0; j<m; j++) y[j] = y[j] + acols[ii+j]*x[icols[ii+j]];
    138:   }
    139: #if defined(PETSC_HAVE_CRAY_VECTOR)
    140: #pragma _CRI ivdep
    141: #endif

    143: #endif
    144:   PetscLogFlops(2.0*aijcrl->nz - m);
    145:   VecRestoreArrayRead(xx,&x);
    146:   VecRestoreArray(yy,&y);
    147:   return(0);
    148: }

    150: /* MatConvert_SeqAIJ_SeqAIJCRL converts a SeqAIJ matrix into a
    151:  * SeqAIJCRL matrix.  This routine is called by the MatCreate_SeqAIJCRL()
    152:  * routine, but can also be used to convert an assembled SeqAIJ matrix
    153:  * into a SeqAIJCRL one. */
    154: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat A,MatType type,MatReuse reuse,Mat *newmat)
    155: {
    157:   Mat            B = *newmat;
    158:   Mat_AIJCRL     *aijcrl;
    159:   PetscBool      sametype;

    162:   if (reuse == MAT_INITIAL_MATRIX) {
    163:     MatDuplicate(A,MAT_COPY_VALUES,&B);
    164:   }
    165:   PetscObjectTypeCompare((PetscObject)A,type,&sametype);
    166:   if (sametype) return(0);

    168:   PetscNewLog(B,&aijcrl);
    169:   B->spptr = (void*) aijcrl;

    171:   /* Set function pointers for methods that we inherit from AIJ but override. */
    172:   B->ops->duplicate   = MatDuplicate_AIJCRL;
    173:   B->ops->assemblyend = MatAssemblyEnd_SeqAIJCRL;
    174:   B->ops->destroy     = MatDestroy_SeqAIJCRL;
    175:   B->ops->mult        = MatMult_AIJCRL;

    177:   /* If A has already been assembled, compute the permutation. */
    178:   if (A->assembled) {
    179:     MatSeqAIJCRL_create_aijcrl(B);
    180:   }
    181:   PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJCRL);
    182:   *newmat = B;
    183:   return(0);
    184: }

    186: /*@C
    187:    MatCreateSeqAIJCRL - Creates a sparse matrix of type SEQAIJCRL.
    188:    This type inherits from AIJ, but stores some additional
    189:    information that is used to allow better vectorization of
    190:    the matrix-vector product. At the cost of increased storage, the AIJ formatted
    191:    matrix can be copied to a format in which pieces of the matrix are
    192:    stored in ELLPACK format, allowing the vectorized matrix multiply
    193:    routine to use stride-1 memory accesses.  As with the AIJ type, it is
    194:    important to preallocate matrix storage in order to get good assembly
    195:    performance.

    197:    Collective

    199:    Input Parameters:
    200: +  comm - MPI communicator, set to PETSC_COMM_SELF
    201: .  m - number of rows
    202: .  n - number of columns
    203: .  nz - number of nonzeros per row (same for all rows)
    204: -  nnz - array containing the number of nonzeros in the various rows
    205:          (possibly different for each row) or NULL

    207:    Output Parameter:
    208: .  A - the matrix

    210:    Notes:
    211:    If nnz is given then nz is ignored

    213:    Level: intermediate

    215: .seealso: MatCreate(), MatCreateMPIAIJPERM(), MatSetValues()
    216: @*/
    217: PetscErrorCode  MatCreateSeqAIJCRL(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
    218: {

    222:   MatCreate(comm,A);
    223:   MatSetSizes(*A,m,n,m,n);
    224:   MatSetType(*A,MATSEQAIJCRL);
    225:   MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);
    226:   return(0);
    227: }

    229: PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJCRL(Mat A)
    230: {

    234:   MatSetType(A,MATSEQAIJ);
    235:   MatConvert_SeqAIJ_SeqAIJCRL(A,MATSEQAIJCRL,MAT_INPLACE_MATRIX,&A);
    236:   return(0);
    237: }