xref: /petsc/src/sys/objects/device/tests/ex3.c (revision f9fea11e2c44c506c9f9a3f6998afbe8a760174b)
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(0);
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  build:
75    requires: defined(PETSC_HAVE_CXX)
76 
77  testset:
78    output_file: ./output/ExitSuccess.out
79    nsize: {{1 4}}
80    args: -device_enable {{lazy eager}}
81    args: -local_device_context_stream_type {{global_blocking default_blocking global_nonblocking}}
82    test:
83      requires: !device
84      suffix: host_no_device
85    test:
86      requires: device
87      args: -default_device_type host -root_device_context_device_type host
88      suffix: host_with_device
89    test:
90      requires: cuda
91      args: -default_device_type cuda -root_device_context_device_type cuda
92      suffix: cuda
93    test:
94      requires: hip
95      args: -default_device_type hip -root_device_context_device_type hip
96      suffix: hip
97 
98 TEST*/
99