xref: /petsc/src/sys/objects/ptype.c (revision 357abbc83298e1b9fe4d8101e73ddb06b38979df)
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
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 = MPIU_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 typedef enum {PETSC_INT_SIZE = sizeof(PetscInt),PETSC_DOUBLE_SIZE = sizeof(double),
58               PETSC_COMPLEX_SIZE = sizeof(PetscScalar),PETSC_LONG_SIZE=sizeof(long),
59               PETSC_SHORT_SIZE = sizeof(short),PETSC_FLOAT_SIZE = sizeof(float),
60               PETSC_CHAR_SIZE = sizeof(char),PETSC_LOGICAL_SIZE = sizeof(char),
61               PETSC_ENUM_SIZE = sizeof(PetscTruth), PETSC_TRUTH_SIZE = sizeof(PetscTruth),
62               PETSC_LONG_DOUBLE_SIZE = sizeof(long double)} PetscDataTypeSize;
63 #if defined(PETSC_USE_COMPLEX)
64 #define PETSC_SCALAR_SIZE PETSC_COMPLEX_SIZE
65 #else
66 #define PETSC_SCALAR_SIZE PETSC_DOUBLE_SIZE
67 #endif
68 #if defined(PETSC_USE_SINGLE)
69 #define PETSC_REAL_SIZE PETSC_FLOAT_SIZE
70 #elif defined(PETSC_USE_LONG_DOUBLE)
71 #define PETSC_REAL_SIZE PETSC_LONG_DOUBLE_SIZE
72 #elif defined(PETSC_USE_INT)
73 #define PETSC_REAL_SIZE PETSC_INT_SIZE
74 #else
75 #define PETSC_REAL_SIZE PETSC_DOUBLE_SIZE
76 #endif
77 #define PETSC_FORTRANADDR_SIZE PETSC_LONG_SIZE
78 
79 
80 #undef __FUNCT__
81 #define __FUNCT__ "PetscDataTypeGetSize"
82 /*@
83      PetscDataTypeGetSize - Gets the size (in bytes) of a PETSc datatype
84 
85    Not collective
86 
87     Input Parameter:
88 .     ptype - the PETSc datatype name (for example PETSC_DOUBLE)
89 
90     Output Parameter:
91 .     size - the size in bytes (for example the size of PETSC_DOUBLE is 8)
92 
93     Level: advanced
94 
95 .seealso: PetscDataType, PetscDataTypeToMPIDataType()
96 @*/
97 PetscErrorCode PETSC_DLLEXPORT PetscDataTypeGetSize(PetscDataType ptype,PetscInt *size)
98 {
99   PetscFunctionBegin;
100   if (ptype == PETSC_INT) {
101     *size = PETSC_INT_SIZE;
102   } else if (ptype == PETSC_DOUBLE) {
103     *size = PETSC_DOUBLE_SIZE;
104 #if defined(PETSC_USE_COMPLEX)
105   } else if (ptype == PETSC_COMPLEX) {
106     *size = PETSC_COMPLEX_SIZE;
107 #endif
108   } else if (ptype == PETSC_LONG) {
109     *size = PETSC_LONG_SIZE;
110   } else if (ptype == PETSC_SHORT) {
111     *size = PETSC_SHORT_SIZE;
112   } else if (ptype == PETSC_FLOAT) {
113     *size = PETSC_FLOAT_SIZE;
114   } else if (ptype == PETSC_CHAR) {
115     *size = PETSC_CHAR_SIZE;
116   } else if (ptype == PETSC_ENUM) {
117     *size = PETSC_ENUM_SIZE;
118   } else if (ptype == PETSC_LOGICAL) {
119     *size = PETSC_LOGICAL_SIZE;
120   } else if (ptype == PETSC_TRUTH) {
121     *size = PETSC_TRUTH_SIZE;
122   } else if (ptype == PETSC_LONG_DOUBLE) {
123     *size = PETSC_LONG_DOUBLE_SIZE;
124   } else {
125     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Unknown PETSc datatype");
126   }
127   PetscFunctionReturn(0);
128 }
129