xref: /petsc/src/mat/interface/matreg.c (revision 7adad9577d7f34aa90d02da3975546c0571d04a7)
1be1d678aSKris Buschelman #define PETSCMAT_DLL
2be1d678aSKris Buschelman 
37e14e8a7SBarry Smith /*
499cd5145SBarry Smith      Mechanism for register PETSc matrix types
57e14e8a7SBarry Smith */
6b9147fbbSdalcinl #include "include/private/matimpl.h"      /*I "petscmat.h" I*/
799cd5145SBarry Smith #include "petscsys.h"
87e14e8a7SBarry Smith 
991af72abSBarry Smith PetscTruth MatRegisterAllCalled = PETSC_FALSE;
107e14e8a7SBarry Smith 
117e14e8a7SBarry Smith /*
1299cd5145SBarry Smith    Contains the list of registered Mat routines
137e14e8a7SBarry Smith */
14b0a32e0cSBarry Smith PetscFList MatList = 0;
157e14e8a7SBarry Smith 
164a2ae208SSatish Balay #undef __FUNCT__
174a2ae208SSatish Balay #define __FUNCT__ "MatSetType"
187e14e8a7SBarry Smith /*@C
1999cd5145SBarry Smith    MatSetType - Builds matrix object for a particular matrix type
207e14e8a7SBarry Smith 
2199cd5145SBarry Smith    Collective on Mat
227e14e8a7SBarry Smith 
237e14e8a7SBarry Smith    Input Parameters:
2499cd5145SBarry Smith +  mat      - the matrix object
2599cd5145SBarry Smith -  matype   - matrix type
267e14e8a7SBarry Smith 
277e14e8a7SBarry Smith    Options Database Key:
2899cd5145SBarry Smith .  -mat_type  <method> - Sets the type; use -help for a list
2999cd5145SBarry Smith     of available methods (for instance, seqaij)
307e14e8a7SBarry Smith 
317e14e8a7SBarry Smith    Notes:
3299cd5145SBarry Smith    See "${PETSC_DIR}/include/petscmat.h" for available methods
337e14e8a7SBarry Smith 
347e14e8a7SBarry Smith   Level: intermediate
357e14e8a7SBarry Smith 
36c2bf8781Svictorle .keywords: Mat, MatType, set, method
377e14e8a7SBarry Smith 
386e0d5acbSBarry Smith .seealso: PCSetType(), VecSetType(), MatCreate(), MatType, Mat
397e14e8a7SBarry Smith @*/
40f69a0ea3SMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatSetType(Mat mat, MatType matype)
417e14e8a7SBarry Smith {
42dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(Mat);
4399cd5145SBarry Smith   PetscTruth     sametype;
447e14e8a7SBarry Smith 
457e14e8a7SBarry Smith   PetscFunctionBegin;
464482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
47899cda47SBarry Smith   if (mat->rmap.n < 0 && mat->rmap.N < 0 && mat->cmap.n < 0 && mat->cmap.N < 0) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call MatSetSizes() first");
4899cd5145SBarry Smith   ierr = PetscTypeCompare((PetscObject)mat,matype,&sametype);CHKERRQ(ierr);
4992ff6ae8SBarry Smith   if (sametype) PetscFunctionReturn(0);
5092ff6ae8SBarry Smith 
51*7adad957SLisandro Dalcin   ierr =  PetscFListFind(MatList,((PetscObject)mat)->comm,matype,(void(**)(void))&r);CHKERRQ(ierr);
52958c9bccSBarry Smith   if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown Mat type given: %s",matype);
537e14e8a7SBarry Smith 
5435d8aa7fSBarry Smith   /* free the old data structure if it existed */
5535d8aa7fSBarry Smith   if (mat->ops->destroy) {
56b50b34bdSBarry Smith     ierr = MatPreallocated(mat);CHKERRQ(ierr);
5735d8aa7fSBarry Smith     ierr = (*mat->ops->destroy)(mat);CHKERRQ(ierr);
580dd8e64fSKris Buschelman     mat->ops->destroy = PETSC_NULL;
59a2ec6df8SKris Buschelman     mat->preallocated = PETSC_FALSE;
6035d8aa7fSBarry Smith   }
61a2ec6df8SKris Buschelman 
6235d8aa7fSBarry Smith   /* create the new data structure */
6399cd5145SBarry Smith   ierr = (*r)(mat);CHKERRQ(ierr);
6435d8aa7fSBarry Smith   ierr = PetscPublishAll(mat);CHKERRQ(ierr);
657e14e8a7SBarry Smith   PetscFunctionReturn(0);
667e14e8a7SBarry Smith }
677e14e8a7SBarry Smith 
6835d8aa7fSBarry Smith 
694a2ae208SSatish Balay #undef __FUNCT__
704a2ae208SSatish Balay #define __FUNCT__ "MatRegisterDestroy"
717e14e8a7SBarry Smith /*@C
7299cd5145SBarry Smith    MatRegisterDestroy - Frees the list of matrix types that were
733cea93caSBarry Smith    registered by MatRegister()/MatRegisterDynamic().
747e14e8a7SBarry Smith 
757e14e8a7SBarry Smith    Not Collective
767e14e8a7SBarry Smith 
777e14e8a7SBarry Smith    Level: advanced
787e14e8a7SBarry Smith 
7999cd5145SBarry Smith .keywords: Mat, register, destroy
807e14e8a7SBarry Smith 
813cea93caSBarry Smith .seealso: MatRegister(), MatRegisterAll(), MatRegisterDynamic()
827e14e8a7SBarry Smith @*/
83be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRegisterDestroy(void)
847e14e8a7SBarry Smith {
85dfbe8321SBarry Smith   PetscErrorCode ierr;
867e14e8a7SBarry Smith 
877e14e8a7SBarry Smith   PetscFunctionBegin;
881441b1d3SBarry Smith   ierr = PetscFListDestroy(&MatList);CHKERRQ(ierr);
890e9836bfSBarry Smith   MatRegisterAllCalled = PETSC_FALSE;
907e14e8a7SBarry Smith   PetscFunctionReturn(0);
917e14e8a7SBarry Smith }
927e14e8a7SBarry Smith 
934a2ae208SSatish Balay #undef __FUNCT__
944a2ae208SSatish Balay #define __FUNCT__ "MatGetType"
957e14e8a7SBarry Smith /*@C
96f87b78b8SBarry Smith    MatGetType - Gets the matrix 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 
108c2bf8781Svictorle .keywords: Mat, MatType, get, method, name
1097e14e8a7SBarry Smith 
11099cd5145SBarry Smith .seealso: MatSetType()
1117e14e8a7SBarry Smith @*/
112be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatGetType(Mat mat,MatType *type)
1137e14e8a7SBarry Smith {
1147e14e8a7SBarry Smith   PetscFunctionBegin;
115*7adad957SLisandro Dalcin   *type = ((PetscObject)mat)->type_name;
1167e14e8a7SBarry Smith   PetscFunctionReturn(0);
1177e14e8a7SBarry Smith }
1187e14e8a7SBarry Smith 
1197e14e8a7SBarry Smith 
1204a2ae208SSatish Balay #undef __FUNCT__
1214a2ae208SSatish Balay #define __FUNCT__ "MatRegister"
1223cea93caSBarry Smith /*@C
1233cea93caSBarry Smith   MatRegister - See MatRegisterDynamic()
1243cea93caSBarry Smith 
1257f6c08e0SMatthew Knepley   Level: advanced
1263cea93caSBarry Smith @*/
127be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(Mat))
1287e14e8a7SBarry Smith {
129dfbe8321SBarry Smith   PetscErrorCode ierr;
130e10c49a3SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
1317e14e8a7SBarry Smith 
1327e14e8a7SBarry Smith   PetscFunctionBegin;
133b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
134c134de8dSSatish Balay   ierr = PetscFListAdd(&MatList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
13599cd5145SBarry Smith   PetscFunctionReturn(0);
13699cd5145SBarry Smith }
13799cd5145SBarry Smith 
13899cd5145SBarry Smith 
13999cd5145SBarry Smith 
14099cd5145SBarry Smith 
141273d9f13SBarry Smith 
142273d9f13SBarry Smith 
143273d9f13SBarry Smith 
144273d9f13SBarry Smith 
145273d9f13SBarry Smith 
146273d9f13SBarry Smith 
147