Actual source code: ptype.c
petsc-3.3-p7 2013-05-11
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___FLOAT128) {
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___FLOAT128;
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___FLOAT128_SIZE = sizeof(long double)} PetscDataTypeSize;
122: /*@
123: PetscDataTypeGetSize - Gets the size (in bytes) of a PETSc datatype
125: Not collective
127: Input Parameter:
128: . ptype - the PETSc datatype name (for example PETSC_DOUBLE)
130: Output Parameter:
131: . size - the size in bytes (for example the size of PETSC_DOUBLE is 8)
133: Level: advanced
134:
135: .seealso: PetscDataType, PetscDataTypeToMPIDataType()
136: @*/
137: PetscErrorCode PetscDataTypeGetSize(PetscDataType ptype,size_t *size)
138: {
140: if ((int) ptype < 0) {
141: *size = -(int) ptype;
142: return(0);
143: }
145: if (ptype == PETSC_INT) {
146: *size = PETSC_INT_SIZE;
147: } else if (ptype == PETSC_DOUBLE) {
148: *size = PETSC_DOUBLE_SIZE;
149: #if defined(PETSC_USE_COMPLEX)
150: } else if (ptype == PETSC_COMPLEX) {
151: *size = PETSC_COMPLEX_SIZE;
152: #endif
153: } else if (ptype == PETSC_LONG) {
154: *size = PETSC_LONG_SIZE;
155: } else if (ptype == PETSC_SHORT) {
156: *size = PETSC_SHORT_SIZE;
157: } else if (ptype == PETSC_FLOAT) {
158: *size = PETSC_FLOAT_SIZE;
159: } else if (ptype == PETSC_CHAR) {
160: *size = PETSC_CHAR_SIZE;
161: } else if (ptype == PETSC_ENUM) {
162: *size = PETSC_ENUM_SIZE;
163: } else if (ptype == PETSC_BIT_LOGICAL) {
164: *size = PETSC_BIT_LOGICAL_SIZE;
165: } else if (ptype == PETSC_BOOL) {
166: *size = PETSC_BOOL_SIZE;
167: } else if (ptype == PETSC___FLOAT128) {
168: *size = PETSC___FLOAT128_SIZE;
169: } else {
170: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown PETSc datatype");
171: }
172: return(0);
173: }