Actual source code: matreg.c

petsc-3.4.5 2014-06-29
  2: /*
  3:      Mechanism for register PETSc matrix types
  4: */
  5: #include <petsc-private/matimpl.h>      /*I "petscmat.h" I*/

  7: PetscBool MatRegisterAllCalled = PETSC_FALSE;

  9: /*
 10:    Contains the list of registered Mat routines
 11: */
 12: PetscFunctionList MatList = 0;

 16: /*@C
 17:    MatSetType - Builds matrix object for a particular matrix type

 19:    Collective on Mat

 21:    Input Parameters:
 22: +  mat      - the matrix object
 23: -  matype   - matrix type

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

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

 32:   Level: intermediate

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

 36: .seealso: PCSetType(), VecSetType(), MatCreate(), MatType, Mat
 37: @*/
 38: PetscErrorCode  MatSetType(Mat mat, MatType matype)
 39: {
 40:   PetscErrorCode ierr,(*r)(Mat);
 41:   PetscBool      sametype,found;
 42:   MatBaseName    names = MatBaseNameList;


 47:   while (names) {
 48:     PetscStrcmp(matype,names->bname,&found);
 49:     if (found) {
 50:       PetscMPIInt size;
 51:       MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);
 52:       if (size == 1) matype = names->sname;
 53:       else matype = names->mname;
 54:       break;
 55:     }
 56:     names = names->next;
 57:   }

 59:   PetscObjectTypeCompare((PetscObject)mat,matype,&sametype);
 60:   if (sametype) return(0);

 62:    PetscFunctionListFind(MatList,matype,&r);
 63:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown Mat type given: %s",matype);

 65:   /* free the old data structure if it existed */
 66:   if (mat->ops->destroy) {
 67:     (*mat->ops->destroy)(mat);

 69:     mat->ops->destroy = NULL;
 70:   }
 71:   mat->preallocated = PETSC_FALSE;

 73:   /* create the new data structure */
 74:   (*r)(mat);
 75:   return(0);
 76: }

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

 83:    Not Collective

 85:    Input Parameter:
 86: .  mat - the matrix

 88:    Output Parameter:
 89: .  name - name of matrix type

 91:    Level: intermediate

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

 95: .seealso: MatSetType()
 96: @*/
 97: PetscErrorCode  MatGetType(Mat mat,MatType *type)
 98: {
102:   *type = ((PetscObject)mat)->type_name;
103:   return(0);
104: }


109: /*@C
110:   MatRegister -  - Adds a new matrix type

112:    Not Collective

114:    Input Parameters:
115: +  name - name of a new user-defined matrix type
116: -  routine_create - routine to create method context

118:    Notes:
119:    MatRegister() may be called multiple times to add several user-defined solvers.

121:    Sample usage:
122: .vb
123:    MatRegister("my_mat",MyMatCreate);
124: .ve

126:    Then, your solver can be chosen with the procedural interface via
127: $     MatSetType(Mat,"my_mat")
128:    or at runtime via the option
129: $     -mat_type my_mat

131:    Level: advanced

133: .keywords: Mat, register

135: .seealso: MatRegisterAll(), MatRegisterDestroy()


138:   Level: advanced
139: @*/
140: PetscErrorCode  MatRegister(const char sname[],PetscErrorCode (*function)(Mat))
141: {

145:   PetscFunctionListAdd(&MatList,sname,function);
146:   return(0);
147: }

149: MatBaseName MatBaseNameList = 0;

153: /*@C
154:       MatRegisterBaseName - Registers a name that can be used for either a sequential or its corresponding parallel matrix type.

156:   Input Parameters:
157: +     bname - the basename, for example, MATAIJ
158: .     sname - the name of the sequential matrix type, for example, MATSEQAIJ
159: -     mname - the name of the parallel matrix type, for example, MATMPIAIJ


162:   Level: advanced
163: @*/
164: PetscErrorCode  MatRegisterBaseName(const char bname[],const char sname[],const char mname[])
165: {
167:   MatBaseName    names;

170:   PetscNew(struct _p_MatBaseName,&names);
171:   PetscStrallocpy(bname,&names->bname);
172:   PetscStrallocpy(sname,&names->sname);
173:   PetscStrallocpy(mname,&names->mname);
174:   if (!MatBaseNameList) {
175:     MatBaseNameList = names;
176:   } else {
177:     MatBaseName next = MatBaseNameList;
178:     while (next->next) next = next->next;
179:     next->next = names;
180:   }
181:   return(0);
182: }