Actual source code: ptype.c
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
21:
22: .seealso: PetscDataType, PetscMPIDataTypeToPetscDataType()
23: @*/
24: PetscErrorCode PetscDataTypeToMPIDataType(PetscDataType ptype,MPI_Datatype* mtype)
25: {
27: if (ptype == PETSC_INT) {
28: *mtype = MPIU_INT;
29: } else if (ptype == PETSC_DOUBLE) {
30: *mtype = MPI_DOUBLE;
31: #if defined(PETSC_USE_COMPLEX)
32: #if defined(PETSC_USE_REAL_SINGLE)
33: } else if (ptype == PETSC_COMPLEX) {
34: *mtype = MPIU_C_COMPLEX;
35: #else
36: } else if (ptype == PETSC_COMPLEX) {
37: *mtype = MPIU_C_DOUBLE_COMPLEX;
38: #endif
39: #endif
40: } else if (ptype == PETSC_LONG) {
41: *mtype = MPI_LONG;
42: } else if (ptype == PETSC_SHORT) {
43: *mtype = MPI_SHORT;
44: } else if (ptype == PETSC_ENUM) {
45: *mtype = MPI_INT;
46: } else if (ptype == PETSC_BOOL) {
47: *mtype = MPI_INT;
48: } else if (ptype == PETSC_FLOAT) {
49: *mtype = MPI_FLOAT;
50: } else if (ptype == PETSC_CHAR) {
51: *mtype = MPI_CHAR;
52: } else if (ptype == PETSC_BIT_LOGICAL) {
53: *mtype = MPI_BYTE;
54: } else if (ptype == PETSC_LONG_DOUBLE) {
55: *mtype = MPI_LONG_DOUBLE;
56: } else {
57: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown PETSc datatype");
58: }
59: return(0);
60: }
64: /*@C
65: PetscMPIDataTypeToPetscDataType Finds the PETSc name of a datatype from its MPI name
67: Not collective
69: Input Parameter:
70: . mtype - the MPI datatype (for example MPI_DOUBLE, ...)
72: Output Parameter:
73: . ptype - the PETSc datatype name (for example PETSC_DOUBLE)
75: Level: advanced
76:
77: .seealso: PetscDataType, PetscMPIDataTypeToPetscDataType()
78: @*/
79: PetscErrorCode PetscMPIDataTypeToPetscDataType(MPI_Datatype mtype,PetscDataType *ptype)
80: {
82: if (mtype == MPIU_INT) {
83: *ptype = PETSC_INT;
84: } else if (mtype == MPI_INT) {
85: *ptype = PETSC_INT;
86: } else if (mtype == MPI_DOUBLE) {
87: *ptype = PETSC_DOUBLE;
88: #if defined(PETSC_USE_COMPLEX)
89: #if defined(PETSC_USE_REAL_SINGLE)
90: } else if (mtype == MPIU_C_COMPLEX) {
91: *ptype = PETSC_COMPLEX;
92: #else
93: } else if (mtype == MPIU_C_DOUBLE_COMPLEX) {
94: *ptype = PETSC_COMPLEX;
95: #endif
96: #endif
97: } else if (mtype == MPI_LONG) {
98: *ptype = PETSC_LONG;
99: } else if (mtype == MPI_SHORT) {
100: *ptype = PETSC_SHORT;
101: } else if (mtype == MPI_FLOAT) {
102: *ptype = PETSC_FLOAT;
103: } else if (mtype == MPI_CHAR) {
104: *ptype = PETSC_CHAR;
105: } else if (mtype == MPI_LONG_DOUBLE) {
106: *ptype = PETSC_LONG_DOUBLE;
107: } else {
108: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unhandled MPI datatype");
109: }
110: return(0);
111: }
113: typedef enum {PETSC_INT_SIZE = sizeof(PetscInt),PETSC_DOUBLE_SIZE = sizeof(double),
114: PETSC_COMPLEX_SIZE = sizeof(PetscScalar),PETSC_LONG_SIZE=sizeof(long),
115: PETSC_SHORT_SIZE = sizeof(short),PETSC_FLOAT_SIZE = sizeof(float),
116: PETSC_CHAR_SIZE = sizeof(char),PETSC_BIT_LOGICAL_SIZE = sizeof(char),
117: PETSC_ENUM_SIZE = sizeof(PetscBool), PETSC_BOOL_SIZE = sizeof(PetscBool),
118: PETSC_LONG_DOUBLE_SIZE = sizeof(long double)} PetscDataTypeSize;
119: #if defined(PETSC_USE_COMPLEX)
120: #define PETSC_SCALAR_SIZE PETSC_COMPLEX_SIZE
121: #else
122: #define PETSC_SCALAR_SIZE PETSC_DOUBLE_SIZE
123: #endif
124: #if defined(PETSC_USE_REAL_SINGLE)
125: #define PETSC_REAL_SIZE PETSC_FLOAT_SIZE
126: #elif defined(PETSC_USE_REAL_LONG_DOUBLE)
127: #define PETSC_REAL_SIZE PETSC_LONG_DOUBLE_SIZE
128: #else
129: #define PETSC_REAL_SIZE PETSC_DOUBLE_SIZE
130: #endif
131: #define PETSC_FORTRANADDR_SIZE PETSC_LONG_SIZE
136: /*@
137: PetscDataTypeGetSize - Gets the size (in bytes) of a PETSc datatype
139: Not collective
141: Input Parameter:
142: . ptype - the PETSc datatype name (for example PETSC_DOUBLE)
144: Output Parameter:
145: . size - the size in bytes (for example the size of PETSC_DOUBLE is 8)
147: Level: advanced
148:
149: .seealso: PetscDataType, PetscDataTypeToMPIDataType()
150: @*/
151: PetscErrorCode PetscDataTypeGetSize(PetscDataType ptype,size_t *size)
152: {
154: if ((int) ptype < 0) {
155: *size = -(int) ptype;
156: return(0);
157: }
159: if (ptype == PETSC_INT) {
160: *size = PETSC_INT_SIZE;
161: } else if (ptype == PETSC_DOUBLE) {
162: *size = PETSC_DOUBLE_SIZE;
163: #if defined(PETSC_USE_COMPLEX)
164: } else if (ptype == PETSC_COMPLEX) {
165: *size = PETSC_COMPLEX_SIZE;
166: #endif
167: } else if (ptype == PETSC_LONG) {
168: *size = PETSC_LONG_SIZE;
169: } else if (ptype == PETSC_SHORT) {
170: *size = PETSC_SHORT_SIZE;
171: } else if (ptype == PETSC_FLOAT) {
172: *size = PETSC_FLOAT_SIZE;
173: } else if (ptype == PETSC_CHAR) {
174: *size = PETSC_CHAR_SIZE;
175: } else if (ptype == PETSC_ENUM) {
176: *size = PETSC_ENUM_SIZE;
177: } else if (ptype == PETSC_BIT_LOGICAL) {
178: *size = PETSC_BIT_LOGICAL_SIZE;
179: } else if (ptype == PETSC_BOOL) {
180: *size = PETSC_BOOL_SIZE;
181: } else if (ptype == PETSC_LONG_DOUBLE) {
182: *size = PETSC_LONG_DOUBLE_SIZE;
183: } else {
184: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown PETSc datatype");
185: }
186: return(0);
187: }