xref: /petsc/src/mat/interface/matreg.c (revision ce94432eddcd14845bc7e8083b7f8ea723b9bf7d)
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*/
67e14e8a7SBarry Smith 
7ace3abfcSBarry Smith PetscBool MatRegisterAllCalled = PETSC_FALSE;
87e14e8a7SBarry Smith 
97e14e8a7SBarry Smith /*
1099cd5145SBarry Smith    Contains the list of registered Mat routines
117e14e8a7SBarry Smith */
12140e18c1SBarry Smith PetscFunctionList MatList = 0;
137e14e8a7SBarry Smith 
144a2ae208SSatish Balay #undef __FUNCT__
154a2ae208SSatish Balay #define __FUNCT__ "MatSetType"
167e14e8a7SBarry Smith /*@C
1799cd5145SBarry Smith    MatSetType - Builds matrix object for a particular matrix type
187e14e8a7SBarry Smith 
1999cd5145SBarry Smith    Collective on Mat
207e14e8a7SBarry Smith 
217e14e8a7SBarry Smith    Input Parameters:
2299cd5145SBarry Smith +  mat      - the matrix object
2399cd5145SBarry Smith -  matype   - matrix type
247e14e8a7SBarry Smith 
257e14e8a7SBarry Smith    Options Database Key:
2699cd5145SBarry Smith .  -mat_type  <method> - Sets the type; use -help for a list
2799cd5145SBarry Smith     of available methods (for instance, seqaij)
287e14e8a7SBarry Smith 
297e14e8a7SBarry Smith    Notes:
3099cd5145SBarry Smith    See "${PETSC_DIR}/include/petscmat.h" for available methods
317e14e8a7SBarry Smith 
327e14e8a7SBarry Smith   Level: intermediate
337e14e8a7SBarry Smith 
34c2bf8781Svictorle .keywords: Mat, MatType, set, method
357e14e8a7SBarry Smith 
366e0d5acbSBarry Smith .seealso: PCSetType(), VecSetType(), MatCreate(), MatType, Mat
377e14e8a7SBarry Smith @*/
3819fd82e9SBarry Smith PetscErrorCode  MatSetType(Mat mat, MatType matype)
397e14e8a7SBarry Smith {
40dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(Mat);
4101bebe75SBarry Smith   PetscBool      sametype,found;
4201bebe75SBarry Smith   MatBaseName    names = MatBaseNameList;
437e14e8a7SBarry Smith 
447e14e8a7SBarry Smith   PetscFunctionBegin;
450700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
46117016b1SBarry Smith 
4701bebe75SBarry Smith   while (names) {
4801bebe75SBarry Smith     ierr = PetscStrcmp(matype,names->bname,&found);CHKERRQ(ierr);
4901bebe75SBarry Smith     if (found) {
5001bebe75SBarry Smith       PetscMPIInt size;
51*ce94432eSBarry Smith       ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRQ(ierr);
5201bebe75SBarry Smith       if (size == 1) matype = names->sname;
5301bebe75SBarry Smith       else matype = names->mname;
5401bebe75SBarry Smith       break;
5501bebe75SBarry Smith     }
5601bebe75SBarry Smith     names = names->next;
5701bebe75SBarry Smith   }
5801bebe75SBarry Smith 
59251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)mat,matype,&sametype);CHKERRQ(ierr);
6092ff6ae8SBarry Smith   if (sametype) PetscFunctionReturn(0);
6192ff6ae8SBarry Smith 
62*ce94432eSBarry Smith   ierr =  PetscFunctionListFind(PetscObjectComm((PetscObject)mat),MatList,matype,PETSC_TRUE,(void(**)(void))&r);CHKERRQ(ierr);
63e32f2f54SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown Mat type given: %s",matype);
647e14e8a7SBarry Smith 
6535d8aa7fSBarry Smith   /* free the old data structure if it existed */
6635d8aa7fSBarry Smith   if (mat->ops->destroy) {
6735d8aa7fSBarry Smith     ierr = (*mat->ops->destroy)(mat);CHKERRQ(ierr);
6826fbe8dcSKarl Rupp 
690298fd71SBarry Smith     mat->ops->destroy = NULL;
7035d8aa7fSBarry Smith   }
7167d6de01SJed Brown   mat->preallocated = PETSC_FALSE;
72a2ec6df8SKris Buschelman 
7335d8aa7fSBarry Smith   /* create the new data structure */
7499cd5145SBarry Smith   ierr = (*r)(mat);CHKERRQ(ierr);
759fb22e1aSBarry Smith #if defined(PETSC_HAVE_AMS)
769fb22e1aSBarry Smith   if (PetscAMSPublishAll) {
7742eaa7f3SBarry Smith     /*    ierr = PetscObjectAMSPublish((PetscObject)mat);CHKERRQ(ierr); */
789fb22e1aSBarry Smith   }
799fb22e1aSBarry Smith #endif
807e14e8a7SBarry Smith   PetscFunctionReturn(0);
817e14e8a7SBarry Smith }
827e14e8a7SBarry Smith 
8335d8aa7fSBarry Smith 
844a2ae208SSatish Balay #undef __FUNCT__
854a2ae208SSatish Balay #define __FUNCT__ "MatRegisterDestroy"
867e14e8a7SBarry Smith /*@C
8799cd5145SBarry Smith    MatRegisterDestroy - Frees the list of matrix types that were
883cea93caSBarry Smith    registered by MatRegister()/MatRegisterDynamic().
897e14e8a7SBarry Smith 
907e14e8a7SBarry Smith    Not Collective
917e14e8a7SBarry Smith 
927e14e8a7SBarry Smith    Level: advanced
937e14e8a7SBarry Smith 
9499cd5145SBarry Smith .keywords: Mat, register, destroy
957e14e8a7SBarry Smith 
963cea93caSBarry Smith .seealso: MatRegister(), MatRegisterAll(), MatRegisterDynamic()
977e14e8a7SBarry Smith @*/
987087cfbeSBarry Smith PetscErrorCode  MatRegisterDestroy(void)
997e14e8a7SBarry Smith {
100dfbe8321SBarry Smith   PetscErrorCode ierr;
1017e14e8a7SBarry Smith 
1027e14e8a7SBarry Smith   PetscFunctionBegin;
103140e18c1SBarry Smith   ierr = PetscFunctionListDestroy(&MatList);CHKERRQ(ierr);
10426fbe8dcSKarl Rupp 
1050e9836bfSBarry Smith   MatRegisterAllCalled = PETSC_FALSE;
1067e14e8a7SBarry Smith   PetscFunctionReturn(0);
1077e14e8a7SBarry Smith }
1087e14e8a7SBarry Smith 
1094a2ae208SSatish Balay #undef __FUNCT__
1104a2ae208SSatish Balay #define __FUNCT__ "MatGetType"
1117e14e8a7SBarry Smith /*@C
112f87b78b8SBarry Smith    MatGetType - Gets the matrix type as a string from the matrix object.
1137e14e8a7SBarry Smith 
1147e14e8a7SBarry Smith    Not Collective
1157e14e8a7SBarry Smith 
1167e14e8a7SBarry Smith    Input Parameter:
11799cd5145SBarry Smith .  mat - the matrix
1187e14e8a7SBarry Smith 
1197e14e8a7SBarry Smith    Output Parameter:
12099cd5145SBarry Smith .  name - name of matrix type
1217e14e8a7SBarry Smith 
1227e14e8a7SBarry Smith    Level: intermediate
1237e14e8a7SBarry Smith 
124c2bf8781Svictorle .keywords: Mat, MatType, get, method, name
1257e14e8a7SBarry Smith 
12699cd5145SBarry Smith .seealso: MatSetType()
1277e14e8a7SBarry Smith @*/
12819fd82e9SBarry Smith PetscErrorCode  MatGetType(Mat mat,MatType *type)
1297e14e8a7SBarry Smith {
1307e14e8a7SBarry Smith   PetscFunctionBegin;
1310700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
132c4e43342SLisandro Dalcin   PetscValidPointer(type,2);
1337adad957SLisandro Dalcin   *type = ((PetscObject)mat)->type_name;
1347e14e8a7SBarry Smith   PetscFunctionReturn(0);
1357e14e8a7SBarry Smith }
1367e14e8a7SBarry Smith 
1377e14e8a7SBarry Smith 
1384a2ae208SSatish Balay #undef __FUNCT__
1394a2ae208SSatish Balay #define __FUNCT__ "MatRegister"
1403cea93caSBarry Smith /*@C
1413cea93caSBarry Smith   MatRegister - See MatRegisterDynamic()
1423cea93caSBarry Smith 
1437f6c08e0SMatthew Knepley   Level: advanced
1443cea93caSBarry Smith @*/
1457087cfbeSBarry Smith PetscErrorCode  MatRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(Mat))
1467e14e8a7SBarry Smith {
147dfbe8321SBarry Smith   PetscErrorCode ierr;
148e10c49a3SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
1497e14e8a7SBarry Smith 
1507e14e8a7SBarry Smith   PetscFunctionBegin;
151140e18c1SBarry Smith   ierr = PetscFunctionListConcat(path,name,fullname);CHKERRQ(ierr);
152140e18c1SBarry Smith   ierr = PetscFunctionListAdd(PETSC_COMM_WORLD,&MatList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
15399cd5145SBarry Smith   PetscFunctionReturn(0);
15499cd5145SBarry Smith }
15599cd5145SBarry Smith 
15601bebe75SBarry Smith MatBaseName MatBaseNameList = 0;
15701bebe75SBarry Smith 
15801bebe75SBarry Smith #undef __FUNCT__
15901bebe75SBarry Smith #define __FUNCT__ "MatRegisterBaseName"
16001bebe75SBarry Smith /*@C
16101bebe75SBarry Smith       MatRegisterBaseName - Registers a name that can be used for either a sequential or its corresponding parallel matrix type.
16201bebe75SBarry Smith 
16301bebe75SBarry Smith   Input Parameters:
16401bebe75SBarry Smith +     bname - the basename, for example, MATAIJ
16501bebe75SBarry Smith .     sname - the name of the sequential matrix type, for example, MATSEQAIJ
16601bebe75SBarry Smith -     mname - the name of the parallel matrix type, for example, MATMPIAIJ
16701bebe75SBarry Smith 
16801bebe75SBarry Smith 
16901bebe75SBarry Smith   Level: advanced
17001bebe75SBarry Smith @*/
17101bebe75SBarry Smith PetscErrorCode  MatRegisterBaseName(const char bname[],const char sname[],const char mname[])
17201bebe75SBarry Smith {
17301bebe75SBarry Smith   PetscErrorCode ierr;
17401bebe75SBarry Smith   MatBaseName    names;
17501bebe75SBarry Smith 
17601bebe75SBarry Smith   PetscFunctionBegin;
17701bebe75SBarry Smith   ierr = PetscNew(struct _p_MatBaseName,&names);CHKERRQ(ierr);
17801bebe75SBarry Smith   ierr = PetscStrallocpy(bname,&names->bname);CHKERRQ(ierr);
17901bebe75SBarry Smith   ierr = PetscStrallocpy(sname,&names->sname);CHKERRQ(ierr);
18001bebe75SBarry Smith   ierr = PetscStrallocpy(mname,&names->mname);CHKERRQ(ierr);
18101bebe75SBarry Smith   if (!MatBaseNameList) {
18201bebe75SBarry Smith     MatBaseNameList = names;
18301bebe75SBarry Smith   } else {
18401bebe75SBarry Smith     MatBaseName next = MatBaseNameList;
18501bebe75SBarry Smith     while (next->next) next = next->next;
18601bebe75SBarry Smith     next->next = names;
18701bebe75SBarry Smith   }
18801bebe75SBarry Smith   PetscFunctionReturn(0);
18901bebe75SBarry Smith }
19001bebe75SBarry Smith 
19199cd5145SBarry Smith 
192273d9f13SBarry Smith 
193273d9f13SBarry Smith 
194273d9f13SBarry Smith 
195273d9f13SBarry Smith 
196273d9f13SBarry Smith 
197