Skip to content

Commit c3aed36

Browse files
committed
offloadable
1 parent f5e573c commit c3aed36

File tree

7 files changed

+106
-61
lines changed

7 files changed

+106
-61
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#pragma once
2+
3+
#include <vector>
4+
#include <atomic>
5+
#include <cstdint>
6+
7+
// forward declaration
8+
struct CUstream_st;
9+
10+
namespace gtsam_points {
11+
12+
/**
13+
* @brief An interface class for offloading data on the GPU memory.
14+
*/
15+
class OffloadableGPU {
16+
public:
17+
OffloadableGPU();
18+
virtual ~OffloadableGPU();
19+
20+
// GPU memory offloading
21+
static std::uint64_t current_access_time();
22+
std::uint64_t last_accessed_time() const;
23+
24+
virtual size_t memory_usage_gpu() const { return 0; }
25+
26+
virtual bool touch(CUstream_st* stream = 0) = 0;
27+
virtual bool offload_gpu(CUstream_st* stream = 0) = 0;
28+
virtual bool reload_gpu(CUstream_st* stream = 0) = 0;
29+
30+
private:
31+
static std::atomic_uint64_t access_counter; ///< Counter for the last access time
32+
std::uint64_t last_access;
33+
};
34+
35+
} // namespace gtsam_points

