Actual source code: ptype.c

petsc-3.10.5 2019-03-28
Report Typos and Errors

  2: /*
  3:      Provides utility routines for manipulating any type of PETSc object.
  4: */
  5:  #include <petscsys.h>

  7: /*@C
  8:      PetscDataTypeToMPIDataType - Converts the PETSc name of a datatype to its MPI name.

 10:    Not collective

 12:     Input Parameter:
 13: .     ptype - the PETSc datatype name (for example PETSC_DOUBLE)

 15:     Output Parameter:
 16: .     mtype - the MPI datatype (for example MPI_DOUBLE, ...)

 18:     Level: advanced

 20: .seealso: PetscDataType, PetscMPIDataTypeToPetscDataType()
 21: @*/
 22: PetscErrorCode  PetscDataTypeToMPIDataType(PetscDataType ptype,MPI_Datatype *mtype)
 23: {
 25:   if (ptype == PETSC_INT)              *mtype = MPIU_INT;
 26:   else if (ptype == PETSC_DOUBLE)      *mtype = MPI_DOUBLE;
 27: #if defined(PETSC_USE_COMPLEX)
 28: #if defined(PETSC_USE_REAL_SINGLE)
 29:   else if (ptype == PETSC_COMPLEX)     *mtype = MPIU_C_COMPLEX;
 30: #elif defined(PETSC_USE_REAL___FLOAT128)
 31:   else if (ptype == PETSC_COMPLEX)     *mtype = MPIU___COMPLEX128;
 32: #else
 33:   else if (ptype == PETSC_COMPLEX)     *mtype = MPIU_C_DOUBLE_COMPLEX;
 34: #endif
 35: #endif
 36:   else if (ptype == PETSC_LONG)        *mtype = MPI_LONG;
 37:   else if (ptype == PETSC_SHORT)       *mtype = MPI_SHORT;
 38:   else if (ptype == PETSC_ENUM)        *mtype = MPI_INT;
 39:   else if (ptype == PETSC_BOOL)        *mtype = MPI_INT;
 40:   else if (ptype == PETSC_FLOAT)       *mtype = MPI_FLOAT;
 41:   else if (ptype == PETSC_CHAR)        *mtype = MPI_CHAR;
 42:   else if (ptype == PETSC_BIT_LOGICAL) *mtype = MPI_BYTE;
 43: #if defined(PETSC_USE_REAL___FLOAT128)
 44:   else if (ptype == PETSC___FLOAT128)  *mtype = MPIU___FLOAT128;
 45: #elif defined(PETSC_USE_REAL___FP16)
 46:   else if (ptype == PETSC___FP16)  *mtype = MPIU___FP16;
 47: #endif
 48:   else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown PETSc datatype");
 49:   return(0);
 50: }

 52: /*@C
 53:      PetscMPIDataTypeToPetscDataType Finds the PETSc name of a datatype from its MPI name

 55:    Not collective

 57:     Input Parameter:
 58: .     mtype - the MPI datatype (for example MPI_DOUBLE, ...)

 60:     Output Parameter:
 61: .     ptype - the PETSc datatype name (for example PETSC_DOUBLE)

 63:     Level: advanced

 65: .seealso: PetscDataType, PetscMPIDataTypeToPetscDataType()
 66: @*/
 67: PetscErrorCode  PetscMPIDataTypeToPetscDataType(MPI_Datatype mtype,PetscDataType *ptype)
 68: {
 70:   if (mtype == MPIU_INT)             *ptype = PETSC_INT;
 71: #if defined(PETSC_USE_64BIT_INDICES)
 72:   else if (mtype == MPI_INT)         *ptype = PETSC_ENUM;
 73: #endif
 74:   else if (mtype == MPI_DOUBLE)      *ptype = PETSC_DOUBLE;
 75: #if defined(PETSC_USE_COMPLEX)
 76: #if defined(PETSC_USE_REAL_SINGLE)
 77:   else if (mtype == MPIU_C_COMPLEX)  *ptype = PETSC_COMPLEX;
 78: #elif defined(PETSC_USE_REAL___FLOAT128)
 79:   else if (mtype == MPIU___COMPLEX128) *ptype = PETSC_COMPLEX;
 80: #else
 81:   else if (mtype == MPIU_C_DOUBLE_COMPLEX) *ptype = PETSC_COMPLEX;
 82: #endif
 83: #endif
 84:   else if (mtype == MPI_LONG)        *ptype = PETSC_LONG;
 85:   else if (mtype == MPI_SHORT)       *ptype = PETSC_SHORT;
 86:   else if (mtype == MPI_FLOAT)       *ptype = PETSC_FLOAT;
 87:   else if (mtype == MPI_CHAR)        *ptype = PETSC_CHAR;
 88: #if defined(PETSC_USE_REAL___FLOAT128)
 89:   else if (mtype == MPIU___FLOAT128) *ptype = PETSC___FLOAT128;
 90: #elif defined(PETSC_USE_REAL___FP16)
 91:   else if (mtype == MPIU___FP16) *ptype = PETSC___FP16;
 92: #endif
 93:   else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unhandled MPI datatype");
 94:   return(0);
 95: }

 97: typedef enum {PETSC_INT_SIZE         = sizeof(PetscInt),
 98:               PETSC_DOUBLE_SIZE      = sizeof(double),
 99:               PETSC_COMPLEX_SIZE     = sizeof(PetscScalar),
100:               PETSC_LONG_SIZE        = sizeof(long),
101:               PETSC_SHORT_SIZE       = sizeof(short),
102:               PETSC_FLOAT_SIZE       = sizeof(float),
103:               PETSC_CHAR_SIZE        = sizeof(char),
104:               PETSC_BIT_LOGICAL_SIZE = sizeof(char),
105:               PETSC_ENUM_SIZE        = sizeof(PetscBool),
106:               PETSC_BOOL_SIZE        = sizeof(PetscBool)
107: #if defined(PETSC_USE_REAL___FLOAT128)
108:               ,PETSC___FLOAT128_SIZE  = sizeof(__float128)
109: #elif defined(PETSC_USE_REAL___FP16)
110:               ,PETSC___FP16_SIZE      = sizeof(__fp16)
111: #endif
112:              } PetscDataTypeSize;

