Actual source code: matreg.c

petsc-master 2015-05-06
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);

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

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

 82:   /* create the new data structure */
 83:   (*r)(mat);
 84:   return(0);
 85: }

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

 92:    Not Collective

 94:    Input Parameter:
 95: .  mat - the matrix

 97:    Output Parameter:
 98: .  name - name of matrix type

100:    Level: intermediate

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

104: .seealso: MatSetType()
105: @*/
106: PetscErrorCode  MatGetType(Mat mat,MatType *type)
107: {
111:   *type = ((PetscObject)mat)->type_name;
112:   return(0);
113: }


118: /*@C
119:   MatRegister -  - Adds a new matrix type

121:    Not Collective

123:    Input Parameters:
124: +  name - name of a new user-defined matrix type
125: -  routine_create - routine to create method context

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

130:    Sample usage:
131: .vb
132:    MatRegister("my_mat",MyMatCreate);
133: .ve

135:    Then, your solver can be chosen with the procedural interface via
136: $     MatSetType(Mat,"my_mat")
137:    or at runtime via the option
138: $     -mat_type my_mat

140:    Level: advanced

142: .keywords: Mat, register

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


147:   Level: advanced
148: @*/
149: PetscErrorCode  MatRegister(const char sname[],PetscErrorCode (*function)(Mat))
150: {

154:   PetscFunctionListAdd(&MatList,sname,function);
155:   return(0);
156: }

158: MatBaseName MatBaseNameList = 0;

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

165:   Input Parameters:
166: +     bname - the basename, for example, MATAIJ
167: .     sname - the name of the sequential matrix type, for example, MATSEQAIJ
168: -     mname - the name of the parallel matrix type, for example, MATMPIAIJ


171:   Level: advanced
172: @*/
173: PetscErrorCode  MatRegisterBaseName(const char bname[],const char sname[],const char mname[])
174: {
176:   MatBaseName    names;

179:   PetscNew(&names);
180:   PetscStrallocpy(bname,&names->bname);
181:   PetscStrallocpy(sname,&names->sname);
182:   PetscStrallocpy(mname,&names->mname);
183:   if (!MatBaseNameList) {
184:     MatBaseNameList = names;
185:   } else {
186:     MatBaseName next = MatBaseNameList;
187:     while (next->next) next = next->next;
188:     next->next = names;
189:   }
190:   return(0);
191: }