xref: /petsc/src/mat/interface/matreg.c (revision db4deed73f06ae556a0f2e02a0fd6e016e156849)
1 
2 /*
3      Mechanism for register PETSc matrix types
4 */
5 #include <petsc-private/matimpl.h>      /*I "petscmat.h" I*/
6 #include <stdarg.h> /* Variable-length arg lists. */
7 
8 PetscBool  MatRegisterAllCalled = PETSC_FALSE;
9 
10 /*
11    Contains the list of registered Mat routines
12 */
13 PetscFunctionList MatList = 0;
14 
15 #undef __FUNCT__
16 #define __FUNCT__ "MatSetType"
17 /*@C
18    MatSetType - Builds matrix object for a particular matrix type
19 
20    Collective on Mat
21 
22    Input Parameters:
23 +  mat      - the matrix object
24 -  matype   - matrix type
25 
26    Options Database Key:
27 .  -mat_type  <method> - Sets the type; use -help for a list
28     of available methods (for instance, seqaij)
29 
30    Notes:
31    See "${PETSC_DIR}/include/petscmat.h" for available methods
32 
33   Level: intermediate
34 
35 .keywords: Mat, MatType, set, method
36 
37 .seealso: PCSetType(), VecSetType(), MatCreate(), MatType, Mat
38 @*/
39 PetscErrorCode  MatSetType(Mat mat, MatType matype)
40 {
41   PetscErrorCode ierr,(*r)(Mat);
42   PetscBool      sametype,found;
43   MatBaseName    names = MatBaseNameList;
44 
45   PetscFunctionBegin;
46   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
47 
48   while (names) {
49     ierr = PetscStrcmp(matype,names->bname,&found);CHKERRQ(ierr);
50     if (found) {
51       PetscMPIInt size;
52       ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr);
53       if (size == 1) matype = names->sname;
54       else matype = names->mname;
55       break;
56     }
57     names = names->next;
58   }
59 
60   ierr = PetscObjectTypeCompare((PetscObject)mat,matype,&sametype);CHKERRQ(ierr);
61   if (sametype) PetscFunctionReturn(0);
62 
63   ierr =  PetscFunctionListFind(((PetscObject)mat)->comm,MatList,matype,PETSC_TRUE,(void(**)(void))&r);CHKERRQ(ierr);
64   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown Mat type given: %s",matype);
65 
66   /* free the old data structure if it existed */
67   if (mat->ops->destroy) {
68     ierr = (*mat->ops->destroy)(mat);CHKERRQ(ierr);
69     mat->ops->destroy = PETSC_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   MatRegisterAllCalled = PETSC_FALSE;
105   PetscFunctionReturn(0);
106 }
107 
108 #undef __FUNCT__
109 #define __FUNCT__ "MatGetType"
110 /*@C
111    MatGetType - Gets the matrix type as a string from the matrix object.
112 
113    Not Collective
114 
115    Input Parameter:
116 .  mat - the matrix
117 
118    Output Parameter:
119 .  name - name of matrix type
120 
121    Level: intermediate
122 
123 .keywords: Mat, MatType, get, method, name
124 
125 .seealso: MatSetType()
126 @*/
127 PetscErrorCode  MatGetType(Mat mat,MatType *type)
128 {
129   PetscFunctionBegin;
130   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
131   PetscValidPointer(type,2);
132   *type = ((PetscObject)mat)->type_name;
133   PetscFunctionReturn(0);
134 }
135 
136 
137 #undef __FUNCT__
138 #define __FUNCT__ "MatRegister"
139 /*@C
140   MatRegister - See MatRegisterDynamic()
141 
142   Level: advanced
143 @*/
144 PetscErrorCode  MatRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(Mat))
145 {
146   PetscErrorCode ierr;
147   char           fullname[PETSC_MAX_PATH_LEN];
148 
149   PetscFunctionBegin;
150   ierr = PetscFunctionListConcat(path,name,fullname);CHKERRQ(ierr);
151   ierr = PetscFunctionListAdd(PETSC_COMM_WORLD,&MatList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
152   PetscFunctionReturn(0);
153 }
154 
155 MatBaseName MatBaseNameList = 0;
156 
157 #undef __FUNCT__
158 #define __FUNCT__ "MatRegisterBaseName"
159 /*@C
160       MatRegisterBaseName - Registers a name that can be used for either a sequential or its corresponding parallel matrix type.
161 
162   Input Parameters:
163 +     bname - the basename, for example, MATAIJ
164 .     sname - the name of the sequential matrix type, for example, MATSEQAIJ
165 -     mname - the name of the parallel matrix type, for example, MATMPIAIJ
166 
167 
168   Level: advanced
169 @*/
170 PetscErrorCode  MatRegisterBaseName(const char bname[],const char sname[],const char mname[])
171 {
172   PetscErrorCode ierr;
173   MatBaseName    names;
174 
175   PetscFunctionBegin;
176   ierr = PetscNew(struct _p_MatBaseName,&names);CHKERRQ(ierr);
177   ierr = PetscStrallocpy(bname,&names->bname);CHKERRQ(ierr);
178   ierr = PetscStrallocpy(sname,&names->sname);CHKERRQ(ierr);
179   ierr = PetscStrallocpy(mname,&names->mname);CHKERRQ(ierr);
180   if (!MatBaseNameList) {
181     MatBaseNameList = names;
182   } else {
183     MatBaseName next = MatBaseNameList;
184     while (next->next) next = next->next;
185     next->next = names;
186   }
187   PetscFunctionReturn(0);
188 }
189 
190 
191 
192 
193 
194 
195 
196