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