xref: /petsc/src/sys/objects/device/tests/ex1.c (revision ccfb0f9f40a0131988d7995ed9679700dae2a75a)
1 static const char help[] = "Tests creation and destruction of PetscDevice.\n\n";
2 
3 #include "petscdevicetestcommon.h"
4 
5 int main(int argc, char *argv[])
6 {
7   const PetscInt n      = 10;
8   PetscDevice    device = NULL;
9   PetscDevice    devices[10];
10 
11   PetscFunctionBeginUser;
12   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
13   // would have just done
14   //
15   // const PetscInt n = 10;
16   // PetscDevice devices[n];
17   //
18   // but alas the reliably insane MSVC balks at this to the tune of
19   // 'ex1.c(9): error C2057: expected constant expression'. So instead we have a runtime check
20   PetscCheck(PETSC_STATIC_ARRAY_LENGTH(devices) == n, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Forgot to update n");
21 
22   /* normal create and destroy */
23   PetscCall(PetscDeviceCreate(PETSC_DEVICE_DEFAULT(), PETSC_DECIDE, &device));
24   PetscCall(AssertDeviceExists(device));
25   PetscCall(PetscDeviceDestroy(&device));
26   PetscCall(AssertDeviceDoesNotExist(device));
27   /* should not destroy twice */
28   PetscCall(PetscDeviceDestroy(&device));
29   PetscCall(AssertDeviceDoesNotExist(device));
30 
31   /* test reference counting */
32   device = NULL;
33   PetscCall(PetscArrayzero(devices, n));
34   PetscCall(PetscDeviceCreate(PETSC_DEVICE_DEFAULT(), PETSC_DECIDE, &device));
35   PetscCall(AssertDeviceExists(device));
36   for (int i = 0; i < n; ++i) {
37     PetscCall(PetscDeviceReference_Internal(device));
38     devices[i] = device;
39   }
40   PetscCall(AssertDeviceExists(device));
41   for (int i = 0; i < n; ++i) {
42     PetscCall(PetscDeviceDestroy(&devices[i]));
43     PetscCall(AssertDeviceExists(device));
44     PetscCall(AssertDeviceDoesNotExist(devices[i]));
45   }
46   PetscCall(PetscDeviceDestroy(&device));
47   PetscCall(AssertDeviceDoesNotExist(device));
48 
49   /* test the default devices exist */
50   device = NULL;
51   PetscCall(PetscArrayzero(devices, n));
52   {
53     PetscDeviceContext dctx;
54     /* global context will have the default device */
55     PetscCall(PetscDeviceContextGetCurrentContext(&dctx));
56     PetscCall(PetscDeviceContextGetDevice(dctx, &device));
57   }
58   PetscCall(AssertDeviceExists(device));
59   /* test reference counting for default device */
60   for (int i = 0; i < n; ++i) {
61     PetscCall(PetscDeviceReference_Internal(device));
62     devices[i] = device;
63   }
64   PetscCall(AssertDeviceExists(device));
65   for (int i = 0; i < n; ++i) {
66     PetscCall(PetscDeviceDestroy(&devices[i]));
67     PetscCall(AssertDeviceExists(device));
68     PetscCall(AssertDeviceDoesNotExist(devices[i]));
69   }
70 
71   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "EXIT_SUCCESS\n"));
72   PetscCall(PetscFinalize());
73   return 0;
74 }
75 
76 /*TEST
77 
78   testset:
79     requires: defined(PETSC_DEVICELANGUAGE_CXX)
80     output_file: output/ExitSuccess.out
81     nsize: {{1 2 5}}
82     args: -device_enable {{none 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   testset:
104     requires: !defined(PETSC_DEVICE_LANGUAGE_CXX)
105     output_file: output/ExitSuccess.out
106     suffix: no_cxx
107 
108 TEST*/
109