Actual source code: ex1.c

  1: static const char help[] = "Tests creation and destruction of PetscDevice.\n\n";

  3: #include "petscdevicetestcommon.h"

  5: int main(int argc, char *argv[])
  6: {
  7:   const PetscInt n      = 10;
  8:   PetscDevice    device = NULL;
  9:   PetscDevice    devices[n];

 12:   PetscInitialize(&argc, &argv, NULL, help);

 14:   /* normal create and destroy */
 15:   PetscDeviceCreate(PETSC_DEVICE_DEFAULT(), PETSC_DECIDE, &device);
 16:   AssertDeviceExists(device);
 17:   PetscDeviceDestroy(&device);
 18:   AssertDeviceDoesNotExist(device);
 19:   /* should not destroy twice */
 20:   PetscDeviceDestroy(&device);
 21:   AssertDeviceDoesNotExist(device);

 23:   /* test reference counting */
 24:   device = NULL;
 25:   PetscArrayzero(devices, n);
 26:   PetscDeviceCreate(PETSC_DEVICE_DEFAULT(), PETSC_DECIDE, &device);
 27:   AssertDeviceExists(device);
 28:   for (int i = 0; i < n; ++i) {
 29:     PetscDeviceReference_Internal(device);
 30:     devices[i] = device;
 31:   }
 32:   AssertDeviceExists(device);
 33:   for (int i = 0; i < n; ++i) {
 34:     PetscDeviceDestroy(&devices[i]);
 35:     AssertDeviceExists(device);
 36:     AssertDeviceDoesNotExist(devices[i]);
 37:   }
 38:   PetscDeviceDestroy(&device);
 39:   AssertDeviceDoesNotExist(device);

 41:   /* test the default devices exist */
 42:   device = NULL;
 43:   PetscArrayzero(devices, n);
 44:   {
 45:     PetscDeviceContext dctx;
 46:     /* global context will have the default device */
 47:     PetscDeviceContextGetCurrentContext(&dctx);
 48:     PetscDeviceContextGetDevice(dctx, &device);
 49:   }
 50:   AssertDeviceExists(device);
 51:   /* test reference counting for default device */
 52:   for (int i = 0; i < n; ++i) {
 53:     PetscDeviceReference_Internal(device);
 54:     devices[i] = device;
 55:   }
 56:   AssertDeviceExists(device);
 57:   for (int i = 0; i < n; ++i) {
 58:     PetscDeviceDestroy(&devices[i]);
 59:     AssertDeviceExists(device);
 60:     AssertDeviceDoesNotExist(devices[i]);
 61:   }

 63:   PetscPrintf(PETSC_COMM_WORLD, "EXIT_SUCCESS\n");
 64:   PetscFinalize();
 65:   return 0;
 66: }

 68: /*TEST

 70:  build:
 71:    requires: defined(PETSC_HAVE_CXX)

 73:  testset:
 74:    output_file: ./output/ExitSuccess.out
 75:    nsize: {{1 2 5}}
 76:    args: -device_enable {{lazy eager}}
 77:    test:
 78:      requires: !device
 79:      suffix: host_no_device
 80:    test:
 81:      requires: device
 82:      args: -default_device_type host
 83:      suffix: host_with_device
 84:    test:
 85:      requires: cuda
 86:      args: -default_device_type cuda
 87:      suffix: cuda
 88:    test:
 89:      requires: hip
 90:      args: -default_device_type hip
 91:      suffix: hip
 92:    test:
 93:      requires: sycl
 94:      args: -default_device_type sycl
 95:      suffix: sycl

 97: TEST*/