17e14e8a7SBarry Smith #ifdef PETSC_RCS_HEADER 2*b0a32e0cSBarry Smith static char vcid[] = "$Id: matreg.c,v 1.12 2000/11/28 17:28:46 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 */ 15*b0a32e0cSBarry Smith PetscFList 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 @*/ 41273d9f13SBarry 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); 5035d8aa7fSBarry Smith if (!sametype) { 517e14e8a7SBarry Smith 5299cd5145SBarry Smith /* Get the function pointers for the matrix requested */ 5399cd5145SBarry Smith if (!MatRegisterAllCalled) {ierr = MatRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 54*b0a32e0cSBarry Smith ierr = PetscFListFind(mat->comm,MatList,matype,(int(**)(void*))&r);CHKERRQ(ierr); 5529bbc08cSBarry Smith if (!r) SETERRQ1(1,"Unknown Mat type given: %s",matype); 567e14e8a7SBarry Smith 5735d8aa7fSBarry Smith /* free the old data structure if it existed */ 5835d8aa7fSBarry Smith if (mat->ops->destroy) { 5935d8aa7fSBarry Smith ierr = (*mat->ops->destroy)(mat);CHKERRQ(ierr); 6035d8aa7fSBarry Smith } 6135d8aa7fSBarry Smith if (mat->rmap) { 6235d8aa7fSBarry Smith ierr = MapDestroy(mat->rmap);CHKERRQ(ierr); 6335d8aa7fSBarry Smith mat->rmap = 0; 6435d8aa7fSBarry Smith } 6535d8aa7fSBarry Smith if (mat->cmap) { 6635d8aa7fSBarry Smith ierr = MapDestroy(mat->cmap);CHKERRQ(ierr); 6735d8aa7fSBarry Smith mat->cmap = 0; 6835d8aa7fSBarry Smith } 6935d8aa7fSBarry Smith 7035d8aa7fSBarry Smith /* create the new data structure */ 7199cd5145SBarry Smith ierr = (*r)(mat);CHKERRQ(ierr); 727e14e8a7SBarry Smith 73b9b97703SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)mat,matype);CHKERRQ(ierr); 7435d8aa7fSBarry Smith } 7535d8aa7fSBarry Smith ierr = PetscPublishAll(mat);CHKERRQ(ierr); 767e14e8a7SBarry Smith PetscFunctionReturn(0); 777e14e8a7SBarry Smith } 787e14e8a7SBarry Smith 7935d8aa7fSBarry Smith 807e14e8a7SBarry Smith #undef __FUNC__ 8199cd5145SBarry Smith #define __FUNC__ "MatRegisterDestroy" 827e14e8a7SBarry Smith /*@C 8399cd5145SBarry Smith MatRegisterDestroy - Frees the list of matrix types that were 8499cd5145SBarry Smith registered by MatRegister(). 857e14e8a7SBarry Smith 867e14e8a7SBarry Smith Not Collective 877e14e8a7SBarry Smith 887e14e8a7SBarry Smith Level: advanced 897e14e8a7SBarry Smith 9099cd5145SBarry Smith .keywords: Mat, register, destroy 917e14e8a7SBarry Smith 9299cd5145SBarry Smith .seealso: MatRegister(), MatRegisterAll() 937e14e8a7SBarry Smith @*/ 9499cd5145SBarry Smith int MatRegisterDestroy(void) 957e14e8a7SBarry Smith { 967e14e8a7SBarry Smith int ierr; 977e14e8a7SBarry Smith 987e14e8a7SBarry Smith PetscFunctionBegin; 9999cd5145SBarry Smith if (MatList) { 100*b0a32e0cSBarry Smith ierr = PetscFListDestroy(&MatList);CHKERRQ(ierr); 10199cd5145SBarry Smith MatList = 0; 1027e14e8a7SBarry Smith } 1030e9836bfSBarry Smith MatRegisterAllCalled = PETSC_FALSE; 1047e14e8a7SBarry Smith PetscFunctionReturn(0); 1057e14e8a7SBarry Smith } 1067e14e8a7SBarry Smith 1077e14e8a7SBarry Smith #undef __FUNC__ 10899cd5145SBarry Smith #define __FUNC__ "MatGetType" 1097e14e8a7SBarry Smith /*@C 11099cd5145SBarry Smith MatGetType - Gets the matrx type as a string from the matrix object. 1117e14e8a7SBarry Smith 1127e14e8a7SBarry Smith Not Collective 1137e14e8a7SBarry Smith 1147e14e8a7SBarry Smith Input Parameter: 11599cd5145SBarry Smith . mat - the matrix 1167e14e8a7SBarry Smith 1177e14e8a7SBarry Smith Output Parameter: 11899cd5145SBarry Smith . name - name of matrix type 1197e14e8a7SBarry Smith 1207e14e8a7SBarry Smith Level: intermediate 1217e14e8a7SBarry Smith 12299cd5145SBarry Smith .keywords: Mat, get, method, name 1237e14e8a7SBarry Smith 12499cd5145SBarry Smith .seealso: MatSetType() 1257e14e8a7SBarry Smith @*/ 126273d9f13SBarry Smith int MatGetType(Mat mat,MatType *type) 1277e14e8a7SBarry Smith { 1287e14e8a7SBarry Smith PetscFunctionBegin; 12999cd5145SBarry Smith *type = mat->type_name; 1307e14e8a7SBarry Smith PetscFunctionReturn(0); 1317e14e8a7SBarry Smith } 1327e14e8a7SBarry Smith 1337e14e8a7SBarry Smith /*MC 13499cd5145SBarry Smith MatRegisterDynamic - Adds a new matrix type 1357e14e8a7SBarry Smith 1367e14e8a7SBarry Smith Synopsis: 13799cd5145SBarry Smith MatRegisterDynamic(char *name,char *path,char *name_create,int (*routine_create)(Mat)) 1387e14e8a7SBarry Smith 1397e14e8a7SBarry Smith Not Collective 1407e14e8a7SBarry Smith 1417e14e8a7SBarry Smith Input Parameters: 14299cd5145SBarry Smith + name - name of a new user-defined matrix type 1437e14e8a7SBarry Smith . path - path (either absolute or relative) the library containing this solver 1447e14e8a7SBarry Smith . name_create - name of routine to create method context 1457e14e8a7SBarry Smith - routine_create - routine to create method context 1467e14e8a7SBarry Smith 1477e14e8a7SBarry Smith Notes: 14899cd5145SBarry Smith MatRegister() may be called multiple times to add several user-defined solvers. 1497e14e8a7SBarry Smith 1507e14e8a7SBarry Smith If dynamic libraries are used, then the fourth input argument (routine_create) 1517e14e8a7SBarry Smith is ignored. 1527e14e8a7SBarry Smith 1537e14e8a7SBarry Smith Sample usage: 1547e14e8a7SBarry Smith .vb 15599cd5145SBarry Smith MatRegisterDynamic("my_mat",/home/username/my_lib/lib/libO/solaris/mylib.a, 15699cd5145SBarry Smith "MyMatCreate",MyMatCreate); 1577e14e8a7SBarry Smith .ve 1587e14e8a7SBarry Smith 1597e14e8a7SBarry Smith Then, your solver can be chosen with the procedural interface via 16099cd5145SBarry Smith $ MatSetType(Mat,"my_mat") 1617e14e8a7SBarry Smith or at runtime via the option 16299cd5145SBarry Smith $ -mat_type my_mat 1637e14e8a7SBarry Smith 1647e14e8a7SBarry Smith Level: advanced 1657e14e8a7SBarry Smith 16699cd5145SBarry Smith ${PETSC_ARCH} and ${BOPT} occuring in pathname will be replaced with appropriate values. 1677e14e8a7SBarry Smith 16899cd5145SBarry Smith .keywords: Mat, register 1697e14e8a7SBarry Smith 17099cd5145SBarry Smith .seealso: MatRegisterAll(), MatRegisterDestroy(), MatRegister() 1717e14e8a7SBarry Smith 1727e14e8a7SBarry Smith M*/ 1737e14e8a7SBarry Smith 1747e14e8a7SBarry Smith #undef __FUNC__ 17599cd5145SBarry Smith #define __FUNC__ "MatRegister" 17699cd5145SBarry Smith int MatRegister(char *sname,char *path,char *name,int (*function)(Mat)) 1777e14e8a7SBarry Smith { 1787e14e8a7SBarry Smith int ierr; 1797e14e8a7SBarry Smith char fullname[256]; 1807e14e8a7SBarry Smith 1817e14e8a7SBarry Smith PetscFunctionBegin; 182*b0a32e0cSBarry Smith ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 183*b0a32e0cSBarry Smith ierr = PetscFListAdd(&MatList,sname,fullname,(int (*)(void*))function);CHKERRQ(ierr); 18499cd5145SBarry Smith PetscFunctionReturn(0); 18599cd5145SBarry Smith } 18699cd5145SBarry Smith 18799cd5145SBarry Smith 18899cd5145SBarry Smith 18999cd5145SBarry Smith 190273d9f13SBarry Smith 191273d9f13SBarry Smith 192273d9f13SBarry Smith 193273d9f13SBarry Smith 194273d9f13SBarry Smith 195273d9f13SBarry Smith 196