123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- #ifndef CERES_INTERNAL_CUDA_VECTOR_H_
- #define CERES_INTERNAL_CUDA_VECTOR_H_
- #include "ceres/internal/config.h"
- #include <math.h>
- #include <memory>
- #include <string>
- #include "ceres/context_impl.h"
- #include "ceres/internal/export.h"
- #include "ceres/types.h"
- #ifndef CERES_NO_CUDA
- #include "ceres/cuda_buffer.h"
- #include "ceres/cuda_kernels_vector_ops.h"
- #include "ceres/internal/eigen.h"
- #include "cublas_v2.h"
- #include "cusparse.h"
- namespace ceres::internal {
- class CERES_NO_EXPORT CudaVector {
- public:
-
-
-
- CudaVector(ContextImpl* context, int size);
- CudaVector(CudaVector&& other);
- ~CudaVector();
- void Resize(int size);
-
- CudaVector& operator=(const CudaVector&);
-
- double Dot(const CudaVector& x) const;
-
- double Norm() const;
-
- void SetZero();
-
- void CopyFromCpu(const Vector& x);
-
- void CopyFromCpu(const double* x);
-
- void CopyTo(Vector* x) const;
-
-
- void CopyTo(double* x) const;
-
- void Axpby(double a, const CudaVector& x, double b);
-
- void DtDxpy(const CudaVector& D, const CudaVector& x);
-
- void Scale(double s);
- int num_rows() const { return num_rows_; }
- int num_cols() const { return 1; }
- const double* data() const { return data_.data(); }
- double* mutable_data() { return data_.data(); }
- const cusparseDnVecDescr_t& descr() const { return descr_; }
- private:
- CudaVector(const CudaVector&) = delete;
- void DestroyDescriptor();
- int num_rows_ = 0;
- ContextImpl* context_ = nullptr;
- CudaBuffer<double> data_;
-
- cusparseDnVecDescr_t descr_ = nullptr;
- };
- inline double Norm(const CudaVector& x,
- ContextImpl* context = nullptr,
- int num_threads = 1) {
- (void)context;
- (void)num_threads;
- return x.Norm();
- }
- inline void SetZero(CudaVector& x,
- ContextImpl* context = nullptr,
- int num_threads = 1) {
- (void)context;
- (void)num_threads;
- x.SetZero();
- }
- inline void Axpby(double a,
- const CudaVector& x,
- double b,
- const CudaVector& y,
- CudaVector& z,
- ContextImpl* context = nullptr,
- int num_threads = 1) {
- (void)context;
- (void)num_threads;
- if (&x == &y && &y == &z) {
-
- z.Scale(a + b);
- } else if (&x == &z) {
-
-
-
- z.Axpby(b, y, a);
- } else if (&y == &z) {
-
-
- z.Axpby(a, x, b);
- } else {
-
- z = y;
- z.Axpby(a, x, b);
- }
- }
- inline double Dot(const CudaVector& x,
- const CudaVector& y,
- ContextImpl* context = nullptr,
- int num_threads = 1) {
- (void)context;
- (void)num_threads;
- return x.Dot(y);
- }
- inline void Copy(const CudaVector& from,
- CudaVector& to,
- ContextImpl* context = nullptr,
- int num_threads = 1) {
- (void)context;
- (void)num_threads;
- to = from;
- }
- }
- #endif
- #endif
|