TestScopedContext

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#include "catch.hpp"

#include "CUDADataFormats/Common/interface/Product.h"
#include "HeterogeneousCore/CUDACore/interface/ScopedContext.h"
#include "HeterogeneousCore/CUDAUtilities/interface/cudaCheck.h"
#include "HeterogeneousCore/CUDAUtilities/interface/requireDevices.h"
#include "HeterogeneousCore/CUDAUtilities/interface/StreamCache.h"
#include "HeterogeneousCore/CUDAUtilities/interface/EventCache.h"

#include <cuda_runtime_api.h>

namespace cms::cudatest {
  class TestScopedContext {
  public:
    static cuda::ScopedContextProduce make(int dev, bool createEvent) {
      cms::cuda::SharedEventPtr event;
      if (createEvent) {
        event = cms::cuda::getEventCache().get();
      }
      return cuda::ScopedContextProduce(dev, cms::cuda::getStreamCache().get(), std::move(event));
    }
  };
}  // namespace cms::cudatest

TEST_CASE("Use of cms::cuda::Product template", "[CUDACore]") {
  SECTION("Default constructed") {
    auto foo = cms::cuda::Product<int>();
    REQUIRE(!foo.isValid());

    auto bar = std::move(foo);
  }

  if (not cms::cudatest::testDevices()) {
    return;
  }

  constexpr int defaultDevice = 0;
  cudaCheck(cudaSetDevice(defaultDevice));
  {
    auto ctx = cms::cudatest::TestScopedContext::make(defaultDevice, true);
    std::unique_ptr<cms::cuda::Product<int>> dataPtr = ctx.wrap(10);
    auto& data = *dataPtr;

    SECTION("Construct from cms::cuda::ScopedContext") {
      REQUIRE(data.isValid());
      REQUIRE(data.device() == defaultDevice);
      REQUIRE(data.stream() == ctx.stream());
      REQUIRE(data.event() != nullptr);
    }

    SECTION("Move constructor") {
      auto data2 = cms::cuda::Product<int>(std::move(data));
      REQUIRE(data2.isValid());
      REQUIRE(!data.isValid());
    }

    SECTION("Move assignment") {
      cms::cuda::Product<int> data2;
      data2 = std::move(data);
      REQUIRE(data2.isValid());
      REQUIRE(!data.isValid());
    }
  }

  cudaCheck(cudaSetDevice(defaultDevice));
  cudaCheck(cudaDeviceSynchronize());
  // Note: CUDA resources are cleaned up by the destructors of the global cache objects
}