xref: /petsc/src/sys/objects/ptype.c (revision 82a7e548ee6828edd9c3163a10f8ebb094d15d3d)
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_LONG_DOUBLE) {
55     *mtype = MPI_LONG_DOUBLE;
56   } else {
57     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown PETSc datatype");
58   }
59   PetscFunctionReturn(0);
60 }
61 
62 #undef __FUNCT__
63 #define __FUNCT__ "PetscMPIDataTypeToPetscDataType"
64 /*@C
65      PetscMPIDataTypeToPetscDataType Finds the PETSc name of a datatype from its MPI name
66 
67    Not collective
68 
69     Input Parameter:
70 .     mtype - the MPI datatype (for example MPI_DOUBLE, ...)
71 
72     Output Parameter:
73 .     ptype - the PETSc datatype name (for example PETSC_DOUBLE)
74 
75     Level: advanced
76 
77 .seealso: PetscDataType, PetscMPIDataTypeToPetscDataType()
78 @*/
79 PetscErrorCode  PetscMPIDataTypeToPetscDataType(MPI_Datatype mtype,PetscDataType *ptype)
80 {
81   PetscFunctionBegin;
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   PetscFunctionReturn(0);
111 }
112 
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 #else
127 #define PETSC_REAL_SIZE PETSC_DOUBLE_SIZE
128 #endif
129 #define PETSC_FORTRANADDR_SIZE PETSC_LONG_SIZE
130 
131 
132 #undef __FUNCT__
133 #define __FUNCT__ "PetscDataTypeGetSize"
134 /*@
135      PetscDataTypeGetSize - Gets the size (in bytes) of a PETSc datatype
136 
137    Not collective
138 
139     Input Parameter:
140 .     ptype - the PETSc datatype name (for example PETSC_DOUBLE)
141 
142     Output Parameter:
143 .     size - the size in bytes (for example the size of PETSC_DOUBLE is 8)
144 
145     Level: advanced
146 
147 .seealso: PetscDataType, PetscDataTypeToMPIDataType()
148 @*/
149 PetscErrorCode  PetscDataTypeGetSize(PetscDataType ptype,size_t *size)
150 {
151   PetscFunctionBegin;
152   if ((int) ptype < 0) {
153     *size = -(int) ptype;
154     PetscFunctionReturn(0);
155   }
156 
157   if (ptype == PETSC_INT) {
158     *size = PETSC_INT_SIZE;
159   } else if (ptype == PETSC_DOUBLE) {
160     *size = PETSC_DOUBLE_SIZE;
161 #if defined(PETSC_USE_COMPLEX)
162   } else if (ptype == PETSC_COMPLEX) {
163     *size = PETSC_COMPLEX_SIZE;
164 #endif
165   } else if (ptype == PETSC_LONG) {
166     *size = PETSC_LONG_SIZE;
167   } else if (ptype == PETSC_SHORT) {
168     *size = PETSC_SHORT_SIZE;
169   } else if (ptype == PETSC_FLOAT) {
170     *size = PETSC_FLOAT_SIZE;
171   } else if (ptype == PETSC_CHAR) {
172     *size = PETSC_CHAR_SIZE;
173   } else if (ptype == PETSC_ENUM) {
174     *size = PETSC_ENUM_SIZE;
175   } else if (ptype == PETSC_BIT_LOGICAL) {
176     *size = PETSC_BIT_LOGICAL_SIZE;
177   } else if (ptype == PETSC_BOOL) {
178     *size = PETSC_BOOL_SIZE;
179   } else if (ptype == PETSC_LONG_DOUBLE) {
180     *size = PETSC_LONG_DOUBLE_SIZE;
181   } else {
182     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown PETSc datatype");
183   }
184   PetscFunctionReturn(0);
185 }
186