xref: /petsc/src/mat/interface/matreg.c (revision be1d678a52e6eff2808b2fa31ae986cdbf03c9fe)
1*be1d678aSKris Buschelman #define PETSCMAT_DLL
2*be1d678aSKris Buschelman 
37e14e8a7SBarry Smith /*
499cd5145SBarry Smith      Mechanism for register PETSc matrix types
57e14e8a7SBarry Smith */
6aa59fb91SSatish Balay #include "src/mat/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 @*/
40*be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSetType(Mat mat,const 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);
47c71e6ed7SBarry Smith   if (mat->m < 0 && mat->M < 0 && mat->n < 0 && mat->N < 0) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call MatSetSizes() first");
4899cd5145SBarry Smith   ierr = PetscTypeCompare((PetscObject)mat,matype,&sametype);CHKERRQ(ierr);
4935d8aa7fSBarry Smith   if (!sametype) {
5099cd5145SBarry Smith     /* Get the function pointers for the matrix requested */
5199cd5145SBarry Smith     if (!MatRegisterAllCalled) {ierr = MatRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
52c134de8dSSatish Balay     ierr =  PetscFListFind(mat->comm,MatList,matype,(void(**)(void))&r);CHKERRQ(ierr);
53958c9bccSBarry Smith     if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown Mat type given: %s",matype);
547e14e8a7SBarry Smith 
5535d8aa7fSBarry Smith     /* free the old data structure if it existed */
5635d8aa7fSBarry Smith     if (mat->ops->destroy) {
57a2ec6df8SKris Buschelman       MatPreallocated(mat);
5835d8aa7fSBarry Smith       ierr = (*mat->ops->destroy)(mat);CHKERRQ(ierr);
590dd8e64fSKris Buschelman       mat->ops->destroy = PETSC_NULL;
60a2ec6df8SKris Buschelman       mat->preallocated = PETSC_FALSE;
6135d8aa7fSBarry Smith     }
62a2ec6df8SKris Buschelman 
6335d8aa7fSBarry Smith     if (mat->rmap) {
648a124369SBarry Smith       ierr = PetscMapDestroy(mat->rmap);CHKERRQ(ierr);
6535d8aa7fSBarry Smith       mat->rmap = 0;
6635d8aa7fSBarry Smith     }
6735d8aa7fSBarry Smith     if (mat->cmap) {
688a124369SBarry Smith       ierr = PetscMapDestroy(mat->cmap);CHKERRQ(ierr);
6935d8aa7fSBarry Smith       mat->cmap = 0;
7035d8aa7fSBarry Smith     }
7135d8aa7fSBarry Smith     /* create the new data structure */
7299cd5145SBarry Smith     ierr = (*r)(mat);CHKERRQ(ierr);
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 
804a2ae208SSatish Balay #undef __FUNCT__
814a2ae208SSatish Balay #define __FUNCT__ "MatRegisterDestroy"
827e14e8a7SBarry Smith /*@C
8399cd5145SBarry Smith    MatRegisterDestroy - Frees the list of matrix types that were
843cea93caSBarry Smith    registered by MatRegister()/MatRegisterDynamic().
857e14e8a7SBarry Smith 
867e14e8a7SBarry Smith    Not Collective
877e14e8a7SBarry Smith 
887e14e8a7SBarry Smith    Level: advanced
897e14e8a7SBarry Smith 
9099cd5145SBarry Smith .keywords: Mat, register, destroy
917e14e8a7SBarry Smith 
923cea93caSBarry Smith .seealso: MatRegister(), MatRegisterAll(), MatRegisterDynamic()
937e14e8a7SBarry Smith @*/
94*be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRegisterDestroy(void)
957e14e8a7SBarry Smith {
96dfbe8321SBarry Smith   PetscErrorCode ierr;
977e14e8a7SBarry Smith 
987e14e8a7SBarry Smith   PetscFunctionBegin;
9999cd5145SBarry Smith   if (MatList) {
100b0a32e0cSBarry 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 
1074a2ae208SSatish Balay #undef __FUNCT__
1084a2ae208SSatish Balay #define __FUNCT__ "MatGetType"
1097e14e8a7SBarry Smith /*@C
110f87b78b8SBarry Smith    MatGetType - Gets the matrix 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 
122c2bf8781Svictorle .keywords: Mat, MatType, get, method, name
1237e14e8a7SBarry Smith 
12499cd5145SBarry Smith .seealso: MatSetType()
1257e14e8a7SBarry Smith @*/
126*be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT 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 
1344a2ae208SSatish Balay #undef __FUNCT__
1354a2ae208SSatish Balay #define __FUNCT__ "MatRegister"
1363cea93caSBarry Smith /*@C
1373cea93caSBarry Smith   MatRegister - See MatRegisterDynamic()
1383cea93caSBarry Smith 
1397f6c08e0SMatthew Knepley   Level: advanced
1403cea93caSBarry Smith @*/
141*be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(Mat))
1427e14e8a7SBarry Smith {
143dfbe8321SBarry Smith   PetscErrorCode ierr;
144e10c49a3SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
1457e14e8a7SBarry Smith 
1467e14e8a7SBarry Smith   PetscFunctionBegin;
147b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
148c134de8dSSatish Balay   ierr = PetscFListAdd(&MatList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
14999cd5145SBarry Smith   PetscFunctionReturn(0);
15099cd5145SBarry Smith }
15199cd5145SBarry Smith 
15299cd5145SBarry Smith 
15399cd5145SBarry Smith 
15499cd5145SBarry Smith 
155273d9f13SBarry Smith 
156273d9f13SBarry Smith 
157273d9f13SBarry Smith 
158273d9f13SBarry Smith 
159273d9f13SBarry Smith 
160273d9f13SBarry Smith 
161