Actual source code: matreg.c

petsc-3.7.7 2017-09-25
Report Typos and Errors
  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);
 68:     mat->ops->destroy = NULL;

 70:     /* should these null spaces be removed? */
 71:     MatNullSpaceDestroy(&mat->nullsp);
 72:     MatNullSpaceDestroy(&mat->nearnullsp);
 73:     mat->preallocated = PETSC_FALSE;
 74:     mat->assembled = PETSC_FALSE;
 75:     mat->was_assembled = PETSC_FALSE;

 77:     /*
 78:      Increment, rather than reset these: the object is logically the same, so its logging and
 79:      state is inherited.  Furthermore, resetting makes it possible for the same state to be
 80:      obtained with a different structure, confusing the PC.
 81:     */
 82:     ++mat->nonzerostate;
 83:     PetscObjectStateIncrease((PetscObject)mat);
 84:   }
 85:   mat->preallocated  = PETSC_FALSE;
 86:   mat->assembled     = PETSC_FALSE;
 87:   mat->was_assembled = PETSC_FALSE;

 89:   /* increase the state so that any code holding the current state knows the matrix has been changed */
 90:   mat->nonzerostate++;
 91:   PetscObjectStateIncrease((PetscObject)mat);

 93:   /* create the new data structure */
 94:   (*r)(mat);
 95:   return(0);
 96: }

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

103:    Not Collective

105:    Input Parameter:
106: .  mat - the matrix

108:    Output Parameter:
109: .  name - name of matrix type

111:    Level: intermediate

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

115: .seealso: MatSetType()
116: @*/
117: PetscErrorCode  MatGetType(Mat mat,MatType *type)
118: {
122:   *type = ((PetscObject)mat)->type_name;
123:   return(0);
124: }


129: /*@C
130:   MatRegister -  - Adds a new matrix type

132:    Not Collective

134:    Input Parameters:
135: +  name - name of a new user-defined matrix type
136: -  routine_create - routine to create method context

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

141:    Sample usage:
142: .vb
143:    MatRegister("my_mat",MyMatCreate);
144: .ve

146:    Then, your solver can be chosen with the procedural interface via
147: $     MatSetType(Mat,"my_mat")
148:    or at runtime via the option
149: $     -mat_type my_mat

151:    Level: advanced

153: .keywords: Mat, register

155: .seealso: MatRegisterAll()


158:   Level: advanced
159: @*/
160: PetscErrorCode  MatRegister(const char sname[],PetscErrorCode (*function)(Mat))
161: {

165:   PetscFunctionListAdd(&MatList,sname,function);
166:   return(0);
167: }

169: MatBaseName MatBaseNameList = 0;

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

176:   Input Parameters:
177: +     bname - the basename, for example, MATAIJ
178: .     sname - the name of the sequential matrix type, for example, MATSEQAIJ
179: -     mname - the name of the parallel matrix type, for example, MATMPIAIJ


182:   Level: advanced
183: @*/
184: PetscErrorCode  MatRegisterBaseName(const char bname[],const char sname[],const char mname[])
185: {
187:   MatBaseName    names;

190:   PetscNew(&names);
191:   PetscStrallocpy(bname,&names->bname);
192:   PetscStrallocpy(sname,&names->sname);
193:   PetscStrallocpy(mname,&names->mname);
194:   if (!MatBaseNameList) {
195:     MatBaseNameList = names;
196:   } else {
197:     MatBaseName next = MatBaseNameList;
198:     while (next->next) next = next->next;
199:     next->next = names;
200:   }
201:   return(0);
202: }