xref: /petsc/src/sys/objects/device/tests/petscdevicetestcommon.h (revision 9371c9d470a9602b6d10a8bf50c9b2280a79e45a)
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__) \
13   do { \
14     PetscCheck(((int)(_p_dev_type__) >= (int)PETSC_DEVICE_INVALID) && ((_p_dev_type__) <= PETSC_DEVICE_MAX), PETSC_COMM_SELF, PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscDeviceType '%d': Argument #%d", (_p_dev_type__), (_p_arg__)); \
15     if (PetscUnlikely(!PetscDeviceConfiguredFor_Internal(_p_dev_type__))) { \
16       switch (_p_dev_type__) { \
17       case PETSC_DEVICE_INVALID: \
18         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, \
19                 "Invalid PetscDeviceType '%s': Argument #%d;" \
20                 " PETSc is not configured with device support", \
21                 PetscDeviceTypes[_p_dev_type__], (_p_arg__)); \
22         break; \
23       case PETSC_DEVICE_MAX: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Invalid PetscDeviceType '%s': Argument #%d", PetscDeviceTypes[_p_dev_type__], (_p_arg__)); break; \
24       default: \
25         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, \
26                 "Not configured for PetscDeviceType '%s': Argument #%d;" \
27                 " run configure --help %s for available options", \
28                 PetscDeviceTypes[_p_dev_type__], (_p_arg__), PetscDeviceTypes[_p_dev_type__]); \
29         break; \
30       } \
31     } \
32   } while (0)
33 
34 #undef PetscValidDevice
35 #define PetscValidDevice(_p_dev__, _p_arg__) \
36   do { \
37     PetscValidPointer(_p_dev__, _p_arg__); \
38     PetscValidDeviceType((_p_dev__)->type, _p_arg__); \
39     PetscCheck((_p_dev__)->id >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid PetscDevice: Argument #%d; id %" PetscInt_FMT " < 0", (_p_arg__), (_p_dev__)->id); \
40     PetscCheck((_p_dev__)->refcnt >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid PetscDevice: Argument #%d; negative reference count %" PetscInt_FMT, (_p_arg__), (_p_dev__)->refcnt); \
41   } while (0)
42 
43 #undef PetscCheckCompatibleDevices
44 #define PetscCheckCompatibleDevices(_p_dev1__, _p_arg1__, _p_dev2__, _p_arg2__) \
45   do { \
46     PetscValidDevice(_p_dev1__, _p_arg1__); \
47     PetscValidDevice(_p_dev2__, _p_arg2__); \
48     PetscCheck((_p_dev1__)->type == (_p_dev2__)->type, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "PetscDevices are incompatible: Arguments #%d and #%d", (_p_arg1__), (_p_arg2__)); \
49   } while (0)
50 
51 #undef PetscValidStreamType
52 #define PetscValidStreamType(_p_strm_type__, _p_arg__) \
53   do { \
54     PetscCheck(((int)(_p_strm_type__) >= 0) && ((_p_strm_type__) <= PETSC_STREAM_MAX), PETSC_COMM_SELF, PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscStreamType '%d': Argument #%d", (_p_strm_type__), (_p_arg__)); \
55     PetscCheck((_p_strm_type__) != PETSC_STREAM_MAX, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Invalid PetscStreamType '%s': Argument #%d", PetscStreamTypes[_p_strm_type__], (_p_arg__)); \
56   } while (0)
57 
58 #undef PetscValidDeviceContext
59 #define PetscValidDeviceContext(_p_dev_ctx__, _p_arg__) \
60   do { \
61     PetscValidPointer(_p_dev_ctx__, _p_arg__); \
62     PetscValidStreamType((_p_dev_ctx__)->streamType, _p_arg__); \
63     if ((_p_dev_ctx__)->device) PetscValidDevice((_p_dev_ctx__)->device, _p_arg__); \
64     else \
65       PetscCheck(!((_p_dev_ctx__)->setup), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, \
66                  "Invalid PetscDeviceContext: Argument #%d; " \
67                  "PetscDeviceContext is setup but has no PetscDevice", \
68                  (_p_arg__)); \
69     PetscCheck((_p_dev_ctx__)->id >= 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid PetscDeviceContext: Argument #%d; id %" PetscInt_FMT " < 1", (_p_arg__), (_p_dev_ctx__)->id); \
70     PetscCheck((_p_dev_ctx__)->numChildren <= (_p_dev_ctx__)->maxNumChildren, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Invalid PetscDeviceContext: Argument #%d; number of children %" PetscInt_FMT " > max number of children %" PetscInt_FMT, (_p_arg__), \
71                (_p_dev_ctx__)->numChildren, (_p_dev_ctx__)->maxNumChildren); \
72   } while (0)
73 
74 #undef PetscCheckCompatibleDeviceContexts
75 #define PetscCheckCompatibleDeviceContexts(_p_dev_ctx1__, _p_arg1__, _p_dev_ctx2__, _p_arg2__) \
76   do { \
77     PetscValidDeviceContext(_p_dev_ctx1__, _p_arg1__); \
78     PetscValidDeviceContext(_p_dev_ctx2__, _p_arg2__); \
79     PetscCheckCompatibleDevices((_p_dev_ctx1__)->device, _p_arg1__, (_p_dev_ctx2__)->device, _p_arg2__); \
80   } while (0)
81 
82 /*  This header file should NEVER #include another file and should be the last thing included
83  *  in the test file. This is to guard against ill-formed PetscDevice header files!
84  */
85 static inline PetscErrorCode AssertDeviceExists(PetscDevice device) {
86   PetscFunctionBegin;
87   PetscValidDevice(device, 1);
88   PetscFunctionReturn(0);
89 }
90 
91 static inline PetscErrorCode AssertDeviceDoesNotExist(PetscDevice device) {
92   PetscFunctionBegin;
93   PetscCheck(!device, PETSC_COMM_SELF, PETSC_ERR_PLIB, "PetscDevice was not destroyed for type %s", PetscDeviceTypes[device->type]);
94   PetscFunctionReturn(0);
95 }
96 
97 static inline PetscErrorCode AssertDeviceContextExists(PetscDeviceContext dctx) {
98   PetscFunctionBegin;
99   PetscValidDeviceContext(dctx, 1);
100   PetscFunctionReturn(0);
101 }
102 
103 static inline PetscErrorCode AssertDeviceContextDoesNotExist(PetscDeviceContext dctx) {
104   PetscFunctionBegin;
105   PetscCheck(!dctx, PETSC_COMM_SELF, PETSC_ERR_PLIB, "PetscDeviceContext was not destroyed");
106   PetscFunctionReturn(0);
107 }
108 
109 static inline PetscErrorCode AssertPetscStreamTypesValidAndEqual(PetscStreamType left, PetscStreamType right, const char *errStr) {
110   PetscFunctionBegin;
111   PetscValidStreamType(left, 1);
112   PetscValidStreamType(right, 2);
113   PetscCheck(left == right, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, errStr, PetscStreamTypes[left], PetscStreamTypes[right]);
114   PetscFunctionReturn(0);
115 }
116 
117 static inline PetscErrorCode AssertPetscDevicesValidAndEqual(PetscDevice left, PetscDevice right, const char *errStr) {
118   PetscFunctionBegin;
119   PetscCheckCompatibleDevices(left, 1, right, 2);
120   PetscCheck(left == right, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "%s", errStr);
121   PetscFunctionReturn(0);
122 }
123 
124 static inline PetscErrorCode AssertPetscDeviceContextsValidAndEqual(PetscDeviceContext left, PetscDeviceContext right, const char *errStr) {
125   PetscFunctionBegin;
126   PetscCheckCompatibleDeviceContexts(left, 1, right, 2);
127   PetscCheck(left == right, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "%s", errStr);
128   PetscFunctionReturn(0);
129 }
130 #endif /* PETSCDEVICETESTCOMMON_H */
131