xref: /petsc/src/sys/objects/device/tests/ex1.c (revision f97672e55eacc8688507b9471cd7ec2664d7f203)
1 static const char help[] = "Tests creation and destruction of PetscDevice.\n\n";
2 
3 #include <petsc/private/deviceimpl.h>
4 #include "petscdevicetestcommon.h"
5 
6 int main(int argc, char *argv[])
7 {
8   const PetscInt n = 10;
9   PetscDevice    device = NULL;
10   PetscDevice    devices[n];
11 
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    TODO: broken in ci
75    requires: !device
76    suffix: no_device
77    filter: Error: grep -E -o -e ".*No support for this operation for this object type" -e ".*PETSc is not configured with device support.*" -e "^\[0\]PETSC ERROR:.*[0-9]{1} [A-z]+\(\)"
78    test:
79      requires: debug
80      suffix:   debug
81    test:
82      requires: !debug
83      suffix:   opt
84 
85  testset:
86    output_file: ./output/ExitSuccess.out
87    nsize: {{1 2 5}}
88    test:
89      requires: cuda
90      suffix: cuda
91    test:
92      requires: hip
93      suffix: hip
94    test:
95      requires: sycl
96      suffix: sycl
97 
98 TEST*/
99