include/gtsam_points/types/point_cloud.hpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,6 @@ struct PointCloud {
9999
*/
100100
void save_compact(const std::string& path) const;
101101

102-
/**
103-
* @brief Calculate the CPU memory usage of this point cloud
104-
*/
105-
size_t memory_usage() const;
106-
107-
/**
108-
* @brief Calculate the GPU memory usage of this point cloud
109-
*/
110-
size_t memory_usage_gpu() const;
111-
112102
public:
113103
size_t num_points; ///< Number of points
114104

include/gtsam_points/types/point_cloud_gpu.hpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <gtsam_points/types/point_cloud.hpp>
1111
#include <gtsam_points/types/point_cloud_cpu.hpp>
12+
#include <gtsam_points/types/offloadable.hpp>
1213

1314
// forward declaration
1415
struct CUstream_st;
@@ -18,7 +19,7 @@ namespace gtsam_points {
1819
/**
1920
* @brief Point cloud frame on GPU memory
2021
*/
21-
struct PointCloudGPU : public PointCloudCPU {
22+
struct PointCloudGPU : public PointCloudCPU, public OffloadableGPU {
2223
public:
2324
using Ptr = std::shared_ptr<PointCloudGPU>;
2425
using ConstPtr = std::shared_ptr<const PointCloudGPU>;
@@ -128,16 +129,10 @@ struct PointCloudGPU : public PointCloudCPU {
128129
void download_points(CUstream_st* stream = 0);
129130

130131
// GPU memory offloading
131-
static std::uint64_t current_access_time() { return access_time_counter.load(); }
132-
std::uint64_t last_accessed_time() const { return last_access; }
132+
virtual size_t memory_usage_gpu() const override;
133133

134-
bool touch(CUstream_st* stream = 0);
135134
bool offload_gpu(CUstream_st* stream = 0);
136135
bool reload_gpu(CUstream_st* stream = 0);
137-
138-
private:
139-
static std::atomic_uint64_t access_time_counter;
140-
std::uint64_t last_access;
141136
};
142137

143138
// Device to host data transfer
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <gtsam_points/types/offloadable.hpp>
2+
3+
namespace gtsam_points {
4+
5+
std::atomic_uint64_t OffloadableGPU::access_counter(0); ///< Counter for the last access time
6+
7+
OffloadableGPU::OffloadableGPU() : last_access(0) {}
8+
9+
OffloadableGPU::~OffloadableGPU() {}
10+
11+
// GPU memory offloading
12+
std::uint64_t OffloadableGPU::current_access_time() {
13+
return access_counter.load();
14+
}
15+
16+
std::uint64_t OffloadableGPU::last_accessed_time() const {
17+
return last_access;
18+
}
19+
20+
bool OffloadableGPU::touch(CUstream_st* stream) {
21+
last_access = (access_counter++);
22+
return reload_gpu(stream);
23+
}
24+
25+
} // namespace gtsam_points

src/gtsam_points/types/point_cloud.cpp

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -202,47 +202,4 @@ void PointCloud::save_compact(const std::string& path) const {
202202
}
203203
}
204204

205-
size_t PointCloud::memory_usage() const {
206-
size_t bytes = 0;
207-
if (times) {
208-
bytes += sizeof(double) * num_points;
209-
}
210-
if (points) {
211-
bytes += sizeof(Eigen::Vector4d) * num_points;
212-
}
213-
if (normals) {
214-
bytes += sizeof(Eigen::Vector4d) * num_points;
215-
}
216-
if (covs) {
217-
bytes += sizeof(Eigen::Matrix4d) * num_points;
218-
}
219-
if (intensities) {
220-
bytes += sizeof(double) * num_points;
221-
}
222-
for (const auto& attrib : aux_attributes) {
223-
bytes += attrib.second.first * num_points; // size of each element * number of points
224-
}
225-
return bytes;
226-
}
227-
228-
size_t PointCloud::memory_usage_gpu() const {
229-
size_t bytes = 0;
230-
if (times_gpu) {
231-
bytes += sizeof(float) * num_points;
232-
}
233-
if (points_gpu) {
234-
bytes += sizeof(Eigen::Vector3f) * num_points;
235-
}
236-
if (normals_gpu) {
237-
bytes += sizeof(Eigen::Vector3f) * num_points;
238-
}
239-
if (covs_gpu) {
240-
bytes += sizeof(Eigen::Matrix3f) * num_points;
241-
}
242-
if (intensities_gpu) {
243-
bytes += sizeof(float) * num_points;
244-
}
245-
return bytes;
246-
}
247-
248205
} // namespace gtsam_points

src/gtsam_points/types/point_cloud_cpu.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,27 @@ PointCloudCPU::Ptr PointCloudCPU::load(const std::string& path) {
293293
return frame;
294294
}
295295

296+
size_t PointCloudCPU::memory_usage() const {
297+
size_t bytes = 0;
298+
if (times) {
299+
bytes += sizeof(double) * num_points;
300+
}
301+
if (points) {
302+
bytes += sizeof(Eigen::Vector4d) * num_points;
303+
}
304+
if (normals) {
305+
bytes += sizeof(Eigen::Vector4d) * num_points;
306+
}
307+
if (covs) {
308+
bytes += sizeof(Eigen::Matrix4d) * num_points;
309+
}
310+
if (intensities) {
311+
bytes += sizeof(double) * num_points;
312+
}
313+
for (const auto& attrib : aux_attributes) {
314+
bytes += attrib.second.first * num_points; // size of each element * number of points
315+
}
316+
return bytes;
317+
}
318+
296319
} // namespace gtsam_points

src/gtsam_points/types/point_cloud_gpu.cu

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,26 @@ bool PointCloudGPU::touch(CUstream_st* stream) {
285285
return reload_gpu(stream);
286286
}
287287

288+
size_t PointCloudGPU::memory_usage_gpu() const {
289+
size_t bytes = 0;
290+
if (times_gpu) {
291+
bytes += sizeof(float) * num_points;
292+
}
293+
if (points_gpu) {
294+
bytes += sizeof(Eigen::Vector3f) * num_points;
295+
}
296+
if (normals_gpu) {
297+
bytes += sizeof(Eigen::Vector3f) * num_points;
298+
}
299+
if (covs_gpu) {
300+
bytes += sizeof(Eigen::Matrix3f) * num_points;
301+
}
302+
if (intensities_gpu) {
303+
bytes += sizeof(float) * num_points;
304+
}
305+
return bytes;
306+
}
307+
288308
bool PointCloudGPU::offload_gpu(CUstream_st* stream) {
289309
if (!points_gpu && !times_gpu && !normals_gpu && !covs_gpu && !intensities_gpu) {
290310
return false; // Nothing to offload

0 commit comments

Comments
 (0)