summaryrefslogtreecommitdiff
path: root/tnet_io/KaldiLib/Vector.cc
blob: 020bae296e1dbfe057204f705ab7bc3910fa1b03 (plain) (blame)
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
#ifndef TNet_Vector_cc
#define TNet_Vector_cc

#include <cstdlib>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include "Common.h"

#ifdef HAVE_ATLAS
extern "C"{
  #include <cblas.h>
}
#endif

#include "Common.h"
#include "Matrix.h"
#include "Vector.h"

namespace TNet
{

#ifdef HAVE_ATLAS
  template<>
     float
    BlasDot<>(const Vector<float>& rA, const Vector<float>& rB)
    {
      assert(rA.mDim == rB.mDim);
      return cblas_sdot(rA.mDim, rA.pData(), 1, rB.pData(), 1);
    }

  template<>
     double
    BlasDot<>(const Vector<double>& rA, const Vector<double>& rB)
    {
      assert(rA.mDim == rB.mDim);
      return cblas_ddot(rA.mDim, rA.pData(), 1, rB.pData(), 1);
    }

  template<>
     Vector<float>&
    Vector<float>::
    BlasAxpy(const float alpha, const Vector<float>& rV)
    {
      assert(mDim == rV.mDim);
      cblas_saxpy(mDim, alpha, rV.pData(), 1, mpData, 1);
      return *this;
    }

  template<>
     Vector<double>&
    Vector<double>::
    BlasAxpy(const double alpha, const Vector<double>& rV)
    {
      assert(mDim == rV.mDim);
      cblas_daxpy(mDim, alpha, rV.pData(), 1, mpData, 1);
      return *this;
    }

  template<>
     Vector<int>&
    Vector<int>::
    BlasAxpy(const int alpha, const Vector<int>& rV)
    {
      assert(mDim == rV.mDim);
      for(int i=0; i<Dim(); i++) {
        (*this)[i] += rV[i];
      }
      return *this;
    }


  template<>
     Vector<float>&
    Vector<float>::
    BlasGemv(const float alpha, const Matrix<float>& rM, MatrixTrasposeType trans, const Vector<float>& rV, const float beta)
    {
      assert((trans == NO_TRANS && rM.Cols() == rV.mDim && rM.Rows() == mDim)
          || (trans ==    TRANS && rM.Rows() == rV.mDim && rM.Cols() == mDim));

      cblas_sgemv(CblasRowMajor, static_cast<CBLAS_TRANSPOSE>(trans), rM.Rows(), rM.Cols(), alpha, rM.pData(), rM.Stride(),
                  rV.pData(), 1, beta, mpData, 1);
      return *this;
    }



  template<>
     Vector<double>&
    Vector<double>::
    BlasGemv(const double alpha, const Matrix<double>& rM, MatrixTrasposeType trans, const Vector<double>& rV, const double beta)
    {
      assert((trans == NO_TRANS && rM.Cols() == rV.mDim && rM.Rows() == mDim)
          || (trans ==    TRANS && rM.Rows() == rV.mDim && rM.Cols() == mDim));

      cblas_dgemv(CblasRowMajor, static_cast<CBLAS_TRANSPOSE>(trans), rM.Rows(), rM.Cols(), alpha, rM.pData(), rM.Stride(),
                  rV.pData(), 1, beta, mpData, 1);
      return *this;
    }


#else
      #error Routines in this section are not implemented yet without BLAS
#endif

} // namespace TNet


#endif // TNet_Vector_tcc