blob: 7bfe116d7ee68232e259f1d9dea7100e176b27d8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
|