xref: /petsc/src/sys/objects/device/tests/ex3.c (revision d52a580b706c59ca78066c1e38754e45b6b56e2b)
1 static const char help[] = "Tests PetscDeviceContextDuplicate.\n\n";
2 
3 #include "petscdevicetestcommon.h"
4 
5 /* test duplication creates the same object type */
6 static PetscErrorCode TestPetscDeviceContextDuplicate(PetscDeviceContext dctx)
7 {
8   PetscDevice        origDevice;
9   PetscStreamType    origStype;
10   PetscDeviceContext ddup;
11 
12   PetscFunctionBegin;
13   /* get everything we want first before any duplication */
14   PetscCall(PetscDeviceContextGetStreamType(dctx, &origStype));
15   PetscCall(PetscDeviceContextGetDevice(dctx, &origDevice));
16 
17   /* duplicate */
18   PetscCall(PetscDeviceContextDuplicate(dctx, &ddup));
19   PetscValidDeviceContext(ddup, 2);
20   if (dctx) PetscCheckCompatibleDeviceContexts(dctx, 1, ddup, 2);
21 
22   {
23     PetscDevice parDevice, dupDevice;
24 
25     PetscCall(PetscDeviceContextGetDevice(dctx, &parDevice));
26     PetscCall(AssertPetscDevicesValidAndEqual(parDevice, origDevice, "Parent PetscDevice after duplication does not match parent original PetscDevice"));
27     PetscCall(PetscDeviceContextGetDevice(ddup, &dupDevice));
28     PetscCall(AssertPetscDevicesValidAndEqual(dupDevice, origDevice, "Duplicated PetscDevice does not match parent original PetscDevice"));
29   }
30 
31   {
32     PetscStreamType parStype, dupStype;
33 
34     PetscCall(PetscDeviceContextGetStreamType(dctx, &parStype));
35     PetscCall(AssertPetscStreamTypesValidAndEqual(parStype, origStype, "Parent PetscStreamType after duplication does not match parent original PetscStreamType"));
36     PetscCall(PetscDeviceContextGetStreamType(ddup, &dupStype));
37     PetscCall(AssertPetscStreamTypesValidAndEqual(dupStype, origStype, "Duplicated PetscStreamType '%s' does not match parent original PetscStreamType '%s'"));
38   }
39 
40   PetscCall(PetscDeviceContextDestroy(&ddup));
41   /* duplicate should not take the original down with it */
42   if (dctx) PetscValidDeviceContext(dctx, 1);
43   PetscFunctionReturn(PETSC_SUCCESS);
44 }
45 
46 int main(int argc, char *argv[])
47 {
48   MPI_Comm           comm;
49   PetscDeviceContext dctx;
50 
51   PetscFunctionBeginUser;
52   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
53   comm = PETSC_COMM_WORLD;
54 
55   /* basic creation and destruction */
56   PetscCall(PetscDeviceContextCreate(&dctx));
57   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dctx, "local_"));
58   PetscCall(PetscDeviceContextSetFromOptions(comm, dctx));
59   PetscCall(TestPetscDeviceContextDuplicate(dctx));
60   PetscCall(PetscDeviceContextDestroy(&dctx));
61 
62   PetscCall(PetscDeviceContextGetCurrentContext(&dctx));
63   PetscCall(TestPetscDeviceContextDuplicate(dctx));
64 
65   PetscCall(TestPetscDeviceContextDuplicate(NULL));
66 
67   PetscCall(PetscPrintf(comm, "EXIT_SUCCESS\n"));
68   PetscCall(PetscFinalize());
69   return 0;
70 }
71 
72 /*TEST
73 
74   testset:
75     requires: defined(PETSC_DEVICELANGUAGE_CXX)
76     output_file: output/ExitSuccess.out
77     nsize: {{1 4}}
78     args: -device_enable {{lazy eager}}
79     args: -local_device_context_stream_type {{default nonblocking default_with_barrier nonblocking_with_barrier}}
80     test:
81       requires: !device
82       suffix: host_no_device
83     test:
84       requires: device
85       args: -default_device_type host -root_device_context_device_type host
86       suffix: host_with_device
87     test:
88       requires: cuda
89       args: -default_device_type cuda -root_device_context_device_type cuda
90       suffix: cuda
91     test:
92       requires: hip
93       args: -default_device_type hip -root_device_context_device_type hip
94       suffix: hip
95     test:
96       requires: sycl
97       args: -default_device_type sycl -root_device_context_device_type sycl
98       suffix: sycl
99 
100 TEST*/
101