xref: /petsc/src/sys/objects/ptype.c (revision 4d25b1e8fcd3ad4ffb67bd2c1c0590886f5a9001)
1 
2 /*
3      Provides utility routines for manipulating any type of PETSc object.
4 */
5 #include <petscsys.h>  /*I   "petscsys.h"    I*/
6 
7 #undef __FUNCT__
8 #define __FUNCT__ "PetscDataTypeToMPIDataType"
9 /*@C
10      PetscDataTypeToMPIDataType - Converts the PETSc name of a datatype to its MPI name.
11 
12    Not collective
13 
14     Input Parameter:
15 .     ptype - the PETSc datatype name (for example PETSC_DOUBLE)
16 
17     Output Parameter:
18 .     mtype - the MPI datatype (for example MPI_DOUBLE, ...)
19 
20     Level: advanced
21 
22 .seealso: PetscDataType, PetscMPIDataTypeToPetscDataType()
23 @*/
24 PetscErrorCode  PetscDataTypeToMPIDataType(PetscDataType ptype,MPI_Datatype* mtype)
25 {
26   PetscFunctionBegin;
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 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown PETSc datatype");
57   PetscFunctionReturn(0);
58 }
59 
60 #undef __FUNCT__
61 #define __FUNCT__ "PetscMPIDataTypeToPetscDataType"
62 /*@C
63      PetscMPIDataTypeToPetscDataType Finds the PETSc name of a datatype from its MPI name
64 
65    Not collective
66 
67     Input Parameter:
68 .     mtype - the MPI datatype (for example MPI_DOUBLE, ...)
69 
70     Output Parameter:
71 .     ptype - the PETSc datatype name (for example PETSC_DOUBLE)
72 
73     Level: advanced
74 
75 .seealso: PetscDataType, PetscMPIDataTypeToPetscDataType()
76 @*/
77 PetscErrorCode  PetscMPIDataTypeToPetscDataType(MPI_Datatype mtype,PetscDataType *ptype)
78 {
79   PetscFunctionBegin;
80   if (mtype == MPIU_INT) {
81     *ptype = PETSC_INT;
82   } else if (mtype == MPI_INT) {
83     *ptype = PETSC_INT;
84   } else if (mtype == MPI_DOUBLE) {
85     *ptype = PETSC_DOUBLE;
86 #if defined(PETSC_USE_COMPLEX)
87 #if defined(PETSC_USE_REAL_SINGLE)
88   } else if (mtype == MPIU_C_COMPLEX) {
89     *ptype = PETSC_COMPLEX;
90 #else
91   } else if (mtype == MPIU_C_DOUBLE_COMPLEX) {
92     *ptype = PETSC_COMPLEX;
93 #endif
94 #endif
95   } else if (mtype == MPI_LONG) {
96     *ptype = PETSC_LONG;
97   } else if (mtype == MPI_SHORT) {
98     *ptype = PETSC_SHORT;
99   } else if (mtype == MPI_FLOAT) {
100     *ptype = PETSC_FLOAT;
101   } else if (mtype == MPI_CHAR) {
102     *ptype = PETSC_CHAR;
103   } else if (mtype == MPI_LONG_DOUBLE) {
104     *ptype = PETSC___FLOAT128;
105   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unhandled MPI datatype");
106   PetscFunctionReturn(0);
107 }
108 
109 typedef enum {PETSC_INT_SIZE = sizeof(PetscInt),PETSC_DOUBLE_SIZE = sizeof(double),
110               PETSC_COMPLEX_SIZE = sizeof(PetscScalar),PETSC_LONG_SIZE=sizeof(long),
111               PETSC_SHORT_SIZE = sizeof(short),PETSC_FLOAT_SIZE = sizeof(float),
112               PETSC_CHAR_SIZE = sizeof(char),PETSC_BIT_LOGICAL_SIZE = sizeof(char),
113               PETSC_ENUM_SIZE = sizeof(PetscBool), PETSC_BOOL_SIZE = sizeof(PetscBool),
114               PETSC___FLOAT128_SIZE = sizeof(long double)
115              } PetscDataTypeSize;
116 
117 #undef __FUNCT__
118 #define __FUNCT__ "PetscDataTypeGetSize"
119 /*@
120      PetscDataTypeGetSize - Gets the size (in bytes) of a PETSc datatype
121 
122    Not collective
123 
124     Input Parameter:
125 .     ptype - the PETSc datatype name (for example PETSC_DOUBLE)
126 
127     Output Parameter:
128 .     size - the size in bytes (for example the size of PETSC_DOUBLE is 8)
129 
130     Level: advanced
131 
132 .seealso: PetscDataType, PetscDataTypeToMPIDataType()
133 @*/
134 PetscErrorCode  PetscDataTypeGetSize(PetscDataType ptype,size_t *size)
135 {
136   PetscFunctionBegin;
137   if ((int) ptype < 0) {
138     *size = -(int) ptype;
139     PetscFunctionReturn(0);
140   }
141 
142   if (ptype == PETSC_INT) {
143     *size = PETSC_INT_SIZE;
144   } else if (ptype == PETSC_DOUBLE) {
145     *size = PETSC_DOUBLE_SIZE;
146 #if defined(PETSC_USE_COMPLEX)
147   } else if (ptype == PETSC_COMPLEX) {
148     *size = PETSC_COMPLEX_SIZE;
149 #endif
150   } else if (ptype == PETSC_LONG) {
151     *size = PETSC_LONG_SIZE;
152   } else if (ptype == PETSC_SHORT) {
153     *size = PETSC_SHORT_SIZE;
154   } else if (ptype == PETSC_FLOAT) {
155     *size = PETSC_FLOAT_SIZE;
156   } else if (ptype == PETSC_CHAR) {
157     *size = PETSC_CHAR_SIZE;
158   } else if (ptype == PETSC_ENUM) {
159     *size = PETSC_ENUM_SIZE;
160   } else if (ptype == PETSC_BIT_LOGICAL) {
161     *size = PETSC_BIT_LOGICAL_SIZE;
162   } else if (ptype == PETSC_BOOL) {
163     *size = PETSC_BOOL_SIZE;
164   } else if (ptype == PETSC___FLOAT128) {
165     *size = PETSC___FLOAT128_SIZE;
166   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown PETSc datatype");
167   PetscFunctionReturn(0);
168 }
169