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