xref: /petsc/src/sys/objects/device/tests/ex1.c (revision b122ec5aa1bd4469eb4e0673542fb7de3f411254)
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   CHKERRQ(PetscInitialize(&argc,&argv,NULL,help));
13 
14   /* normal create and destroy */
15   CHKERRQ(PetscDeviceCreate(PETSC_DEVICE_DEFAULT,PETSC_DECIDE,&device));
16   CHKERRQ(AssertDeviceExists(device));
17   CHKERRQ(PetscDeviceDestroy(&device));
18   CHKERRQ(AssertDeviceDoesNotExist(device));
19   /* should not destroy twice */
20   CHKERRQ(PetscDeviceDestroy(&device));
21   CHKERRQ(AssertDeviceDoesNotExist(device));
22 
23   /* test reference counting */
24   device = NULL;
25   CHKERRQ(PetscArrayzero(devices,n));
26   CHKERRQ(PetscDeviceCreate(PETSC_DEVICE_DEFAULT,PETSC_DECIDE,&device));
27   CHKERRQ(AssertDeviceExists(device));
28   for (int i = 0; i < n; ++i) {
29     CHKERRQ(PetscDeviceReference_Internal(device));
30     devices[i] = device;
31   }
32   CHKERRQ(AssertDeviceExists(device));
33   for (int i = 0; i < n; ++i) {
34     CHKERRQ(PetscDeviceDestroy(&devices[i]));
35     CHKERRQ(AssertDeviceExists(device));
36     CHKERRQ(AssertDeviceDoesNotExist(devices[i]));
37   }
38   CHKERRQ(PetscDeviceDestroy(&device));
39   CHKERRQ(AssertDeviceDoesNotExist(device));
40 
41   /* test the default devices exist */
42   device = NULL;
43   CHKERRQ(PetscArrayzero(devices,n));
44   {
45     PetscDeviceContext dctx;
46     /* global context will have the default device */
47     CHKERRQ(PetscDeviceContextGetCurrentContext(&dctx));
48     CHKERRQ(PetscDeviceContextGetDevice(dctx,&device));
49   }
50   CHKERRQ(AssertDeviceExists(device));
51   /* test reference counting for default device */
52   for (int i = 0; i < n; ++i) {
53     CHKERRQ(PetscDeviceReference_Internal(device));
54     devices[i] = device;
55   }
56   CHKERRQ(AssertDeviceExists(device));
57   for (int i = 0; i < n; ++i) {
58     CHKERRQ(PetscDeviceDestroy(&devices[i]));
59     CHKERRQ(AssertDeviceExists(device));
60     CHKERRQ(AssertDeviceDoesNotExist(devices[i]));
61   }
62 
63   CHKERRQ(PetscPrintf(PETSC_COMM_WORLD,"EXIT_SUCCESS\n"));
64   CHKERRQ(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