xref: /petsc/src/mat/interface/matreg.c (revision 26fbe8dc1a3c99fb8dddfa572c8c6b3b4ce3ca53)
1be1d678aSKris Buschelman 
27e14e8a7SBarry Smith /*
399cd5145SBarry Smith      Mechanism for register PETSc matrix types
47e14e8a7SBarry Smith */
5b45d2f2cSJed Brown #include <petsc-private/matimpl.h>      /*I "petscmat.h" I*/
6c0c8ee5eSDmitry Karpeev #include <stdarg.h> /* Variable-length arg lists. */
77e14e8a7SBarry Smith 
8ace3abfcSBarry Smith PetscBool MatRegisterAllCalled = PETSC_FALSE;
97e14e8a7SBarry Smith 
107e14e8a7SBarry Smith /*
1199cd5145SBarry Smith    Contains the list of registered Mat routines
127e14e8a7SBarry Smith */
13140e18c1SBarry Smith PetscFunctionList MatList = 0;
147e14e8a7SBarry Smith 
154a2ae208SSatish Balay #undef __FUNCT__
164a2ae208SSatish Balay #define __FUNCT__ "MatSetType"
177e14e8a7SBarry Smith /*@C
1899cd5145SBarry Smith    MatSetType - Builds matrix object for a particular matrix type
197e14e8a7SBarry Smith 
2099cd5145SBarry Smith    Collective on Mat
217e14e8a7SBarry Smith 
227e14e8a7SBarry Smith    Input Parameters:
2399cd5145SBarry Smith +  mat      - the matrix object
2499cd5145SBarry Smith -  matype   - matrix type
257e14e8a7SBarry Smith 
267e14e8a7SBarry Smith    Options Database Key:
2799cd5145SBarry Smith .  -mat_type  <method> - Sets the type; use -help for a list
2899cd5145SBarry Smith     of available methods (for instance, seqaij)
297e14e8a7SBarry Smith 
307e14e8a7SBarry Smith    Notes:
3199cd5145SBarry Smith    See "${PETSC_DIR}/include/petscmat.h" for available methods
327e14e8a7SBarry Smith 
337e14e8a7SBarry Smith   Level: intermediate
347e14e8a7SBarry Smith 
35c2bf8781Svictorle .keywords: Mat, MatType, set, method
367e14e8a7SBarry Smith 
376e0d5acbSBarry Smith .seealso: PCSetType(), VecSetType(), MatCreate(), MatType, Mat
387e14e8a7SBarry Smith @*/
3919fd82e9SBarry Smith PetscErrorCode  MatSetType(Mat mat, MatType matype)
407e14e8a7SBarry Smith {
41dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(Mat);
4201bebe75SBarry Smith   PetscBool      sametype,found;
4301bebe75SBarry Smith   MatBaseName    names = MatBaseNameList;
447e14e8a7SBarry Smith 
457e14e8a7SBarry Smith   PetscFunctionBegin;
460700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
47117016b1SBarry Smith 
4801bebe75SBarry Smith   while (names) {
4901bebe75SBarry Smith     ierr = PetscStrcmp(matype,names->bname,&found);CHKERRQ(ierr);
5001bebe75SBarry Smith     if (found) {
5101bebe75SBarry Smith       PetscMPIInt size;
5201bebe75SBarry Smith       ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr);
5301bebe75SBarry Smith       if (size == 1) matype = names->sname;
5401bebe75SBarry Smith       else matype = names->mname;
5501bebe75SBarry Smith       break;
5601bebe75SBarry Smith     }
5701bebe75SBarry Smith     names = names->next;
5801bebe75SBarry Smith   }
5901bebe75SBarry Smith 
60251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)mat,matype,&sametype);CHKERRQ(ierr);
6192ff6ae8SBarry Smith   if (sametype) PetscFunctionReturn(0);
6292ff6ae8SBarry Smith 
63140e18c1SBarry Smith   ierr =  PetscFunctionListFind(((PetscObject)mat)->comm,MatList,matype,PETSC_TRUE,(void(**)(void))&r);CHKERRQ(ierr);
64e32f2f54SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown Mat type given: %s",matype);
657e14e8a7SBarry Smith 
6635d8aa7fSBarry Smith   /* free the old data structure if it existed */
6735d8aa7fSBarry Smith   if (mat->ops->destroy) {
6835d8aa7fSBarry Smith     ierr = (*mat->ops->destroy)(mat);CHKERRQ(ierr);
69*26fbe8dcSKarl Rupp 
700dd8e64fSKris Buschelman     mat->ops->destroy = PETSC_NULL;
7135d8aa7fSBarry Smith   }
7267d6de01SJed Brown   mat->preallocated = PETSC_FALSE;
73a2ec6df8SKris Buschelman 
7435d8aa7fSBarry Smith   /* create the new data structure */
7599cd5145SBarry Smith   ierr = (*r)(mat);CHKERRQ(ierr);
769fb22e1aSBarry Smith #if defined(PETSC_HAVE_AMS)
779fb22e1aSBarry Smith   if (PetscAMSPublishAll) {
7842eaa7f3SBarry Smith     /*    ierr = PetscObjectAMSPublish((PetscObject)mat);CHKERRQ(ierr); */
799fb22e1aSBarry Smith   }
809fb22e1aSBarry Smith #endif
817e14e8a7SBarry Smith   PetscFunctionReturn(0);
827e14e8a7SBarry Smith }
837e14e8a7SBarry Smith 
8435d8aa7fSBarry Smith 
854a2ae208SSatish Balay #undef __FUNCT__
864a2ae208SSatish Balay #define __FUNCT__ "MatRegisterDestroy"
877e14e8a7SBarry Smith /*@C
8899cd5145SBarry Smith    MatRegisterDestroy - Frees the list of matrix types that were
893cea93caSBarry Smith    registered by MatRegister()/MatRegisterDynamic().
907e14e8a7SBarry Smith 
917e14e8a7SBarry Smith    Not Collective
927e14e8a7SBarry Smith 
937e14e8a7SBarry Smith    Level: advanced
947e14e8a7SBarry Smith 
9599cd5145SBarry Smith .keywords: Mat, register, destroy
967e14e8a7SBarry Smith 
973cea93caSBarry Smith .seealso: MatRegister(), MatRegisterAll(), MatRegisterDynamic()
987e14e8a7SBarry Smith @*/
997087cfbeSBarry Smith PetscErrorCode  MatRegisterDestroy(void)
1007e14e8a7SBarry Smith {
101dfbe8321SBarry Smith   PetscErrorCode ierr;
1027e14e8a7SBarry Smith 
1037e14e8a7SBarry Smith   PetscFunctionBegin;
104140e18c1SBarry Smith   ierr = PetscFunctionListDestroy(&MatList);CHKERRQ(ierr);
105*26fbe8dcSKarl Rupp 
1060e9836bfSBarry Smith   MatRegisterAllCalled = PETSC_FALSE;
1077e14e8a7SBarry Smith   PetscFunctionReturn(0);
1087e14e8a7SBarry Smith }
1097e14e8a7SBarry Smith 
1104a2ae208SSatish Balay #undef __FUNCT__
1114a2ae208SSatish Balay #define __FUNCT__ "MatGetType"
1127e14e8a7SBarry Smith /*@C
113f87b78b8SBarry Smith    MatGetType - Gets the matrix type as a string from the matrix object.
1147e14e8a7SBarry Smith 
1157e14e8a7SBarry Smith    Not Collective
1167e14e8a7SBarry Smith 
1177e14e8a7SBarry Smith    Input Parameter:
11899cd5145SBarry Smith .  mat - the matrix
1197e14e8a7SBarry Smith 
1207e14e8a7SBarry Smith    Output Parameter:
12199cd5145SBarry Smith .  name - name of matrix type
1227e14e8a7SBarry Smith 
1237e14e8a7SBarry Smith    Level: intermediate
1247e14e8a7SBarry Smith 
125c2bf8781Svictorle .keywords: Mat, MatType, get, method, name
1267e14e8a7SBarry Smith 
12799cd5145SBarry Smith .seealso: MatSetType()
1287e14e8a7SBarry Smith @*/
12919fd82e9SBarry Smith PetscErrorCode  MatGetType(Mat mat,MatType *type)
1307e14e8a7SBarry Smith {
1317e14e8a7SBarry Smith   PetscFunctionBegin;
1320700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
133c4e43342SLisandro Dalcin   PetscValidPointer(type,2);
1347adad957SLisandro Dalcin   *type = ((PetscObject)mat)->type_name;
1357e14e8a7SBarry Smith   PetscFunctionReturn(0);
1367e14e8a7SBarry Smith }
1377e14e8a7SBarry Smith 
1387e14e8a7SBarry Smith 
1394a2ae208SSatish Balay #undef __FUNCT__
1404a2ae208SSatish Balay #define __FUNCT__ "MatRegister"
1413cea93caSBarry Smith /*@C
1423cea93caSBarry Smith   MatRegister - See MatRegisterDynamic()
1433cea93caSBarry Smith 
1447f6c08e0SMatthew Knepley   Level: advanced
1453cea93caSBarry Smith @*/
1467087cfbeSBarry Smith PetscErrorCode  MatRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(Mat))
1477e14e8a7SBarry Smith {
148dfbe8321SBarry Smith   PetscErrorCode ierr;
149e10c49a3SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
1507e14e8a7SBarry Smith 
1517e14e8a7SBarry Smith   PetscFunctionBegin;
152140e18c1SBarry Smith   ierr = PetscFunctionListConcat(path,name,fullname);CHKERRQ(ierr);
153140e18c1SBarry Smith   ierr = PetscFunctionListAdd(PETSC_COMM_WORLD,&MatList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
15499cd5145SBarry Smith   PetscFunctionReturn(0);
15599cd5145SBarry Smith }
15699cd5145SBarry Smith 
15701bebe75SBarry Smith MatBaseName MatBaseNameList = 0;
15801bebe75SBarry Smith 
15901bebe75SBarry Smith #undef __FUNCT__
16001bebe75SBarry Smith #define __FUNCT__ "MatRegisterBaseName"
16101bebe75SBarry Smith /*@C
16201bebe75SBarry Smith       MatRegisterBaseName - Registers a name that can be used for either a sequential or its corresponding parallel matrix type.
16301bebe75SBarry Smith 
16401bebe75SBarry Smith   Input Parameters:
16501bebe75SBarry Smith +     bname - the basename, for example, MATAIJ
16601bebe75SBarry Smith .     sname - the name of the sequential matrix type, for example, MATSEQAIJ
16701bebe75SBarry Smith -     mname - the name of the parallel matrix type, for example, MATMPIAIJ
16801bebe75SBarry Smith 
16901bebe75SBarry Smith 
17001bebe75SBarry Smith   Level: advanced
17101bebe75SBarry Smith @*/
17201bebe75SBarry Smith PetscErrorCode  MatRegisterBaseName(const char bname[],const char sname[],const char mname[])
17301bebe75SBarry Smith {
17401bebe75SBarry Smith   PetscErrorCode ierr;
17501bebe75SBarry Smith   MatBaseName    names;
17601bebe75SBarry Smith 
17701bebe75SBarry Smith   PetscFunctionBegin;
17801bebe75SBarry Smith   ierr = PetscNew(struct _p_MatBaseName,&names);CHKERRQ(ierr);
17901bebe75SBarry Smith   ierr = PetscStrallocpy(bname,&names->bname);CHKERRQ(ierr);
18001bebe75SBarry Smith   ierr = PetscStrallocpy(sname,&names->sname);CHKERRQ(ierr);
18101bebe75SBarry Smith   ierr = PetscStrallocpy(mname,&names->mname);CHKERRQ(ierr);
18201bebe75SBarry Smith   if (!MatBaseNameList) {
18301bebe75SBarry Smith     MatBaseNameList = names;
18401bebe75SBarry Smith   } else {
18501bebe75SBarry Smith     MatBaseName next = MatBaseNameList;
18601bebe75SBarry Smith     while (next->next) next = next->next;
18701bebe75SBarry Smith     next->next = names;
18801bebe75SBarry Smith   }
18901bebe75SBarry Smith   PetscFunctionReturn(0);
19001bebe75SBarry Smith }
19101bebe75SBarry Smith 
19299cd5145SBarry Smith 
193273d9f13SBarry Smith 
194273d9f13SBarry Smith 
195273d9f13SBarry Smith 
196273d9f13SBarry Smith 
197273d9f13SBarry Smith 
198