opm-simulators/opm/simulators/linalg/cuistl/GpuView.cpp

83 lines
2.3 KiB
C++
Raw Normal View History

2024-05-15 06:43:46 -05:00
/*
Copyright 2024 SINTEF AS
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cuda.h>
#include <cuda_runtime.h>
#include <algorithm>
#include <fmt/core.h>
2024-08-22 08:27:23 -05:00
#include <opm/simulators/linalg/cuistl/GpuView.hpp>
2024-05-15 06:43:46 -05:00
#include <opm/simulators/linalg/cuistl/detail/cuda_safe_call.hpp>
2024-08-22 06:52:50 -05:00
namespace Opm::gpuistl
2024-05-15 06:43:46 -05:00
{
template <class T>
2024-08-22 08:27:23 -05:00
GpuView<T>::GpuView(std::vector<T>& data)
: GpuView(data.data(), data.size())
2024-05-15 06:43:46 -05:00
{
}
template <typename T>
std::vector<T>
2024-08-22 08:27:23 -05:00
GpuView<T>::asStdVector() const
2024-05-15 06:43:46 -05:00
{
std::vector<T> temporary(m_numberOfElements);
2024-05-15 06:43:46 -05:00
copyToHost(temporary);
return temporary;
}
template <class T>
void
2024-08-22 08:27:23 -05:00
GpuView<T>::copyFromHost(const T* dataPointer, size_t numberOfElements)
2024-05-15 06:43:46 -05:00
{
if (numberOfElements > size()) {
OPM_THROW(std::runtime_error,
fmt::format("Requesting to copy too many elements. View has {} elements, while {} was requested.",
size(),
numberOfElements));
}
OPM_CUDA_SAFE_CALL(cudaMemcpy(data(), dataPointer, numberOfElements * sizeof(T), cudaMemcpyHostToDevice));
}
template <class T>
void
2024-08-22 08:27:23 -05:00
GpuView<T>::copyToHost(T* dataPointer, size_t numberOfElements) const
2024-05-15 06:43:46 -05:00
{
assertSameSize(numberOfElements);
2024-05-15 06:43:46 -05:00
OPM_CUDA_SAFE_CALL(cudaMemcpy(dataPointer, data(), numberOfElements * sizeof(T), cudaMemcpyDeviceToHost));
}
template <class T>
void
2024-08-22 08:27:23 -05:00
GpuView<T>::copyFromHost(const std::vector<T>& data)
2024-05-15 06:43:46 -05:00
{
copyFromHost(data.data(), data.size());
}
template <class T>
void
2024-08-22 08:27:23 -05:00
GpuView<T>::copyToHost(std::vector<T>& data) const
2024-05-15 06:43:46 -05:00
{
copyToHost(data.data(), data.size());
}
2024-08-22 08:27:23 -05:00
template class GpuView<double>;
template class GpuView<float>;
template class GpuView<int>;
2024-05-15 06:43:46 -05:00
2024-08-22 06:52:50 -05:00
} // namespace Opm::gpuistl