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