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