Skip to content

Commit e423376

Browse files
committed
Centralize eigen matrix to/from buffer conversions
1 parent 727df1e commit e423376

File tree

12 files changed

+143
-274
lines changed

12 files changed

+143
-274
lines changed

source/pbat/gpu/common/Buffer.cuh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Buffer
4040
Data() const;
4141

4242
void Resize(std::size_t count);
43+
void SetConstant(T value);
4344

4445
std::conditional_t<(D > 1), std::array<T*, D>, T*> Raw();
4546
std::conditional_t<(D > 1), std::array<T const*, D>, T const*> Raw() const;
@@ -159,6 +160,15 @@ void Buffer<T, D>::Resize(std::size_t count)
159160
}
160161
}
161162

163+
template <class T, int D>
164+
void Buffer<T, D>::SetConstant(T value)
165+
{
166+
for (auto d = 0; d < Dimensions(); ++d)
167+
{
168+
thrust::fill(mBuffers[d].begin(), mBuffers[d].end(), value);
169+
}
170+
}
171+
162172
template <class T, int D>
163173
std::conditional_t<(D > 1), std::array<T*, D>, T*> Buffer<T, D>::Raw()
164174
{

source/pbat/gpu/common/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ target_sources(PhysicsBasedAnimationToolkit_PhysicsBasedAnimationToolkit
44
FILES
55
"Buffer.cuh"
66
"Cuda.cuh"
7+
"Eigen.cuh"
78
"SynchronizedList.cuh"
89
"Var.cuh"
910
)
@@ -12,6 +13,7 @@ target_sources(PhysicsBasedAnimationToolkit_PhysicsBasedAnimationToolkit
1213
PRIVATE
1314
"Buffer.cu"
1415
"Cuda.cu"
16+
"Eigen.cu"
1517
"SynchronizedList.cu"
1618
"Var.cu"
1719
)

source/pbat/gpu/common/Eigen.cu

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "Eigen.cuh"

source/pbat/gpu/common/Eigen.cuh

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#ifndef PBAT_GPU_COMMON_EIGEN_CUH
2+
#define PBAT_GPU_COMMON_EIGEN_CUH
3+
4+
#include "Buffer.cuh"
5+
#include "pbat/common/Eigen.h"
6+
7+
#include <Eigen/Core>
8+
9+
namespace pbat {
10+
namespace gpu {
11+
namespace common {
12+
13+
template <
14+
class ScalarType,
15+
auto D,
16+
auto StorageOrder = Eigen::ColMajor,
17+
auto EigenRows = Eigen::Dynamic,
18+
auto EigenCols = Eigen::Dynamic>
19+
void ToBuffer(
20+
Eigen::Ref<Eigen::Matrix<ScalarType, EigenRows, EigenCols, StorageOrder> const> const& A,
21+
Buffer<ScalarType, D>& buf)
22+
{
23+
if constexpr (D > 1)
24+
{
25+
if (A.rows() != buf.Dimensions() and A.cols() != buf.Size())
26+
{
27+
std::ostringstream ss{};
28+
ss << "Expected input dimensions " << buf.Dimensions() << "x" << buf.Size()
29+
<< ", but got " << A.rows() << "x" << A.cols() << "\n";
30+
throw std::invalid_argument(ss.str());
31+
}
32+
for (auto d = 0; d < buf.Dimensions(); ++d)
33+
{
34+
thrust::copy(A.row(d).begin(), A.row(d).end(), buf[d].begin());
35+
}
36+
}
37+
else
38+
{
39+
if (A.size() != buf.Size())
40+
{
41+
std::ostringstream ss{};
42+
ss << "Expected input dimensions " << buf.Dimensions() << "x" << buf.Size()
43+
<< " or its transpose, but got " << A.rows() << "x" << A.cols() << "\n";
44+
throw std::invalid_argument(ss.str());
45+
}
46+
thrust::copy(A.data(), A.data() + A.size(), buf.Data());
47+
}
48+
}
49+
50+
template <class ScalarType, auto D, auto StorageOrder = Eigen::RowMajor>
51+
Eigen::Matrix<ScalarType, Eigen::Dynamic, Eigen::Dynamic, StorageOrder>
52+
ToEigen(Buffer<ScalarType, D> const& buf)
53+
{
54+
return pbat::common::ToEigen(buf.Get()).reshaped(buf.Size(), buf.Dimensions()).transpose();
55+
}
56+
57+
} // namespace common
58+
} // namespace gpu
59+
} // namespace pbat
60+
61+
#endif // PBAT_GPU_COMMON_EIGEN_CUH

source/pbat/gpu/geometry/BvhImpl.cu

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include "BvhImpl.cuh"
66
#include "BvhImplKernels.cuh"
7-
#include "pbat/common/Eigen.h"
7+
#include "pbat/gpu/common/Eigen.cuh"
88

99
#include <exception>
1010
#include <string>
@@ -30,7 +30,7 @@ BvhImpl::BvhImpl(std::size_t nPrimitives, std::size_t nOverlaps)
3030
visits(nPrimitives - 1),
3131
overlaps(nOverlaps)
3232
{
33-
thrust::fill(thrust::device, parent.Data(), parent.Data() + parent.Size(), GpuIndex{-1});
33+
parent.SetConstant(GpuIndex(-1));
3434
}
3535

3636
void BvhImpl::Build(
@@ -50,7 +50,7 @@ void BvhImpl::Build(
5050
}
5151

5252
// 0. Reset intermediate data
53-
thrust::fill(thrust::device, visits.Raw(), visits.Raw() + visits.Size(), GpuIndex{0});
53+
visits.SetConstant(GpuIndex(0));
5454

5555
// 1. Construct leaf node (i.e. simplex) bounding boxes
5656
auto const leafBegin = n - 1;
@@ -155,50 +155,42 @@ std::size_t BvhImpl::NumberOfAllocatedBoxes() const
155155

156156
Eigen::Matrix<GpuScalar, Eigen::Dynamic, Eigen::Dynamic> BvhImpl::Min() const
157157
{
158-
using pbat::common::ToEigen;
159-
return ToEigen(b.Get()).reshaped(b.Size(), b.Dimensions());
158+
return common::ToEigen(b);
160159
}
161160

162161
Eigen::Matrix<GpuScalar, Eigen::Dynamic, Eigen::Dynamic> BvhImpl::Max() const
163162
{
164-
using pbat::common::ToEigen;
165-
return ToEigen(e.Get()).reshaped(e.Size(), e.Dimensions());
163+
return common::ToEigen(e);
166164
}
167165

168166
Eigen::Vector<GpuIndex, Eigen::Dynamic> BvhImpl::SimplexOrdering() const
169167
{
170-
using pbat::common::ToEigen;
171-
return ToEigen(simplex.Get());
168+
return common::ToEigen(simplex);
172169
}
173170

174171
Eigen::Vector<typename BvhImpl::MortonCodeType, Eigen::Dynamic> BvhImpl::MortonCodes() const
175172
{
176-
using pbat::common::ToEigen;
177-
return ToEigen(morton.Get());
173+
return common::ToEigen(morton);
178174
}
179175

180176
Eigen::Matrix<GpuIndex, Eigen::Dynamic, 2> BvhImpl::Child() const
181177
{
182-
using pbat::common::ToEigen;
183-
return ToEigen(child.Get()).reshaped(child.Size(), child.Dimensions());
178+
return common::ToEigen(child);
184179
}
185180

186181
Eigen::Vector<GpuIndex, Eigen::Dynamic> BvhImpl::Parent() const
187182
{
188-
using pbat::common::ToEigen;
189-
return ToEigen(parent.Get());
183+
return common::ToEigen(parent);
190184
}
191185

192186
Eigen::Matrix<GpuIndex, Eigen::Dynamic, 2> BvhImpl::Rightmost() const
193187
{
194-
using pbat::common::ToEigen;
195-
return ToEigen(rightmost.Get()).reshaped(rightmost.Size(), rightmost.Dimensions());
188+
return common::ToEigen(rightmost);
196189
}
197190

198191
Eigen::Vector<GpuIndex, Eigen::Dynamic> BvhImpl::Visits() const
199192
{
200-
using pbat::common::ToEigen;
201-
return ToEigen(visits.Get());
193+
return common::ToEigen(visits);
202194
}
203195

204196
} // namespace geometry

source/pbat/gpu/geometry/Primitives.cu

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include "Primitives.h"
66
#include "PrimitivesImpl.cuh"
7-
#include "pbat/common/Eigen.h"
7+
#include "pbat/gpu/common/Eigen.cuh"
88

99
#include <array>
1010
#include <thrust/copy.h>
@@ -46,12 +46,7 @@ PointsImpl const* Points::Impl() const
4646

4747
GpuMatrixX Points::Get() const
4848
{
49-
GpuMatrixX V(mImpl->x.Dimensions(), mImpl->x.Size());
50-
for (auto d = 0; d < V.rows(); ++d)
51-
{
52-
thrust::copy(mImpl->x[d].begin(), mImpl->x[d].end(), V.row(d).begin());
53-
}
54-
return V;
49+
return common::ToEigen(mImpl->x);
5550
}
5651

5752
Points::~Points()
@@ -126,8 +121,7 @@ Bodies& geometry::Bodies::operator=(Bodies&& other) noexcept
126121

127122
GpuIndexMatrixX geometry::Bodies::Get() const
128123
{
129-
using pbat::common::ToEigen;
130-
return ToEigen(mImpl->body.Get());
124+
return common::ToEigen(mImpl->body);
131125
}
132126

133127
std::size_t Bodies::NumberOfBodies() const

source/pbat/gpu/geometry/PrimitivesImpl.cu

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// clang-format on
44

55
#include "PrimitivesImpl.cuh"
6+
#include "pbat/gpu/common/Eigen.cuh"
67

78
#include <array>
89
#include <exception>
@@ -31,12 +32,8 @@ std::size_t PointsImpl::Dimensions() const
3132

3233
void PointsImpl::Update(Eigen::Ref<GpuMatrixX const> const& V)
3334
{
34-
for (auto d = 0; d < 3; ++d)
35-
x[d].resize(V.cols());
36-
for (auto d = 0; d < 3; ++d)
37-
{
38-
thrust::copy(V.row(d).begin(), V.row(d).end(), x[d].begin());
39-
}
35+
x.Resize(V.cols());
36+
common::ToBuffer(V, x);
4037
}
4138

4239
SimplicesImpl::SimplicesImpl(Eigen::Ref<GpuIndexMatrixX const> const& C) : eSimplexType(), inds()
@@ -74,7 +71,7 @@ GpuIndex SimplicesImpl::NumberOfSimplices() const
7471
BodiesImpl::BodiesImpl(Eigen::Ref<GpuIndexVectorX const> const& B)
7572
: body(B.size()), nBodies(static_cast<GpuIndex>(B.maxCoeff() + 1))
7673
{
77-
thrust::copy(B.data(), B.data() + B.size(), body.Data());
74+
common::ToBuffer(B, body);
7875
}
7976

8077
GpuIndex BodiesImpl::NumberOfBodies() const

source/pbat/gpu/geometry/SweepAndPruneImpl.cu

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ void SweepAndPruneImpl::SortAndSweep(
4545
}
4646

4747
// 0. Preprocess internal data
48-
thrust::fill(mu.Data(), mu.Data() + mu.Size(), 0.f);
49-
thrust::fill(sigma.Data(), sigma.Data() + sigma.Size(), 0.f);
48+
mu.SetConstant(GpuScalar(0));
49+
sigma.SetConstant(GpuScalar(0));
5050
overlaps.Clear();
5151
thrust::sequence(thrust::device, binds.Data(), binds.Data() + nBoxes);
5252

source/pbat/gpu/vbd/Vbd.cu

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include "Vbd.h"
66
#include "VbdImpl.cuh"
7-
#include "pbat/common/Eigen.h"
7+
#include "pbat/gpu/common/Eigen.cuh"
88

99
namespace pbat {
1010
namespace gpu {
@@ -114,17 +114,12 @@ void Vbd::SetBlockSize(GpuIndex blockSize)
114114

115115
GpuMatrixX Vbd::GetPositions() const
116116
{
117-
using pbat::common::ToEigen;
118-
return ToEigen(mImpl->X.x.Get())
119-
.reshaped(mImpl->X.x.Size(), mImpl->X.x.Dimensions())
120-
.transpose();
117+
return common::ToEigen(mImpl->X.x);
121118
}
122119

123120
GpuMatrixX Vbd::GetVelocities() const
124121
{
125-
using pbat::common::ToEigen;
126-
auto const& velocity = mImpl->GetVelocity();
127-
return ToEigen(velocity.Get()).reshaped(velocity.Size(), velocity.Dimensions()).transpose();
122+
return common::ToEigen(mImpl->GetVelocity());
128123
}
129124

130125
} // namespace vbd

0 commit comments

Comments
 (0)