Actual source code: ptype.c

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

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

 12:    Not collective

 14:     Input Parameter:
 15: .     ptype - the PETSc datatype name (for example PETSC_DOUBLE)

 17:     Output Parameter:
 18: .     mtype - the MPI datatype (for example MPI_DOUBLE, ...)

 20:     Level: advanced
 21:    
 22: .seealso: PetscDataType, PetscMPIDataTypeToPetscDataType()
 23: @*/
 24: PetscErrorCode  PetscDataTypeToMPIDataType(PetscDataType ptype,MPI_Datatype* mtype)
 25: {
 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_SCALAR_SINGLE)
 33:   } else if (ptype == PETSC_COMPLEX) {
 34:     *mtype = MPI_C_COMPLEX;
 35: #else
 36:   } else if (ptype == PETSC_COMPLEX) {
 37:     *mtype = MPI_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_TRUTH) {
 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_LOGICAL) {
 53:     *mtype = MPI_BYTE;
 54:   } else if (ptype == PETSC_LONG_DOUBLE) {
 55:     *mtype = MPI_LONG_DOUBLE;
 56:   } else {
 57:     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Unknown PETSc datatype");
 58:   }
 59:   return(0);
 60: }

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

 67:    Not collective

 69:     Input Parameter:
 70: .     mtype - the MPI datatype (for example MPI_DOUBLE, ...)

 72:     Output Parameter:
 73: .     ptype - the PETSc datatype name (for example PETSC_DOUBLE)

 75:     Level: advanced
 76:    
 77: .seealso: PetscDataType, PetscMPIDataTypeToPetscDataType()
 78: @*/
 79: PetscErrorCode  PetscMPIDataTypeToPetscDataType(MPI_Datatype mtype,PetscDataType *ptype)
 80: {
 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_SCALAR_SINGLE)
 90:   } else if (mtype == MPI_C_COMPLEX) {
 91:     *ptype = PETSC_COMPLEX;
 92: #else
 93:   } else if (mtype == MPI_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_ERR_SUP,"Unhandled MPI datatype");
109:   }
110:   return(0);
111: }

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_LOGICAL_SIZE = sizeof(char),
117:               PETSC_ENUM_SIZE = sizeof(PetscTruth), PETSC_TRUTH_SIZE = sizeof(PetscTruth),
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_SCALAR_SINGLE)
125: #define PETSC_REAL_SIZE PETSC_FLOAT_SIZE
126: #elif defined(PETSC_USE_SCALAR_LONG_DOUBLE)
127: #define PETSC_REAL_SIZE PETSC_LONG_DOUBLE_SIZE
128: #elif defined(PETSC_USE_SCALAR_INT)
129: #define PETSC_REAL_SIZE PETSC_INT_SIZE
130: #else
131: #define PETSC_REAL_SIZE PETSC_DOUBLE_SIZE
132: #endif
133: #define PETSC_FORTRANADDR_SIZE PETSC_LONG_SIZE


138: /*@
139:      PetscDataTypeGetSize - Gets the size (in bytes) of a PETSc datatype

141:    Not collective

143:     Input Parameter:
144: .     ptype - the PETSc datatype name (for example PETSC_DOUBLE)

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

149:     Level: advanced
150:    
151: .seealso: PetscDataType, PetscDataTypeToMPIDataType()
152: @*/
153: PetscErrorCode  PetscDataTypeGetSize(PetscDataType ptype,size_t *size)
154: {
156:   if ((int) ptype < 0) {
157:     *size = -(int) ptype;
158:     return(0);
159:   }

161:   if (ptype == PETSC_INT) {
162:     *size = PETSC_INT_SIZE;
163:   } else if (ptype == PETSC_DOUBLE) {
164:     *size = PETSC_DOUBLE_SIZE;
165: #if defined(PETSC_USE_COMPLEX)
166:   } else if (ptype == PETSC_COMPLEX) {
167:     *size = PETSC_COMPLEX_SIZE;
168: #endif
169:   } else if (ptype == PETSC_LONG) {
170:     *size = PETSC_LONG_SIZE;
171:   } else if (ptype == PETSC_SHORT) {
172:     *size = PETSC_SHORT_SIZE;
173:   } else if (ptype == PETSC_FLOAT) {
174:     *size = PETSC_FLOAT_SIZE;
175:   } else if (ptype == PETSC_CHAR) {
176:     *size = PETSC_CHAR_SIZE;
177:   } else if (ptype == PETSC_ENUM) {
178:     *size = PETSC_ENUM_SIZE;
179:   } else if (ptype == PETSC_LOGICAL) {
180:     *size = PETSC_LOGICAL_SIZE;
181:   } else if (ptype == PETSC_TRUTH) {
182:     *size = PETSC_TRUTH_SIZE;
183:   } else if (ptype == PETSC_LONG_DOUBLE) {
184:     *size = PETSC_LONG_DOUBLE_SIZE;
185:   } else {
186:     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Unknown PETSc datatype");
187:   }
188:   return(0);
189: }