xref: /petsc/src/mat/interface/matreg.c (revision b9b9770376f6bdad54cc53bb7a8b79bbb53b7e80) !
1 #ifdef PETSC_RCS_HEADER
2 static char vcid[] = "$Id: matreg.c,v 1.3 2000/05/24 21:13:24 balay 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: MatPrintHelp(), MatSetFromOptions(), SLESSetFromOptions(),
135           SLESSetTypeFromOptions()
136 @*/
137 int MatSetTypeFromOptions(Mat mat)
138 {
139   int        ierr;
140   char       method[256];
141   PetscTruth flg;
142 
143   PetscFunctionBegin;
144   PetscValidHeaderSpecific(mat,MAT_COOKIE);
145 
146   ierr = OptionsGetString(mat->prefix,"-mat_type",method,256,&flg);
147   if (flg){
148     ierr = MatSetType(mat,method);CHKERRQ(ierr);
149   }
150   /*
151     Set the type if it was never set.
152   */
153   if (!mat->type_name) {
154     ierr = MatSetType(mat,"mpiaij");CHKERRQ(ierr);
155   }
156   PetscFunctionReturn(0);
157 }
158 
159 /*MC
160    MatRegisterDynamic - Adds a new matrix type
161 
162    Synopsis:
163    MatRegisterDynamic(char *name,char *path,char *name_create,int (*routine_create)(Mat))
164 
165    Not Collective
166 
167    Input Parameters:
168 +  name - name of a new user-defined matrix type
169 .  path - path (either absolute or relative) the library containing this solver
170 .  name_create - name of routine to create method context
171 -  routine_create - routine to create method context
172 
173    Notes:
174    MatRegister() may be called multiple times to add several user-defined solvers.
175 
176    If dynamic libraries are used, then the fourth input argument (routine_create)
177    is ignored.
178 
179    Sample usage:
180 .vb
181    MatRegisterDynamic("my_mat",/home/username/my_lib/lib/libO/solaris/mylib.a,
182                "MyMatCreate",MyMatCreate);
183 .ve
184 
185    Then, your solver can be chosen with the procedural interface via
186 $     MatSetType(Mat,"my_mat")
187    or at runtime via the option
188 $     -mat_type my_mat
189 
190    Level: advanced
191 
192    ${PETSC_ARCH} and ${BOPT} occuring in pathname will be replaced with appropriate values.
193 
194 .keywords: Mat, register
195 
196 .seealso: MatRegisterAll(), MatRegisterDestroy(), MatRegister()
197 
198 M*/
199 
200 #undef __FUNC__
201 #define __FUNC__ "MatRegister"
202 int MatRegister(char *sname,char *path,char *name,int (*function)(Mat))
203 {
204   int  ierr;
205   char fullname[256];
206 
207   PetscFunctionBegin;
208   ierr = FListConcat(path,name,fullname);CHKERRQ(ierr);
209   ierr = FListAdd(&MatList,sname,fullname,(int (*)(void*))function);CHKERRQ(ierr);
210   PetscFunctionReturn(0);
211 }
212 
213 #undef __FUNC__
214 #define __FUNC__ "MATCreate"
215 int MATCreate(MPI_Comm comm,int m,int n,int M,int N,Mat *A)
216 {
217   Mat B;
218 
219   PetscFunctionBegin;
220   PetscHeaderCreate(B,_p_Mat,struct _MatOps,MAT_COOKIE,0,"Mat",comm,MatDestroy,MatView);
221   PLogObjectCreate(B);
222 
223   B->m = m;
224   B->n = n;
225   B->M = M;
226   B->N = N;
227 
228   *A = B;
229   PetscFunctionReturn(0);
230 }
231