1#ifndef PBAT_GPU_IMPL_COMMON_EIGEN_H
2#define PBAT_GPU_IMPL_COMMON_EIGEN_H
14template <
class ScalarType, auto D,
class TDerived>
17 if constexpr (not std::is_same_v<typename TDerived::Scalar, ScalarType>)
19 auto A2 = A.template cast<ScalarType>().eval();
23 using SizeType =
decltype(buf.Size());
24 auto ncols =
static_cast<SizeType
>(A.cols());
25 auto nrows =
static_cast<SizeType
>(A.rows());
28 if (nrows != buf.Dimensions())
30 std::ostringstream ss{};
31 ss <<
"Expected input dimensions " << buf.Dimensions() <<
"x" << buf.Size()
32 <<
", but got " << A.rows() <<
"x" << A.cols() <<
"\n";
33 throw std::invalid_argument(ss.str());
35 if (buf.Size() != ncols)
37 for (
auto d = 0; d < buf.Dimensions(); ++d)
39 thrust::copy(A.row(d).begin(), A.row(d).end(), buf[d].begin());
44 auto nsize =
static_cast<SizeType
>(A.size());
45 if (buf.Size() != nsize)
47 thrust::copy(A.data(), A.data() + A.size(), buf.Data());
54 auto StorageOrder = Eigen::ColMajor,
55 auto EigenRows = Eigen::Dynamic,
56 auto EigenCols = Eigen::Dynamic>
58 Eigen::Ref<Eigen::Matrix<ScalarType, EigenRows, EigenCols, StorageOrder>
const>
const& A,
61 using SizeType =
decltype(buf.Size());
64 auto nrows =
static_cast<SizeType
>(A.rows());
65 auto ncols =
static_cast<SizeType
>(A.cols());
66 if (nrows != buf.Dimensions())
68 std::ostringstream ss{};
69 ss <<
"Expected input dimensions " << buf.Dimensions() <<
"x" << buf.Size()
70 <<
", but got " << A.rows() <<
"x" << A.cols() <<
"\n";
71 throw std::invalid_argument(ss.str());
73 if (buf.Size() != ncols)
75 for (
auto d = 0; d < buf.Dimensions(); ++d)
77 thrust::copy(A.row(d).begin(), A.row(d).end(), buf[d].begin());
82 auto nsize =
static_cast<SizeType
>(A.size());
83 if (buf.Size() != nsize)
85 thrust::copy(A.data(), A.data() + A.size(), buf.Data());
89template <
class ScalarType, auto D, auto StorageOrder = (D > 1) ? Eigen::RowMajor : Eigen::ColMajor>
90Eigen::
Matrix<ScalarType, Eigen::Dynamic, Eigen::Dynamic, StorageOrder>
91ToEigen(
Buffer<ScalarType, D> const& buf)
103template <
class ScalarType, auto D,
class TDerived>
114 auto StorageOrder = Eigen::ColMajor,
115 auto EigenRows = Eigen::Dynamic,
116 auto EigenCols = Eigen::Dynamic>
118 Eigen::Ref<Eigen::Matrix<ScalarType, EigenRows, EigenCols, StorageOrder>
const>
const& A)
Eigen adaptors for ranges.
auto ToEigen(R &&r) -> Eigen::Map< Eigen::Vector< std::ranges::range_value_t< R >, Eigen::Dynamic > const >
Map a range of scalars to an eigen vector of such scalars.
Definition Eigen.h:28
GPU algorithm implementations.
Definition VertexTriangleMixedCcdDcd.h:21
GPU related public functionality.
Definition Buffer.cu:16
The main namespace of the library.
Definition Aliases.h:15
Eigen::Matrix< Scalar, Rows, Cols > Matrix
Fixed-size matrix type.
Definition Aliases.h:31