From 0b887fdcbc2e7cb2e8959bde26672175bc988e1c Mon Sep 17 00:00:00 2001 From: Tobias Meyer Andersen Date: Wed, 12 Feb 2025 11:04:28 +0100 Subject: [PATCH 1/7] simplify tests --- opm/simulators/linalg/gpuistl/GpuBuffer.cpp | 15 ++++++++++++--- opm/simulators/linalg/gpuistl/GpuBuffer.hpp | 3 +++ .../gpuistl/test_gpu_linear_two_phase_material.cu | 5 ++--- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/opm/simulators/linalg/gpuistl/GpuBuffer.cpp b/opm/simulators/linalg/gpuistl/GpuBuffer.cpp index b4132ce09f..b67542d2fa 100644 --- a/opm/simulators/linalg/gpuistl/GpuBuffer.cpp +++ b/opm/simulators/linalg/gpuistl/GpuBuffer.cpp @@ -199,13 +199,22 @@ template class GpuBuffer; template class GpuBuffer; template class GpuBuffer; +template +GpuView make_view(GpuBuffer& buf) { + return GpuView(buf.data(), buf.size()); +} + +template GpuView make_view(GpuBuffer&); +template GpuView make_view(GpuBuffer&); +template GpuView make_view(GpuBuffer&); + template GpuView make_view(const GpuBuffer& buf) { return GpuView(buf.data(), buf.size()); } -template GpuView make_view(const GpuBuffer&); -template GpuView make_view(const GpuBuffer&); -template GpuView make_view(const GpuBuffer&); +template GpuView make_view(const GpuBuffer&); +template GpuView make_view(const GpuBuffer&); +template GpuView make_view(const GpuBuffer&); } // namespace Opm::gpuistl diff --git a/opm/simulators/linalg/gpuistl/GpuBuffer.hpp b/opm/simulators/linalg/gpuistl/GpuBuffer.hpp index 369ea3ba28..c0dd49ea71 100644 --- a/opm/simulators/linalg/gpuistl/GpuBuffer.hpp +++ b/opm/simulators/linalg/gpuistl/GpuBuffer.hpp @@ -229,6 +229,9 @@ private: void assertHasElements() const; }; +template +GpuView make_view(GpuBuffer&); + template GpuView make_view(const GpuBuffer&); diff --git a/tests/gpuistl/test_gpu_linear_two_phase_material.cu b/tests/gpuistl/test_gpu_linear_two_phase_material.cu index 5e4fe42af2..68512d9ab8 100644 --- a/tests/gpuistl/test_gpu_linear_two_phase_material.cu +++ b/tests/gpuistl/test_gpu_linear_two_phase_material.cu @@ -44,11 +44,10 @@ using Scalar = float; using ValueVector = std::vector; using GPUBuffer = Opm::gpuistl::GpuBuffer; - using GPUView = Opm::gpuistl::GpuView; + using GPUView = Opm::gpuistl::GpuView; using TraitsT = Opm::TwoPhaseMaterialTraits; using CPUParams = Opm::PiecewiseLinearTwoPhaseMaterialParams; - using constGPUBufferParams = Opm::PiecewiseLinearTwoPhaseMaterialParams; using GPUBufferParams = Opm::PiecewiseLinearTwoPhaseMaterialParams; using GPUViewParams = Opm::PiecewiseLinearTwoPhaseMaterialParams; @@ -73,7 +72,7 @@ BOOST_AUTO_TEST_CASE(TestSimpleInterpolation) cpuParams.setKrnSamples(cx, cy); cpuParams.finalize(); - constGPUBufferParams gpuBufferParams = Opm::gpuistl::copy_to_gpu(cpuParams); + GPUBufferParams gpuBufferParams = Opm::gpuistl::copy_to_gpu(cpuParams); GPUViewParams gpuViewParams = Opm::gpuistl::make_view(gpuBufferParams); From 1511004f0a132574ff81c396a92af1c99ae4da3f Mon Sep 17 00:00:00 2001 From: Tobias Meyer Andersen Date: Thu, 13 Feb 2025 13:10:31 +0100 Subject: [PATCH 2/7] refactor make_view and copy_to_gpu --- tests/gpuistl/test_GpuBuffer.cu | 24 +++++++++++++----------- tests/gpuistl/test_gpuPvt.cu | 15 ++++++++------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/tests/gpuistl/test_GpuBuffer.cu b/tests/gpuistl/test_GpuBuffer.cu index 44dec8d717..cf31bcc30b 100644 --- a/tests/gpuistl/test_GpuBuffer.cu +++ b/tests/gpuistl/test_GpuBuffer.cu @@ -33,21 +33,23 @@ BOOST_AUTO_TEST_CASE(TestMakeView) { - // test that we can create buffers and make views of the buffers using the pointer constructor + // check creation of buffers and views for mutable buffers auto buf = std::vector({1, 2, 3, 4, 5, 6}); - const auto gpubuf = ::Opm::gpuistl::GpuBuffer(buf); - auto gpuview = ::Opm::gpuistl::GpuView(buf.data(), buf.size()); - bool gpuBufCreatedView = std::is_same<::Opm::gpuistl::GpuView, decltype(gpuview)>::value; - + auto gpubuf = ::Opm::gpuistl::GpuBuffer(buf); + auto gpuview = ::Opm::gpuistl::make_view(gpubuf); + bool gpuBufCreatedView = std::is_same_v<::Opm::gpuistl::GpuView, decltype(gpuview)>; BOOST_CHECK(gpuBufCreatedView); - // test that we can make views of buffers by using the GpuBuffer constructor - auto gpuview2 = ::Opm::gpuistl::make_view(gpubuf); - bool gpuBufCreatedView2 = std::is_same<::Opm::gpuistl::GpuView, decltype(gpuview2)>::value; + auto gpubufOnCpu = gpubuf.asStdVector(); + BOOST_CHECK_EQUAL_COLLECTIONS(gpubufOnCpu.begin(), gpubufOnCpu.end(), buf.begin(), buf.end()); + // check creation of buffers and views for const buffers + const auto buf2 = std::vector({2, 3, 4, 5, 6}); + const auto gpubuf2 = ::Opm::gpuistl::GpuBuffer(buf2); + auto gpuview2 = ::Opm::gpuistl::make_view(gpubuf2); + bool gpuBufCreatedView2 = std::is_same_v<::Opm::gpuistl::GpuView, decltype(gpuview2)>; BOOST_CHECK(gpuBufCreatedView2); - // check that we retrieve the same values when pulling the data back to the cpu as a vector - auto gpuBufOnCpu = gpubuf.asStdVector(); - BOOST_CHECK_EQUAL_COLLECTIONS(gpuBufOnCpu.begin(), gpuBufOnCpu.end(), buf.begin(), buf.end()); + auto gpubufOnCpu2 = gpubuf2.asStdVector(); + BOOST_CHECK_EQUAL_COLLECTIONS(gpubufOnCpu2.begin(), gpubufOnCpu2.end(), buf2.begin(), buf2.end()); } diff --git a/tests/gpuistl/test_gpuPvt.cu b/tests/gpuistl/test_gpuPvt.cu index b830e92c71..1b556a4a59 100644 --- a/tests/gpuistl/test_gpuPvt.cu +++ b/tests/gpuistl/test_gpuPvt.cu @@ -26,8 +26,9 @@ #include using Evaluation = Opm::DenseAd::Evaluation; -using GpuB = const Opm::gpuistl::GpuBuffer; -using GpuV = Opm::gpuistl::GpuView; +using GpuB = Opm::gpuistl::GpuBuffer; +using GpuV = Opm::gpuistl::GpuView; +using ConstGpuV = Opm::gpuistl::GpuView; using GpuTab = Opm::UniformTabulated2DFunction; @@ -268,9 +269,9 @@ BOOST_FIXTURE_TEST_CASE(TestCo2GasPvt, Fixture) { CpuCo2Pvt cpuCo2Pvt(salinities); double internalEnergyReference = cpuCo2Pvt.internalEnergy(1, temp, pressure, Evaluation(0.4), Evaluation(0.0)).value(); - const GpuBufCo2Pvt gpuBufCo2Pvt = Opm::gpuistl::copy_to_gpu(cpuCo2Pvt); - const auto brineReferenceDensityCPUCopy = gpuBufCo2Pvt.getBrineReferenceDensity().asStdVector(); - const GpuViewCo2Pvt gpuViewCo2Pvt = Opm::gpuistl::make_view(gpuBufCo2Pvt); + GpuBufCo2Pvt gpuBufCo2Pvt = Opm::gpuistl::copy_to_gpu(cpuCo2Pvt); + auto brineReferenceDensityCPUCopy = gpuBufCo2Pvt.getBrineReferenceDensity().asStdVector(); + GpuViewCo2Pvt gpuViewCo2Pvt = Opm::gpuistl::make_view(gpuBufCo2Pvt); gpuComputedResultOnCpu = launchKernelAndRetrieveResult(co2GasPvtInternalEnergy, gpuViewCo2Pvt, gpuTemp, gpuPressure); @@ -287,8 +288,8 @@ BOOST_FIXTURE_TEST_CASE(TestBrineCo2Pvt, Fixture) { CpuBrineCo2Pvt cpuBrineCo2Pvt(salinities); double internalEnergyReference = cpuBrineCo2Pvt.internalEnergy(1, temp, pressure, rs, saltConcentration).value(); - const GpuBufBrineCo2Pvt gpuBufBrineCo2Pvt = Opm::gpuistl::copy_to_gpu(cpuBrineCo2Pvt); - const GpuViewBrineCo2Pvt gpuViewBrineCo2Pvt = Opm::gpuistl::make_view(gpuBufBrineCo2Pvt); + GpuBufBrineCo2Pvt gpuBufBrineCo2Pvt = Opm::gpuistl::copy_to_gpu(cpuBrineCo2Pvt); + GpuViewBrineCo2Pvt gpuViewBrineCo2Pvt = Opm::gpuistl::make_view(gpuBufBrineCo2Pvt); // Allocate memory for the result on the GPU double* resultOnGpu = nullptr; From b21a29e8654c9a40a0de7ff8e1c415f396b089fa Mon Sep 17 00:00:00 2001 From: Tobias Meyer Andersen Date: Thu, 13 Feb 2025 13:22:25 +0100 Subject: [PATCH 3/7] remove unused type --- tests/gpuistl/test_gpuPvt.cu | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/gpuistl/test_gpuPvt.cu b/tests/gpuistl/test_gpuPvt.cu index 1b556a4a59..9674305da5 100644 --- a/tests/gpuistl/test_gpuPvt.cu +++ b/tests/gpuistl/test_gpuPvt.cu @@ -28,7 +28,6 @@ using Evaluation = Opm::DenseAd::Evaluation; using GpuB = Opm::gpuistl::GpuBuffer; using GpuV = Opm::gpuistl::GpuView; -using ConstGpuV = Opm::gpuistl::GpuView; using GpuTab = Opm::UniformTabulated2DFunction; From c7018dda29c661d6066789b4435df6c332bda74a Mon Sep 17 00:00:00 2001 From: Tobias Meyer Andersen Date: Thu, 13 Feb 2025 16:30:04 +0100 Subject: [PATCH 4/7] optimize uniformtabulated... template order --- tests/gpuistl/test_gpuPvt.cu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/gpuistl/test_gpuPvt.cu b/tests/gpuistl/test_gpuPvt.cu index 9674305da5..e6fee49e4a 100644 --- a/tests/gpuistl/test_gpuPvt.cu +++ b/tests/gpuistl/test_gpuPvt.cu @@ -172,7 +172,7 @@ BOOST_FIXTURE_TEST_CASE(TestEvaluateUniformTabulated2DFunctionOnGpu, Fixture) { Opm::UniformTabulated2DFunction cpuTab(1.0, 6.0, 3, 1.0, 6.0, 2, tabData); // Move data to GPU buffer and create a view for GPU operations - Opm::UniformTabulated2DFunction gpuBufTab = Opm::gpuistl::copy_to_gpu(cpuTab); + Opm::UniformTabulated2DFunction gpuBufTab = Opm::gpuistl::copy_to_gpu(cpuTab); GpuTab gpuViewTab = Opm::gpuistl::make_view(gpuBufTab); // Evaluation points on the CPU From 54d4974a90a6032347ae501df3c7191343d95d97 Mon Sep 17 00:00:00 2001 From: Tobias Meyer Andersen Date: Thu, 13 Feb 2025 16:38:07 +0100 Subject: [PATCH 5/7] optimize co2tables template order --- tests/gpuistl/test_gpuPvt.cu | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/gpuistl/test_gpuPvt.cu b/tests/gpuistl/test_gpuPvt.cu index e6fee49e4a..aa2a881001 100644 --- a/tests/gpuistl/test_gpuPvt.cu +++ b/tests/gpuistl/test_gpuPvt.cu @@ -204,7 +204,7 @@ BOOST_FIXTURE_TEST_CASE(TestUseCO2OnGpu, Fixture) { // use the CO2 tables to aquire the viscosity at 290[K] and 2e5[Pa] double viscosityReference = Opm::CO2>>::gasViscosity(co2Tables, temp, pressure, true).value(); - GpuBufCo2Tables gpuBufCo2Table = Opm::gpuistl::copy_to_gpu, GpuB>(co2Tables); + GpuBufCo2Tables gpuBufCo2Table = Opm::gpuistl::copy_to_gpu(co2Tables); GpuViewCO2Tables gpuViewCo2Table = Opm::gpuistl::make_view(gpuBufCo2Table); gpuComputedResultOnCpu = launchKernelAndRetrieveResult(gpuCO2GasViscosity, gpuViewCo2Table, gpuTemp, gpuPressure); @@ -252,7 +252,7 @@ BOOST_FIXTURE_TEST_CASE(TestBrine_CO2OnGPU, Fixture) { // use the CO2 tables to aquire the viscosity at 290[K] and 2e5[Pa] double viscosity = Opm::CO2>>::gasViscosity(co2Tables, temp, pressure, true).value(); - GpuBufCo2Tables gpuBufCo2Table = Opm::gpuistl::copy_to_gpu, GpuB>(co2Tables); + GpuBufCo2Tables gpuBufCo2Table = Opm::gpuistl::copy_to_gpu(co2Tables); GpuViewCO2Tables gpuViewCo2Table = Opm::gpuistl::make_view(gpuBufCo2Table); gpuComputedResultOnCpu = launchKernelAndRetrieveResult(brineCO2GasDiffCoeff, gpuViewCo2Table, gpuTemp, gpuPressure); From 37306cc2deeb7ee930b18807b5040321109ab32b Mon Sep 17 00:00:00 2001 From: Tobias Meyer Andersen Date: Thu, 13 Feb 2025 16:45:59 +0100 Subject: [PATCH 6/7] optimize co2gaspvt template order --- tests/gpuistl/test_gpuPvt.cu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/gpuistl/test_gpuPvt.cu b/tests/gpuistl/test_gpuPvt.cu index aa2a881001..12f58c1f41 100644 --- a/tests/gpuistl/test_gpuPvt.cu +++ b/tests/gpuistl/test_gpuPvt.cu @@ -268,7 +268,7 @@ BOOST_FIXTURE_TEST_CASE(TestCo2GasPvt, Fixture) { CpuCo2Pvt cpuCo2Pvt(salinities); double internalEnergyReference = cpuCo2Pvt.internalEnergy(1, temp, pressure, Evaluation(0.4), Evaluation(0.0)).value(); - GpuBufCo2Pvt gpuBufCo2Pvt = Opm::gpuistl::copy_to_gpu(cpuCo2Pvt); + GpuBufCo2Pvt gpuBufCo2Pvt = Opm::gpuistl::copy_to_gpu(cpuCo2Pvt); auto brineReferenceDensityCPUCopy = gpuBufCo2Pvt.getBrineReferenceDensity().asStdVector(); GpuViewCo2Pvt gpuViewCo2Pvt = Opm::gpuistl::make_view(gpuBufCo2Pvt); From 013d75db6329152a454632c4e51fc3bc666ae5a0 Mon Sep 17 00:00:00 2001 From: Tobias Meyer Andersen Date: Thu, 13 Feb 2025 16:50:48 +0100 Subject: [PATCH 7/7] optimize brineco2pvt template order --- tests/gpuistl/test_gpuPvt.cu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/gpuistl/test_gpuPvt.cu b/tests/gpuistl/test_gpuPvt.cu index 12f58c1f41..71076e42d3 100644 --- a/tests/gpuistl/test_gpuPvt.cu +++ b/tests/gpuistl/test_gpuPvt.cu @@ -287,7 +287,7 @@ BOOST_FIXTURE_TEST_CASE(TestBrineCo2Pvt, Fixture) { CpuBrineCo2Pvt cpuBrineCo2Pvt(salinities); double internalEnergyReference = cpuBrineCo2Pvt.internalEnergy(1, temp, pressure, rs, saltConcentration).value(); - GpuBufBrineCo2Pvt gpuBufBrineCo2Pvt = Opm::gpuistl::copy_to_gpu(cpuBrineCo2Pvt); + GpuBufBrineCo2Pvt gpuBufBrineCo2Pvt = Opm::gpuistl::copy_to_gpu(cpuBrineCo2Pvt); GpuViewBrineCo2Pvt gpuViewBrineCo2Pvt = Opm::gpuistl::make_view(gpuBufBrineCo2Pvt); // Allocate memory for the result on the GPU