xref: /petsc/src/mat/interface/matreg.c (revision 93adecfc72bedd5e853cdc999a90879e248dbda8)
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"      /*I "petscmat.h" I*/
8 #include "petscsys.h"
9 
10 PetscTruth MatRegisterAllCalled = PETSC_FALSE;
11 
12 /*
13    Contains the list of registered Mat routines
14 */
15 PetscFList MatList = 0;
16 
17 #undef __FUNCT__
18 #define __FUNCT__ "MatSetType"
19 /*@C
20    MatSetType - Builds matrix object for a particular matrix type
21 
22    Collective on Mat
23 
24    Input Parameters:
25 +  mat      - the matrix object
26 -  matype   - matrix type
27 
28    Options Database Key:
29 .  -mat_type  <method> - Sets the type; use -help for a list
30     of available methods (for instance, seqaij)
31 
32    Notes:
33    See "${PETSC_DIR}/include/petscmat.h" for available methods
34 
35   Level: intermediate
36 
37 .keywords: Mat, set, method
38 
39 .seealso: PCSetType(), VecSetType(), MatCreate(), MatType, Mat
40 @*/
41 int MatSetType(Mat mat,MatType matype)
42 {
43   int        ierr,(*r)(Mat);
44   PetscTruth sametype;
45 
46   PetscFunctionBegin;
47   PetscValidHeaderSpecific(mat,MAT_COOKIE);
48 
49   ierr = PetscTypeCompare((PetscObject)mat,matype,&sametype);CHKERRQ(ierr);
50   if (!sametype) {
51 
52     /* Get the function pointers for the matrix requested */
53     if (!MatRegisterAllCalled) {ierr = MatRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
54     ierr =  PetscFListFind(mat->comm,MatList,matype,(void(**)(void))&r);CHKERRQ(ierr);
55     if (!r) SETERRQ1(1,"Unknown Mat type given: %s",matype);
56 
57     /* free the old data structure if it existed */
58     if (mat->ops->destroy) {
59       ierr = (*mat->ops->destroy)(mat);CHKERRQ(ierr);
60     }
61     if (mat->rmap) {
62       ierr = PetscMapDestroy(mat->rmap);CHKERRQ(ierr);
63       mat->rmap = 0;
64     }
65     if (mat->cmap) {
66       ierr = PetscMapDestroy(mat->cmap);CHKERRQ(ierr);
67       mat->cmap = 0;
68     }
69 
70     /* create the new data structure */
71     ierr = (*r)(mat);CHKERRQ(ierr);
72 
73     ierr = PetscObjectChangeTypeName((PetscObject)mat,matype);CHKERRQ(ierr);
74   }
75   ierr = PetscPublishAll(mat);CHKERRQ(ierr);
76   PetscFunctionReturn(0);
77 }
78 
79 
80 #undef __FUNCT__
81 #define __FUNCT__ "MatRegisterDestroy"
82 /*@C
83    MatRegisterDestroy - Frees the list of matrix types that were
84    registered by MatRegister()/MatRegisterDynamic().
85 
86    Not Collective
87 
88    Level: advanced
89 
90 .keywords: Mat, register, destroy
91 
92 .seealso: MatRegister(), MatRegisterAll(), MatRegisterDynamic()
93 @*/
94 int MatRegisterDestroy(void)
95 {
96   int ierr;
97 
98   PetscFunctionBegin;
99   if (MatList) {
100     ierr = PetscFListDestroy(&MatList);CHKERRQ(ierr);
101     MatList = 0;
102   }
103   MatRegisterAllCalled = PETSC_FALSE;
104   PetscFunctionReturn(0);
105 }
106 
107 #undef __FUNCT__
108 #define __FUNCT__ "MatGetType"
109 /*@C
110    MatGetType - Gets the matrix type as a string from the matrix object.
111 
112    Not Collective
113 
114    Input Parameter:
115 .  mat - the matrix
116 
117    Output Parameter:
118 .  name - name of matrix type
119 
120    Level: intermediate
121 
122 .keywords: Mat, get, method, name
123 
124 .seealso: MatSetType()
125 @*/
126 int MatGetType(Mat mat,MatType *type)
127 {
128   PetscFunctionBegin;
129   *type = mat->type_name;
130   PetscFunctionReturn(0);
131 }
132 
133 
134 #undef __FUNCT__
135 #define __FUNCT__ "MatRegister"
136 /*@C
137   MatRegister - See MatRegisterDynamic()
138 
139   Level: advanced
140 @*/
141 int MatRegister(char *sname,char *path,char *name,int (*function)(Mat))
142 {
143   int  ierr;
144   char fullname[PETSC_MAX_PATH_LEN];
145 
146   PetscFunctionBegin;
147   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
148   ierr = PetscFListAdd(&MatList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
149   PetscFunctionReturn(0);
150 }
151 
152 
153 
154 
155 
156 
157 
158 
159 
160 
161