xref: /petsc/src/mat/interface/matreg.c (revision 273d9f13de75c4ed17021f7f2c11eebb99d26f0d)
17e14e8a7SBarry Smith #ifdef PETSC_RCS_HEADER
2*273d9f13SBarry Smith static char vcid[] = "$Id: matreg.c,v 1.10 2000/09/28 21:10:52 bsmith Exp bsmith $";
37e14e8a7SBarry Smith #endif
47e14e8a7SBarry Smith /*
599cd5145SBarry Smith      Mechanism for register PETSc matrix types
67e14e8a7SBarry Smith */
7aa59fb91SSatish Balay #include "src/mat/matimpl.h"      /*I "petscmat.h" I*/
899cd5145SBarry Smith #include "petscsys.h"
97e14e8a7SBarry Smith 
1091af72abSBarry Smith PetscTruth MatRegisterAllCalled = PETSC_FALSE;
117e14e8a7SBarry Smith 
127e14e8a7SBarry Smith /*
1399cd5145SBarry Smith    Contains the list of registered Mat routines
147e14e8a7SBarry Smith */
1599cd5145SBarry Smith FList MatList = 0;
167e14e8a7SBarry Smith 
177e14e8a7SBarry Smith #undef __FUNC__
1899cd5145SBarry Smith #define __FUNC__ "MatSetType"
197e14e8a7SBarry Smith /*@C
2099cd5145SBarry Smith    MatSetType - Builds matrix object for a particular matrix type
217e14e8a7SBarry Smith 
2299cd5145SBarry Smith    Collective on Mat
237e14e8a7SBarry Smith 
247e14e8a7SBarry Smith    Input Parameters:
2599cd5145SBarry Smith +  mat      - the matrix object
2699cd5145SBarry Smith -  matype   - matrix type
277e14e8a7SBarry Smith 
287e14e8a7SBarry Smith    Options Database Key:
2999cd5145SBarry Smith .  -mat_type  <method> - Sets the type; use -help for a list
3099cd5145SBarry Smith     of available methods (for instance, seqaij)
317e14e8a7SBarry Smith 
327e14e8a7SBarry Smith    Notes:
3399cd5145SBarry Smith    See "${PETSC_DIR}/include/petscmat.h" for available methods
347e14e8a7SBarry Smith 
357e14e8a7SBarry Smith   Level: intermediate
367e14e8a7SBarry Smith 
3799cd5145SBarry Smith .keywords: Mat, set, method
387e14e8a7SBarry Smith 
3999cd5145SBarry Smith .seealso: PCSetType(), VecSetType(), MatCreate()
407e14e8a7SBarry Smith @*/
41*273d9f13SBarry Smith int MatSetType(Mat mat,MatType matype)
427e14e8a7SBarry Smith {
4399cd5145SBarry Smith   int        ierr,(*r)(Mat);
4499cd5145SBarry Smith   PetscTruth sametype;
457e14e8a7SBarry Smith 
467e14e8a7SBarry Smith   PetscFunctionBegin;
4799cd5145SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
487e14e8a7SBarry Smith 
4999cd5145SBarry Smith   ierr = PetscTypeCompare((PetscObject)mat,matype,&sametype);CHKERRQ(ierr);
5099cd5145SBarry Smith   if (sametype) PetscFunctionReturn(0);
517e14e8a7SBarry Smith 
5299cd5145SBarry Smith   /* Get the function pointers for the matrix requested */
5399cd5145SBarry Smith   if (!MatRegisterAllCalled) {ierr = MatRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
547e14e8a7SBarry Smith 
5599cd5145SBarry Smith   ierr =  FListFind(mat->comm,MatList,matype,(int(**)(void*))&r);CHKERRQ(ierr);
567e14e8a7SBarry Smith 
5729bbc08cSBarry Smith   if (!r) SETERRQ1(1,"Unknown Mat type given: %s",matype);
587e14e8a7SBarry Smith 
5999cd5145SBarry Smith   mat->data        = 0;
6099cd5145SBarry Smith   ierr = (*r)(mat);CHKERRQ(ierr);
617e14e8a7SBarry Smith 
62b9b97703SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)mat,matype);CHKERRQ(ierr);
637e14e8a7SBarry Smith   PetscFunctionReturn(0);
647e14e8a7SBarry Smith }
657e14e8a7SBarry Smith 
667e14e8a7SBarry Smith #undef __FUNC__
6799cd5145SBarry Smith #define __FUNC__ "MatRegisterDestroy"
687e14e8a7SBarry Smith /*@C
6999cd5145SBarry Smith    MatRegisterDestroy - Frees the list of matrix types that were
7099cd5145SBarry Smith    registered by MatRegister().
717e14e8a7SBarry Smith 
727e14e8a7SBarry Smith    Not Collective
737e14e8a7SBarry Smith 
747e14e8a7SBarry Smith    Level: advanced
757e14e8a7SBarry Smith 
7699cd5145SBarry Smith .keywords: Mat, register, destroy
777e14e8a7SBarry Smith 
7899cd5145SBarry Smith .seealso: MatRegister(), MatRegisterAll()
797e14e8a7SBarry Smith @*/
8099cd5145SBarry Smith int MatRegisterDestroy(void)
817e14e8a7SBarry Smith {
827e14e8a7SBarry Smith   int ierr;
837e14e8a7SBarry Smith 
847e14e8a7SBarry Smith   PetscFunctionBegin;
8599cd5145SBarry Smith   if (MatList) {
861d1367b7SBarry Smith     ierr = FListDestroy(&MatList);CHKERRQ(ierr);
8799cd5145SBarry Smith     MatList = 0;
887e14e8a7SBarry Smith   }
890e9836bfSBarry Smith   MatRegisterAllCalled = PETSC_FALSE;
907e14e8a7SBarry Smith   PetscFunctionReturn(0);
917e14e8a7SBarry Smith }
927e14e8a7SBarry Smith 
937e14e8a7SBarry Smith #undef __FUNC__
9499cd5145SBarry Smith #define __FUNC__ "MatGetType"
957e14e8a7SBarry Smith /*@C
9699cd5145SBarry 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:
10199cd5145SBarry Smith .  mat - the matrix
1027e14e8a7SBarry Smith 
1037e14e8a7SBarry Smith    Output Parameter:
10499cd5145SBarry Smith .  name - name of matrix type
1057e14e8a7SBarry Smith 
1067e14e8a7SBarry Smith    Level: intermediate
1077e14e8a7SBarry Smith 
10899cd5145SBarry Smith .keywords: Mat, get, method, name
1097e14e8a7SBarry Smith 
11099cd5145SBarry Smith .seealso: MatSetType()
1117e14e8a7SBarry Smith @*/
112*273d9f13SBarry Smith int MatGetType(Mat mat,MatType *type)
1137e14e8a7SBarry Smith {
1147e14e8a7SBarry Smith   PetscFunctionBegin;
11599cd5145SBarry Smith   *type = mat->type_name;
1167e14e8a7SBarry Smith   PetscFunctionReturn(0);
1177e14e8a7SBarry Smith }
1187e14e8a7SBarry Smith 
1197e14e8a7SBarry Smith /*MC
12099cd5145SBarry Smith    MatRegisterDynamic - Adds a new matrix type
1217e14e8a7SBarry Smith 
1227e14e8a7SBarry Smith    Synopsis:
12399cd5145SBarry Smith    MatRegisterDynamic(char *name,char *path,char *name_create,int (*routine_create)(Mat))
1247e14e8a7SBarry Smith 
1257e14e8a7SBarry Smith    Not Collective
1267e14e8a7SBarry Smith 
1277e14e8a7SBarry Smith    Input Parameters:
12899cd5145SBarry Smith +  name - name of a new user-defined matrix type
1297e14e8a7SBarry Smith .  path - path (either absolute or relative) the library containing this solver
1307e14e8a7SBarry Smith .  name_create - name of routine to create method context
1317e14e8a7SBarry Smith -  routine_create - routine to create method context
1327e14e8a7SBarry Smith 
1337e14e8a7SBarry Smith    Notes:
13499cd5145SBarry Smith    MatRegister() may be called multiple times to add several user-defined solvers.
1357e14e8a7SBarry Smith 
1367e14e8a7SBarry Smith    If dynamic libraries are used, then the fourth input argument (routine_create)
1377e14e8a7SBarry Smith    is ignored.
1387e14e8a7SBarry Smith 
1397e14e8a7SBarry Smith    Sample usage:
1407e14e8a7SBarry Smith .vb
14199cd5145SBarry Smith    MatRegisterDynamic("my_mat",/home/username/my_lib/lib/libO/solaris/mylib.a,
14299cd5145SBarry Smith                "MyMatCreate",MyMatCreate);
1437e14e8a7SBarry Smith .ve
1447e14e8a7SBarry Smith 
1457e14e8a7SBarry Smith    Then, your solver can be chosen with the procedural interface via
14699cd5145SBarry Smith $     MatSetType(Mat,"my_mat")
1477e14e8a7SBarry Smith    or at runtime via the option
14899cd5145SBarry Smith $     -mat_type my_mat
1497e14e8a7SBarry Smith 
1507e14e8a7SBarry Smith    Level: advanced
1517e14e8a7SBarry Smith 
15299cd5145SBarry Smith    ${PETSC_ARCH} and ${BOPT} occuring in pathname will be replaced with appropriate values.
1537e14e8a7SBarry Smith 
15499cd5145SBarry Smith .keywords: Mat, register
1557e14e8a7SBarry Smith 
15699cd5145SBarry Smith .seealso: MatRegisterAll(), MatRegisterDestroy(), MatRegister()
1577e14e8a7SBarry Smith 
1587e14e8a7SBarry Smith M*/
1597e14e8a7SBarry Smith 
1607e14e8a7SBarry Smith #undef __FUNC__
16199cd5145SBarry Smith #define __FUNC__ "MatRegister"
16299cd5145SBarry Smith int MatRegister(char *sname,char *path,char *name,int (*function)(Mat))
1637e14e8a7SBarry Smith {
1647e14e8a7SBarry Smith   int  ierr;
1657e14e8a7SBarry Smith   char fullname[256];
1667e14e8a7SBarry Smith 
1677e14e8a7SBarry Smith   PetscFunctionBegin;
16899cd5145SBarry Smith   ierr = FListConcat(path,name,fullname);CHKERRQ(ierr);
16999cd5145SBarry Smith   ierr = FListAdd(&MatList,sname,fullname,(int (*)(void*))function);CHKERRQ(ierr);
17099cd5145SBarry Smith   PetscFunctionReturn(0);
17199cd5145SBarry Smith }
17299cd5145SBarry Smith 
17399cd5145SBarry Smith 
17499cd5145SBarry Smith 
17599cd5145SBarry Smith 
176*273d9f13SBarry Smith 
177*273d9f13SBarry Smith 
178*273d9f13SBarry Smith 
179*273d9f13SBarry Smith 
180*273d9f13SBarry Smith 
181*273d9f13SBarry Smith 
182