xref: /petsc/src/sys/objects/device/tests/ex3.c (revision ff87db43c5082475ccdd828c5a732608447f441c)
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   PetscDevice        origDevice;
8   PetscStreamType    origStype;
9   PetscDeviceContext ddup;
10 
11   PetscFunctionBegin;
12   PetscValidDeviceContext(dctx, 1);
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   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   PetscValidDeviceContext(dctx, 1);
43   PetscFunctionReturn(0);
44 }
45 
46 int main(int argc, char *argv[]) {
47   MPI_Comm           comm;
48   PetscDeviceContext dctx;
49 
50   PetscFunctionBeginUser;
51   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
52   comm = PETSC_COMM_WORLD;
53 
54   /* basic creation and destruction */
55   PetscCall(PetscDeviceContextCreate(&dctx));
56   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dctx, "local_"));
57   PetscCall(PetscDeviceContextSetFromOptions(comm, dctx));
58   PetscCall(TestPetscDeviceContextDuplicate(dctx));
59   PetscCall(PetscDeviceContextDestroy(&dctx));
60 
61   PetscCall(PetscDeviceContextGetCurrentContext(&dctx));
62   PetscCall(TestPetscDeviceContextDuplicate(dctx));
63 
64   PetscCall(PetscPrintf(comm, "EXIT_SUCCESS\n"));
65   PetscCall(PetscFinalize());
66   return 0;
67 }
68 
69 /*TEST
70 
71  build:
72    requires: defined(PETSC_HAVE_CXX)
73 
74  testset:
75    output_file: ./output/ExitSuccess.out
76    nsize: {{1 4}}
77    args: -device_enable {{lazy eager}}
78    args: -local_device_context_stream_type {{global_blocking default_blocking global_nonblocking}}
79    test:
80      requires: !device
81      suffix: host_no_device
82    test:
83      requires: device
84      args: -default_device_type host -root_device_context_device_type host
85      suffix: host_with_device
86    test:
87      requires: cuda
88      args: -default_device_type cuda -root_device_context_device_type cuda
89      suffix: cuda
90    test:
91      requires: hip
92      args: -default_device_type hip -root_device_context_device_type hip
93      suffix: hip
94 
95 TEST*/
96