xref: /petsc/src/sys/objects/device/tests/petscdevicetestcommon.h (revision 73fdd05bb67e49f40fd8fd311695ff6fdf0b9b8a)
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 {
19   PetscFunctionBegin;
20   PetscValidDevice(device, 1);
21   PetscFunctionReturn(PETSC_SUCCESS);
22 }
23 
24 static inline PetscErrorCode AssertDeviceDoesNotExist(PetscDevice device)
25 {
26   PetscFunctionBegin;
27   PetscCheck(!device, PETSC_COMM_SELF, PETSC_ERR_PLIB, "PetscDevice was not destroyed for type %s", PetscDeviceTypes[device->type]);
28   PetscFunctionReturn(PETSC_SUCCESS);
29 }
30 
31 static inline PetscErrorCode AssertDeviceContextExists(PetscDeviceContext dctx)
32 {
33   PetscFunctionBegin;
34   PetscValidDeviceContext(dctx, 1);
35   PetscFunctionReturn(PETSC_SUCCESS);
36 }
37 
38 static inline PetscErrorCode AssertDeviceContextDoesNotExist(PetscDeviceContext dctx)
39 {
40   PetscFunctionBegin;
41   PetscCheck(!dctx, PETSC_COMM_SELF, PETSC_ERR_PLIB, "PetscDeviceContext was not destroyed");
42   PetscFunctionReturn(PETSC_SUCCESS);
43 }
44 
45 static inline PetscErrorCode AssertPetscStreamTypesValidAndEqual(PetscStreamType left, PetscStreamType right, const char *errStr)
46 {
47   PetscFunctionBegin;
48   PetscValidStreamType(left, 1);
49   PetscValidStreamType(right, 2);
50   PetscCheck(left == right, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, errStr, PetscStreamTypes[left], PetscStreamTypes[right]);
51   PetscFunctionReturn(PETSC_SUCCESS);
52 }
53 
54 static inline PetscErrorCode AssertPetscDeviceTypesValidAndEqual(PetscDeviceType left, PetscDeviceType right, const char *errStr)
55 {
56   PetscFunctionBegin;
57   PetscValidDeviceType(left, 1);
58   PetscValidDeviceType(right, 2);
59   PetscCheck(left == right, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, errStr, PetscDeviceTypes[left], PetscDeviceTypes[right]);
60   PetscFunctionReturn(PETSC_SUCCESS);
61 }
62 
63 static inline PetscErrorCode AssertPetscDevicesValidAndEqual(PetscDevice left, PetscDevice right, const char *errStr)
64 {
65   PetscFunctionBegin;
66   PetscCheckCompatibleDevices(left, 1, right, 2);
67   PetscCheck(left == right, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "%s: %p != %p", errStr, left, right);
68   PetscFunctionReturn(PETSC_SUCCESS);
69 }
70 
71 static inline PetscErrorCode AssertPetscDeviceContextsValidAndEqual(PetscDeviceContext left, PetscDeviceContext right, const char *errStr)
72 {
73   PetscFunctionBegin;
74   PetscCheckCompatibleDeviceContexts(left, 1, right, 2);
75   PetscCheck(left == right, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "%s", errStr);
76   PetscFunctionReturn(PETSC_SUCCESS);
77 }
78 #endif /* PETSCDEVICETESTCOMMON_H */
79