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