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