xref: /petsc/src/mat/interface/matreg.c (revision 99cd5145c18727bd2b2390c0d8042c463eb3ca7c)
17e14e8a7SBarry Smith #ifdef PETSC_RCS_HEADER
2*99cd5145SBarry Smith static char vcid[] = "$Id: matreg.c,v 1.1 1999/05/08 16:53:40 bsmith Exp bsmith $";
37e14e8a7SBarry Smith #endif
47e14e8a7SBarry Smith /*
5*99cd5145SBarry Smith      Mechanism for register PETSc matrix types
67e14e8a7SBarry Smith */
7*99cd5145SBarry Smith #include "src/mat/matimpl.h"      /*I "mat.h" I*/
8*99cd5145SBarry Smith #include "petscsys.h"
97e14e8a7SBarry Smith 
10*99cd5145SBarry Smith int MatRegisterAllCalled = 0;
117e14e8a7SBarry Smith 
127e14e8a7SBarry Smith /*
13*99cd5145SBarry Smith    Contains the list of registered Mat routines
147e14e8a7SBarry Smith */
15*99cd5145SBarry Smith FList MatList = 0;
167e14e8a7SBarry Smith 
177e14e8a7SBarry Smith #undef __FUNC__
18*99cd5145SBarry Smith #define __FUNC__ "MatSetType"
197e14e8a7SBarry Smith /*@C
20*99cd5145SBarry Smith    MatSetType - Builds matrix object for a particular matrix type
217e14e8a7SBarry Smith 
22*99cd5145SBarry Smith    Collective on Mat
237e14e8a7SBarry Smith 
247e14e8a7SBarry Smith    Input Parameters:
25*99cd5145SBarry Smith +  mat      - the matrix object
26*99cd5145SBarry Smith -  matype   - matrix type
277e14e8a7SBarry Smith 
287e14e8a7SBarry Smith    Options Database Key:
29*99cd5145SBarry Smith .  -mat_type  <method> - Sets the type; use -help for a list
30*99cd5145SBarry Smith     of available methods (for instance, seqaij)
317e14e8a7SBarry Smith 
327e14e8a7SBarry Smith    Notes:
33*99cd5145SBarry Smith    See "${PETSC_DIR}/include/petscmat.h" for available methods
347e14e8a7SBarry Smith 
357e14e8a7SBarry Smith   Level: intermediate
367e14e8a7SBarry Smith 
37*99cd5145SBarry Smith .keywords: Mat, set, method
387e14e8a7SBarry Smith 
39*99cd5145SBarry Smith .seealso: PCSetType(), VecSetType(), MatCreate()
407e14e8a7SBarry Smith @*/
41*99cd5145SBarry Smith int MatSetType(Mat mat,MATType matype)
427e14e8a7SBarry Smith {
43*99cd5145SBarry Smith   int        ierr,(*r)(Mat);
44*99cd5145SBarry Smith   PetscTruth sametype;
457e14e8a7SBarry Smith 
467e14e8a7SBarry Smith   PetscFunctionBegin;
47*99cd5145SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
487e14e8a7SBarry Smith 
49*99cd5145SBarry Smith   ierr = PetscTypeCompare((PetscObject)mat,matype,&sametype);CHKERRQ(ierr);
50*99cd5145SBarry Smith   if (sametype) PetscFunctionReturn(0);
517e14e8a7SBarry Smith 
52*99cd5145SBarry Smith   /* Get the function pointers for the matrix requested */
53*99cd5145SBarry Smith   if (!MatRegisterAllCalled) {ierr = MatRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
547e14e8a7SBarry Smith 
55*99cd5145SBarry Smith   ierr =  FListFind(mat->comm,MatList,matype,(int(**)(void*))&r);CHKERRQ(ierr);
567e14e8a7SBarry Smith 
57*99cd5145SBarry Smith   if (!r) SETERRQ1(1,1,"Unknown Mat type given: %s",matype);
587e14e8a7SBarry Smith 
59*99cd5145SBarry Smith   mat->data        = 0;
60*99cd5145SBarry Smith   ierr = (*r)(mat);CHKERRQ(ierr);
617e14e8a7SBarry Smith 
62*99cd5145SBarry Smith   ierr = PetscObjectSetName((PetscObject)mat,matype);CHKERRQ(ierr);
637e14e8a7SBarry Smith   PetscFunctionReturn(0);
647e14e8a7SBarry Smith }
657e14e8a7SBarry Smith 
667e14e8a7SBarry Smith #undef __FUNC__
67*99cd5145SBarry Smith #define __FUNC__ "MatRegisterDestroy"
687e14e8a7SBarry Smith /*@C
69*99cd5145SBarry Smith    MatRegisterDestroy - Frees the list of matrix types that were
70*99cd5145SBarry Smith    registered by MatRegister().
717e14e8a7SBarry Smith 
727e14e8a7SBarry Smith    Not Collective
737e14e8a7SBarry Smith 
747e14e8a7SBarry Smith    Level: advanced
757e14e8a7SBarry Smith 
76*99cd5145SBarry Smith .keywords: Mat, register, destroy
777e14e8a7SBarry Smith 
78*99cd5145SBarry Smith .seealso: MatRegister(), MatRegisterAll()
797e14e8a7SBarry Smith @*/
80*99cd5145SBarry Smith int MatRegisterDestroy(void)
817e14e8a7SBarry Smith {
827e14e8a7SBarry Smith   int ierr;
837e14e8a7SBarry Smith 
847e14e8a7SBarry Smith   PetscFunctionBegin;
85*99cd5145SBarry Smith   if (MatList) {
86*99cd5145SBarry Smith     ierr = FListDestroy(MatList);CHKERRQ(ierr);
87*99cd5145SBarry Smith     MatList = 0;
887e14e8a7SBarry Smith   }
89*99cd5145SBarry Smith   MatRegisterAllCalled = 0;
907e14e8a7SBarry Smith   PetscFunctionReturn(0);
917e14e8a7SBarry Smith }
927e14e8a7SBarry Smith 
937e14e8a7SBarry Smith #undef __FUNC__
94*99cd5145SBarry Smith #define __FUNC__ "MatGetType"
957e14e8a7SBarry Smith /*@C
96*99cd5145SBarry Smith    MatGetType - Gets the matrx type as a string from the matrix object.
977e14e8a7SBarry Smith 
987e14e8a7SBarry Smith    Not Collective
997e14e8a7SBarry Smith 
1007e14e8a7SBarry Smith    Input Parameter:
101*99cd5145SBarry Smith .  mat - the matrix
1027e14e8a7SBarry Smith 
1037e14e8a7SBarry Smith    Output Parameter:
104*99cd5145SBarry Smith .  name - name of matrix type
1057e14e8a7SBarry Smith 
1067e14e8a7SBarry Smith    Level: intermediate
1077e14e8a7SBarry Smith 
108*99cd5145SBarry Smith .keywords: Mat, get, method, name
1097e14e8a7SBarry Smith 
110*99cd5145SBarry Smith .seealso: MatSetType()
1117e14e8a7SBarry Smith @*/
112*99cd5145SBarry Smith int MATGetType(Mat mat,MATType *type)
1137e14e8a7SBarry Smith {
1147e14e8a7SBarry Smith   PetscFunctionBegin;
115*99cd5145SBarry Smith   *type = mat->type_name;
1167e14e8a7SBarry Smith   PetscFunctionReturn(0);
1177e14e8a7SBarry Smith }
1187e14e8a7SBarry Smith 
1197e14e8a7SBarry Smith #undef __FUNC__
120*99cd5145SBarry Smith #define __FUNC__ "MatSetTypeFromOptions"
1217e14e8a7SBarry Smith /*@
122*99cd5145SBarry Smith    MatSetTypeFromOptions - Sets Mat type from the options database, if not
1237e14e8a7SBarry Smith        given then sets default.
1247e14e8a7SBarry Smith 
125*99cd5145SBarry Smith    Collective on Mat
1267e14e8a7SBarry Smith 
1277e14e8a7SBarry Smith    Input Parameters:
128*99cd5145SBarry Smith .  Mat - the Krylov space context
1297e14e8a7SBarry Smith 
1307e14e8a7SBarry Smith    Level: developer
1317e14e8a7SBarry Smith 
132*99cd5145SBarry Smith .keywords: Mat, set, from, options, database
1337e14e8a7SBarry Smith 
134*99cd5145SBarry Smith .seealso: MatPrintHelp(), MatSetFromOptions(), SLESSetFromOptions(),
1357e14e8a7SBarry Smith           SLESSetTypeFromOptions()
1367e14e8a7SBarry Smith @*/
137*99cd5145SBarry Smith int MatSetTypeFromOptions(Mat mat)
1387e14e8a7SBarry Smith {
139*99cd5145SBarry Smith   int        ierr;
1407e14e8a7SBarry Smith   char       method[256];
141*99cd5145SBarry Smith   PetscTruth flg;
1427e14e8a7SBarry Smith 
1437e14e8a7SBarry Smith   PetscFunctionBegin;
144*99cd5145SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1457e14e8a7SBarry Smith 
146*99cd5145SBarry Smith   ierr = OptionsGetString(mat->prefix,"-mat_type",method,256,&flg);
1477e14e8a7SBarry Smith   if (flg){
148*99cd5145SBarry Smith     ierr = MatSetType(mat,method);CHKERRQ(ierr);
1497e14e8a7SBarry Smith   }
1507e14e8a7SBarry Smith   /*
1517e14e8a7SBarry Smith     Set the type if it was never set.
1527e14e8a7SBarry Smith   */
153*99cd5145SBarry Smith   if (!mat->type_name) {
154*99cd5145SBarry Smith     ierr = MatSetType(mat,"mpiaij");CHKERRQ(ierr);
1557e14e8a7SBarry Smith   }
1567e14e8a7SBarry Smith   PetscFunctionReturn(0);
1577e14e8a7SBarry Smith }
1587e14e8a7SBarry Smith 
1597e14e8a7SBarry Smith /*MC
160*99cd5145SBarry Smith    MatRegisterDynamic - Adds a new matrix type
1617e14e8a7SBarry Smith 
1627e14e8a7SBarry Smith    Synopsis:
163*99cd5145SBarry Smith    MatRegisterDynamic(char *name,char *path,char *name_create,int (*routine_create)(Mat))
1647e14e8a7SBarry Smith 
1657e14e8a7SBarry Smith    Not Collective
1667e14e8a7SBarry Smith 
1677e14e8a7SBarry Smith    Input Parameters:
168*99cd5145SBarry Smith +  name - name of a new user-defined matrix type
1697e14e8a7SBarry Smith .  path - path (either absolute or relative) the library containing this solver
1707e14e8a7SBarry Smith .  name_create - name of routine to create method context
1717e14e8a7SBarry Smith -  routine_create - routine to create method context
1727e14e8a7SBarry Smith 
1737e14e8a7SBarry Smith    Notes:
174*99cd5145SBarry Smith    MatRegister() may be called multiple times to add several user-defined solvers.
1757e14e8a7SBarry Smith 
1767e14e8a7SBarry Smith    If dynamic libraries are used, then the fourth input argument (routine_create)
1777e14e8a7SBarry Smith    is ignored.
1787e14e8a7SBarry Smith 
1797e14e8a7SBarry Smith    Sample usage:
1807e14e8a7SBarry Smith .vb
181*99cd5145SBarry Smith    MatRegisterDynamic("my_mat",/home/username/my_lib/lib/libO/solaris/mylib.a,
182*99cd5145SBarry Smith                "MyMatCreate",MyMatCreate);
1837e14e8a7SBarry Smith .ve
1847e14e8a7SBarry Smith 
1857e14e8a7SBarry Smith    Then, your solver can be chosen with the procedural interface via
186*99cd5145SBarry Smith $     MatSetType(Mat,"my_mat")
1877e14e8a7SBarry Smith    or at runtime via the option
188*99cd5145SBarry Smith $     -mat_type my_mat
1897e14e8a7SBarry Smith 
1907e14e8a7SBarry Smith    Level: advanced
1917e14e8a7SBarry Smith 
192*99cd5145SBarry Smith    ${PETSC_ARCH} and ${BOPT} occuring in pathname will be replaced with appropriate values.
1937e14e8a7SBarry Smith 
194*99cd5145SBarry Smith .keywords: Mat, register
1957e14e8a7SBarry Smith 
196*99cd5145SBarry Smith .seealso: MatRegisterAll(), MatRegisterDestroy(), MatRegister()
1977e14e8a7SBarry Smith 
1987e14e8a7SBarry Smith M*/
1997e14e8a7SBarry Smith 
2007e14e8a7SBarry Smith #undef __FUNC__
201*99cd5145SBarry Smith #define __FUNC__ "MatRegister"
202*99cd5145SBarry Smith int MatRegister(char *sname,char *path,char *name,int (*function)(Mat))
2037e14e8a7SBarry Smith {
2047e14e8a7SBarry Smith   int  ierr;
2057e14e8a7SBarry Smith   char fullname[256];
2067e14e8a7SBarry Smith 
2077e14e8a7SBarry Smith   PetscFunctionBegin;
208*99cd5145SBarry Smith   ierr = FListConcat(path,name,fullname);CHKERRQ(ierr);
209*99cd5145SBarry Smith   ierr = FListAdd(&MatList,sname,fullname,(int (*)(void*))function);CHKERRQ(ierr);
210*99cd5145SBarry Smith   PetscFunctionReturn(0);
211*99cd5145SBarry Smith }
212*99cd5145SBarry Smith 
213*99cd5145SBarry Smith #undef __FUNC__
214*99cd5145SBarry Smith #define __FUNC__ "MATCreate"
215*99cd5145SBarry Smith int MATCreate(MPI_Comm comm,int m,int n,int M,int N,Mat *A)
216*99cd5145SBarry Smith {
217*99cd5145SBarry Smith   Mat B;
218*99cd5145SBarry Smith   int ierr;
219*99cd5145SBarry Smith 
220*99cd5145SBarry Smith   PetscFunctionBegin;
221*99cd5145SBarry Smith   PetscHeaderCreate(B,_p_Mat,struct _MatOps,MAT_COOKIE,0,"Mat",comm,MatDestroy,MatView);
222*99cd5145SBarry Smith   PLogObjectCreate(B);
223*99cd5145SBarry Smith 
224*99cd5145SBarry Smith   B->m = m;
225*99cd5145SBarry Smith   B->n = N;
226*99cd5145SBarry Smith   B->M = M;
227*99cd5145SBarry Smith   B->N = N;
228*99cd5145SBarry Smith 
229*99cd5145SBarry Smith   *A = B;
230*99cd5145SBarry Smith 
231*99cd5145SBarry Smith 
2327e14e8a7SBarry Smith   PetscFunctionReturn(0);
2337e14e8a7SBarry Smith }
234