Actual source code: ex3.c

  1: static const char help[] = "Tests PetscDeviceContextDuplicate.\n\n";

  3: #include "petscdevicetestcommon.h"

  5: /* test duplication creates the same object type */
  6: static PetscErrorCode TestPetscDeviceContextDuplicate(PetscDeviceContext dctx)
  7: {
  8:   PetscDevice        origDevice;
  9:   PetscStreamType    origStype;
 10:   PetscDeviceContext ddup;

 12:   PetscFunctionBegin;
 13:   /* get everything we want first before any duplication */
 14:   PetscCall(PetscDeviceContextGetStreamType(dctx, &origStype));
 15:   PetscCall(PetscDeviceContextGetDevice(dctx, &origDevice));

 17:   /* duplicate */
 18:   PetscCall(PetscDeviceContextDuplicate(dctx, &ddup));
 20:   if (dctx) PetscCheckCompatibleDeviceContexts(dctx, 1, ddup, 2);

 22:   {
 23:     PetscDevice parDevice, dupDevice;

 25:     PetscCall(PetscDeviceContextGetDevice(dctx, &parDevice));
 26:     PetscCall(AssertPetscDevicesValidAndEqual(parDevice, origDevice, "Parent PetscDevice after duplication does not match parent original PetscDevice"));
 27:     PetscCall(PetscDeviceContextGetDevice(ddup, &dupDevice));
 28:     PetscCall(AssertPetscDevicesValidAndEqual(dupDevice, origDevice, "Duplicated PetscDevice does not match parent original PetscDevice"));
 29:   }

 31:   {
 32:     PetscStreamType parStype, dupStype;

 34:     PetscCall(PetscDeviceContextGetStreamType(dctx, &parStype));
 35:     PetscCall(AssertPetscStreamTypesValidAndEqual(parStype, origStype, "Parent PetscStreamType after duplication does not match parent original PetscStreamType"));
 36:     PetscCall(PetscDeviceContextGetStreamType(ddup, &dupStype));
 37:     PetscCall(AssertPetscStreamTypesValidAndEqual(dupStype, origStype, "Duplicated PetscStreamType '%s' does not match parent original PetscStreamType '%s'"));
 38:   }

 40:   PetscCall(PetscDeviceContextDestroy(&ddup));
 41:   /* duplicate should not take the original down with it */
 43:   PetscFunctionReturn(PETSC_SUCCESS);
 44: }

 46: int main(int argc, char *argv[])
 47: {
 48:   MPI_Comm           comm;
 49:   PetscDeviceContext dctx;

 51:   PetscFunctionBeginUser;
 52:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
 53:   comm = PETSC_COMM_WORLD;

 55:   /* basic creation and destruction */
 56:   PetscCall(PetscDeviceContextCreate(&dctx));
 57:   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dctx, "local_"));
 58:   PetscCall(PetscDeviceContextSetFromOptions(comm, dctx));
 59:   PetscCall(TestPetscDeviceContextDuplicate(dctx));
 60:   PetscCall(PetscDeviceContextDestroy(&dctx));

 62:   PetscCall(PetscDeviceContextGetCurrentContext(&dctx));
 63:   PetscCall(TestPetscDeviceContextDuplicate(dctx));

 65:   PetscCall(TestPetscDeviceContextDuplicate(NULL));

 67:   PetscCall(PetscPrintf(comm, "EXIT_SUCCESS\n"));
 68:   PetscCall(PetscFinalize());
 69:   return 0;
 70: }

 72: /*TEST

 74:   testset:
 75:     requires: cxx
 76:     output_file: ./output/ExitSuccess.out
 77:     nsize: {{1 4}}
 78:     args: -device_enable {{lazy eager}}
 79:     args: -local_device_context_stream_type {{global_blocking default_blocking global_nonblocking}}
 80:     test:
 81:       requires: !device
 82:       suffix: host_no_device
 83:     test:
 84:       requires: device
 85:       args: -default_device_type host -root_device_context_device_type host
 86:       suffix: host_with_device
 87:     test:
 88:       requires: cuda
 89:       args: -default_device_type cuda -root_device_context_device_type cuda
 90:       suffix: cuda
 91:     test:
 92:       requires: hip
 93:       args: -default_device_type hip -root_device_context_device_type hip
 94:       suffix: hip
 95:     test:
 96:       requires: sycl
 97:       args: -default_device_type sycl -root_device_context_device_type sycl
 98:       suffix: sycl

100: TEST*/