xref: /petsc/src/sys/objects/device/tests/ex1.c (revision 4dfa11a44d5adf2389f1d3acbc8f3c1116dc6c3a)
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   const PetscInt n      = 10;
7   PetscDevice    device = NULL;
8   PetscDevice    devices[n];
9 
10   PetscFunctionBeginUser;
11   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
12 
13   /* normal create and destroy */
14   PetscCall(PetscDeviceCreate(PETSC_DEVICE_DEFAULT(), PETSC_DECIDE, &device));
15   PetscCall(AssertDeviceExists(device));
16   PetscCall(PetscDeviceDestroy(&device));
17   PetscCall(AssertDeviceDoesNotExist(device));
18   /* should not destroy twice */
19   PetscCall(PetscDeviceDestroy(&device));
20   PetscCall(AssertDeviceDoesNotExist(device));
21 
22   /* test reference counting */
23   device = NULL;
24   PetscCall(PetscArrayzero(devices, n));
25   PetscCall(PetscDeviceCreate(PETSC_DEVICE_DEFAULT(), PETSC_DECIDE, &device));
26   PetscCall(AssertDeviceExists(device));
27   for (int i = 0; i < n; ++i) {
28     PetscCall(PetscDeviceReference_Internal(device));
29     devices[i] = device;
30   }
31   PetscCall(AssertDeviceExists(device));
32   for (int i = 0; i < n; ++i) {
33     PetscCall(PetscDeviceDestroy(&devices[i]));
34     PetscCall(AssertDeviceExists(device));
35     PetscCall(AssertDeviceDoesNotExist(devices[i]));
36   }
37   PetscCall(PetscDeviceDestroy(&device));
38   PetscCall(AssertDeviceDoesNotExist(device));
39 
40   /* test the default devices exist */
41   device = NULL;
42   PetscCall(PetscArrayzero(devices, n));
43   {
44     PetscDeviceContext dctx;
45     /* global context will have the default device */
46     PetscCall(PetscDeviceContextGetCurrentContext(&dctx));
47     PetscCall(PetscDeviceContextGetDevice(dctx, &device));
48   }
49   PetscCall(AssertDeviceExists(device));
50   /* test reference counting for default device */
51   for (int i = 0; i < n; ++i) {
52     PetscCall(PetscDeviceReference_Internal(device));
53     devices[i] = device;
54   }
55   PetscCall(AssertDeviceExists(device));
56   for (int i = 0; i < n; ++i) {
57     PetscCall(PetscDeviceDestroy(&devices[i]));
58     PetscCall(AssertDeviceExists(device));
59     PetscCall(AssertDeviceDoesNotExist(devices[i]));
60   }
61 
62   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "EXIT_SUCCESS\n"));
63   PetscCall(PetscFinalize());
64   return 0;
65 }
66 
67 /*TEST
68 
69  build:
70    requires: defined(PETSC_HAVE_CXX)
71 
72  testset:
73    output_file: ./output/ExitSuccess.out
74    nsize: {{1 2 5}}
75    args: -device_enable {{lazy eager}}
76    test:
77      requires: !device
78      suffix: host_no_device
79    test:
80      requires: device
81      args: -default_device_type host
82      suffix: host_with_device
83    test:
84      requires: cuda
85      args: -default_device_type cuda
86      suffix: cuda
87    test:
88      requires: hip
89      args: -default_device_type hip
90      suffix: hip
91    test:
92      requires: sycl
93      args: -default_device_type sycl
94      suffix: sycl
95 
96 TEST*/
97