xref: /petsc/src/sys/objects/device/tests/ex4.c (revision f97672e55eacc8688507b9471cd7ec2664d7f203)
1 static const char help[] = "Tests PetscDeviceContextFork/Join.\n\n";
2 
3 #include <petsc/private/deviceimpl.h>
4 #include "petscdevicetestcommon.h"
5 
6 static PetscErrorCode TestNestedPetscDeviceContextForkJoin(PetscDeviceContext parCtx, PetscDeviceContext *sub)
7 {
8   const PetscInt      nsub = 4;
9   PetscDeviceContext *subsub;
10 
11   PetscFunctionBegin;
12   PetscValidDeviceContext(parCtx,1);
13   PetscValidPointer(sub,2);
14   PetscCall(AssertPetscDeviceContextsValidAndEqual(parCtx,sub[0],"Current global context does not match expected global context"));
15   /* create some children from an active child */
16   PetscCall(PetscDeviceContextFork(sub[1],nsub,&subsub));
17   /* join on a sibling to the parent */
18   PetscCall(PetscDeviceContextJoin(sub[2],nsub-2,PETSC_DEVICE_CONTEXT_JOIN_SYNC,&subsub));
19   /* join on the grandparent */
20   PetscCall(PetscDeviceContextJoin(parCtx,nsub-2,PETSC_DEVICE_CONTEXT_JOIN_NO_SYNC,&subsub));
21   PetscCall(PetscDeviceContextJoin(sub[1],nsub,PETSC_DEVICE_CONTEXT_JOIN_DESTROY,&subsub));
22   PetscFunctionReturn(0);
23 }
24 
25 /* test fork-join */
26 static PetscErrorCode TestPetscDeviceContextForkJoin(PetscDeviceContext dctx)
27 {
28   PetscDeviceContext *sub;
29   const PetscInt      n = 10;
30 
31   PetscFunctionBegin;
32   PetscValidDeviceContext(dctx,1);
33   /* mostly for valgrind to catch errors */
34   PetscCall(PetscDeviceContextFork(dctx,n,&sub));
35   PetscCall(PetscDeviceContextJoin(dctx,n,PETSC_DEVICE_CONTEXT_JOIN_DESTROY,&sub));
36   /* do it twice */
37   PetscCall(PetscDeviceContextFork(dctx,n,&sub));
38   PetscCall(PetscDeviceContextJoin(dctx,n,PETSC_DEVICE_CONTEXT_JOIN_DESTROY,&sub));
39 
40   /* create some children */
41   PetscCall(PetscDeviceContextFork(dctx,n+1,&sub));
42   /* test forking within nested function */
43   PetscCall(TestNestedPetscDeviceContextForkJoin(sub[0],sub));
44   /* join a subset */
45   PetscCall(PetscDeviceContextJoin(dctx,n-1,PETSC_DEVICE_CONTEXT_JOIN_NO_SYNC,&sub));
46   /* back to the ether from whence they came */
47   PetscCall(PetscDeviceContextJoin(dctx,n+1,PETSC_DEVICE_CONTEXT_JOIN_DESTROY,&sub));
48   PetscFunctionReturn(0);
49 }
50 
51 int main(int argc, char *argv[])
52 {
53   PetscDeviceContext dctx;
54 
55   PetscCall(PetscInitialize(&argc,&argv,NULL,help));
56 
57   PetscCall(PetscDeviceContextCreate(&dctx));
58   PetscCall(PetscDeviceContextSetFromOptions(PETSC_COMM_WORLD,"local_",dctx));
59   PetscCall(PetscDeviceContextSetUp(dctx));
60   PetscCall(TestPetscDeviceContextForkJoin(dctx));
61   PetscCall(PetscDeviceContextDestroy(&dctx));
62 
63   PetscCall(PetscDeviceContextGetCurrentContext(&dctx));
64   PetscCall(TestPetscDeviceContextForkJoin(dctx));
65 
66   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"EXIT_SUCCESS\n"));
67   PetscCall(PetscFinalize());
68   return 0;
69 }
70 
71 /*TEST
72 
73  build:
74    requires: defined(PETSC_HAVE_CXX)
75 
76  test:
77    TODO: broken in ci
78    requires: !device
79    suffix: no_device
80    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]+\(\)"
81 
82  testset:
83    output_file: ./output/ExitSuccess.out
84    nsize: {{1 3}}
85    args: -local_device_context_stream_type {{global_blocking default_blocking global_nonblocking}}
86    test:
87      requires: cuda
88      suffix: cuda
89    test:
90      requires: hip
91      suffix: hip
92 
93 TEST*/
94