114: /*@C
115:      PetscDataTypeGetSize - Gets the size (in bytes) of a PETSc datatype

117:    Not collective

119:     Input Parameter:
120: .     ptype - the PETSc datatype name (for example PETSC_DOUBLE)

122:     Output Parameter:
123: .     size - the size in bytes (for example the size of PETSC_DOUBLE is 8)

125:     Level: advanced

127: .seealso: PetscDataType, PetscDataTypeToMPIDataType()
128: @*/
129: PetscErrorCode  PetscDataTypeGetSize(PetscDataType ptype,size_t *size)
130: {
132:   if ((int) ptype < 0) {
133:     *size = -(int) ptype;
134:     return(0);
135:   }

137:   if (ptype == PETSC_INT)              *size = PETSC_INT_SIZE;
138:   else if (ptype == PETSC_DOUBLE)      *size = PETSC_DOUBLE_SIZE;
139: #if defined(PETSC_USE_COMPLEX)
140:   else if (ptype == PETSC_COMPLEX)     *size = PETSC_COMPLEX_SIZE;
141: #endif
142:   else if (ptype == PETSC_LONG)        *size = PETSC_LONG_SIZE;
143:   else if (ptype == PETSC_SHORT)       *size = PETSC_SHORT_SIZE;
144:   else if (ptype == PETSC_FLOAT)       *size = PETSC_FLOAT_SIZE;
145:   else if (ptype == PETSC_CHAR)        *size = PETSC_CHAR_SIZE;
146:   else if (ptype == PETSC_ENUM)        *size = PETSC_ENUM_SIZE;
147:   else if (ptype == PETSC_BIT_LOGICAL) *size = PETSC_BIT_LOGICAL_SIZE;
148:   else if (ptype == PETSC_BOOL)        *size = PETSC_BOOL_SIZE;
149: #if defined(PETSC_USE_REAL___FLOAT128)
150:   else if (ptype == PETSC___FLOAT128)  *size = PETSC___FLOAT128_SIZE;
151: #elif defined(PETSC_USE_REAL___FP16)
152:   else if (ptype == PETSC___FP16)      *size = PETSC___FP16_SIZE;
153: #endif
154:   else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown PETSc datatype");
155:   return(0);
156: }

158: /*@C
159:      PetscDataTypeFromString - Gets the enum value of a PETSc datatype represented as a string

161:    Not collective

163:     Input Parameter:
164: .     name - the PETSc datatype name (for example, DOUBLE or real or Scalar)

166:     Output Parameter:
167: +    ptype - the enum value
168: -    found - the string matches one of the data types

170:     Level: advanced

172: .seealso: PetscDataType, PetscDataTypeToMPIDataType(), PetscDataTypeGetSize()
173: @*/
174: PetscErrorCode  PetscDataTypeFromString(const char*name, PetscDataType *ptype,PetscBool *found)
175: {

179:   PetscEnumFind(PetscDataTypes,name,(PetscEnum*)ptype,found);
180:   if (!*found) {
181:     char formatted[16];

183:     PetscStrncpy(formatted,name,16);
184:     PetscStrtolower(formatted);
185:     PetscStrcmp(formatted,"scalar",found);
186:     if (*found) {
187:       *ptype = PETSC_SCALAR;
188:     } else {
189:       PetscStrcmp(formatted,"real",found);
190:       if (*found) {
191:         *ptype = PETSC_REAL;
192:       }
193:     }
194:   }
195:   return(0);
196: }