summaryrefslogtreecommitdiff
path: root/src/CuBaseLib/cuvector.h
diff options
context:
space:
mode:
authorJoe Zhao <ztuowen@gmail.com>2014-04-14 08:14:45 +0800
committerJoe Zhao <ztuowen@gmail.com>2014-04-14 08:14:45 +0800
commitcccccbf6cca94a3eaf813b4468453160e91c332b (patch)
tree23418cb73a10ae3b0688681a7f0ba9b06424583e /src/CuBaseLib/cuvector.h
downloadtnet-cccccbf6cca94a3eaf813b4468453160e91c332b.tar.gz
tnet-cccccbf6cca94a3eaf813b4468453160e91c332b.tar.bz2
tnet-cccccbf6cca94a3eaf813b4468453160e91c332b.zip
First commit
Diffstat (limited to 'src/CuBaseLib/cuvector.h')
-rw-r--r--src/CuBaseLib/cuvector.h121
1 files changed, 121 insertions, 0 deletions
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<typename _ElemT> class CuMatrix;
+
+ /**
+ * \brief Vector for CUDA computing
+ *
+ * Vector is a matrix consist of a single row
+ */
+ template<typename _ElemT>
+ 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<typename _ElemT>
+ 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