xref: /petsc/src/sys/objects/ptype.c (revision 5ebcd58d2ef61c69f1c1a24f2d3c9c0afa946639)
1 #define PETSC_DLL
2 /*
3      Provides utility routines for manipulating any type of PETSc object.
4 */
5 #include "petsc.h"  /*I   "petsc.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 PETSC_DLLEXPORT 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   } else if (ptype == PETSC_COMPLEX) {
33     *mtype = MPI_C_DOUBLE_COMPLEX;
34 #endif
35   } else if (ptype == PETSC_LONG) {
36     *mtype = MPI_LONG;
37   } else if (ptype == PETSC_SHORT) {
38     *mtype = MPI_SHORT;
39   } else if (ptype == PETSC_ENUM) {
40     *mtype = MPI_INT;
41   } else if (ptype == PETSC_TRUTH) {
42     *mtype = MPI_INT;
43   } else if (ptype == PETSC_FLOAT) {
44     *mtype = MPI_FLOAT;
45   } else if (ptype == PETSC_CHAR) {
46     *mtype = MPI_CHAR;
47   } else if (ptype == PETSC_LOGICAL) {
48     *mtype = MPI_BYTE;
49   } else if (ptype == PETSC_LONG_DOUBLE) {
50     *mtype = MPI_LONG_DOUBLE;
51   } else {
52     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Unknown PETSc datatype");
53   }
54   PetscFunctionReturn(0);
55 }
56 
57 #undef __FUNCT__
58 #define __FUNCT__ "PetscMPIDataTypeToPetscDataType"
59 /*@C
60      PetscMPIDataTypeToPetscDataType Finds the PETSc name of a datatype from its MPI name
61 
62    Not collective
63 
64     Input Parameter:
65 .     mtype - the MPI datatype (for example MPI_DOUBLE, ...)
66 
67     Output Parameter:
68 .     ptype - the PETSc datatype name (for example PETSC_DOUBLE)
69 
70     Level: advanced
71 
72 .seealso: PetscDataType, PetscMPIDataTypeToPetscDataType()
73 @*/
74 PetscErrorCode PETSC_DLLEXPORT PetscMPIDataTypeToPetscDataType(MPI_Datatype mtype,PetscDataType *ptype)
75 {
76   PetscFunctionBegin;
77   if (mtype == MPIU_INT) {
78     *ptype = PETSC_INT;
79   } else if (mtype == MPI_INT) {
80     *ptype = PETSC_INT;
81   } else if (mtype == MPI_DOUBLE) {
82     *ptype = PETSC_DOUBLE;
83 #if defined(PETSC_USE_COMPLEX)
84   } else if (mtype == MPI_C_DOUBLE_COMPLEX) {
85     *ptype = PETSC_COMPLEX;
86 #endif
87   } else if (mtype == MPI_LONG) {
88     *ptype = PETSC_LONG;
89   } else if (mtype == MPI_SHORT) {
90     *ptype = PETSC_SHORT;
91   } else if (mtype == MPI_FLOAT) {
92     *ptype = PETSC_FLOAT;
93   } else if (mtype == MPI_CHAR) {
94     *ptype = PETSC_CHAR;
95   } else if (mtype == MPI_LONG_DOUBLE) {
96     *ptype = PETSC_LONG_DOUBLE;
97   } else {
98     SETERRQ(PETSC_ERR_SUP,"Unhandled MPI datatype");
99   }
100   PetscFunctionReturn(0);
101 }
102 
103 typedef enum {PETSC_INT_SIZE = sizeof(PetscInt),PETSC_DOUBLE_SIZE = sizeof(double),
104               PETSC_COMPLEX_SIZE = sizeof(PetscScalar),PETSC_LONG_SIZE=sizeof(long),
105               PETSC_SHORT_SIZE = sizeof(short),PETSC_FLOAT_SIZE = sizeof(float),
106               PETSC_CHAR_SIZE = sizeof(char),PETSC_LOGICAL_SIZE = sizeof(char),
107               PETSC_ENUM_SIZE = sizeof(PetscTruth), PETSC_TRUTH_SIZE = sizeof(PetscTruth),
108               PETSC_LONG_DOUBLE_SIZE = sizeof(long double)} PetscDataTypeSize;
109 #if defined(PETSC_USE_COMPLEX)
110 #define PETSC_SCALAR_SIZE PETSC_COMPLEX_SIZE
111 #else
112 #define PETSC_SCALAR_SIZE PETSC_DOUBLE_SIZE
113 #endif
114 #if defined(PETSC_USE_SCALAR_SINGLE)
115 #define PETSC_REAL_SIZE PETSC_FLOAT_SIZE
116 #elif defined(PETSC_USE_SCALAR_LONG_DOUBLE)
117 #define PETSC_REAL_SIZE PETSC_LONG_DOUBLE_SIZE
118 #elif defined(PETSC_USE_SCALAR_INT)
119 #define PETSC_REAL_SIZE PETSC_INT_SIZE
120 #else
121 #define PETSC_REAL_SIZE PETSC_DOUBLE_SIZE
122 #endif
123 #define PETSC_FORTRANADDR_SIZE PETSC_LONG_SIZE
124 
125 
126 #undef __FUNCT__
127 #define __FUNCT__ "PetscDataTypeGetSize"
128 /*@
129      PetscDataTypeGetSize - Gets the size (in bytes) of a PETSc datatype
130 
131    Not collective
132 
133     Input Parameter:
134 .     ptype - the PETSc datatype name (for example PETSC_DOUBLE)
135 
136     Output Parameter:
137 .     size - the size in bytes (for example the size of PETSC_DOUBLE is 8)
138 
139     Level: advanced
140 
141 .seealso: PetscDataType, PetscDataTypeToMPIDataType()
142 @*/
143 PetscErrorCode PETSC_DLLEXPORT PetscDataTypeGetSize(PetscDataType ptype,size_t *size)
144 {
145   PetscFunctionBegin;
146   if ((int) ptype < 0) {
147     *size = -(int) ptype;
148     PetscFunctionReturn(0);
149   }
150 
151   if (ptype == PETSC_INT) {
152     *size = PETSC_INT_SIZE;
153   } else if (ptype == PETSC_DOUBLE) {
154     *size = PETSC_DOUBLE_SIZE;
155 #if defined(PETSC_USE_COMPLEX)
156   } else if (ptype == PETSC_COMPLEX) {
157     *size = PETSC_COMPLEX_SIZE;
158 #endif
159   } else if (ptype == PETSC_LONG) {
160     *size = PETSC_LONG_SIZE;
161   } else if (ptype == PETSC_SHORT) {
162     *size = PETSC_SHORT_SIZE;
163   } else if (ptype == PETSC_FLOAT) {
164     *size = PETSC_FLOAT_SIZE;
165   } else if (ptype == PETSC_CHAR) {
166     *size = PETSC_CHAR_SIZE;
167   } else if (ptype == PETSC_ENUM) {
168     *size = PETSC_ENUM_SIZE;
169   } else if (ptype == PETSC_LOGICAL) {
170     *size = PETSC_LOGICAL_SIZE;
171   } else if (ptype == PETSC_TRUTH) {
172     *size = PETSC_TRUTH_SIZE;
173   } else if (ptype == PETSC_LONG_DOUBLE) {
174     *size = PETSC_LONG_DOUBLE_SIZE;
175   } else {
176     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Unknown PETSc datatype");
177   }
178   PetscFunctionReturn(0);
179 }
180