From cccccbf6cca94a3eaf813b4468453160e91c332b Mon Sep 17 00:00:00 2001 From: Joe Zhao Date: Mon, 14 Apr 2014 08:14:45 +0800 Subject: First commit --- src/CuBaseLib/cuvector.h | 121 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 src/CuBaseLib/cuvector.h (limited to 'src/CuBaseLib/cuvector.h') diff --git a/src/CuBaseLib/cuvector.h b/src/CuBaseLib/cuvector.h new file mode 100644 index 0000000..7bfe116 --- /dev/null +++ b/src/CuBaseLib/cuvector.h @@ -0,0 +1,121 @@ +#ifndef _CUVECTOR_H_ +#define _CUVECTOR_H_ + +#include "Vector.h" + +namespace TNet { + + template class CuMatrix; + + /** + * \brief Vector for CUDA computing + * + * Vector is a matrix consist of a single row + */ + template + class CuVector + { + typedef CuVector<_ElemT> ThisType; + + public: + + /// Default Constructor + CuVector<_ElemT>() + : mDim(0), mpCUData(NULL) + { } + /// Constructor with memory initialisation + CuVector<_ElemT>(size_t dim) + : mDim(0), mpCUData(NULL) + { Init(dim); } + + /// Destructor + ~CuVector() + { Destroy(); } + + /// Dimensions + size_t Dim() const + { return mDim; } + + /* + ::MatrixDim Dim() const + { ::MatrixDim d = { mDim, 1, 1 }; return d; } + */ + + /// Get raw pointer + const _ElemT* pCUData() const + { return mpCUData; } + _ElemT* pCUData() + { return mpCUData; } + + /// Allocate the memory + ThisType& Init(size_t dim); + + /// Deallocate the memory + void Destroy(); + + /// Copy functions (reallocates when needed) + ThisType& CopyFrom(const CuVector<_ElemT>& rSrc); + ThisType& CopyFrom(const Vector<_ElemT>& rSrc); + Vector<_ElemT>& CopyTo(Vector<_ElemT>& rDst) const; + + + + // Math operations + /// Set to All Zeros + void SetZero(); + + /// Set to Constant + /// + /// \param[in] value Desired constant value + void SetConst(_ElemT value) + { Error("__func__ Not implemented"); } + + /// Add scaled vector + /// + /// \param[in] alpha + /// \param[in] vec + /// \param[in] beta + /// Cal \f$ A=\alpha vec + \beta A \f$ + void AddScaled(_ElemT alpha, const CuVector<_ElemT>& vec, _ElemT beta) + { Error("__func__ Not implemented"); } + + //// Add scaled vector + /// + /// \param[in] alpha + /// \param[in] mat + /// \param[in] beta + /// Cal \f$ A=\alpha mat.ColSum() + \beta A \f$ + void AddColSum(_ElemT alpha, const CuMatrix<_ElemT>& mat, _ElemT beta) + { Error("__func__ Not implemented"); } + + void Print() const + { + Vector<_ElemT> vec(Dim()); + CopyTo(vec); + std::cout << vec << "\n"; + } + + + private: + size_t mDim; + _ElemT* mpCUData; + }; + + + /// Prints the matrix dimensions and pointer to stream + template + inline std::ostream& operator << (std::ostream& out, const CuVector<_ElemT>& vec) + { + size_t d = vec.Dim(); + out << "[CuVector D" << d + << " PTR" << vec.pCUData() << "]" << std::flush; + return out; + } + + +} + + +#include "cuvector.tcc" + +#endif -- cgit v1.2.3-70-g09d2