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