123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- #ifndef CERES_INTERNAL_CUDA_BUFFER_H_
- #define CERES_INTERNAL_CUDA_BUFFER_H_
- #include "ceres/context_impl.h"
- #include "ceres/internal/config.h"
- #ifndef CERES_NO_CUDA
- #include <vector>
- #include "cuda_runtime.h"
- #include "glog/logging.h"
- namespace ceres::internal {
- template <typename T>
- class CudaBuffer {
- public:
- explicit CudaBuffer(ContextImpl* context) : context_(context) {}
- CudaBuffer(ContextImpl* context, int size) : context_(context) {
- Reserve(size);
- }
- CudaBuffer(CudaBuffer&& other)
- : data_(other.data_), size_(other.size_), context_(other.context_) {
- other.data_ = nullptr;
- other.size_ = 0;
- }
- CudaBuffer(const CudaBuffer&) = delete;
- CudaBuffer& operator=(const CudaBuffer&) = delete;
- ~CudaBuffer() {
- if (data_ != nullptr) {
- CHECK_EQ(cudaFree(data_), cudaSuccess);
- }
- }
-
-
- void Reserve(const size_t size) {
- if (size > size_) {
- if (data_ != nullptr) {
- CHECK_EQ(cudaFree(data_), cudaSuccess);
- }
- CHECK_EQ(cudaMalloc(&data_, size * sizeof(T)), cudaSuccess)
- << "Failed to allocate " << size * sizeof(T)
- << " bytes of GPU memory";
- size_ = size;
- }
- }
-
-
- void CopyFromCpu(const T* data, const size_t size) {
- Reserve(size);
- CHECK_EQ(cudaMemcpyAsync(data_,
- data,
- size * sizeof(T),
- cudaMemcpyHostToDevice,
- context_->DefaultStream()),
- cudaSuccess);
- }
-
-
- void CopyFromCpuVector(const std::vector<T>& data) {
- Reserve(data.size());
- CHECK_EQ(cudaMemcpyAsync(data_,
- data.data(),
- data.size() * sizeof(T),
- cudaMemcpyHostToDevice,
- context_->DefaultStream()),
- cudaSuccess);
- }
-
-
- void CopyFromGPUArray(const T* data, const size_t size) {
- Reserve(size);
- CHECK_EQ(cudaMemcpyAsync(data_,
- data,
- size * sizeof(T),
- cudaMemcpyDeviceToDevice,
- context_->DefaultStream()),
- cudaSuccess);
- }
-
-
-
-
-
-
- void CopyToCpu(T* data, const size_t size) const {
- CHECK(data_ != nullptr);
- CHECK_EQ(cudaMemcpyAsync(data,
- data_,
- size * sizeof(T),
- cudaMemcpyDeviceToHost,
- context_->DefaultStream()),
- cudaSuccess);
- CHECK_EQ(cudaStreamSynchronize(context_->DefaultStream()), cudaSuccess);
- }
-
-
-
- void CopyNItemsFrom(int n, const CudaBuffer<T>& other) {
- Reserve(n);
- CHECK(other.data_ != nullptr);
- CHECK(data_ != nullptr);
- CHECK_EQ(cudaMemcpyAsync(data_,
- other.data_,
- size_ * sizeof(T),
- cudaMemcpyDeviceToDevice,
- context_->DefaultStream()),
- cudaSuccess);
- }
-
- T* data() { return data_; }
- const T* data() const { return data_; }
-
-
- size_t size() const { return size_; }
- private:
- T* data_ = nullptr;
- size_t size_ = 0;
- ContextImpl* context_ = nullptr;
- };
- }
- #endif
- #endif
|