xref: /petsc/src/mat/interface/matreg.c (revision 4bbc92c1ffdc419abb1af95adee3d231624e4f0c)
1 #ifdef PETSC_RCS_HEADER
2 static char vcid[] = "$Id: matreg.c,v 1.6 2000/08/24 22:41:43 bsmith Exp bsmith $";
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 int MatRegisterAllCalled = 0;
11 
12 /*
13    Contains the list of registered Mat routines
14 */
15 FList MatList = 0;
16 
17 #undef __FUNC__
18 #define __FUNC__ "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()
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) PetscFunctionReturn(0);
51 
52   /* Get the function pointers for the matrix requested */
53   if (!MatRegisterAllCalled) {ierr = MatRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
54 
55   ierr =  FListFind(mat->comm,MatList,matype,(int(**)(void*))&r);CHKERRQ(ierr);
56 
57   if (!r) SETERRQ1(1,1,"Unknown Mat type given: %s",matype);
58 
59   mat->data        = 0;
60   ierr = (*r)(mat);CHKERRQ(ierr);
61 
62   ierr = PetscObjectChangeTypeName((PetscObject)mat,matype);CHKERRQ(ierr);
63   PetscFunctionReturn(0);
64 }
65 
66 #undef __FUNC__
67 #define __FUNC__ "MatRegisterDestroy"
68 /*@C
69    MatRegisterDestroy - Frees the list of matrix types that were
70    registered by MatRegister().
71 
72    Not Collective
73 
74    Level: advanced
75 
76 .keywords: Mat, register, destroy
77 
78 .seealso: MatRegister(), MatRegisterAll()
79 @*/
80 int MatRegisterDestroy(void)
81 {
82   int ierr;
83 
84   PetscFunctionBegin;
85   if (MatList) {
86     ierr = FListDestroy(&MatList);CHKERRQ(ierr);
87     MatList = 0;
88   }
89   MatRegisterAllCalled = 0;
90   PetscFunctionReturn(0);
91 }
92 
93 #undef __FUNC__
94 #define __FUNC__ "MatGetType"
95 /*@C
96    MatGetType - Gets the matrx type as a string from the matrix object.
97 
98    Not Collective
99 
100    Input Parameter:
101 .  mat - the matrix
102 
103    Output Parameter:
104 .  name - name of matrix type
105 
106    Level: intermediate
107 
108 .keywords: Mat, get, method, name
109 
110 .seealso: MatSetType()
111 @*/
112 int MATGetType(Mat mat,MATType *type)
113 {
114   PetscFunctionBegin;
115   *type = mat->type_name;
116   PetscFunctionReturn(0);
117 }
118 
119 #undef __FUNC__
120 #define __FUNC__ "MatSetTypeFromOptions"
121 /*@
122    MatSetTypeFromOptions - Sets Mat type from the options database, if not
123        given then sets default.
124 
125    Collective on Mat
126 
127    Input Parameters:
128 .  Mat - the Krylov space context
129 
130    Level: developer
131 
132 .keywords: Mat, set, from, options, database
133 
134 .seealso: MatSetFromOptions(), SLESSetFromOptions()
135 @*/
136 int MatSetTypeFromOptions(Mat mat)
137 {
138   int        ierr;
139   char       method[256];
140   PetscTruth flg;
141 
142   PetscFunctionBegin;
143   PetscValidHeaderSpecific(mat,MAT_COOKIE);
144 
145   ierr = OptionsGetString(mat->prefix,"-mat_type",method,256,&flg);
146   if (flg){
147     ierr = MatSetType(mat,method);CHKERRQ(ierr);
148   }
149   /*
150     Set the type if it was never set.
151   */
152   if (!mat->type_name) {
153     ierr = MatSetType(mat,"mpiaij");CHKERRQ(ierr);
154   }
155   PetscFunctionReturn(0);
156 }
157 
158 /*MC
159    MatRegisterDynamic - Adds a new matrix type
160 
161    Synopsis:
162    MatRegisterDynamic(char *name,char *path,char *name_create,int (*routine_create)(Mat))
163 
164    Not Collective
165 
166    Input Parameters:
167 +  name - name of a new user-defined matrix type
168 .  path - path (either absolute or relative) the library containing this solver
169 .  name_create - name of routine to create method context
170 -  routine_create - routine to create method context
171 
172    Notes:
173    MatRegister() may be called multiple times to add several user-defined solvers.
174 
175    If dynamic libraries are used, then the fourth input argument (routine_create)
176    is ignored.
177 
178    Sample usage:
179 .vb
180    MatRegisterDynamic("my_mat",/home/username/my_lib/lib/libO/solaris/mylib.a,
181                "MyMatCreate",MyMatCreate);
182 .ve
183 
184    Then, your solver can be chosen with the procedural interface via
185 $     MatSetType(Mat,"my_mat")
186    or at runtime via the option
187 $     -mat_type my_mat
188 
189    Level: advanced
190 
191    ${PETSC_ARCH} and ${BOPT} occuring in pathname will be replaced with appropriate values.
192 
193 .keywords: Mat, register
194 
195 .seealso: MatRegisterAll(), MatRegisterDestroy(), MatRegister()
196 
197 M*/
198 
199 #undef __FUNC__
200 #define __FUNC__ "MatRegister"
201 int MatRegister(char *sname,char *path,char *name,int (*function)(Mat))
202 {
203   int  ierr;
204   char fullname[256];
205 
206   PetscFunctionBegin;
207   ierr = FListConcat(path,name,fullname);CHKERRQ(ierr);
208   ierr = FListAdd(&MatList,sname,fullname,(int (*)(void*))function);CHKERRQ(ierr);
209   PetscFunctionReturn(0);
210 }
211 
212 #undef __FUNC__
213 #define __FUNC__ "MATCreate"
214 int MATCreate(MPI_Comm comm,int m,int n,int M,int N,Mat *A)
215 {
216   Mat B;
217 
218   PetscFunctionBegin;
219   PetscHeaderCreate(B,_p_Mat,struct _MatOps,MAT_COOKIE,0,"Mat",comm,MatDestroy,MatView);
220   PLogObjectCreate(B);
221 
222   B->m = m;
223   B->n = n;
224   B->M = M;
225   B->N = N;
226 
227   *A = B;
228   PetscFunctionReturn(0);
229 }
230