Actual source code: matreg.c

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

  7: PetscBool  MatRegisterAllCalled = PETSC_FALSE;

  9: /*
 10:    Contains the list of registered Mat routines
 11: */
 12: PetscFList 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, const 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(((PetscObject)mat)->comm,&size);
 52:       if (size == 1) matype = names->sname;
 53:       else matype = names->mname;
 54:       break;
 55:     }
 56:     names = names->next;
 57:   }

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

 62:    PetscFListFind(MatList,((PetscObject)mat)->comm,matype,PETSC_TRUE,(void(**)(void))&r);
 63:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown Mat type given: %s",matype);
 64: 
 65:   /* free the old data structure if it existed */
 66:   if (mat->ops->destroy) {
 67:     (*mat->ops->destroy)(mat);
 68:     mat->ops->destroy = PETSC_NULL;
 69:   }
 70:   mat->preallocated = PETSC_FALSE;

 72:   /* create the new data structure */
 73:   (*r)(mat);
 74: #if defined(PETSC_HAVE_AMS)
 75:   if (PetscAMSPublishAll) {
 76:     /*    PetscObjectAMSPublish((PetscObject)mat); */
 77:   }
 78: #endif
 79:   return(0);
 80: }


 85: /*@C
 86:    MatRegisterDestroy - Frees the list of matrix types that were
 87:    registered by MatRegister()/MatRegisterDynamic().

 89:    Not Collective

 91:    Level: advanced

 93: .keywords: Mat, register, destroy

 95: .seealso: MatRegister(), MatRegisterAll(), MatRegisterDynamic()
 96: @*/
 97: PetscErrorCode  MatRegisterDestroy(void)
 98: {

102:   PetscFListDestroy(&MatList);
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  MatGetType(Mat mat,const MatType *type)
127: {
131:   *type = ((PetscObject)mat)->type_name;
132:   return(0);
133: }


138: /*@C
139:   MatRegister - See MatRegisterDynamic()

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

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

154: MatBaseName MatBaseNameList = 0;

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

161:   Input Parameters:
162: +     bname - the basename, for example, MATAIJ
163: .     sname - the name of the sequential matrix type, for example, MATSEQAIJ
164: -     mname - the name of the parallel matrix type, for example, MATMPIAIJ


167:   Level: advanced
168: @*/
169: PetscErrorCode  MatRegisterBaseName(const char bname[],const char sname[],const char mname[])
170: {
172:   MatBaseName    names;

175:   PetscNew(struct _p_MatBaseName,&names);
176:   PetscStrallocpy(bname,&names->bname);
177:   PetscStrallocpy(sname,&names->sname);
178:   PetscStrallocpy(mname,&names->mname);
179:   if (!MatBaseNameList) {
180:     MatBaseNameList = names;
181:   } else {
182:     MatBaseName next = MatBaseNameList;
183:     while (next->next) next = next->next;
184:     next->next = names;
185:   }
186:   return(0);
187: }