xref: /petsc/src/sys/objects/device/tests/petscdevicetestcommon.h (revision f97672e55eacc8688507b9471cd7ec2664d7f203)
1 #ifndef PETSCDEVICE_H
2 #error "included this file before petscdevice.h, this file must be included last to ensure that public petsc headers are well formed"
3 #endif
4 #ifndef PETSCDEVICETESTCOMMON_H
5 #define PETSCDEVICETESTCOMMON_H
6 
7 /* all of the error checking macros are undefined and redefined verbatim so that they are also
8  * defined for optimized builds.
9  */
10 
11 #undef  PetscValidDeviceType
12 #define PetscValidDeviceType(_p_dev_type__,_p_arg__) do {                                      \
13     PetscCheck(                                                                               \
14       ((int)(_p_dev_type__) >= (int)PETSC_DEVICE_INVALID) && ((_p_dev_type__) <= PETSC_DEVICE_MAX), \
15       PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscDeviceType '%d': Argument #%d", \
16       (_p_dev_type__),(_p_arg__)                                                               \
17     );                                                                                         \
18     if (PetscUnlikely(!PetscDeviceConfiguredFor_Internal(_p_dev_type__))) {                    \
19       switch(_p_dev_type__) {                                                                  \
20       case PETSC_DEVICE_INVALID:                                                               \
21         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Invalid PetscDeviceType '%s': Argument #%d;"    \
22                 " PETSc is not configured with device support",                                \
23                 PetscDeviceTypes[_p_dev_type__],(_p_arg__));                                   \
24         break;                                                                                 \
25       case PETSC_DEVICE_MAX:                                                                   \
26         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,                                          \
27                 "Invalid PetscDeviceType '%s': Argument #%d",                                  \
28                 PetscDeviceTypes[_p_dev_type__],(_p_arg__));                                   \
29         break;                                                                                 \
30       default:                                                                                 \
31         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,                                                 \
32                 "Not configured for PetscDeviceType '%s': Argument #%d;"                       \
33                 " run configure --help %s for available options",                              \
34                 PetscDeviceTypes[_p_dev_type__],(_p_arg__),PetscDeviceTypes[_p_dev_type__]);   \
35         break;                                                                                 \
36       }                                                                                        \
37     }                                                                                          \
38   } while (0)
39 
40 #undef  PetscValidDevice
41 #define PetscValidDevice(_p_dev__,_p_arg__)          do {                                      \
42     PetscValidPointer(_p_dev__,_p_arg__);                                                      \
43     PetscValidDeviceType((_p_dev__)->type,_p_arg__);                                           \
44     PetscCheck(                                                                               \
45       (_p_dev__)->id >= 0,PETSC_COMM_SELF,PETSC_ERR_PLIB,                                      \
46       "Invalid PetscDevice: Argument #%d; id %" PetscInt_FMT " < 0",(_p_arg__),(_p_dev__)->id  \
47     );                                                                                         \
48     PetscCheck(                                                                               \
49       (_p_dev__)->refcnt >= 0,PETSC_COMM_SELF,PETSC_ERR_PLIB,                                  \
50       "Invalid PetscDevice: Argument #%d; negative reference count %" PetscInt_FMT,            \
51       (_p_arg__),(_p_dev__)->refcnt                                                            \
52     );                                                                                         \
53   } while (0)
54 
55 #undef  PetscCheckCompatibleDevices
56 #define PetscCheckCompatibleDevices(_p_dev1__,_p_arg1__,_p_dev2__,_p_arg2__) do {       \
57     PetscValidDevice(_p_dev1__,_p_arg1__);                                              \
58     PetscValidDevice(_p_dev2__,_p_arg2__);                                              \
59     PetscCheck(                                                                        \
60       (_p_dev1__)->type == (_p_dev2__)->type,PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,      \
61       "PetscDevices are incompatible: Arguments #%d and #%d",(_p_arg1__),(_p_arg2__)    \
62     );                                                                                  \
63   } while (0)
64 
65 #undef  PetscValidStreamType
66 #define PetscValidStreamType(_p_strm_type__,_p_arg__)  do {                                    \
67     PetscCheck(                                                                               \
68       ((int)(_p_strm_type__) >= 0) && ((_p_strm_type__) <= PETSC_STREAM_MAX), \
69       PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscStreamType '%d': Argument #%d", \
70       (_p_strm_type__),(_p_arg__)                                                              \
71     );                                                                                         \
72     PetscCheck(                                                                               \
73       (_p_strm_type__) != PETSC_STREAM_MAX,PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,               \
74       "Invalid PetscStreamType '%s': Argument #%d",PetscStreamTypes[_p_strm_type__],(_p_arg__) \
75     );                                                                                         \
76   } while (0)
77 
78 #undef  PetscValidDeviceContext
79 #define PetscValidDeviceContext(_p_dev_ctx__,_p_arg__) do {                                    \
80     PetscValidPointer(_p_dev_ctx__,_p_arg__);                                                  \
81     PetscValidStreamType((_p_dev_ctx__)->streamType,_p_arg__);                                 \
82     if ((_p_dev_ctx__)->device) PetscValidDevice((_p_dev_ctx__)->device,_p_arg__);             \
83     else PetscCheck(                                                                          \
84       !((_p_dev_ctx__)->setup),PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,                       \
85       "Invalid PetscDeviceContext: Argument #%d; "                                             \
86       "PetscDeviceContext is setup but has no PetscDevice",(_p_arg__)                          \
87     );                                                                                         \
88     PetscCheck(                                                                               \
89       (_p_dev_ctx__)->id >= 1,PETSC_COMM_SELF,PETSC_ERR_PLIB,                                  \
90       "Invalid PetscDeviceContext: Argument #%d; id %" PetscInt_FMT " < 1",                    \
91       (_p_arg__),(_p_dev_ctx__)->id                                                            \
92     );                                                                                         \
93     PetscCheck(                                                                               \
94       (_p_dev_ctx__)->numChildren <= (_p_dev_ctx__)->maxNumChildren,PETSC_COMM_SELF,           \
95       PETSC_ERR_ARG_CORRUPT,"Invalid PetscDeviceContext: Argument #%d; number of children %"   \
96       PetscInt_FMT " > max number of children %" PetscInt_FMT,                                 \
97       (_p_arg__),(_p_dev_ctx__)->numChildren,(_p_dev_ctx__)->maxNumChildren                    \
98     );                                                                                         \
99   } while (0)
100 
101 #undef  PetscCheckCompatibleDeviceContexts
102 #define PetscCheckCompatibleDeviceContexts(_p_dev_ctx1__,_p_arg1__,_p_dev_ctx2__,_p_arg2__)    \
103   do {                                                                                         \
104     PetscValidDeviceContext(_p_dev_ctx1__,_p_arg1__);                                          \
105     PetscValidDeviceContext(_p_dev_ctx2__,_p_arg2__);                                          \
106     PetscCheckCompatibleDevices(                                                               \
107       (_p_dev_ctx1__)->device,_p_arg1__,(_p_dev_ctx2__)->device,_p_arg2__                      \
108     );                                                                                         \
109   } while (0)
110 
111 /*  This header file should NEVER #include another file and should be the last thing included
112  *  in the test file. This is to guard against ill-formed PetscDevice header files!
113  */
114 static inline PetscErrorCode AssertDeviceExists(PetscDevice device)
115 {
116   PetscFunctionBegin;
117   PetscValidDevice(device,1);
118   PetscFunctionReturn(0);
119 }
120 
121 static inline PetscErrorCode AssertDeviceDoesNotExist(PetscDevice device)
122 {
123   PetscFunctionBegin;
124   PetscCheck(!device,PETSC_COMM_SELF,PETSC_ERR_PLIB,"PetscDevice was not destroyed for type %s",PetscDeviceTypes[device->type]);
125   PetscFunctionReturn(0);
126 }
127 
128 static inline PetscErrorCode AssertDeviceContextExists(PetscDeviceContext dctx)
129 {
130   PetscFunctionBegin;
131   PetscValidDeviceContext(dctx,1);
132   PetscFunctionReturn(0);
133 }
134 
135 static inline PetscErrorCode AssertDeviceContextDoesNotExist(PetscDeviceContext dctx)
136 {
137   PetscFunctionBegin;
138   PetscCheck(!dctx,PETSC_COMM_SELF,PETSC_ERR_PLIB,"PetscDeviceContext was not destroyed");
139   PetscFunctionReturn(0);
140 }
141 
142 static inline PetscErrorCode AssertPetscStreamTypesValidAndEqual(PetscStreamType left, PetscStreamType right, const char *errStr)
143 {
144   PetscFunctionBegin;
145   PetscValidStreamType(left,1);
146   PetscValidStreamType(right,2);
147   PetscCheck(left == right,PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,errStr,PetscStreamTypes[left],PetscStreamTypes[right]);
148   PetscFunctionReturn(0);
149 }
150 
151 static inline PetscErrorCode AssertPetscDevicesValidAndEqual(PetscDevice left, PetscDevice right, const char *errStr)
152 {
153   PetscFunctionBegin;
154   PetscCheckCompatibleDevices(left,1,right,2);
155   PetscCheck(left == right,PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"%s",errStr);
156   PetscFunctionReturn(0);
157 }
158 
159 static inline PetscErrorCode AssertPetscDeviceContextsValidAndEqual(PetscDeviceContext left, PetscDeviceContext right, const char *errStr)
160 {
161   PetscFunctionBegin;
162   PetscCheckCompatibleDeviceContexts(left,1,right,2);
163   PetscCheck(left == right,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"%s",errStr);
164   PetscFunctionReturn(0);
165 }
166 #endif /* PETSCDEVICETESTCOMMON_H */
167