xref: /petsc/src/mat/interface/matreg.c (revision 074cc835faa3d6c800494dd2ea2bd8f95d70c354)
1 
2 /*
3      Mechanism for register PETSc matrix types
4 */
5 #include <private/matimpl.h>      /*I "petscmat.h" I*/
6 
7 PetscBool  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   PetscBool      sametype,found;
42   MatBaseName    names = MatBaseNameList;
43 
44   PetscFunctionBegin;
45   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
46 
47   while (names) {
48     ierr = PetscStrcmp(matype,names->bname,&found);CHKERRQ(ierr);
49     if (found) {
50       PetscMPIInt size;
51       ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr);
52       if (size == 1) matype = names->sname;
53       else matype = names->mname;
54       break;
55     }
56     names = names->next;
57   }
58 
59   ierr = PetscTypeCompare((PetscObject)mat,matype,&sametype);CHKERRQ(ierr);
60   if (sametype) PetscFunctionReturn(0);
61 
62   ierr =  PetscFListFind(MatList,((PetscObject)mat)->comm,matype,PETSC_TRUE,(void(**)(void))&r);CHKERRQ(ierr);
63   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown Mat type given: %s",matype);
64 
65   /* free the old data structure if it existed */
66   if (mat->ops->destroy) {
67     ierr = (*mat->ops->destroy)(mat);CHKERRQ(ierr);
68     mat->ops->destroy = PETSC_NULL;
69   }
70   mat->preallocated = PETSC_FALSE;
71 
72   /* create the new data structure */
73   ierr = (*r)(mat);CHKERRQ(ierr);
74 #if defined(PETSC_HAVE_AMS)
75   if (PetscAMSPublishAll) {
76     /*    ierr = PetscObjectAMSPublish((PetscObject)mat);CHKERRQ(ierr); */
77   }
78 #endif
79   PetscFunctionReturn(0);
80 }
81 
82 
83 #undef __FUNCT__
84 #define __FUNCT__ "MatRegisterDestroy"
85 /*@C
86    MatRegisterDestroy - Frees the list of matrix types that were
87    registered by MatRegister()/MatRegisterDynamic().
88 
89    Not Collective
90 
91    Level: advanced
92 
93 .keywords: Mat, register, destroy
94 
95 .seealso: MatRegister(), MatRegisterAll(), MatRegisterDynamic()
96 @*/
97 PetscErrorCode  MatRegisterDestroy(void)
98 {
99   PetscErrorCode ierr;
100 
101   PetscFunctionBegin;
102   ierr = PetscFListDestroy(&MatList);CHKERRQ(ierr);
103   MatRegisterAllCalled = PETSC_FALSE;
104   PetscFunctionReturn(0);
105 }
106 
107 #undef __FUNCT__
108 #define __FUNCT__ "MatGetType"
109 /*@C
110    MatGetType - Gets the matrix type as a string from the matrix object.
111 
112    Not Collective
113 
114    Input Parameter:
115 .  mat - the matrix
116 
117    Output Parameter:
118 .  name - name of matrix type
119 
120    Level: intermediate
121 
122 .keywords: Mat, MatType, get, method, name
123 
124 .seealso: MatSetType()
125 @*/
126 PetscErrorCode  MatGetType(Mat mat,const MatType *type)
127 {
128   PetscFunctionBegin;
129   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
130   PetscValidPointer(type,2);
131   *type = ((PetscObject)mat)->type_name;
132   PetscFunctionReturn(0);
133 }
134 
135 
136 #undef __FUNCT__
137 #define __FUNCT__ "MatRegister"
138 /*@C
139   MatRegister - See MatRegisterDynamic()
140 
141   Level: advanced
142 @*/
143 PetscErrorCode  MatRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(Mat))
144 {
145   PetscErrorCode ierr;
146   char           fullname[PETSC_MAX_PATH_LEN];
147 
148   PetscFunctionBegin;
149   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
150   ierr = PetscFListAdd(&MatList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
151   PetscFunctionReturn(0);
152 }
153 
154 MatBaseName MatBaseNameList = 0;
155 
156 #undef __FUNCT__
157 #define __FUNCT__ "MatRegisterBaseName"
158 /*@C
159       MatRegisterBaseName - Registers a name that can be used for either a sequential or its corresponding parallel matrix type.
160 
161   Input Parameters:
162 +     bname - the basename, for example, MATAIJ
163 .     sname - the name of the sequential matrix type, for example, MATSEQAIJ
164 -     mname - the name of the parallel matrix type, for example, MATMPIAIJ
165 
166 
167   Level: advanced
168 @*/
169 PetscErrorCode  MatRegisterBaseName(const char bname[],const char sname[],const char mname[])
170 {
171   PetscErrorCode ierr;
172   MatBaseName    names;
173 
174   PetscFunctionBegin;
175   ierr = PetscNew(struct _p_MatBaseName,&names);CHKERRQ(ierr);
176   ierr = PetscStrallocpy(bname,&names->bname);CHKERRQ(ierr);
177   ierr = PetscStrallocpy(sname,&names->sname);CHKERRQ(ierr);
178   ierr = PetscStrallocpy(mname,&names->mname);CHKERRQ(ierr);
179   if (!MatBaseNameList) {
180     MatBaseNameList = names;
181   } else {
182     MatBaseName next = MatBaseNameList;
183     while (next->next) next = next->next;
184     next->next = names;
185   }
186   PetscFunctionReturn(0);
187 }
188 
189 
190 
191 
192 
193 
194 
195 
196 
197 
198