summaryrefslogtreecommitdiff
path: root/htk_io/src/KaldiLib/Matrix.cc
blob: f9d59092c14caef4246f518d9681faa49cd3a8de (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/** 
 * @file Matrix.cc 
 * 
 * Implementation of specialized Matrix template methods 
 */


#include "Matrix.h"

#if defined(HAVE_CLAPACK)
#include "CLAPACK-3.1.1.1/INCLUDE/f2c.h"
extern "C" {
#include "CLAPACK-3.1.1.1/INCLUDE/clapack.h"
}
// These are some stupid clapack things that we want to get rid of
#ifdef min
#undef min
#endif

#ifdef max
#undef max
#endif

#endif




namespace TNet
{
  //***************************************************************************
  //***************************************************************************
#ifdef HAVE_ATLAS
  //***************************************************************************
  //***************************************************************************
  template<>
    Matrix<float> &
    Matrix<float>::
     Invert(float *LogDet, float *DetSign, bool inverse_needed)
  { 
      assert(Rows() == Cols());
      
#if defined(HAVE_CLAPACK)
      integer* pivot = new integer[mMRows];
      integer  M = Rows();
      integer  N = Cols();
      integer  LDA = mStride;
      integer  result;
      integer  l_work = std::max<integer>(1, N);
      float*   p_work = new float[l_work];

      sgetrf_(&M, &N, mpData, &LDA, pivot, &result);
	  const int pivot_offset=1;
#else
      int* pivot = new int[mMRows];
      int result = clapack_sgetrf(CblasColMajor, Rows(), Cols(), mpData, mStride, pivot);
	  const int pivot_offset=0;
#endif
      assert(result >= 0 && "Call to CLAPACK sgetrf_ or ATLAS clapack_sgetrf called with wrong arguments");
      if(result != 0) {
        Error("Matrix is singular");
      }
	  if(DetSign!=NULL){ *DetSign=1.0; for(size_t i=0;i<mMRows;i++) if(pivot[i]!=(int)i+pivot_offset) *DetSign *= -1.0; }
	  if(LogDet!=NULL||DetSign!=NULL){ // Compute log determinant...
		assert(mMRows==mMCols); // Can't take determinant of non-square matrix.
		*LogDet = 0.0;  float prod = 1.0;
		for(size_t i=0;i<mMRows;i++){ 
		  prod *= (*this)(i,i); 
		  if(i==mMRows-1 || fabs(prod)<1.0e-10 || fabs(prod)>1.0e+10){ 
			if(LogDet!=NULL) *LogDet += log(fabs(prod)); 
			if(DetSign!=NULL) *DetSign *= (prod>0?1.0:-1.0);
			prod=1.0;
		  }
		}
	  }
#if defined(HAVE_CLAPACK)
      if(inverse_needed) sgetri_(&M, mpData, &LDA, pivot, p_work, &l_work, &result);
      delete [] pivot;
#else
      if(inverse_needed) result = clapack_sgetri(CblasColMajor, Rows(), mpData, mStride, pivot);
      delete [] pivot;
#endif
      assert(result == 0 && "Call to CLAPACK sgetri_ or ATLAS clapack_sgetri called with wrong arguments");
      return *this;
    }

  
  //***************************************************************************
  //***************************************************************************
  template<>
    Matrix<double> &
    Matrix<double>::
     Invert(double *LogDet, double *DetSign, bool inverse_needed)
    { 
      assert(Rows() == Cols());
      
#if defined(HAVE_CLAPACK)
      integer* pivot = new integer[mMRows];
      integer  M = Rows();
      integer  N = Cols();
      integer  LDA = mStride;
      integer  result;
      integer  l_work = std::max<integer>(1, N);
      double*   p_work = new double[l_work];

      dgetrf_(&M, &N, mpData, &LDA, pivot, &result);
	  const int pivot_offset=1;
#else
      int* pivot = new int[mMRows];
      int result = clapack_dgetrf(CblasColMajor, Rows(), Cols(), mpData, mStride, pivot);
	  const int pivot_offset=0;
#endif
      assert(result >= 0 && "Call to CLAPACK dgetrf_ or ATLAS clapack_dgetrf called with wrong arguments");
      if(result != 0) {
        Error("Matrix is singular");
      }
	  if(DetSign!=NULL){ *DetSign=1.0; for(size_t i=0;i<mMRows;i++) if(pivot[i]!=(int)i+pivot_offset) *DetSign *= -1.0; }
	  if(LogDet!=NULL||DetSign!=NULL){ // Compute log determinant...
		assert(mMRows==mMCols); // Can't take determinant of non-square matrix.
		*LogDet = 0.0;  double prod = 1.0;
		for(size_t i=0;i<mMRows;i++){ 
		  prod *= (*this)(i,i); 
		  if(i==mMRows-1 || fabs(prod)<1.0e-10 || fabs(prod)>1.0e+10){ 
			if(LogDet!=NULL) *LogDet += log(fabs(prod)); 
			if(DetSign!=NULL) *DetSign *= (prod>0?1.0:-1.0);
			prod=1.0;
		  }
		}
	  }
#if defined(HAVE_CLAPACK)
      if(inverse_needed) dgetri_(&M, mpData, &LDA, pivot, p_work, &l_work, &result);
      delete [] pivot;
#else
      if(inverse_needed) result = clapack_dgetri(CblasColMajor, Rows(), mpData, mStride, pivot);
      delete [] pivot;
#endif
      assert(result == 0 && "Call to CLAPACK dgetri_ or ATLAS clapack_dgetri called with wrong arguments");
      return *this;
    }

  template<>
    Matrix<float> &
    Matrix<float>::
    BlasGer(const float alpha, const Vector<float>& rA, const Vector<float>& rB)
    {
      assert(rA.Dim() == mMRows && rB.Dim() == mMCols);
      cblas_sger(CblasRowMajor, rA.Dim(), rB.Dim(), alpha, rA.pData(), 1, rB.pData(), 1, mpData, mStride);
      return *this;
    }

  template<>
    Matrix<double> &
    Matrix<double>::
  BlasGer(const double alpha, const Vector<double>& rA, const Vector<double>& rB)
    {
      assert(rA.Dim() == mMRows && rB.Dim() == mMCols);
      cblas_dger(CblasRowMajor, rA.Dim(), rB.Dim(), alpha, rA.pData(), 1, rB.pData(), 1, mpData, mStride);
      return *this;
    }
  
  template<>
    Matrix<float>&
    Matrix<float>::
    BlasGemm(const float alpha,
              const Matrix<float>& rA, MatrixTrasposeType transA,
              const Matrix<float>& rB, MatrixTrasposeType transB,
              const float beta)
    {
      assert((transA == NO_TRANS && transB == NO_TRANS && rA.Cols() == rB.Rows() && rA.Rows() == Rows() && rB.Cols() == Cols())
	     || (transA ==    TRANS && transB == NO_TRANS && rA.Rows() == rB.Rows() && rA.Cols() == Rows() && rB.Cols() == Cols())
	     || (transA == NO_TRANS && transB ==    TRANS && rA.Cols() == rB.Cols() && rA.Rows() == Rows() && rB.Rows() == Cols())
	     || (transA ==    TRANS && transB ==    TRANS && rA.Rows() == rB.Cols() && rA.Cols() == Rows() && rB.Rows() == Cols()));

      cblas_sgemm(CblasRowMajor, static_cast<CBLAS_TRANSPOSE>(transA), static_cast<CBLAS_TRANSPOSE>(transB),
                  Rows(), Cols(), transA == NO_TRANS ? rA.Cols() : rA.Rows(),
                  alpha, rA.mpData, rA.mStride, rB.mpData, rB.mStride,
                  beta, mpData, mStride);
      return *this;
    }

  template<>
   Matrix<double>&
    Matrix<double>::
    BlasGemm(const double alpha,
              const Matrix<double>& rA, MatrixTrasposeType transA,
              const Matrix<double>& rB, MatrixTrasposeType transB,
              const double beta)
    {
      assert((transA == NO_TRANS && transB == NO_TRANS && rA.Cols() == rB.Rows() && rA.Rows() == Rows()