18#ifndef OPM_GPU_FLOW_GASWATER_ENERGY_BRIDGE_HPP
19#define OPM_GPU_FLOW_GASWATER_ENERGY_BRIDGE_HPP
26#include <opm/material/fluidsystems/BlackOilFluidSystem.hpp>
50template <
class CpuTypeTag,
class DeviceTypeTag>
51class GpuFlowGasWaterEnergyBridge
53 static constexpr unsigned numTimeSlots = 2;
55 using CpuPrototypeTypeTag = Properties::TTag::FlowGasWaterEnergyCpuDeviceContract<CpuTypeTag>;
56 using Scalar = GetPropType<CpuTypeTag, Properties::Scalar>;
57 using Problem = GetPropType<CpuTypeTag, Properties::Problem>;
58 using SolutionVector = GetPropType<CpuTypeTag, Properties::SolutionVector>;
59 using HostIntensiveQuantities = GetPropType<CpuTypeTag, Properties::IntensiveQuantities>;
60 using CpuFluidSystem = GetPropType<CpuTypeTag, Properties::FluidSystem>;
62 using DevicePrimaryVariables = GetPropType<DeviceTypeTag, Properties::PrimaryVariables>;
63 using DeviceIntensiveQuantities = BlackOilIntensiveQuantities<DeviceTypeTag>;
64 using DeviceFluidSystem = GetPropType<DeviceTypeTag, Properties::FluidSystem>;
65 using DeviceProblem = GetPropType<DeviceTypeTag, Properties::Problem>;
66 using DeviceModelView = SimpleFIBlackOilModel<DeviceTypeTag, gpuistl::GpuView>;
68 using DynamicCpuFluidSystem =
69 std::remove_reference_t<
decltype(CpuFluidSystem::getNonStaticInstance())>;
70 using FluidSystemBuffer =
72 using MaterialLawManager =
73 typename GetProp<DeviceTypeTag, Properties::MaterialLaw>::EclMaterialLawManager;
74 using GpuMaterialLawManager =
75 EclMaterialLaw::GpuManager<
typename MaterialLawManager::Traits,
76 typename MaterialLawManager::GasOilLaw,
77 typename MaterialLawManager::OilWaterLaw,
79 typename MaterialLawManager::MaterialLaw>;
80 using GpuThermalLawManager =
81 EclThermalLaw::GpuManager<Scalar, DeviceFluidSystem, gpuistl::GpuBuffer, gpuistl::GpuView>;
82 using GpuProblemBuffer =
83 GpuFlowProblem<Scalar, GpuMaterialLawManager, gpuistl::GpuBuffer, GpuThermalLawManager>;
86 static_assert(std::is_same_v<DeviceIntensiveQuantities,
87 GetPropType<DeviceTypeTag, Properties::IntensiveQuantities>>);
88 static_assert(std::is_same_v<DeviceProblem, GpuProblemView>);
91 using DeviceTypeTagPublic = DeviceTypeTag;
92 using DeviceIntensiveQuantitiesView = gpuistl::GpuView<DeviceIntensiveQuantities>;
93 using DevicePrimaryVariablesView = gpuistl::GpuView<const DevicePrimaryVariables>;
94 using DeviceModelViewPublic = DeviceModelView;
96 GpuFlowGasWaterEnergyBridge() =
default;
98 GpuFlowGasWaterEnergyBridge(
const GpuFlowGasWaterEnergyBridge&) =
delete;
99 GpuFlowGasWaterEnergyBridge& operator=(
const GpuFlowGasWaterEnergyBridge&) =
delete;
101 ~GpuFlowGasWaterEnergyBridge()
104 waitForLastWriter_();
107 void updatePrimaryVariables(
const Problem& cpuProblem,
108 const SolutionVector& solution,
111 validateTimeIdx_(timeIdx);
112 ensureInitialized_(cpuProblem, timeIdx, solution.size());
114 if (solution.size() != numDof_) {
115 OPM_THROW(std::invalid_argument,
116 "GPU property bridge received a solution with a mismatched number of DoFs");
119 auto& hostPrimaryVariables = hostPrimaryVariables_[timeIdx];
120 for (std::size_t i = 0; i < numDof_; ++i) {
121 hostPrimaryVariables[i] = DevicePrimaryVariables(solution[i]);
125 hostPrimaryVariables.data(), numDof_, stream_.get());
126 deviceIqValid_[timeIdx] =
false;
127 lastWriterTimeIdx_ = timeIdx;
132 return stream_.get();
135 DevicePrimaryVariablesView primaryVariablesView(
unsigned timeIdx)
const
137 validateReadySlot_(timeIdx);
138 return DevicePrimaryVariablesView(primaryVariablesBuffer_[timeIdx]->data(), numDof_);
141 DeviceIntensiveQuantitiesView intensiveQuantitiesView(
unsigned timeIdx)
const
143 validateReadySlot_(timeIdx);
144 return DeviceIntensiveQuantitiesView(intensiveQuantitiesBuffer_[timeIdx]->data(), numDof_);
147 void recordPropertyReady(
unsigned timeIdx)
149 validateReadySlot_(timeIdx);
151 deviceIqValid_[timeIdx] =
true;
152 lastWriterTimeIdx_ = timeIdx;
160 void waitForAssembly(
unsigned timeIdx)
const
162 validateValidSlot_(timeIdx);
163 if (lastWriterTimeIdx_ != invalidTimeIdx) {
168 DeviceModelView modelView()
const
170 validateValidSlot_(0);
171 validateValidSlot_(1);
177 GpuProblemView flowProblemView()
const
179 if (!problemBuffer_) {
180 OPM_THROW(std::logic_error,
"GPU property bridge has not been initialized");
191 const DeviceFluidSystem& deviceFluidSystem()
const
193 if (!deviceFluidSystem_) {
194 OPM_THROW(std::logic_error,
"GPU property bridge has not been initialized");
196 return *deviceFluidSystem_;
199 bool hasModelView()
const
201 return deviceIqValid_[0] && deviceIqValid_[1];
204 bool hasIntensiveQuantities(
unsigned timeIdx)
const
206 return timeIdx < numTimeSlots && deviceIqValid_[timeIdx];
212 void materializeHostIntensiveQuantities(
unsigned timeIdx,
213 HostIntensiveQuantities*
const* destination,
216 validateValidSlot_(timeIdx);
217 if (destination ==
nullptr || numDof != numDof_) {
218 OPM_THROW(std::invalid_argument,
219 "GPU property bridge received an invalid host IQ materialization destination");
221 for (std::size_t i = 0; i < numDof_; ++i) {
222 if (destination[i] ==
nullptr) {
223 OPM_THROW(std::invalid_argument,
224 "GPU property bridge received a null host IQ materialization entry");
228 if (lastWriterTimeIdx_ != invalidTimeIdx) {
231 hostIntensiveQuantities_.assign(numDof_, *prototype_);
232 intensiveQuantitiesBuffer_[timeIdx]->copyToHost(hostIntensiveQuantities_);
233 for (std::size_t i = 0; i < numDof_; ++i) {
234 destination[i]->overlayBlackOilFieldsFrom(hostIntensiveQuantities_[i]);
241 void advanceTimeLevel()
243 if (!deviceIqValid_[0]) {
244 deviceIqValid_[1] =
false;
248 intensiveQuantitiesBuffer_[0]->data(),
249 numDof_, stream_.get());
250 recordPropertyReady(1);
254 static constexpr unsigned invalidTimeIdx = numTimeSlots;
256 void ensureInitialized_(
const Problem& cpuProblem,
unsigned currentTimeIdx, std::size_t numDof)
259 OPM_THROW(std::invalid_argument,
"GPU property bridge cannot be initialized with zero DoFs");
261 if (numDof_ == numDof && problemBuffer_) {
265 waitForLastWriter_();
269 auto fluidSystemBuffer =
271 fluidSystemBuffer_ = std::make_unique<FluidSystemBuffer>(std::move(fluidSystemBuffer));
273 deviceFluidSystem_ = ::Opm::gpuistl::make_gpu_shared_ptr<DeviceFluidSystem>(fluidSystemView);
275 BlackOilIntensiveQuantities<CpuPrototypeTypeTag> cpuPrototype;
277 cpuPrototype.template withOtherFluidSystem<DeviceTypeTag>(deviceFluidSystem());
279 std::vector<DeviceIntensiveQuantities> initialIq(numDof_, *prototype_);
280 for (
unsigned timeIdx = 0; timeIdx < numTimeSlots; ++timeIdx) {
281 hostPrimaryVariables_[timeIdx].resize(numDof_);
282 primaryVariablesBuffer_[timeIdx] =
283 std::make_unique<gpuistl::GpuBuffer<DevicePrimaryVariables>>(numDof_);
284 intensiveQuantitiesBuffer_[timeIdx] =
285 std::make_unique<gpuistl::GpuBuffer<DeviceIntensiveQuantities>>(initialIq);
288 std::vector<Scalar> volumes(numDof_);
289 for (std::size_t i = 0; i < numDof_; ++i) {
290 volumes[i] = cpuProblem.model().dofTotalVolume(
static_cast<unsigned>(i));
292 volumesBuffer_ = std::make_unique<gpuistl::GpuBuffer<Scalar>>(volumes);
293 problemBuffer_ = std::make_unique<GpuProblemBuffer>(cpuProblem);
298 initializePreviousSlot_(cpuProblem, currentTimeIdx);
301 void initializePreviousSlot_(
const Problem& cpuProblem,
unsigned currentTimeIdx)
303 const unsigned previousTimeIdx = currentTimeIdx == 0 ? 1 : 0;
304 const auto& cpuModel = cpuProblem.model();
305 if (cpuModel.intensiveQuantityCache().size() <= previousTimeIdx) {
309 if constexpr (!getPropValue<CpuTypeTag, Properties::EnableDiffusion>()
310 && !getPropValue<CpuTypeTag, Properties::EnableDispersion>()) {
311 std::vector<DeviceIntensiveQuantities> initialIq;
312 initialIq.reserve(numDof_);
313 for (std::size_t i = 0; i < numDof_; ++i) {
315 cpuModel.cachedIntensiveQuantities(
static_cast<unsigned>(i), previousTimeIdx);
316 if (cached ==
nullptr) {
319 initialIq.emplace_back(
320 cached->template withOtherFluidSystem<DeviceTypeTag>(deviceFluidSystem()));
322 intensiveQuantitiesBuffer_[previousTimeIdx]->copyFromHost(initialIq);
323 deviceIqValid_[previousTimeIdx] =
true;
327 void validateTimeIdx_(
unsigned timeIdx)
const
329 if (timeIdx >= numTimeSlots) {
330 OPM_THROW(std::invalid_argument,
331 "GPU property bridge supports only current and previous IQ time slots");
335 void validateReadySlot_(
unsigned timeIdx)
const
337 validateTimeIdx_(timeIdx);
338 if (!intensiveQuantitiesBuffer_[timeIdx] || !primaryVariablesBuffer_[timeIdx]) {
339 OPM_THROW(std::logic_error,
"GPU property bridge has not been initialized");
343 void validateValidSlot_(
unsigned timeIdx)
const
345 validateReadySlot_(timeIdx);
346 if (!deviceIqValid_[timeIdx]) {
347 OPM_THROW(std::logic_error,
348 "GPU property bridge attempted to consume an invalid intensive-quantity slot");
352 void waitForLastWriter_()
const
359 for (
unsigned timeIdx = 0; timeIdx < numTimeSlots; ++timeIdx) {
360 primaryVariablesBuffer_[timeIdx].reset();
361 intensiveQuantitiesBuffer_[timeIdx].reset();
362 hostPrimaryVariables_[timeIdx].clear();
363 deviceIqValid_[timeIdx] =
false;
365 volumesBuffer_.reset();
366 problemBuffer_.reset();
367 deviceFluidSystem_.reset();
368 fluidSystemBuffer_.reset();
369 hostIntensiveQuantities_.clear();
371 lastWriterTimeIdx_ = invalidTimeIdx;
377 GPUEvent propertyReady_;
378 std::size_t numDof_{0};
379 std::array<std::unique_ptr<gpuistl::GpuBuffer<DevicePrimaryVariables>>, numTimeSlots>
380 primaryVariablesBuffer_{};
381 std::array<std::unique_ptr<gpuistl::GpuBuffer<DeviceIntensiveQuantities>>, numTimeSlots>
382 intensiveQuantitiesBuffer_{};
383 std::array<std::vector<DevicePrimaryVariables>, numTimeSlots> hostPrimaryVariables_{};
384 std::vector<DeviceIntensiveQuantities> hostIntensiveQuantities_{};
385 std::optional<DeviceIntensiveQuantities> prototype_;
386 std::unique_ptr<gpuistl::GpuBuffer<Scalar>> volumesBuffer_;
387 std::unique_ptr<FluidSystemBuffer> fluidSystemBuffer_;
388 std::shared_ptr<DeviceFluidSystem> deviceFluidSystem_;
389 std::unique_ptr<GpuProblemBuffer> problemBuffer_;
390 std::array<bool, numTimeSlots> deviceIqValid_{
false,
false};
391 mutable unsigned lastWriterTimeIdx_{invalidTimeIdx};
Dependency-light GPU TypeTag contract for the CO2STORE flow path.
void gpuMemcpyHostToDeviceAsync(T *dstDevice, const T *srcHost, std::size_t count, cudaStream_t stream)
gpuMemcpyHostToDeviceAsync copies count elements of type T from host to device asynchronously.
Definition: gpu_memcpy.hpp:142
constexpr cudaStream_t DEFAULT_STREAM
The default GPU stream (stream 0)
Definition: gpu_constants.hpp:31
void gpuMemcpyDeviceToDeviceAsync(T *dstDevice, const T *srcDevice, std::size_t count, cudaStream_t stream)
Copy count elements between device buffers on the specified stream.
Definition: gpu_memcpy.hpp:194
A small, fixed‑dimension MiniVector class backed by std::array that can be used in both host and CUDA...
Definition: GpuFlowProblem.hpp:49
void recordEvent(const GPUEvent &event, cudaStream_t stream)
Record completion of the work already queued on a stream.
Definition: gpu_resources.hpp:143
inline ::Opm::NoThermalLawManager make_view(::Opm::NoThermalLawManager &)
make_view overload for the no-op thermal manager.
Definition: GpuFlowProblem.hpp:102
void synchronizeEvent(const GPUEvent &event)
Block the host until the recorded event completes.
Definition: gpu_resources.hpp:158
void synchronizeStreamNoThrow(cudaStream_t stream) noexcept
Drain a stream during teardown, reporting errors without throwing.
Definition: gpu_resources.hpp:167
inline ::Opm::NoThermalLawManager copy_to_gpu(const ::Opm::NoThermalLawManager &)
copy_to_gpu overload for the no-op thermal manager.
Definition: GpuFlowProblem.hpp:95
void waitForEventAsync(cudaStream_t stream, const GPUEvent &event)
Order subsequent stream work after the recorded event without blocking the host.
Definition: gpu_resources.hpp:152
The Opm property system, traits with inheritance.