Actual source code: ptype.c
petsc-3.7.3 2016-08-01
2: /*
3: Provides utility routines for manipulating any type of PETSc object.
4: */
5: #include <petscsys.h> /*I "petscsys.h" I*/
9: /*@C
10: PetscDataTypeToMPIDataType - Converts the PETSc name of a datatype to its MPI name.
12: Not collective
14: Input Parameter:
15: . ptype - the PETSc datatype name (for example PETSC_DOUBLE)
17: Output Parameter:
18: . mtype - the MPI datatype (for example MPI_DOUBLE, ...)
20: Level: advanced
22: .seealso: PetscDataType, PetscMPIDataTypeToPetscDataType()
23: @*/
24: PetscErrorCode PetscDataTypeToMPIDataType(PetscDataType ptype,MPI_Datatype *mtype)
25: {
27: if (ptype == PETSC_INT) *mtype = MPIU_INT;
28: else if (ptype == PETSC_DOUBLE) *mtype = MPI_DOUBLE;
29: #if defined(PETSC_USE_COMPLEX)
30: #if defined(PETSC_USE_REAL_SINGLE)
31: else if (ptype == PETSC_COMPLEX) *mtype = MPIU_C_COMPLEX;
32: #elif defined(PETSC_USE_REAL___FLOAT128)
33: else if (ptype == PETSC_COMPLEX) *mtype = MPIU___COMPLEX128;
34: #else
35: else if (ptype == PETSC_COMPLEX) *mtype = MPIU_C_DOUBLE_COMPLEX;
36: #endif
37: #endif
38: else if (ptype == PETSC_LONG) *mtype = MPI_LONG;
39: else if (ptype == PETSC_SHORT) *mtype = MPI_SHORT;
40: else if (ptype == PETSC_ENUM) *mtype = MPI_INT;
41: else if (ptype == PETSC_BOOL) *mtype = MPI_INT;
42: else if (ptype == PETSC_FLOAT) *mtype = MPI_FLOAT;
43: else if (ptype == PETSC_CHAR) *mtype = MPI_CHAR;
44: else if (ptype == PETSC_BIT_LOGICAL) *mtype = MPI_BYTE;
45: #if defined(PETSC_USE_REAL___FLOAT128)
46: else if (ptype == PETSC___FLOAT128) *mtype = MPIU___FLOAT128;
47: #endif
48: else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown PETSc datatype");
49: return(0);
50: }
54: /*@C
55: PetscMPIDataTypeToPetscDataType Finds the PETSc name of a datatype from its MPI name
57: Not collective
59: Input Parameter:
60: . mtype - the MPI datatype (for example MPI_DOUBLE, ...)
62: Output Parameter:
63: . ptype - the PETSc datatype name (for example PETSC_DOUBLE)
65: Level: advanced
67: .seealso: PetscDataType, PetscMPIDataTypeToPetscDataType()
68: @*/
69: PetscErrorCode PetscMPIDataTypeToPetscDataType(MPI_Datatype mtype,PetscDataType *ptype)
70: {
72: if (mtype == MPIU_INT) *ptype = PETSC_INT;
73: else if (mtype == MPI_INT) *ptype = PETSC_INT;
74: else if (mtype == MPI_DOUBLE) *ptype = PETSC_DOUBLE;
75: #if defined(PETSC_USE_COMPLEX)
76: #if defined(PETSC_USE_REAL_SINGLE)
77: else if (mtype == MPIU_C_COMPLEX) *ptype = PETSC_COMPLEX;
78: #elif defined(PETSC_USE_REAL___FLOAT128)
79: else if (mtype == MPIU___COMPLEX128) *ptype = PETSC_COMPLEX;
80: #else
81: else if (mtype == MPIU_C_DOUBLE_COMPLEX) *ptype = PETSC_COMPLEX;
82: #endif
83: #endif
84: else if (mtype == MPI_LONG) *ptype = PETSC_LONG;
85: else if (mtype == MPI_SHORT) *ptype = PETSC_SHORT;
86: else if (mtype == MPI_FLOAT) *ptype = PETSC_FLOAT;
87: else if (mtype == MPI_CHAR) *ptype = PETSC_CHAR;
88: #if defined(PETSC_USE_REAL___FLOAT128)
89: else if (mtype == MPIU___FLOAT128) *ptype = PETSC___FLOAT128;
90: #endif
91: else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unhandled MPI datatype");
92: return(0);
93: }
95: typedef enum {PETSC_INT_SIZE = sizeof(PetscInt),
96: PETSC_DOUBLE_SIZE = sizeof(double),
97: PETSC_COMPLEX_SIZE = sizeof(PetscScalar),
98: PETSC_LONG_SIZE = sizeof(long),
99: PETSC_SHORT_SIZE = sizeof(short),
100: PETSC_FLOAT_SIZE = sizeof(float),
101: PETSC_CHAR_SIZE = sizeof(char),
102: PETSC_BIT_LOGICAL_SIZE = sizeof(char),
103: PETSC_ENUM_SIZE = sizeof(PetscBool),
104: PETSC_BOOL_SIZE = sizeof(PetscBool),
105: PETSC___FLOAT128_SIZE = sizeof(long double)
106: } PetscDataTypeSize;
110: /*@
111: PetscDataTypeGetSize - Gets the size (in bytes) of a PETSc datatype
113: Not collective
115: Input Parameter:
116: . ptype - the PETSc datatype name (for example PETSC_DOUBLE)
118: Output Parameter:
119: . size - the size in bytes (for example the size of PETSC_DOUBLE is 8)
121: Level: advanced
123: .seealso: PetscDataType, PetscDataTypeToMPIDataType()
124: @*/
125: PetscErrorCode PetscDataTypeGetSize(PetscDataType ptype,size_t *size)
126: {
128: if ((int) ptype < 0) {
129: *size = -(int) ptype;
130: return(0);
131: }
133: if (ptype == PETSC_INT) *size = PETSC_INT_SIZE;
134: else if (ptype == PETSC_DOUBLE) *size = PETSC_DOUBLE_SIZE;
135: #if defined(PETSC_USE_COMPLEX)
136: else if (ptype == PETSC_COMPLEX) *size = PETSC_COMPLEX_SIZE;
137: #endif
138: else if (ptype == PETSC_LONG) *size = PETSC_LONG_SIZE;
139: else if (ptype == PETSC_SHORT) *size = PETSC_SHORT_SIZE;
140: else if (ptype == PETSC_FLOAT) *size = PETSC_FLOAT_SIZE;
141: else if (ptype == PETSC_CHAR) *size = PETSC_CHAR_SIZE;
142: else if (ptype == PETSC_ENUM) *size = PETSC_ENUM_SIZE;
143: else if (ptype == PETSC_BIT_LOGICAL) *size = PETSC_BIT_LOGICAL_SIZE;
144: else if (ptype == PETSC_BOOL) *size = PETSC_BOOL_SIZE;
145: else if (ptype == PETSC___FLOAT128) *size = PETSC___FLOAT128_SIZE;
146: else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown PETSc datatype");
147: return(0);
148: }
152: /*@
153: PetscDataTypeFromString - Gets the enum value of a PETSc datatype represented as a string
155: Not collective
157: Input Parameter:
158: . name - the PETSc datatype name (for example, DOUBLE or real or Scalar)
160: Output Parameter:
161: + ptype - the enum value
162: - found - the string matches one of the data types
164: Level: advanced
166: .seealso: PetscDataType, PetscDataTypeToMPIDataType(), PetscDataTypeGetSize()
167: @*/
168: PetscErrorCode PetscDataTypeFromString(const char*name, PetscDataType *ptype,PetscBool *found)
169: {
173: PetscEnumFind(PetscDataTypes,name,(PetscEnum*)ptype,found);
174: if (!*found) {
175: char formatted[16];
177: PetscStrncpy(formatted,name,16);
178: PetscStrtolower(formatted);
179: PetscStrcmp(formatted,"scalar",found);
180: if (*found) {
181: *ptype = PETSC_SCALAR;
182: } else {
183: PetscStrcmp(formatted,"real",found);
184: if (*found) {
185: *ptype = PETSC_REAL;
186: }
187: }
188: }
189: return(0);
190: }