Actual source code: ptype.c
petsc-3.11.4 2019-09-28
2: /*
3: Provides utility routines for manipulating any type of PETSc object.
4: */
5: #include <petscsys.h>
7: const char *const PetscDataTypes[] = {"UNKNOWN",
8: "DOUBLE","COMPLEX","LONG","SHORT","FLOAT",
9: "CHAR","LOGICAL","ENUM","BOOL","LONGDOUBLE",
10: "OBJECT","FUNCTION","STRING","FP16","STRUCT",
11: "INT","INT64",
12: "PetscDataType","PETSC_",0};
14: /*@C
15: PetscDataTypeToMPIDataType - Converts the PETSc name of a datatype to its MPI name.
17: Not collective
19: Input Parameter:
20: . ptype - the PETSc datatype name (for example PETSC_DOUBLE)
22: Output Parameter:
23: . mtype - the MPI datatype (for example MPI_DOUBLE, ...)
25: Level: advanced
27: .seealso: PetscDataType, PetscMPIDataTypeToPetscDataType()
28: @*/
29: PetscErrorCode PetscDataTypeToMPIDataType(PetscDataType ptype,MPI_Datatype *mtype)
30: {
32: if (ptype == PETSC_INT) *mtype = MPIU_INT;
33: else if (ptype == PETSC_DOUBLE) *mtype = MPI_DOUBLE;
34: #if defined(PETSC_USE_COMPLEX)
35: #if defined(PETSC_USE_REAL_SINGLE)
36: else if (ptype == PETSC_COMPLEX) *mtype = MPIU_C_COMPLEX;
37: #elif defined(PETSC_USE_REAL___FLOAT128)
38: else if (ptype == PETSC_COMPLEX) *mtype = MPIU___COMPLEX128;
39: #else
40: else if (ptype == PETSC_COMPLEX) *mtype = MPIU_C_DOUBLE_COMPLEX;
41: #endif
42: #endif
43: else if (ptype == PETSC_LONG) *mtype = MPI_LONG;
44: else if (ptype == PETSC_SHORT) *mtype = MPI_SHORT;
45: else if (ptype == PETSC_ENUM) *mtype = MPI_INT;
46: else if (ptype == PETSC_BOOL) *mtype = MPI_INT;
47: else if (ptype == PETSC_INT64) *mtype = MPIU_INT64;
48: else if (ptype == PETSC_FLOAT) *mtype = MPI_FLOAT;
49: else if (ptype == PETSC_CHAR) *mtype = MPI_CHAR;
50: else if (ptype == PETSC_BIT_LOGICAL) *mtype = MPI_BYTE;
51: #if defined(PETSC_USE_REAL___FLOAT128)
52: else if (ptype == PETSC___FLOAT128) *mtype = MPIU___FLOAT128;
53: #elif defined(PETSC_USE_REAL___FP16)
54: else if (ptype == PETSC___FP16) *mtype = MPIU___FP16;
55: #endif
56: else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown PETSc datatype");
57: return(0);
58: }
60: /*@C
61: PetscMPIDataTypeToPetscDataType Finds the PETSc name of a datatype from its MPI name
63: Not collective
65: Input Parameter:
66: . mtype - the MPI datatype (for example MPI_DOUBLE, ...)
68: Output Parameter:
69: . ptype - the PETSc datatype name (for example PETSC_DOUBLE)
71: Level: advanced
73: .seealso: PetscDataType, PetscMPIDataTypeToPetscDataType()
74: @*/
75: PetscErrorCode PetscMPIDataTypeToPetscDataType(MPI_Datatype mtype,PetscDataType *ptype)
76: {
78: if (mtype == MPIU_INT) *ptype = PETSC_INT;
79: #if defined(PETSC_USE_64BIT_INDICES)
80: else if (mtype == MPI_INT) *ptype = PETSC_ENUM;
81: #endif
82: else if (mtype == MPIU_INT64) *ptype = PETSC_INT64;
83: else if (mtype == MPI_DOUBLE) *ptype = PETSC_DOUBLE;
84: #if defined(PETSC_USE_COMPLEX)
85: #if defined(PETSC_USE_REAL_SINGLE)
86: else if (mtype == MPIU_C_COMPLEX) *ptype = PETSC_COMPLEX;
87: #elif defined(PETSC_USE_REAL___FLOAT128)
88: else if (mtype == MPIU___COMPLEX128) *ptype = PETSC_COMPLEX;
89: #else
90: else if (mtype == MPIU_C_DOUBLE_COMPLEX) *ptype = PETSC_COMPLEX;
91: #endif
92: #endif
93: else if (mtype == MPI_LONG) *ptype = PETSC_LONG;
94: else if (mtype == MPI_SHORT) *ptype = PETSC_SHORT;
95: else if (mtype == MPI_FLOAT) *ptype = PETSC_FLOAT;
96: else if (mtype == MPI_CHAR) *ptype = PETSC_CHAR;
97: #if defined(PETSC_USE_REAL___FLOAT128)
98: else if (mtype == MPIU___FLOAT128) *ptype = PETSC___FLOAT128;
99: #elif defined(PETSC_USE_REAL___FP16)
100: else if (mtype == MPIU___FP16) *ptype = PETSC___FP16;
101: #endif
102: else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unhandled MPI datatype");
103: return(0);
104: }
106: typedef enum {PETSC_INT_SIZE = sizeof(PetscInt),
107: PETSC_DOUBLE_SIZE = sizeof(double),
108: PETSC_COMPLEX_SIZE = sizeof(PetscScalar),
109: PETSC_LONG_SIZE = sizeof(long),
110: PETSC_SHORT_SIZE = sizeof(short),
111: PETSC_FLOAT_SIZE = sizeof(float),
112: PETSC_CHAR_SIZE = sizeof(char),
113: PETSC_ENUM_SIZE = sizeof(PetscEnum),
114: PETSC_BOOL_SIZE = sizeof(PetscBool),
115: PETSC_INT64_SIZE = sizeof(PetscInt64),
116: PETSC_BIT_LOGICAL_SIZE = sizeof(char)
117: #if defined(PETSC_USE_REAL___FLOAT128)
118: ,PETSC___FLOAT128_SIZE = sizeof(__float128)
119: #elif defined(PETSC_USE_REAL___FP16)
120: ,PETSC___FP16_SIZE = sizeof(__fp16)
121: #endif
122: } PetscDataTypeSize;
124: /*@C
125: PetscDataTypeGetSize - Gets the size (in bytes) of a PETSc datatype
127: Not collective
129: Input Parameter:
130: . ptype - the PETSc datatype name (for example PETSC_DOUBLE)
132: Output Parameter:
133: . size - the size in bytes (for example the size of PETSC_DOUBLE is 8)
135: Level: advanced
137: .seealso: PetscDataType, PetscDataTypeToMPIDataType()
138: @*/
139: PetscErrorCode PetscDataTypeGetSize(PetscDataType ptype,size_t *size)
140: {
142: if ((int) ptype < 0) {
143: *size = -(int) ptype;
144: return(0);
145: }
147: if (ptype == PETSC_INT) *size = PETSC_INT_SIZE;
148: else if (ptype == PETSC_DOUBLE) *size = PETSC_DOUBLE_SIZE;
149: #if defined(PETSC_USE_COMPLEX)
150: else if (ptype == PETSC_COMPLEX) *size = PETSC_COMPLEX_SIZE;
151: #endif
152: else if (ptype == PETSC_LONG) *size = PETSC_LONG_SIZE;
153: else if (ptype == PETSC_SHORT) *size = PETSC_SHORT_SIZE;
154: else if (ptype == PETSC_FLOAT) *size = PETSC_FLOAT_SIZE;
155: else if (ptype == PETSC_CHAR) *size = PETSC_CHAR_SIZE;
156: else if (ptype == PETSC_ENUM) *size = PETSC_ENUM_SIZE;
157: else if (ptype == PETSC_BOOL) *size = PETSC_BOOL_SIZE;
158: else if (ptype == PETSC_INT64) *size = PETSC_INT64_SIZE;
159: else if (ptype == PETSC_BIT_LOGICAL) *size = PETSC_BIT_LOGICAL_SIZE;
160: #if defined(PETSC_USE_REAL___FLOAT128)
161: else if (ptype == PETSC___FLOAT128) *size = PETSC___FLOAT128_SIZE;
162: #elif defined(PETSC_USE_REAL___FP16)
163: else if (ptype == PETSC___FP16) *size = PETSC___FP16_SIZE;
164: #endif
165: else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown PETSc datatype");
166: return(0);
167: }
169: /*@C
170: PetscDataTypeFromString - Gets the enum value of a PETSc datatype represented as a string
172: Not collective
174: Input Parameter:
175: . name - the PETSc datatype name (for example, DOUBLE or real or Scalar)
177: Output Parameter:
178: + ptype - the enum value
179: - found - the string matches one of the data types
181: Level: advanced
183: .seealso: PetscDataType, PetscDataTypeToMPIDataType(), PetscDataTypeGetSize()
184: @*/
185: PetscErrorCode PetscDataTypeFromString(const char*name, PetscDataType *ptype,PetscBool *found)
186: {
190: PetscEnumFind(PetscDataTypes,name,(PetscEnum*)ptype,found);
191: if (!*found) {
192: char formatted[16];
194: PetscStrncpy(formatted,name,16);
195: PetscStrtolower(formatted);
196: PetscStrcmp(formatted,"scalar",found);
197: if (*found) {
198: *ptype = PETSC_SCALAR;
199: } else {
200: PetscStrcmp(formatted,"real",found);
201: if (*found) {
202: *ptype = PETSC_REAL;
203: }
204: }
205: }
206: return(0);
207: }