Actual source code: ex4.c

  1: static const char help[] = "Tests PetscDeviceContextFork/Join.\n\n";

  3: #include "petscdevicetestcommon.h"

  5: static PetscErrorCode DoFork(PetscDeviceContext parent, PetscInt n, PetscDeviceContext **sub)
  6: {
  7:   PetscDeviceType dtype;
  8:   PetscStreamType stype;

 10:   AssertDeviceContextExists(parent);
 11:   PetscDeviceContextGetDeviceType(parent, &dtype);
 12:   PetscDeviceContextGetStreamType(parent, &stype);
 13:   PetscDeviceContextFork(parent, n, sub);
 15:   for (PetscInt i = 0; i < n; ++i) {
 16:     PetscDeviceType sub_dtype;
 17:     PetscStreamType sub_stype;

 19:     AssertDeviceContextExists((*sub)[i]);
 20:     PetscDeviceContextGetStreamType((*sub)[i], &sub_stype);
 21:     AssertPetscStreamTypesValidAndEqual(sub_stype, stype, "Child stream type %s != parent stream type %s");
 22:     PetscDeviceContextGetDeviceType((*sub)[i], &sub_dtype);
 23:     AssertPetscDeviceTypesValidAndEqual(sub_dtype, dtype, "Child device type %s != parent device type %s");
 24:   }
 25:   return 0;
 26: }

 28: static PetscErrorCode TestNestedPetscDeviceContextForkJoin(PetscDeviceContext parCtx, PetscDeviceContext *sub)
 29: {
 30:   const PetscInt      nsub = 4;
 31:   PetscDeviceContext *subsub;

 35:   AssertPetscDeviceContextsValidAndEqual(parCtx, sub[0], "Current global context does not match expected global context");
 36:   /* create some children from an active child */
 37:   DoFork(sub[1], nsub, &subsub);
 38:   /* join on a sibling to the parent */
 39:   PetscDeviceContextJoin(sub[2], nsub - 2, PETSC_DEVICE_CONTEXT_JOIN_SYNC, &subsub);
 40:   /* join on the grandparent */
 41:   PetscDeviceContextJoin(parCtx, nsub - 2, PETSC_DEVICE_CONTEXT_JOIN_NO_SYNC, &subsub);
 42:   PetscDeviceContextJoin(sub[1], nsub, PETSC_DEVICE_CONTEXT_JOIN_DESTROY, &subsub);
 43:   return 0;
 44: }

 46: /* test fork-join */
 47: static PetscErrorCode TestPetscDeviceContextForkJoin(PetscDeviceContext dctx)
 48: {
 49:   PetscDeviceContext *sub;
 50:   const PetscInt      n = 10;

 53:   /* mostly for valgrind to catch errors */
 54:   DoFork(dctx, n, &sub);
 55:   PetscDeviceContextJoin(dctx, n, PETSC_DEVICE_CONTEXT_JOIN_DESTROY, &sub);
 56:   /* do it twice */
 57:   DoFork(dctx, n, &sub);
 58:   PetscDeviceContextJoin(dctx, n, PETSC_DEVICE_CONTEXT_JOIN_DESTROY, &sub);

 60:   /* create some children */
 61:   DoFork(dctx, n + 1, &sub);
 62:   /* test forking within nested function */
 63:   TestNestedPetscDeviceContextForkJoin(sub[0], sub);
 64:   /* join a subset */
 65:   PetscDeviceContextJoin(dctx, n - 1, PETSC_DEVICE_CONTEXT_JOIN_NO_SYNC, &sub);
 66:   /* back to the ether from whence they came */
 67:   PetscDeviceContextJoin(dctx, n + 1, PETSC_DEVICE_CONTEXT_JOIN_DESTROY, &sub);
 68:   return 0;
 69: }

 71: int main(int argc, char *argv[])
 72: {
 73:   MPI_Comm           comm;
 74:   PetscDeviceContext dctx;

 77:   PetscInitialize(&argc, &argv, NULL, help);
 78:   comm = PETSC_COMM_WORLD;

 80:   PetscDeviceContextCreate(&dctx);
 81:   PetscObjectSetOptionsPrefix((PetscObject)dctx, "local_");
 82:   PetscDeviceContextSetFromOptions(comm, dctx);
 83:   TestPetscDeviceContextForkJoin(dctx);
 84:   PetscDeviceContextDestroy(&dctx);

 86:   PetscDeviceContextGetCurrentContext(&dctx);
 87:   TestPetscDeviceContextForkJoin(dctx);

 89:   PetscPrintf(comm, "EXIT_SUCCESS\n");
 90:   PetscFinalize();
 91:   return 0;
 92: }

 94: /*TEST

 96:  build:
 97:    requires: defined(PETSC_HAVE_CXX)

 99:  testset:
100:    output_file: ./output/ExitSuccess.out
101:    nsize: {{1 3}}
102:    args: -device_enable {{lazy eager}}
103:    args: -local_device_context_stream_type {{global_blocking default_blocking global_nonblocking}}
104:    test:
105:      requires: !device
106:      suffix: host_no_device
107:    test:
108:      requires: device
109:      args: -root_device_context_device_type host
110:      suffix: host_with_device
111:    test:
112:      requires: cuda
113:      args: -root_device_context_device_type cuda
114:      suffix: cuda
115:    test:
116:      requires: hip
117:      args: -root_device_context_device_type hip
118:      suffix: hip

120: TEST*/