Actual source code: ptype.c
1: /*
2: Provides utility routines for manipulating any type of PETSc object.
3: */
4: #include petsc.h
8: /*@
9: PetscDataTypeToMPIDataType - Converts the PETSc name of a datatype to its MPI name.
11: Not collective
13: Input Parameter:
14: . ptype - the PETSc datatype name (for example PETSC_DOUBLE)
16: Output Parameter:
17: . mtype - the MPI datatype (for example MPI_DOUBLE, ...)
19: Level: advanced
20:
21: .seealso: PetscDataType, PetscDataTypeGetName()
22: @*/
23: PetscErrorCode PetscDataTypeToMPIDataType(PetscDataType ptype,MPI_Datatype* mtype)
24: {
26: if (ptype == PETSC_INT) {
27: *mtype = MPIU_INT;
28: } else if (ptype == PETSC_DOUBLE) {
29: *mtype = MPI_DOUBLE;
30: #if defined(PETSC_USE_COMPLEX)
31: } else if (ptype == PETSC_COMPLEX) {
32: *mtype = MPIU_COMPLEX;
33: #endif
34: } else if (ptype == PETSC_LONG) {
35: *mtype = MPI_LONG;
36: } else if (ptype == PETSC_SHORT) {
37: *mtype = MPI_SHORT;
38: } else if (ptype == PETSC_FLOAT) {
39: *mtype = MPI_FLOAT;
40: } else if (ptype == PETSC_CHAR) {
41: *mtype = MPI_CHAR;
42: } else if (ptype == PETSC_LOGICAL) {
43: *mtype = MPI_BYTE;
44: } else {
45: SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Unknown PETSc datatype");
46: }
47: return(0);
48: }
50: typedef enum {PETSC_INT_SIZE = sizeof(PetscInt),PETSC_DOUBLE_SIZE = sizeof(double),
51: PETSC_COMPLEX_SIZE = sizeof(PetscScalar),PETSC_LONG_SIZE=sizeof(long),
52: PETSC_SHORT_SIZE = sizeof(short),PETSC_FLOAT_SIZE = sizeof(float),
53: PETSC_CHAR_SIZE = sizeof(char),PETSC_LOGICAL_SIZE = 1} PetscDataTypeSize;
54: #if defined(PETSC_USE_COMPLEX)
55: #define PETSC_SCALAR_SIZE PETSC_COMPLEX_SIZE
56: #else
57: #define PETSC_SCALAR_SIZE PETSC_DOUBLE_SIZE
58: #endif
59: #if defined(PETSC_USE_SINGLE)
60: #define PETSC_REAL_SIZE PETSC_FLOAT_SIZE
61: #else
62: #define PETSC_REAL_SIZE PETSC_DOUBLE_SIZE
63: #endif
64: #define PETSC_FORTRANADDR_SIZE PETSC_LONG_SIZE
69: /*@
70: PetscDataTypeGetSize - Gets the size (in bytes) of a PETSc datatype
72: Not collective
74: Input Parameter:
75: . ptype - the PETSc datatype name (for example PETSC_DOUBLE)
77: Output Parameter:
78: . size - the size in bytes (for example the size of PETSC_DOUBLE is 8)
80: Level: advanced
81:
82: .seealso: PetscDataType, PetscDataTypeGetName(), PetscDataTypeToMPIDataType()
83: @*/
84: PetscErrorCode PetscDataTypeGetSize(PetscDataType ptype,PetscInt *size)
85: {
87: if (ptype == PETSC_INT) {
88: *size = PETSC_INT_SIZE;
89: } else if (ptype == PETSC_DOUBLE) {
90: *size = PETSC_DOUBLE_SIZE;
91: #if defined(PETSC_USE_COMPLEX)
92: } else if (ptype == PETSC_COMPLEX) {
93: *size = PETSC_COMPLEX_SIZE;
94: #endif
95: } else if (ptype == PETSC_LONG) {
96: *size = PETSC_LONG_SIZE;
97: } else if (ptype == PETSC_SHORT) {
98: *size = PETSC_SHORT_SIZE;
99: } else if (ptype == PETSC_FLOAT) {
100: *size = PETSC_FLOAT_SIZE;
101: } else if (ptype == PETSC_CHAR) {
102: *size = PETSC_CHAR_SIZE;
103: } else if (ptype == PETSC_LOGICAL) {
104: *size = PETSC_LOGICAL_SIZE;
105: } else {
106: SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Unknown PETSc datatype");
107: }
108: return(0);
109: }
113: /*@C
114: PetscDataTypeGetName - Gets the string representation of a PETSc datatype
116: Not collective
118: Input Parameter:
119: . ptype - the PETSc datatype name (for example PETSC_DOUBLE)
121: Output Parameter:
122: . name - the name as a string (for example "double")
124: Level: advanced
125:
126: .seealso: PetscDataType, PetscDataTypeGetSize(), PetscDataTypeToMPIDataType()
127: @*/
128: PetscErrorCode PetscDataTypeGetName(PetscDataType ptype,const char *name[])
129: {
131: if (ptype == PETSC_INT) {
132: *name = "int";
133: } else if (ptype == PETSC_DOUBLE) {
134: *name = "double";
135: #if defined(PETSC_USE_COMPLEX)
136: } else if (ptype == PETSC_COMPLEX) {
137: *name = "complex";
138: #endif
139: } else if (ptype == PETSC_LONG) {
140: *name = "long";
141: } else if (ptype == PETSC_SHORT) {
142: *name = "short";
143: } else if (ptype == PETSC_FLOAT) {
144: *name = "float";
145: } else if (ptype == PETSC_CHAR) {
146: *name = "char";
147: } else if (ptype == PETSC_LOGICAL) {
148: *name = "logical";
149: } else {
150: SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Unknown PETSc datatype");
151: }
152: return(0);
153: }