xref: /petsc/src/sys/objects/device/tests/petscdevicetestcommon.h (revision f4d061e980d13bc62f06124c58b76593bdf99e72)
1 #ifndef PETSCDEVICETESTCOMMON_H
2 #define PETSCDEVICETESTCOMMON_H
3 
4 /* this file needs to be the one to include petsc/private/deviceimpl.h since it needs to define
5  * a special macro to ensure that the error checking macros stay defined even in optimized
6  * builds
7  */
8 #if defined(PETSCDEVICEIMPL_H)
9 #error "must #include this file before petsc/private/deviceimpl.h"
10 #endif
11 
12 #if !defined(PETSC_DEVICE_KEEP_ERROR_CHECKING_MACROS)
13 #define PETSC_DEVICE_KEEP_ERROR_CHECKING_MACROS 1
14 #endif
15 #include <petsc/private/deviceimpl.h>
16 
17 static inline PetscErrorCode AssertDeviceExists(PetscDevice device) {
18   PetscFunctionBegin;
19   PetscValidDevice(device, 1);
20   PetscFunctionReturn(0);
21 }
22 
23 static inline PetscErrorCode AssertDeviceDoesNotExist(PetscDevice device) {
24   PetscFunctionBegin;
25   PetscCheck(!device, PETSC_COMM_SELF, PETSC_ERR_PLIB, "PetscDevice was not destroyed for type %s", PetscDeviceTypes[device->type]);
26   PetscFunctionReturn(0);
27 }
28 
29 static inline PetscErrorCode AssertDeviceContextExists(PetscDeviceContext dctx) {
30   PetscFunctionBegin;
31   PetscValidDeviceContext(dctx, 1);
32   PetscFunctionReturn(0);
33 }
34 
35 static inline PetscErrorCode AssertDeviceContextDoesNotExist(PetscDeviceContext dctx) {
36   PetscFunctionBegin;
37   PetscCheck(!dctx, PETSC_COMM_SELF, PETSC_ERR_PLIB, "PetscDeviceContext was not destroyed");
38   PetscFunctionReturn(0);
39 }
40 
41 static inline PetscErrorCode AssertPetscStreamTypesValidAndEqual(PetscStreamType left, PetscStreamType right, const char *errStr) {
42   PetscFunctionBegin;
43   PetscValidStreamType(left, 1);
44   PetscValidStreamType(right, 2);
45   PetscCheck(left == right, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, errStr, PetscStreamTypes[left], PetscStreamTypes[right]);
46   PetscFunctionReturn(0);
47 }
48 
49 static inline PetscErrorCode AssertPetscDeviceTypesValidAndEqual(PetscDeviceType left, PetscDeviceType right, const char *errStr) {
50   PetscFunctionBegin;
51   PetscValidDeviceType(left, 1);
52   PetscValidDeviceType(right, 2);
53   PetscCheck(left == right, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, errStr, PetscDeviceTypes[left], PetscDeviceTypes[right]);
54   PetscFunctionReturn(0);
55 }
56 
57 static inline PetscErrorCode AssertPetscDevicesValidAndEqual(PetscDevice left, PetscDevice right, const char *errStr) {
58   PetscFunctionBegin;
59   PetscCheckCompatibleDevices(left, 1, right, 2);
60   PetscCheck(left == right, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "%s", errStr);
61   PetscFunctionReturn(0);
62 }
63 
64 static inline PetscErrorCode AssertPetscDeviceContextsValidAndEqual(PetscDeviceContext left, PetscDeviceContext right, const char *errStr) {
65   PetscFunctionBegin;
66   PetscCheckCompatibleDeviceContexts(left, 1, right, 2);
67   PetscCheck(left == right, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "%s", errStr);
68   PetscFunctionReturn(0);
69 }
70 #endif /* PETSCDEVICETESTCOMMON_H */
71