Actual source code: matreg.c

      1: #define PETSCMAT_DLL

      3: /*
      4:      Mechanism for register PETSc matrix types
      5: */
     6:  #include src/mat/matimpl.h
     7:  #include petscsys.h

      9: PetscTruth MatRegisterAllCalled = PETSC_FALSE;

     11: /*
     12:    Contains the list of registered Mat routines
     13: */
     14: PetscFList MatList = 0;

     18: /*@C
     19:    MatSetType - Builds matrix object for a particular matrix type

     21:    Collective on Mat

     23:    Input Parameters:
     24: +  mat      - the matrix object
     25: -  matype   - matrix type

     27:    Options Database Key:
     28: .  -mat_type  <method> - Sets the type; use -help for a list
     29:     of available methods (for instance, seqaij)

     31:    Notes:
     32:    See "${PETSC_DIR}/include/petscmat.h" for available methods

     34:   Level: intermediate

     36: .keywords: Mat, MatType, set, method

     38: .seealso: PCSetType(), VecSetType(), MatCreate(), MatType, Mat
     39: @*/
     40: PetscErrorCode PETSCMAT_DLLEXPORT MatSetType(Mat mat, MatType matype)
     41: {
     42:   PetscErrorCode ierr,(*r)(Mat);
     43:   PetscTruth     sametype;

     47:   if (mat->m < 0 && mat->M < 0 && mat->n < 0 && mat->N < 0) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call MatSetSizes() first");
     48:   PetscTypeCompare((PetscObject)mat,matype,&sametype);
     49:   if (!sametype) {
     50:     /* Get the function pointers for the matrix requested */
     51:     if (!MatRegisterAllCalled) {MatRegisterAll(PETSC_NULL);}
     52:      PetscFListFind(mat->comm,MatList,matype,(void(**)(void))&r);
     53:     if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown Mat type given: %s",matype);

     55:     /* free the old data structure if it existed */
     56:     if (mat->ops->destroy) {
     57:       MatPreallocated(mat);
     58:       (*mat->ops->destroy)(mat);
     59:       mat->ops->destroy = PETSC_NULL;
     60:       mat->preallocated = PETSC_FALSE;
     61:     }

     63:     if (mat->rmap) {
     64:       PetscMapDestroy(mat->rmap);
     65:       mat->rmap = 0;
     66:     }
     67:     if (mat->cmap) {
     68:       PetscMapDestroy(mat->cmap);
     69:       mat->cmap = 0;
     70:     }
     71:     /* create the new data structure */
     72:     (*r)(mat);
     73:     PetscObjectChangeTypeName((PetscObject)mat,matype);
     74:   }
     75:   PetscPublishAll(mat);
     76:   return(0);
     77: }

     82: /*@C
     83:    MatRegisterDestroy - Frees the list of matrix types that were
     84:    registered by MatRegister()/MatRegisterDynamic().

     86:    Not Collective

     88:    Level: advanced

     90: .keywords: Mat, register, destroy

     92: .seealso: MatRegister(), MatRegisterAll(), MatRegisterDynamic()
     93: @*/
     94: PetscErrorCode PETSCMAT_DLLEXPORT MatRegisterDestroy(void)
     95: {

     99:   if (MatList) {
    100:     PetscFListDestroy(&MatList);
    101:     MatList = 0;
    102:   }
    103:   MatRegisterAllCalled = PETSC_FALSE;
    104:   return(0);
    105: }

    109: /*@C
    110:    MatGetType - Gets the matrix type as a string from the matrix object.

    112:    Not Collective

    114:    Input Parameter:
    115: .  mat - the matrix

    117:    Output Parameter:
    118: .  name - name of matrix type

    120:    Level: intermediate

    122: .keywords: Mat, MatType, get, method, name

    124: .seealso: MatSetType()
    125: @*/
    126: PetscErrorCode PETSCMAT_DLLEXPORT MatGetType(Mat mat,MatType *type)
    127: {
    129:   *type = mat->type_name;
    130:   return(0);
    131: }

    136: /*@C
    137:   MatRegister - See MatRegisterDynamic()

    139:   Level: advanced
    140: @*/
    141: PetscErrorCode PETSCMAT_DLLEXPORT MatRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(Mat))
    142: {
    144:   char           fullname[PETSC_MAX_PATH_LEN];

    147:   PetscFListConcat(path,name,fullname);
    148:   PetscFListAdd(&MatList,sname,fullname,(void (*)(void))function);
    149:   return(0);
    150: }