xref: /petsc/src/mat/interface/matreg.c (revision 273d9f13de75c4ed17021f7f2c11eebb99d26f0d)
1 #ifdef PETSC_RCS_HEADER
2 static char vcid[] = "$Id: matreg.c,v 1.10 2000/09/28 21:10:52 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 PetscTruth MatRegisterAllCalled = PETSC_FALSE;
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,"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 = PETSC_FALSE;
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 /*MC
120    MatRegisterDynamic - Adds a new matrix type
121 
122    Synopsis:
123    MatRegisterDynamic(char *name,char *path,char *name_create,int (*routine_create)(Mat))
124 
125    Not Collective
126 
127    Input Parameters:
128 +  name - name of a new user-defined matrix type
129 .  path - path (either absolute or relative) the library containing this solver
130 .  name_create - name of routine to create method context
131 -  routine_create - routine to create method context
132 
133    Notes:
134    MatRegister() may be called multiple times to add several user-defined solvers.
135 
136    If dynamic libraries are used, then the fourth input argument (routine_create)
137    is ignored.
138 
139    Sample usage:
140 .vb
141    MatRegisterDynamic("my_mat",/home/username/my_lib/lib/libO/solaris/mylib.a,
142                "MyMatCreate",MyMatCreate);
143 .ve
144 
145    Then, your solver can be chosen with the procedural interface via
146 $     MatSetType(Mat,"my_mat")
147    or at runtime via the option
148 $     -mat_type my_mat
149 
150    Level: advanced
151 
152    ${PETSC_ARCH} and ${BOPT} occuring in pathname will be replaced with appropriate values.
153 
154 .keywords: Mat, register
155 
156 .seealso: MatRegisterAll(), MatRegisterDestroy(), MatRegister()
157 
158 M*/
159 
160 #undef __FUNC__
161 #define __FUNC__ "MatRegister"
162 int MatRegister(char *sname,char *path,char *name,int (*function)(Mat))
163 {
164   int  ierr;
165   char fullname[256];
166 
167   PetscFunctionBegin;
168   ierr = FListConcat(path,name,fullname);CHKERRQ(ierr);
169   ierr = FListAdd(&MatList,sname,fullname,(int (*)(void*))function);CHKERRQ(ierr);
170   PetscFunctionReturn(0);
171 }
172 
173 
174 
175 
176 
177 
178 
179 
180 
181 
182