Date: 2022-03-31T04:46:07+00:00

Actual source code: ptype.c

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

      7: const char *const PetscDataTypes[] = {"UNKNOWN",
      8:                                       "DOUBLE","COMPLEX","LONG","SHORT","FLOAT",
      9:                                       "CHAR","LOGICAL","ENUM","BOOL","LONGDOUBLE",
     10:                                       "OBJECT","FUNCTION","STRING","FP16","STRUCT",
     11:                                       "INT","INT64",
     12:                                       "PetscDataType","PETSC_",NULL};

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

     17:    Not collective

     19:     Input Parameter:
     20: .     ptype - the PETSc datatype name (for example PETSC_DOUBLE)

     22:     Output Parameter:
     23: .     mtype - the MPI datatype (for example MPI_DOUBLE, ...)

     25:     Level: advanced

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

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

     62:    Not collective

     64:     Input Parameter:
     65: .     mtype - the MPI datatype (for example MPI_DOUBLE, ...)

     67:     Output Parameter:
     68: .     ptype - the PETSc datatype name (for example PETSC_DOUBLE)

     70:     Level: advanced

     72: .seealso: PetscDataType, PetscMPIDataTypeToPetscDataType()
     73: @*/
     74: PetscErrorCode  PetscMPIDataTypeToPetscDataType(MPI_Datatype mtype,PetscDataType *ptype)
     75: {
     76:   if (mtype == MPIU_INT)             *ptype = PETSC_INT;
     77: #if defined(PETSC_USE_64BIT_INDICES)
     78:   else if (mtype == MPI_INT)         *ptype = PETSC_ENUM;
     79: #endif
     80:   else if (mtype == MPIU_INT64)      *ptype = PETSC_INT64;
     81:   else if (mtype == MPI_DOUBLE)      *ptype = PETSC_DOUBLE;
     82: #if defined(PETSC_HAVE_COMPLEX)
     83: #if defined(PETSC_USE_REAL_SINGLE)
     84:   else if (mtype == MPI_C_COMPLEX)   *ptype = PETSC_COMPLEX;
     85: #elif defined(PETSC_USE_REAL___FLOAT128)
     86:   else if (mtype == MPIU___COMPLEX128) *ptype = PETSC_COMPLEX;
     87: #else
     88:   else if (mtype == MPI_C_DOUBLE_COMPLEX) *ptype = PETSC_COMPLEX;
     89: #endif
     90: #endif
     91:   else if (mtype == MPI_LONG)        *ptype = PETSC_LONG;
     92:   else if (mtype == MPI_SHORT)       *ptype = PETSC_SHORT;
     93:   else if (mtype == MPI_FLOAT)       *ptype = PETSC_FLOAT;
     94:   else if (mtype == MPI_CHAR)        *ptype = PETSC_CHAR;
     95: #if defined(PETSC_USE_REAL___FLOAT128)
     96:   else if (mtype == MPIU___FLOAT128) *ptype = PETSC___FLOAT128;
     97: #elif defined(PETSC_USE_REAL___FP16)
     98:   else if (mtype == MPIU___FP16) *ptype = PETSC___FP16;
     99: #endif
    100:   else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unhandled MPI datatype");
    101:   return 0;
    102: }

    104: typedef enum {PETSC_INT_SIZE         = sizeof(PetscInt),
    105:               PETSC_DOUBLE_SIZE      = sizeof(double),
    106: #if defined(PETSC_HAVE_COMPLEX)
    107:               PETSC_COMPLEX_SIZE     = sizeof(PetscComplex),
    108: #else
    109:               PETSC_COMPLEX_SIZE     = 2*sizeof(PetscReal),
    110: #endif
    111:               PETSC_LONG_SIZE        = sizeof(long),
    112:               PETSC_SHORT_SIZE       = sizeof(short),
    113:               PETSC_FLOAT_SIZE       = sizeof(float),
    114:               PETSC_CHAR_SIZE        = sizeof(char),
    115:               PETSC_ENUM_SIZE        = sizeof(PetscEnum),
    116:               PETSC_BOOL_SIZE        = sizeof(PetscBool),
    117:               PETSC_INT64_SIZE       = sizeof(PetscInt64),
    118:               PETSC_BIT_LOGICAL_SIZE = sizeof(char)
    119: #if defined(PETSC_USE_REAL___FLOAT128)
    120:               ,PETSC___FLOAT128_SIZE  = sizeof(__float128)
    121: #elif defined(PETSC_USE_REAL___FP16)
    122:               ,PETSC___FP16_SIZE      = sizeof(__fp16)
    123: #endif
    124:              } PetscDataTypeSize;

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

    129:    Not collective

    131:     Input Parameter:
    132: .     ptype - the PETSc datatype name (for example PETSC_DOUBLE)

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

    137:     Level: advanced

    139: .seealso: PetscDataType, PetscDataTypeToMPIDataType()
    140: @*/
    141: PetscErrorCode  PetscDataTypeGetSize(PetscDataType ptype,size_t *size)
    142: {
    143:   if ((int) ptype < 0)                 *size = -(int)ptype;
    144:   else if (ptype == PETSC_INT)         *size = PETSC_INT_SIZE;
    145:   else if (ptype == PETSC_DOUBLE)      *size = PETSC_DOUBLE_SIZE;
    146:   else if (ptype == PETSC_COMPLEX)     *size = PETSC_COMPLEX_SIZE;
    147:   else if (ptype == PETSC_LONG)        *size = PETSC_LONG_SIZE;
    148:   else if (ptype == PETSC_SHORT)       *size = PETSC_SHORT_SIZE;
    149:   else if (ptype == PETSC_FLOAT)       *size = PETSC_FLOAT_SIZE;
    150:   else if (ptype == PETSC_CHAR)        *size = PETSC_CHAR_SIZE;
    151:   else if (ptype == PETSC_ENUM)        *size = PETSC_ENUM_SIZE;
    152:   else if (ptype == PETSC_BOOL)        *size = PETSC_BOOL_SIZE;
    153:   else if (ptype == PETSC_INT64)       *size = PETSC_INT64_SIZE;
    154:   else if (ptype == PETSC_BIT_LOGICAL) *size = PETSC_BIT_LOGICAL_SIZE;
    155: #if defined(PETSC_USE_REAL___FLOAT128)
    156:   else if (ptype == PETSC___FLOAT128)  *size = PETSC___FLOAT128_SIZE;
    157: #elif defined(PETSC_USE_REAL___FP16)
    158:   else if (ptype == PETSC___FP16)      *size = PETSC___FP16_SIZE;
    159: #endif
    160:   else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown PETSc datatype");
    161:   return 0;
    162: }

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

    167:    Not collective

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

    172:     Output Parameters:
    173: +    ptype - the enum value
    174: -    found - the string matches one of the data types

    176:     Level: advanced

    178: .seealso: PetscDataType, PetscDataTypeToMPIDataType(), PetscDataTypeGetSize()
    179: @*/
    180: PetscErrorCode  PetscDataTypeFromString(const char*name, PetscDataType *ptype,PetscBool *found)
    181: {
    182:   PetscEnumFind(PetscDataTypes,name,(PetscEnum*)ptype,found);
    183:   if (!*found) {
    184:     char formatted[16];

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