17e14e8a7SBarry Smith #ifdef PETSC_RCS_HEADER 2*29bbc08cSBarry Smith static char vcid[] = "$Id: matreg.c,v 1.9 2000/09/26 15:24:59 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 @*/ 4199cd5145SBarry 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 57*29bbc08cSBarry 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 @*/ 11299cd5145SBarry 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 #undef __FUNC__ 12099cd5145SBarry Smith #define __FUNC__ "MatSetTypeFromOptions" 1217e14e8a7SBarry Smith /*@ 12299cd5145SBarry Smith MatSetTypeFromOptions - Sets Mat type from the options database, if not 1237e14e8a7SBarry Smith given then sets default. 1247e14e8a7SBarry Smith 12599cd5145SBarry Smith Collective on Mat 1267e14e8a7SBarry Smith 1277e14e8a7SBarry Smith Input Parameters: 12899cd5145SBarry Smith . Mat - the Krylov space context 1297e14e8a7SBarry Smith 1307e14e8a7SBarry Smith Level: developer 1317e14e8a7SBarry Smith 13299cd5145SBarry Smith .keywords: Mat, set, from, options, database 1337e14e8a7SBarry Smith 1344bbc92c1SBarry Smith .seealso: MatSetFromOptions(), SLESSetFromOptions() 1357e14e8a7SBarry Smith @*/ 13699cd5145SBarry Smith int MatSetTypeFromOptions(Mat mat) 1377e14e8a7SBarry Smith { 13899cd5145SBarry Smith int ierr; 1397e14e8a7SBarry Smith char method[256]; 14099cd5145SBarry Smith PetscTruth flg; 1417e14e8a7SBarry Smith 1427e14e8a7SBarry Smith PetscFunctionBegin; 14399cd5145SBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE); 1447e14e8a7SBarry Smith 14599cd5145SBarry Smith ierr = OptionsGetString(mat->prefix,"-mat_type",method,256,&flg); 1467e14e8a7SBarry Smith if (flg){ 14799cd5145SBarry Smith ierr = MatSetType(mat,method);CHKERRQ(ierr); 1487e14e8a7SBarry Smith } 1497e14e8a7SBarry Smith /* 1507e14e8a7SBarry Smith Set the type if it was never set. 1517e14e8a7SBarry Smith */ 15299cd5145SBarry Smith if (!mat->type_name) { 15399cd5145SBarry Smith ierr = MatSetType(mat,"mpiaij");CHKERRQ(ierr); 1547e14e8a7SBarry Smith } 1557e14e8a7SBarry Smith PetscFunctionReturn(0); 1567e14e8a7SBarry Smith } 1577e14e8a7SBarry Smith 1587e14e8a7SBarry Smith /*MC 15999cd5145SBarry Smith MatRegisterDynamic - Adds a new matrix type 1607e14e8a7SBarry Smith 1617e14e8a7SBarry Smith Synopsis: 16299cd5145SBarry Smith MatRegisterDynamic(char *name,char *path,char *name_create,int (*routine_create)(Mat)) 1637e14e8a7SBarry Smith 1647e14e8a7SBarry Smith Not Collective 1657e14e8a7SBarry Smith 1667e14e8a7SBarry Smith Input Parameters: 16799cd5145SBarry Smith + name - name of a new user-defined matrix type 1687e14e8a7SBarry Smith . path - path (either absolute or relative) the library containing this solver 1697e14e8a7SBarry Smith . name_create - name of routine to create method context 1707e14e8a7SBarry Smith - routine_create - routine to create method context 1717e14e8a7SBarry Smith 1727e14e8a7SBarry Smith Notes: 17399cd5145SBarry Smith MatRegister() may be called multiple times to add several user-defined solvers. 1747e14e8a7SBarry Smith 1757e14e8a7SBarry Smith If dynamic libraries are used, then the fourth input argument (routine_create) 1767e14e8a7SBarry Smith is ignored. 1777e14e8a7SBarry Smith 1787e14e8a7SBarry Smith Sample usage: 1797e14e8a7SBarry Smith .vb 18099cd5145SBarry Smith MatRegisterDynamic("my_mat",/home/username/my_lib/lib/libO/solaris/mylib.a, 18199cd5145SBarry Smith "MyMatCreate",MyMatCreate); 1827e14e8a7SBarry Smith .ve 1837e14e8a7SBarry Smith 1847e14e8a7SBarry Smith Then, your solver can be chosen with the procedural interface via 18599cd5145SBarry Smith $ MatSetType(Mat,"my_mat") 1867e14e8a7SBarry Smith or at runtime via the option 18799cd5145SBarry Smith $ -mat_type my_mat 1887e14e8a7SBarry Smith 1897e14e8a7SBarry Smith Level: advanced 1907e14e8a7SBarry Smith 19199cd5145SBarry Smith ${PETSC_ARCH} and ${BOPT} occuring in pathname will be replaced with appropriate values. 1927e14e8a7SBarry Smith 19399cd5145SBarry Smith .keywords: Mat, register 1947e14e8a7SBarry Smith 19599cd5145SBarry Smith .seealso: MatRegisterAll(), MatRegisterDestroy(), MatRegister() 1967e14e8a7SBarry Smith 1977e14e8a7SBarry Smith M*/ 1987e14e8a7SBarry Smith 1997e14e8a7SBarry Smith #undef __FUNC__ 20099cd5145SBarry Smith #define __FUNC__ "MatRegister" 20199cd5145SBarry Smith int MatRegister(char *sname,char *path,char *name,int (*function)(Mat)) 2027e14e8a7SBarry Smith { 2037e14e8a7SBarry Smith int ierr; 2047e14e8a7SBarry Smith char fullname[256]; 2057e14e8a7SBarry Smith 2067e14e8a7SBarry Smith PetscFunctionBegin; 20799cd5145SBarry Smith ierr = FListConcat(path,name,fullname);CHKERRQ(ierr); 20899cd5145SBarry Smith ierr = FListAdd(&MatList,sname,fullname,(int (*)(void*))function);CHKERRQ(ierr); 20999cd5145SBarry Smith PetscFunctionReturn(0); 21099cd5145SBarry Smith } 21199cd5145SBarry Smith 21299cd5145SBarry Smith #undef __FUNC__ 21399cd5145SBarry Smith #define __FUNC__ "MATCreate" 21499cd5145SBarry Smith int MATCreate(MPI_Comm comm,int m,int n,int M,int N,Mat *A) 21599cd5145SBarry Smith { 21699cd5145SBarry Smith Mat B; 21799cd5145SBarry Smith 21899cd5145SBarry Smith PetscFunctionBegin; 21999cd5145SBarry Smith PetscHeaderCreate(B,_p_Mat,struct _MatOps,MAT_COOKIE,0,"Mat",comm,MatDestroy,MatView); 22099cd5145SBarry Smith PLogObjectCreate(B); 22199cd5145SBarry Smith 22299cd5145SBarry Smith B->m = m; 223b9b97703SBarry Smith B->n = n; 22499cd5145SBarry Smith B->M = M; 22599cd5145SBarry Smith B->N = N; 22699cd5145SBarry Smith 22799cd5145SBarry Smith *A = B; 2287e14e8a7SBarry Smith PetscFunctionReturn(0); 2297e14e8a7SBarry Smith } 230