xref: /petsc/src/sys/objects/device/tests/ex2.c (revision 5f80ce2ab25dff0f4601e710601cbbcecf323266)
1 static const char help[] = "Tests creation and destruction of PetscDeviceContext.\n\n";
2 
3 #include <petsc/private/deviceimpl.h>
4 #include "petscdevicetestcommon.h"
5 
6 int main(int argc, char *argv[])
7 {
8   PetscDeviceContext dctx = NULL,ddup = NULL;
9   PetscErrorCode     ierr;
10 
11   ierr = PetscInitialize(&argc,&argv,NULL,help);if (ierr) return ierr;
12 
13   /* basic creation and destruction */
14   CHKERRQ(PetscDeviceContextCreate(&dctx));
15   CHKERRQ(AssertDeviceContextExists(dctx));
16   CHKERRQ(PetscDeviceContextDestroy(&dctx));
17   CHKERRQ(AssertDeviceContextDoesNotExist(dctx));
18   /* double free is no-op */
19   CHKERRQ(PetscDeviceContextDestroy(&dctx));
20   CHKERRQ(AssertDeviceContextDoesNotExist(dctx));
21 
22   /* test global context returns a valid context */
23   dctx = NULL;
24   CHKERRQ(PetscDeviceContextGetCurrentContext(&dctx));
25   CHKERRQ(AssertDeviceContextExists(dctx));
26   /* test locally setting to null doesn't clobber the global */
27   dctx = NULL;
28   CHKERRQ(PetscDeviceContextGetCurrentContext(&dctx));
29   CHKERRQ(AssertDeviceContextExists(dctx));
30 
31   /* test duplicate */
32   CHKERRQ(PetscDeviceContextDuplicate(dctx,&ddup));
33   /* both device contexts should exist */
34   CHKERRQ(AssertDeviceContextExists(dctx));
35   CHKERRQ(AssertDeviceContextExists(ddup));
36 
37   /* destroying the dup should leave the original untouched */
38   CHKERRQ(PetscDeviceContextDestroy(&ddup));
39   CHKERRQ(AssertDeviceContextDoesNotExist(ddup));
40   CHKERRQ(AssertDeviceContextExists(dctx));
41 
42   CHKERRQ(PetscPrintf(PETSC_COMM_WORLD,"EXIT_SUCCESS\n"));
43   ierr = PetscFinalize();
44   return ierr;
45 }
46 
47 /*TEST
48 
49  build:
50    requires: defined(PETSC_HAVE_CXX)
51 
52  test:
53    TODO: broken in ci
54    requires: !device
55    suffix: no_device
56    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]+\(\)"
57 
58  testset:
59    output_file: ./output/ExitSuccess.out
60    nsize: {{1 2 4}}
61    test:
62      requires: cuda
63      suffix: cuda
64    test:
65      requires: hip
66      suffix: hip
67 
68 TEST*/
69