xref: /petsc/src/sys/objects/device/tests/ex3.c (revision 9371c9d470a9602b6d10a8bf50c9b2280a79e45a)
1 static const char help[] = "Tests PetscDeviceContextDuplicate.\n\n";
2 
3 #include <petsc/private/deviceimpl.h>
4 #include "petscdevicetestcommon.h"
5 
6 /* test duplication creates the same object type */
7 static PetscErrorCode TestPetscDeviceContextDuplicate(PetscDeviceContext dctx) {
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   PetscDeviceContext dctx;
49 
50   PetscFunctionBeginUser;
51   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
52 
53   /* basic creation and destruction */
54   PetscCall(PetscDeviceContextCreate(&dctx));
55   PetscCall(PetscDeviceContextSetFromOptions(PETSC_COMM_WORLD, "local_", dctx));
56   PetscCall(PetscDeviceContextSetUp(dctx));
57   PetscCall(TestPetscDeviceContextDuplicate(dctx));
58   PetscCall(PetscDeviceContextDestroy(&dctx));
59 
60   PetscCall(PetscDeviceContextGetCurrentContext(&dctx));
61   PetscCall(TestPetscDeviceContextDuplicate(dctx));
62 
63   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "EXIT_SUCCESS\n"));
64   PetscCall(PetscFinalize());
65   return 0;
66 }
67 
68 /*TEST
69 
70  build:
71    requires: defined(PETSC_HAVE_CXX)
72 
73  testset:
74    TODO: broken in ci
75    requires: !device
76    suffix: no_device
77    filter: Error: grep -E -o -e ".*No support for this operation for this object type" -e ".*PETSc is not configured with device support.*" -e "^\[0\]PETSC ERROR:.*[0-9]{1} [A-z]+\(\)"
78 
79  testset:
80    output_file: ./output/ExitSuccess.out
81    nsize: {{1 4}}
82    args: -local_device_context_stream_type {{global_blocking default_blocking global_nonblocking}}
83    test:
84      requires: cuda
85      suffix: cuda
86    test:
87      requires: hip
88      suffix: hip
89 
90 TEST*/
91