xref: /petsc/src/sys/objects/device/tests/ex10.c (revision 4ad8454beace47809662cdae21ee081016eaa39a)
1 static const char help[] = "Tests PetscDeviceContextSetStreamType().\n\n";
2 
3 #include "petscdevicetestcommon.h"
4 
5 int main(int argc, char *argv[])
6 {
7   const PetscStreamType stypes[] = {
8 #if PetscDefined(HAVE_CXX)
9     PETSC_STREAM_DEFAULT,
10     PETSC_STREAM_NONBLOCKING,
11     PETSC_STREAM_DEFAULT_WITH_BARRIER,
12     PETSC_STREAM_NONBLOCKING_WITH_BARRIER
13 #else
14     PETSC_STREAM_DEFAULT,
15 #endif
16   };
17   const PetscInt ntypes = PETSC_STATIC_ARRAY_LENGTH(stypes);
18 
19   PetscFunctionBeginUser;
20   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
21 
22   // test that get-set trivially work
23   for (PetscInt i = 0; i < ntypes; ++i) {
24     PetscDeviceContext tmp;
25     PetscStreamType    tmp_type;
26 
27     PetscCall(PetscDeviceContextCreate(&tmp));
28     PetscCall(PetscDeviceContextSetStreamType(tmp, stypes[i]));
29     PetscCall(PetscDeviceContextGetStreamType(tmp, &tmp_type));
30     PetscCall(AssertPetscStreamTypesValidAndEqual(tmp_type, stypes[i], "Set PetscDeviceStreamType %s does not match expected %s"));
31     // test that any combination of get-set trivially works
32     for (PetscInt j = 0; j < ntypes; ++j) {
33       PetscCall(PetscDeviceContextSetStreamType(tmp, stypes[j]));
34       PetscCall(PetscDeviceContextGetStreamType(tmp, &tmp_type));
35       PetscCall(AssertPetscStreamTypesValidAndEqual(tmp_type, stypes[j], "Set PetscDeviceStreamType %s does not match expected %s"));
36       // reset it back to original
37       PetscCall(PetscDeviceContextSetStreamType(tmp, stypes[i]));
38     }
39     PetscCall(PetscDeviceContextDestroy(&tmp));
40   }
41 
42   // test that any combination of get-set works when set up
43   for (PetscInt i = 0; i < ntypes; ++i) {
44     for (PetscInt j = 0; j < ntypes; ++j) {
45       PetscDeviceContext tmp;
46       PetscStreamType    tmp_type;
47 
48       PetscCall(PetscDeviceContextCreate(&tmp));
49       // check this works through setup
50       PetscCall(PetscDeviceContextSetStreamType(tmp, stypes[i]));
51       PetscCall(PetscDeviceContextSetUp(tmp));
52       PetscCall(PetscDeviceContextGetStreamType(tmp, &tmp_type));
53       PetscCall(AssertPetscStreamTypesValidAndEqual(tmp_type, stypes[i], "Set PetscDeviceStreamType %s does not match expected %s after PetscDeviceContextSetUp"));
54       // now change the stream type
55       PetscCall(PetscDeviceContextSetStreamType(tmp, stypes[j]));
56       PetscCall(PetscDeviceContextGetStreamType(tmp, &tmp_type));
57       PetscCall(AssertPetscStreamTypesValidAndEqual(tmp_type, stypes[j], "Set PetscDeviceStreamType %s does not match expected %s when changing after PetscDeviceContextSetUp"));
58       // reset it back to original
59       PetscCall(PetscDeviceContextSetStreamType(tmp, stypes[i]));
60       // and ensure this works
61       PetscCall(PetscDeviceContextGetStreamType(tmp, &tmp_type));
62       PetscCall(AssertPetscStreamTypesValidAndEqual(tmp_type, stypes[i], "Set PetscDeviceStreamType %s does not match expected %s after setting back to original"));
63       // finally set up again
64       PetscCall(PetscDeviceContextSetUp(tmp));
65       // and ensure it has not changed
66       PetscCall(PetscDeviceContextGetStreamType(tmp, &tmp_type));
67       PetscCall(AssertPetscStreamTypesValidAndEqual(tmp_type, stypes[i], "Set PetscDeviceStreamType %s does not match expected %s after setting back to original and PetscDeviceContextSetUp"));
68       PetscCall(PetscDeviceContextDestroy(&tmp));
69     }
70   }
71 
72   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "EXIT_SUCCESS\n"));
73   PetscCall(PetscFinalize());
74   return 0;
75 }
76 
77 /*TEST
78 
79   testset:
80     requires: cxx
81     output_file: ./output/ExitSuccess.out
82     args: -device_enable {{lazy eager}}
83     test:
84       requires: !device
85       suffix: host_no_device
86     test:
87       requires: device
88       args: -default_device_type host
89       suffix: host_with_device
90     test:
91       requires: cuda
92       args: -default_device_type cuda
93       suffix: cuda
94     test:
95       requires: hip
96       args: -default_device_type hip
97       suffix: hip
98     test:
99       requires: sycl
100       args: -default_device_type sycl
101       suffix: sycl
102 
103   test:
104     requires: !cxx
105     output_file: ./output/ExitSuccess.out
106     suffix: no_cxx
107 
108 TEST*/
109