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