xref: /petsc/src/mat/interface/matreg.c (revision 4482741e5b2e2bbc854fb1f8dba65221386520f2)
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, MatType, set, method
38 
39 .seealso: PCSetType(), VecSetType(), MatCreate(), MatType, Mat
40 @*/
41 int MatSetType(Mat mat,const MatType matype)
42 {
43   int        ierr,(*r)(Mat);
44   PetscTruth sametype;
45 
46   PetscFunctionBegin;
47   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
48 
49   ierr = PetscTypeCompare((PetscObject)mat,matype,&sametype);CHKERRQ(ierr);
50   if (!sametype) {
51     /* Get the function pointers for the matrix requested */
52     if (!MatRegisterAllCalled) {ierr = MatRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
53     ierr =  PetscFListFind(mat->comm,MatList,matype,(void(**)(void))&r);CHKERRQ(ierr);
54     if (!r) SETERRQ1(1,"Unknown Mat type given: %s",matype);
55 
56     /* free the old data structure if it existed */
57     if (mat->ops->destroy) {
58       MatPreallocated(mat);
59       ierr = (*mat->ops->destroy)(mat);CHKERRQ(ierr);
60       mat->ops->destroy = PETSC_NULL;
61       mat->preallocated = PETSC_FALSE;
62     }
63 
64     if (mat->rmap) {
65       ierr = PetscMapDestroy(mat->rmap);CHKERRQ(ierr);
66       mat->rmap = 0;
67     }
68     if (mat->cmap) {
69       ierr = PetscMapDestroy(mat->cmap);CHKERRQ(ierr);
70       mat->cmap = 0;
71     }
72     if (mat->qlist) {
73       ierr = PetscFListDestroy(&mat->qlist);CHKERRQ(ierr);
74       mat->qlist = 0;
75     }
76     if (mat->olist) {
77       ierr = PetscOListDestroy(&mat->olist);CHKERRQ(ierr);
78       mat->olist = 0;
79     }
80     /* create the new data structure */
81     ierr = (*r)(mat);CHKERRQ(ierr);
82 
83     ierr = PetscObjectChangeTypeName((PetscObject)mat,matype);CHKERRQ(ierr);
84   }
85   ierr = PetscPublishAll(mat);CHKERRQ(ierr);
86   PetscFunctionReturn(0);
87 }
88 
89 
90 #undef __FUNCT__
91 #define __FUNCT__ "MatRegisterDestroy"
92 /*@C
93    MatRegisterDestroy - Frees the list of matrix types that were
94    registered by MatRegister()/MatRegisterDynamic().
95 
96    Not Collective
97 
98    Level: advanced
99 
100 .keywords: Mat, register, destroy
101 
102 .seealso: MatRegister(), MatRegisterAll(), MatRegisterDynamic()
103 @*/
104 int MatRegisterDestroy(void)
105 {
106   int ierr;
107 
108   PetscFunctionBegin;
109   if (MatList) {
110     ierr = PetscFListDestroy(&MatList);CHKERRQ(ierr);
111     MatList = 0;
112   }
113   MatRegisterAllCalled = PETSC_FALSE;
114   PetscFunctionReturn(0);
115 }
116 
117 #undef __FUNCT__
118 #define __FUNCT__ "MatGetType"
119 /*@C
120    MatGetType - Gets the matrix type as a string from the matrix object.
121 
122    Not Collective
123 
124    Input Parameter:
125 .  mat - the matrix
126 
127    Output Parameter:
128 .  name - name of matrix type
129 
130    Level: intermediate
131 
132 .keywords: Mat, MatType, get, method, name
133 
134 .seealso: MatSetType()
135 @*/
136 int MatGetType(Mat mat,MatType *type)
137 {
138   PetscFunctionBegin;
139   *type = mat->type_name;
140   PetscFunctionReturn(0);
141 }
142 
143 
144 #undef __FUNCT__
145 #define __FUNCT__ "MatRegister"
146 /*@C
147   MatRegister - See MatRegisterDynamic()
148 
149   Level: advanced
150 @*/
151 int MatRegister(const char sname[],const char path[],const char name[],int (*function)(Mat))
152 {
153   int  ierr;
154   char fullname[PETSC_MAX_PATH_LEN];
155 
156   PetscFunctionBegin;
157   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
158   ierr = PetscFListAdd(&MatList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
159   PetscFunctionReturn(0);
160 }
161 
162 
163 
164 
165 
166 
167 
168 
169 
170 
171