xref: /petsc/src/sys/objects/ptype.c (revision d8e47b638cf8f604a99e9678e1df24f82d959cd7)
1e5c89e4eSSatish Balay /*
2e5c89e4eSSatish Balay      Provides utility routines for manipulating any type of PETSc object.
3e5c89e4eSSatish Balay */
4c6db04a5SJed Brown #include <petscsys.h> /*I   "petscsys.h"    I*/
5e5c89e4eSSatish Balay 
6eb114d58SToby Isaac const char *const PetscDataTypes[] = {"UNKNOWN",  "DOUBLE", "COMPLEX", "LONG",   "SHORT", "FLOAT", "CHAR",  "BIT_LOGICAL", "ENUM",          "BOOL",   "__FLOAT128", "OBJECT",
7eb114d58SToby Isaac                                       "FUNCTION", "STRING", "__FP16",  "STRUCT", "INT",   "INT64", "COUNT", "INT32",       "PetscDataType", "PETSC_", NULL};
8b27a5e89SVaclav Hapla 
9cf0ffa5bSSatish Balay /*@C
1020f4b53cSBarry Smith   PetscDataTypeToMPIDataType - Converts the `PetscDataType` name of a datatype to its `MPI_Datatype`
11e5c89e4eSSatish Balay 
1220f4b53cSBarry Smith   Not Collective
13e5c89e4eSSatish Balay 
14e5c89e4eSSatish Balay   Input Parameter:
15811af0c4SBarry Smith . ptype - the PETSc datatype name (for example `PETSC_DOUBLE`)
16e5c89e4eSSatish Balay 
17e5c89e4eSSatish Balay   Output Parameter:
18811af0c4SBarry Smith . mtype - the MPI datatype (for example `MPI_DOUBLE`, ...)
19e5c89e4eSSatish Balay 
20e5c89e4eSSatish Balay   Level: advanced
21e5c89e4eSSatish Balay 
22db781477SPatrick Sanan .seealso: `PetscDataType`, `PetscMPIDataTypeToPetscDataType()`
23e5c89e4eSSatish Balay @*/
PetscDataTypeToMPIDataType(PetscDataType ptype,MPI_Datatype * mtype)24d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDataTypeToMPIDataType(PetscDataType ptype, MPI_Datatype *mtype)
25d71ae5a4SJacob Faibussowitsch {
26e5c89e4eSSatish Balay   PetscFunctionBegin;
27a297a907SKarl Rupp   if (ptype == PETSC_INT) *mtype = MPIU_INT;
28a297a907SKarl Rupp   else if (ptype == PETSC_DOUBLE) *mtype = MPI_DOUBLE;
299860990eSLisandro Dalcin #if defined(PETSC_HAVE_COMPLEX)
30ce63c4c1SBarry Smith   #if defined(PETSC_USE_REAL_SINGLE)
31de272c7aSSatish Balay   else if (ptype == PETSC_COMPLEX) *mtype = MPI_C_COMPLEX;
32d5cd3959SJed Brown   #elif defined(PETSC_USE_REAL___FLOAT128)
33d5cd3959SJed Brown   else if (ptype == PETSC_COMPLEX) *mtype = MPIU___COMPLEX128;
344fcfbe5fSBarry Smith   #else
35de272c7aSSatish Balay   else if (ptype == PETSC_COMPLEX) *mtype = MPI_C_DOUBLE_COMPLEX;
36e5c89e4eSSatish Balay   #endif
374fcfbe5fSBarry Smith #endif
38a297a907SKarl Rupp   else if (ptype == PETSC_LONG) *mtype = MPI_LONG;
39a297a907SKarl Rupp   else if (ptype == PETSC_SHORT) *mtype = MPI_SHORT;
40a297a907SKarl Rupp   else if (ptype == PETSC_ENUM) *mtype = MPI_INT;
41a297a907SKarl Rupp   else if (ptype == PETSC_BOOL) *mtype = MPI_INT;
429e3e4c22SLisandro Dalcin   else if (ptype == PETSC_INT64) *mtype = MPIU_INT64;
43*6497c311SBarry Smith   else if (ptype == PETSC_COUNT) *mtype = MPIU_COUNT;
443321ca25SJames Wright   else if (ptype == PETSC_INT32) *mtype = MPIU_INT32;
45a297a907SKarl Rupp   else if (ptype == PETSC_FLOAT) *mtype = MPI_FLOAT;
46a297a907SKarl Rupp   else if (ptype == PETSC_CHAR) *mtype = MPI_CHAR;
47a297a907SKarl Rupp   else if (ptype == PETSC_BIT_LOGICAL) *mtype = MPI_BYTE;
48d5cd3959SJed Brown #if defined(PETSC_USE_REAL___FLOAT128)
49d5cd3959SJed Brown   else if (ptype == PETSC___FLOAT128) *mtype = MPIU___FLOAT128;
50570b7f6dSBarry Smith #elif defined(PETSC_USE_REAL___FP16)
51570b7f6dSBarry Smith   else if (ptype == PETSC___FP16) *mtype = MPIU___FP16;
52d5cd3959SJed Brown #endif
53a297a907SKarl Rupp   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unknown PETSc datatype");
543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
55e5c89e4eSSatish Balay }
56e5c89e4eSSatish Balay 
57e39fd77fSBarry Smith /*@C
5820f4b53cSBarry Smith   PetscMPIDataTypeToPetscDataType - Finds the `PetscDataType` name of a datatype from its `MPI_Datatype`
59e39fd77fSBarry Smith 
6020f4b53cSBarry Smith   Not Collective
61e39fd77fSBarry Smith 
62e39fd77fSBarry Smith   Input Parameter:
63811af0c4SBarry Smith . mtype - the MPI datatype (for example `MPI_DOUBLE`, ...)
64e39fd77fSBarry Smith 
65e39fd77fSBarry Smith   Output Parameter:
66811af0c4SBarry Smith . ptype - the PETSc datatype name (for example `PETSC_DOUBLE`)
67e39fd77fSBarry Smith 
68e39fd77fSBarry Smith   Level: advanced
69e39fd77fSBarry Smith 
7042747ad1SJacob Faibussowitsch .seealso: `PetscDataType`
71e39fd77fSBarry Smith @*/
PetscMPIDataTypeToPetscDataType(MPI_Datatype mtype,PetscDataType * ptype)72d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscMPIDataTypeToPetscDataType(MPI_Datatype mtype, PetscDataType *ptype)
73d71ae5a4SJacob Faibussowitsch {
74e39fd77fSBarry Smith   PetscFunctionBegin;
75a297a907SKarl Rupp   if (mtype == MPIU_INT) *ptype = PETSC_INT;
7679270f5aSBarry Smith #if defined(PETSC_USE_64BIT_INDICES)
777f92f25eSLisandro Dalcin   else if (mtype == MPI_INT) *ptype = PETSC_ENUM;
7879270f5aSBarry Smith #endif
799e3e4c22SLisandro Dalcin   else if (mtype == MPIU_INT64) *ptype = PETSC_INT64;
80*6497c311SBarry Smith   else if (mtype == MPIU_COUNT) *ptype = PETSC_COUNT;
813321ca25SJames Wright   else if (mtype == MPIU_INT32) *ptype = PETSC_INT32;
82a297a907SKarl Rupp   else if (mtype == MPI_DOUBLE) *ptype = PETSC_DOUBLE;
839860990eSLisandro Dalcin #if defined(PETSC_HAVE_COMPLEX)
84ce63c4c1SBarry Smith   #if defined(PETSC_USE_REAL_SINGLE)
85de272c7aSSatish Balay   else if (mtype == MPI_C_COMPLEX) *ptype = PETSC_COMPLEX;
86d5cd3959SJed Brown   #elif defined(PETSC_USE_REAL___FLOAT128)
87d5cd3959SJed Brown   else if (mtype == MPIU___COMPLEX128) *ptype = PETSC_COMPLEX;
884fcfbe5fSBarry Smith   #else
89de272c7aSSatish Balay   else if (mtype == MPI_C_DOUBLE_COMPLEX) *ptype = PETSC_COMPLEX;
90e39fd77fSBarry Smith   #endif
914fcfbe5fSBarry Smith #endif
92a297a907SKarl Rupp   else if (mtype == MPI_LONG) *ptype = PETSC_LONG;
93a297a907SKarl Rupp   else if (mtype == MPI_SHORT) *ptype = PETSC_SHORT;
94a297a907SKarl Rupp   else if (mtype == MPI_FLOAT) *ptype = PETSC_FLOAT;
95a297a907SKarl Rupp   else if (mtype == MPI_CHAR) *ptype = PETSC_CHAR;
96d5cd3959SJed Brown #if defined(PETSC_USE_REAL___FLOAT128)
97d5cd3959SJed Brown   else if (mtype == MPIU___FLOAT128) *ptype = PETSC___FLOAT128;
98570b7f6dSBarry Smith #elif defined(PETSC_USE_REAL___FP16)
99570b7f6dSBarry Smith   else if (mtype == MPIU___FP16) *ptype = PETSC___FP16;
100d5cd3959SJed Brown #endif
101a297a907SKarl Rupp   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unhandled MPI datatype");
1023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
103e39fd77fSBarry Smith }
104e39fd77fSBarry Smith 
1059371c9d4SSatish Balay typedef enum {
1069371c9d4SSatish Balay   PETSC_INT_SIZE    = sizeof(PetscInt),
107a297a907SKarl Rupp   PETSC_DOUBLE_SIZE = sizeof(double),
1089860990eSLisandro Dalcin #if defined(PETSC_HAVE_COMPLEX)
1099860990eSLisandro Dalcin   PETSC_COMPLEX_SIZE = sizeof(PetscComplex),
1109860990eSLisandro Dalcin #else
1119860990eSLisandro Dalcin   PETSC_COMPLEX_SIZE = 2 * sizeof(PetscReal),
1129860990eSLisandro Dalcin #endif
113a297a907SKarl Rupp   PETSC_LONG_SIZE        = sizeof(long),
114a297a907SKarl Rupp   PETSC_SHORT_SIZE       = sizeof(short),
115a297a907SKarl Rupp   PETSC_FLOAT_SIZE       = sizeof(float),
116a297a907SKarl Rupp   PETSC_CHAR_SIZE        = sizeof(char),
1179e3e4c22SLisandro Dalcin   PETSC_ENUM_SIZE        = sizeof(PetscEnum),
1189e3e4c22SLisandro Dalcin   PETSC_BOOL_SIZE        = sizeof(PetscBool),
1199e3e4c22SLisandro Dalcin   PETSC_INT64_SIZE       = sizeof(PetscInt64),
1203321ca25SJames Wright   PETSC_INT32_SIZE       = sizeof(PetscInt32),
121*6497c311SBarry Smith   PETSC_BIT_LOGICAL_SIZE = sizeof(char),
122*6497c311SBarry Smith   PETSC_COUNT_SIZE       = sizeof(PetscCount)
1233c629287SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
1249371c9d4SSatish Balay     ,
1259371c9d4SSatish Balay   PETSC___FLOAT128_SIZE = sizeof(__float128)
1263c629287SBarry Smith #elif defined(PETSC_USE_REAL___FP16)
1279371c9d4SSatish Balay     ,
1289371c9d4SSatish Balay   PETSC___FP16_SIZE = sizeof(__fp16)
129570b7f6dSBarry Smith #endif
1300ad7597dSKarl Rupp } PetscDataTypeSize;
131e5c89e4eSSatish Balay 
132e8976759SBarry Smith /*@C
133e5c89e4eSSatish Balay   PetscDataTypeGetSize - Gets the size (in bytes) of a PETSc datatype
134e5c89e4eSSatish Balay 
13520f4b53cSBarry Smith   Not Collective
136e5c89e4eSSatish Balay 
137e5c89e4eSSatish Balay   Input Parameter:
138811af0c4SBarry Smith . ptype - the PETSc datatype name (for example `PETSC_DOUBLE`)
139e5c89e4eSSatish Balay 
140e5c89e4eSSatish Balay   Output Parameter:
141811af0c4SBarry Smith . size - the size in bytes (for example the size of `PETSC_DOUBLE` is 8)
142e5c89e4eSSatish Balay 
143e5c89e4eSSatish Balay   Level: advanced
144e5c89e4eSSatish Balay 
145db781477SPatrick Sanan .seealso: `PetscDataType`, `PetscDataTypeToMPIDataType()`
146e5c89e4eSSatish Balay @*/
PetscDataTypeGetSize(PetscDataType ptype,size_t * size)147d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDataTypeGetSize(PetscDataType ptype, size_t *size)
148d71ae5a4SJacob Faibussowitsch {
149e5c89e4eSSatish Balay   PetscFunctionBegin;
1509860990eSLisandro Dalcin   if ((int)ptype < 0) *size = -(int)ptype;
1519860990eSLisandro Dalcin   else if (ptype == PETSC_INT) *size = PETSC_INT_SIZE;
152a297a907SKarl Rupp   else if (ptype == PETSC_DOUBLE) *size = PETSC_DOUBLE_SIZE;
153a297a907SKarl Rupp   else if (ptype == PETSC_COMPLEX) *size = PETSC_COMPLEX_SIZE;
154a297a907SKarl Rupp   else if (ptype == PETSC_LONG) *size = PETSC_LONG_SIZE;
155a297a907SKarl Rupp   else if (ptype == PETSC_SHORT) *size = PETSC_SHORT_SIZE;
156a297a907SKarl Rupp   else if (ptype == PETSC_FLOAT) *size = PETSC_FLOAT_SIZE;
157a297a907SKarl Rupp   else if (ptype == PETSC_CHAR) *size = PETSC_CHAR_SIZE;
158a297a907SKarl Rupp   else if (ptype == PETSC_ENUM) *size = PETSC_ENUM_SIZE;
159a297a907SKarl Rupp   else if (ptype == PETSC_BOOL) *size = PETSC_BOOL_SIZE;
1609e3e4c22SLisandro Dalcin   else if (ptype == PETSC_INT64) *size = PETSC_INT64_SIZE;
1613321ca25SJames Wright   else if (ptype == PETSC_INT32) *size = PETSC_INT32_SIZE;
162*6497c311SBarry Smith   else if (ptype == PETSC_COUNT) *size = PETSC_COUNT_SIZE;
1639e3e4c22SLisandro Dalcin   else if (ptype == PETSC_BIT_LOGICAL) *size = PETSC_BIT_LOGICAL_SIZE;
1643c629287SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
165a297a907SKarl Rupp   else if (ptype == PETSC___FLOAT128) *size = PETSC___FLOAT128_SIZE;
1663c629287SBarry Smith #elif defined(PETSC_USE_REAL___FP16)
167570b7f6dSBarry Smith   else if (ptype == PETSC___FP16) *size = PETSC___FP16_SIZE;
168570b7f6dSBarry Smith #endif
169a297a907SKarl Rupp   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unknown PETSc datatype");
1703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
171e5c89e4eSSatish Balay }
1728e4b2d1dSBarry Smith 
1735d83a8b1SBarry Smith /*@
1748e4b2d1dSBarry Smith   PetscDataTypeFromString - Gets the enum value of a PETSc datatype represented as a string
1758e4b2d1dSBarry Smith 
17620f4b53cSBarry Smith   Not Collective
1778e4b2d1dSBarry Smith 
1788e4b2d1dSBarry Smith   Input Parameter:
179811af0c4SBarry Smith . name - the PETSc datatype name (for example, "double" or "real")
1808e4b2d1dSBarry Smith 
181d8d19677SJose E. Roman   Output Parameters:
182811af0c4SBarry Smith + ptype - the enum value, only valid if found is `PETSC_TRUE`
1838e4b2d1dSBarry Smith - found - the string matches one of the data types
1848e4b2d1dSBarry Smith 
1858e4b2d1dSBarry Smith   Level: advanced
1868e4b2d1dSBarry Smith 
187db781477SPatrick Sanan .seealso: `PetscDataType`, `PetscDataTypeToMPIDataType()`, `PetscDataTypeGetSize()`
1888e4b2d1dSBarry Smith @*/
PetscDataTypeFromString(const char name[],PetscDataType * ptype,PetscBool * found)189cc4c1da9SBarry Smith PetscErrorCode PetscDataTypeFromString(const char name[], PetscDataType *ptype, PetscBool *found)
190d71ae5a4SJacob Faibussowitsch {
1918e4b2d1dSBarry Smith   PetscFunctionBegin;
1929566063dSJacob Faibussowitsch   PetscCall(PetscEnumFind(PetscDataTypes, name, (PetscEnum *)ptype, found));
1938e4b2d1dSBarry Smith   if (!*found) {
194ff32304bSBarry Smith     char formatted[16];
1958e4b2d1dSBarry Smith 
1969566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(formatted, name, 16));
1979566063dSJacob Faibussowitsch     PetscCall(PetscStrtolower(formatted));
1989566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(formatted, "scalar", found));
1998e4b2d1dSBarry Smith     if (*found) {
2008e4b2d1dSBarry Smith       *ptype = PETSC_SCALAR;
2018e4b2d1dSBarry Smith     } else {
2029566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(formatted, "real", found));
203ad540459SPierre Jolivet       if (*found) *ptype = PETSC_REAL;
2048e4b2d1dSBarry Smith     }
2058e4b2d1dSBarry Smith   }
2063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2078e4b2d1dSBarry Smith }
208