xref: /petsc/src/sys/objects/device/tests/ex3.c (revision c3e4dd79e17d3f0a51a524340fae4661630fd09e)
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 {
9   PetscDevice        origDevice;
10   PetscStreamType    origStype;
11   PetscDeviceContext ddup;
12 
13   PetscFunctionBegin;
14   PetscValidDeviceContext(dctx,1);
15   /* get everything we want first before any duplication */
16   PetscCall(PetscDeviceContextGetStreamType(dctx,&origStype));
17   PetscCall(PetscDeviceContextGetDevice(dctx,&origDevice));
18 
19   /* duplicate */
20   PetscCall(PetscDeviceContextDuplicate(dctx,&ddup));
21   PetscValidDeviceContext(ddup,2);
22   PetscCheckCompatibleDeviceContexts(dctx,1,ddup,2);
23 
24   {
25     PetscDevice parDevice,dupDevice;
26 
27     PetscCall(PetscDeviceContextGetDevice(dctx,&parDevice));
28     PetscCall(AssertPetscDevicesValidAndEqual(parDevice,origDevice,"Parent PetscDevice after duplication does not match parent original PetscDevice"));
29     PetscCall(PetscDeviceContextGetDevice(ddup,&dupDevice));
30     PetscCall(AssertPetscDevicesValidAndEqual(dupDevice,origDevice,"Duplicated PetscDevice does not match parent original PetscDevice"));
31   }
32 
33   {
34     PetscStreamType parStype,dupStype;
35 
36     PetscCall(PetscDeviceContextGetStreamType(dctx,&parStype));
37     PetscCall(AssertPetscStreamTypesValidAndEqual(parStype,origStype,"Parent PetscStreamType after duplication does not match parent original PetscStreamType"));
38     PetscCall(PetscDeviceContextGetStreamType(ddup,&dupStype));
39     PetscCall(AssertPetscStreamTypesValidAndEqual(dupStype,origStype,"Duplicated PetscStreamType '%s' does not match parent original PetscStreamType '%s'"));
40   }
41 
42   PetscCall(PetscDeviceContextDestroy(&ddup));
43   /* duplicate should not take the original down with it */
44   PetscValidDeviceContext(dctx,1);
45   PetscFunctionReturn(0);
46 }
47 
48 int main(int argc, char *argv[])
49 {
50   PetscDeviceContext dctx;
51 
52   PetscCall(PetscInitialize(&argc,&argv,NULL,help));
53 
54   /* basic creation and destruction */
55   PetscCall(PetscDeviceContextCreate(&dctx));
56   PetscCall(PetscDeviceContextSetFromOptions(PETSC_COMM_WORLD,"local_",dctx));
57   PetscCall(PetscDeviceContextSetUp(dctx));
58   PetscCall(TestPetscDeviceContextDuplicate(dctx));
59   PetscCall(PetscDeviceContextDestroy(&dctx));
60 
61   PetscCall(PetscDeviceContextGetCurrentContext(&dctx));
62   PetscCall(TestPetscDeviceContextDuplicate(dctx));
63 
64   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"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    TODO: broken in ci
76    requires: !device
77    suffix: no_device
78    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]+\(\)"
79 
80  testset:
81    output_file: ./output/ExitSuccess.out
82    nsize: {{1 4}}
83    args: -local_device_context_stream_type {{global_blocking default_blocking global_nonblocking}}
84    test:
85      requires: cuda
86      suffix: cuda
87    test:
88      requires: hip
89      suffix: hip
90 
91 TEST*/
92