Actual source code: matreg.c

  1: #ifdef PETSC_RCS_HEADER
  2: static char vcid[] = "$Id: matreg.c,v 1.18 2001/07/20 21:19:21 bsmith Exp $";
  3: #endif
  4: /*
  5:      Mechanism for register PETSc matrix types
  6: */
 7:  #include src/mat/matimpl.h
 8:  #include petscsys.h

 10: PetscTruth MatRegisterAllCalled = PETSC_FALSE;

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

 17: #undef __FUNCT__  
 19: /*@C
 20:    MatSetType - Builds matrix object for a particular matrix type

 22:    Collective on Mat

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

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

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

 35:   Level: intermediate

 37: .keywords: Mat, set, method

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


 49:   PetscTypeCompare((PetscObject)mat,matype,&sametype);
 50:   if (!sametype) {

 52:     /* Get the function pointers for the matrix requested */
 53:     if (!MatRegisterAllCalled) {MatRegisterAll(PETSC_NULL);}
 54:      PetscFListFind(mat->comm,MatList,matype,(void(**)(void))&r);
 55:     if (!r) SETERRQ1(1,"Unknown Mat type given: %s",matype);

 57:     /* free the old data structure if it existed */
 58:     if (mat->ops->destroy) {
 59:       (*mat->ops->destroy)(mat);
 60:     }
 61:     if (mat->rmap) {
 62:       PetscMapDestroy(mat->rmap);
 63:       mat->rmap = 0;
 64:     }
 65:     if (mat->cmap) {
 66:       PetscMapDestroy(mat->cmap);
 67:       mat->cmap = 0;
 68:     }

 70:     /* create the new data structure */
 71:     (*r)(mat);

 73:     PetscObjectChangeTypeName((PetscObject)mat,matype);
 74:   }
 75:   PetscPublishAll(mat);
 76:   return(0);
 77: }


 80: #undef __FUNCT__  
 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: int MatRegisterDestroy(void)
 95: {

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

107: #undef __FUNCT__  
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, get, method, name

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

133: /*MC
134:    MatRegisterDynamic - Adds a new matrix type

136:    Synopsis:
137:    int MatRegisterDynamic(char *name,char *path,char *name_create,int (*routine_create)(Mat))

139:    Not Collective

141:    Input Parameters:
142: +  name - name of a new user-defined matrix type
143: .  path - path (either absolute or relative) the library containing this solver
144: .  name_create - name of routine to create method context
145: -  routine_create - routine to create method context

147:    Notes:
148:    MatRegisterDynamic() may be called multiple times to add several user-defined solvers.

150:    If dynamic libraries are used, then the fourth input argument (routine_create)
151:    is ignored.

153:    Sample usage:
154: .vb
155:    MatRegisterDynamic("my_mat",/home/username/my_lib/lib/libO/solaris/mylib.a,
156:                "MyMatCreate",MyMatCreate);
157: .ve

159:    Then, your solver can be chosen with the procedural interface via
160: $     MatSetType(Mat,"my_mat")
161:    or at runtime via the option
162: $     -mat_type my_mat

164:    Level: advanced

166:    Notes: ${PETSC_ARCH} and ${BOPT} occuring in pathname will be replaced with appropriate values.
167:          If your function is not being put into a shared library then use VecRegister() instead

169: .keywords: Mat, register

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

173: M*/

175: #undef __FUNCT__  
177: /*@C
178:   MatRegister - See MatRegisterDynamic()

180:   Level: advanced
181: @*/
182: int MatRegister(char *sname,char *path,char *name,int (*function)(Mat))
183: {
184:   int  ierr;
185:   char fullname[PETSC_MAX_PATH_LEN];

188:   PetscFListConcat(path,name,fullname);
189:   PetscFListAdd(&MatList,sname,fullname,(void (*)(void))function);
190:   return(0);
191: }