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: /*@C
 18:    MatSetType - Builds matrix object for a particular matrix type

 20:    Collective on Mat

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

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

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

 33:   Level: intermediate

 35: .keywords: Mat, set, method

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


 47:   PetscTypeCompare((PetscObject)mat,matype,&sametype);
 48:   if (!sametype) {

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

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

 68:     /* create the new data structure */
 69:     (*r)(mat);

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


 78: /*@C
 79:    MatRegisterDestroy - Frees the list of matrix types that were
 80:    registered by MatRegister().

 82:    Not Collective

 84:    Level: advanced

 86: .keywords: Mat, register, destroy

 88: .seealso: MatRegister(), MatRegisterAll()
 89: @*/
 90: int MatRegisterDestroy(void)
 91: {

 95:   if (MatList) {
 96:     PetscFListDestroy(&MatList);
 97:     MatList = 0;
 98:   }
 99:   MatRegisterAllCalled = PETSC_FALSE;
100:   return(0);
101: }

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

106:    Not Collective

108:    Input Parameter:
109: .  mat - the matrix

111:    Output Parameter:
112: .  name - name of matrix type

114:    Level: intermediate

116: .keywords: Mat, get, method, name

118: .seealso: MatSetType()
119: @*/
120: int MatGetType(Mat mat,MatType *type)
121: {
123:   *type = mat->type_name;
124:   return(0);
125: }

127: /*MC
128:    MatRegisterDynamic - Adds a new matrix type

130:    Synopsis:
131:    int MatRegisterDynamic(char *name,char *path,char *name_create,int (*routine_create)(Mat))

133:    Not Collective

135:    Input Parameters:
136: +  name - name of a new user-defined matrix type
137: .  path - path (either absolute or relative) the library containing this solver
138: .  name_create - name of routine to create method context
139: -  routine_create - routine to create method context

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

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

147:    Sample usage:
148: .vb
149:    MatRegisterDynamic("my_mat",/home/username/my_lib/lib/libO/solaris/mylib.a,
150:                "MyMatCreate",MyMatCreate);
151: .ve

153:    Then, your solver can be chosen with the procedural interface via
154: $     MatSetType(Mat,"my_mat")
155:    or at runtime via the option
156: $     -mat_type my_mat

158:    Level: advanced

160:    ${PETSC_ARCH} and ${BOPT} occuring in pathname will be replaced with appropriate values.

162: .keywords: Mat, register

164: .seealso: MatRegisterAll(), MatRegisterDestroy(), MatRegister()

166: M*/

168: int MatRegister(char *sname,char *path,char *name,int (*function)(Mat))
169: {
170:   int  ierr;
171:   char fullname[256];

174:   PetscFListConcat(path,name,fullname);
175:   PetscFListAdd(&MatList,sname,fullname,(void (*)(void))function);
176:   return(0);
177: }