All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
PackedMatrix< Real > Class Template Reference

Packed matrix: base class for triangular and symmetric matrices. More...

#include <matrix-common.h>

Inheritance diagram for PackedMatrix< Real >:
Collaboration diagram for PackedMatrix< Real >:

Public Member Functions

 PackedMatrix ()
 
 PackedMatrix (MatrixIndexT r, MatrixResizeType resize_type=kSetZero)
 
 PackedMatrix (const PackedMatrix< Real > &orig)
 
template<typename OtherReal >
 PackedMatrix (const PackedMatrix< OtherReal > &orig)
 
void SetZero ()
 
void SetUnit ()
 < Set to zero More...
 
void SetRandn ()
 < Set to unit matrix. More...
 
Real Trace () const
 < Set to random values of a normal distribution More...
 
PackedMatrix< Real > & operator= (const PackedMatrix< Real > &other)
 
 ~PackedMatrix ()
 
void Resize (MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
 Set packed matrix to a specified size (can be zero). More...
 
void AddToDiag (const Real r)
 
void ScaleDiag (const Real alpha)
 
void SetDiag (const Real alpha)
 
template<typename OtherReal >
void CopyFromPacked (const PackedMatrix< OtherReal > &orig)
 
template<typename OtherReal >
void CopyFromVec (const SubVector< OtherReal > &orig)
 CopyFromVec just interprets the vector as having the same layout as the packed matrix. More...
 
Real * Data ()
 
const Real * Data () const
 
MatrixIndexT NumRows () const
 
MatrixIndexT NumCols () const
 
size_t SizeInBytes () const
 
Real operator() (MatrixIndexT r, MatrixIndexT c) const
 
Real & operator() (MatrixIndexT r, MatrixIndexT c)
 
Real Max () const
 
Real Min () const
 
void Scale (Real c)
 
void Read (std::istream &in, bool binary, bool add=false)
 
void Write (std::ostream &out, bool binary) const
 
void Destroy ()
 
void Swap (PackedMatrix< Real > *other)
 Swaps the contents of *this and *other. Shallow swap. More...
 
void Swap (Matrix< Real > *other)
 

Protected Member Functions

void AddPacked (const Real alpha, const PackedMatrix< Real > &M)
 

Protected Attributes

Real * data_
 
MatrixIndexT num_rows_
 

Private Member Functions

void Init (MatrixIndexT dim)
 Init assumes the current contents of the class are is invalid (i.e. More...
 

Friends

class CuPackedMatrix< Real >
 
std::ostream & operator<< (std::ostream &out, const PackedMatrix< Real > &m)
 

Detailed Description

template<typename Real>
class kaldi::PackedMatrix< Real >

Packed matrix: base class for triangular and symmetric matrices.

Definition at line 62 of file matrix-common.h.

Constructor & Destructor Documentation

PackedMatrix ( )
inline

Definition at line 45 of file packed-matrix.h.

45 : data_(NULL), num_rows_(0) {}
MatrixIndexT num_rows_
PackedMatrix ( MatrixIndexT  r,
MatrixResizeType  resize_type = kSetZero 
)
inlineexplicit

Definition at line 47 of file packed-matrix.h.

47  :
48  data_(NULL) { Resize(r, resize_type); }
void Resize(MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
Set packed matrix to a specified size (can be zero).
PackedMatrix ( const PackedMatrix< Real > &  orig)
inlineexplicit

Definition at line 50 of file packed-matrix.h.

50  : data_(NULL) {
51  Resize(orig.num_rows_, kUndefined);
52  CopyFromPacked(orig);
53  }
void Resize(MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
Set packed matrix to a specified size (can be zero).
void CopyFromPacked(const PackedMatrix< OtherReal > &orig)
PackedMatrix ( const PackedMatrix< OtherReal > &  orig)
inlineexplicit

Definition at line 56 of file packed-matrix.h.

56  : data_(NULL) {
57  Resize(orig.NumRows(), kUndefined);
58  CopyFromPacked(orig);
59  }
void Resize(MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
Set packed matrix to a specified size (can be zero).
void CopyFromPacked(const PackedMatrix< OtherReal > &orig)
~PackedMatrix ( )
inline

Definition at line 74 of file packed-matrix.h.

74  {
75  Destroy();
76  }

Member Function Documentation

void AddPacked ( const Real  alpha,
const PackedMatrix< Real > &  M 
)
protected

Definition at line 40 of file packed-matrix.cc.

References kaldi::cblas_Xaxpy(), PackedMatrix< Real >::Data(), data_, KALDI_ASSERT, and PackedMatrix< Real >::NumRows().

Referenced by SpMatrix< BaseFloat >::AddSp(), and TpMatrix< BaseFloat >::AddTp().

40  {
41  KALDI_ASSERT(num_rows_ == rMa.NumRows());
42  size_t nr = num_rows_,
43  sz = (nr * (nr + 1)) / 2;
44  cblas_Xaxpy(sz, alpha, rMa.Data(), 1, data_, 1);
45 }
MatrixIndexT num_rows_
void cblas_Xaxpy(const int N, const float alpha, const float *X, const int incX, float *Y, const int incY)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
template void CopyFromPacked ( const PackedMatrix< OtherReal > &  orig)

Definition at line 153 of file packed-matrix.cc.

References PackedMatrix< Real >::Data(), data_, rnnlm::i, KALDI_ASSERT, and PackedMatrix< Real >::NumRows().

Referenced by SpMatrix< BaseFloat >::CopyFromSp(), TpMatrix< BaseFloat >::CopyFromTp(), CuPackedMatrix< Real >::CopyToPacked(), PackedMatrix< BaseFloat >::operator=(), and PackedMatrix< BaseFloat >::PackedMatrix().

153  {
154  KALDI_ASSERT(NumRows() == orig.NumRows());
155  if (sizeof(Real) == sizeof(OtherReal)) {
156  memcpy(data_, orig.Data(), SizeInBytes());
157  } else {
158  Real *dst = data_;
159  const OtherReal *src = orig.Data();
160  size_t nr = NumRows(),
161  size = (nr * (nr + 1)) / 2;
162  for (size_t i = 0; i < size; i++, dst++, src++)
163  *dst = *src;
164  }
165 }
size_t SizeInBytes() const
MatrixIndexT NumRows() const
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
template void CopyFromVec ( const SubVector< OtherReal > &  orig)

CopyFromVec just interprets the vector as having the same layout as the packed matrix.

Must have the same dimension, i.e. orig.Dim() == (NumRows()*(NumRows()+1)) / 2;

Definition at line 181 of file packed-matrix.cc.

References VectorBase< Real >::Data(), data_, VectorBase< Real >::Dim(), rnnlm::i, and KALDI_ASSERT.

Referenced by CompressedAffineXformStats::ExtractOneG(), kaldi::UnitTestSpVec(), EbwAmSgmm2Updater::UpdateW(), and MleAmSgmm2Updater::UpdateW().

181  {
182  MatrixIndexT size = (NumRows()*(NumRows()+1)) / 2;
183  KALDI_ASSERT(vec.Dim() == size);
184  if (sizeof(Real) == sizeof(OtherReal)) {
185  memcpy(data_, vec.Data(), size * sizeof(Real));
186  } else {
187  Real *dst = data_;
188  const OtherReal *src = vec.Data();
189  for (MatrixIndexT i = 0; i < size; i++, dst++, src++)
190  *dst = *src;
191  }
192 }
MatrixIndexT NumRows() const
int32 MatrixIndexT
Definition: matrix-common.h:96
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
const Real* Data ( ) const
inline

Definition at line 103 of file packed-matrix.h.

103 { return data_; }
void Destroy ( )

Definition at line 227 of file packed-matrix.cc.

References data_, and KALDI_MEMALIGN_FREE.

Referenced by PackedMatrix< BaseFloat >::~PackedMatrix().

227  {
228  // we need to free the data block if it was defined
229  if (data_ != NULL) KALDI_MEMALIGN_FREE(data_);
230  data_ = NULL;
231  num_rows_ = 0;
232 }
#define KALDI_MEMALIGN_FREE(x)
Definition: kaldi-utils.h:56
MatrixIndexT num_rows_
void Init ( MatrixIndexT  dim)
inlineprivate

Init assumes the current contents of the class are is invalid (i.e.

junk or has already been freed), and it sets the matrixd to newly allocated memory with the specified dimension. dim == 0 is acceptable. The memory contents pointed to by data_ will be undefined.

Definition at line 56 of file packed-matrix.cc.

References data_, KALDI_MEMALIGN, and KALDI_WARN.

56  {
57  if (r == 0) {
58  num_rows_ = 0;
59  data_ = 0;
60  return;
61  }
62  size_t size = ((static_cast<size_t>(r) * static_cast<size_t>(r + 1)) / 2);
63 
64  if (static_cast<size_t>(static_cast<MatrixIndexT>(size)) != size) {
65  KALDI_WARN << "Allocating packed matrix whose full dimension does not fit "
66  << "in MatrixIndexT: not all code is tested for this case.";
67  }
68 
69  void *data; // aligned memory block
70  void *temp;
71 
72  if ((data = KALDI_MEMALIGN(16, size * sizeof(Real), &temp)) != NULL) {
73  this->data_ = static_cast<Real *> (data);
74  this->num_rows_ = r;
75  } else {
76  throw std::bad_alloc();
77  }
78 }
#define KALDI_MEMALIGN(align, size, pp_orig)
Definition: kaldi-utils.h:54
#define KALDI_WARN
Definition: kaldi-error.h:130
MatrixIndexT num_rows_
Real Max ( ) const
inline

Definition at line 133 of file packed-matrix.h.

Referenced by kaldi::ApproxEqual(), OnlineNaturalGradient::ReorthogonalizeXt1(), OnlinePreconditioner::ReorthogonalizeXt1(), and kaldi::UnitTestMaxMin().

133  {
134  KALDI_ASSERT(num_rows_ > 0);
135  return * (std::max_element(data_, data_ + ((num_rows_*(num_rows_+1))/2) ));
136  }
MatrixIndexT num_rows_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
Real Min ( ) const
inline

Definition at line 138 of file packed-matrix.h.

Referenced by kaldi::ApproxEqual(), and kaldi::UnitTestMaxMin().

138  {
139  KALDI_ASSERT(num_rows_ > 0);
140  return * (std::min_element(data_, data_ + ((num_rows_*(num_rows_+1))/2) ));
141  }
MatrixIndexT num_rows_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
MatrixIndexT NumRows ( ) const
inline

Definition at line 104 of file packed-matrix.h.

Referenced by SpMatrix< Real >::AddMat2Sp(), PackedMatrix< Real >::AddPacked(), SpMatrix< Real >::AddSmat2Sp(), MatrixBase< Real >::AddSp(), MatrixBase< Real >::AddSpSp(), VectorBase< Real >::AddSpVec(), VectorBase< Real >::AddTpVec(), SpMatrix< Real >::AddVec2Sp(), SpMatrix< Real >::ApplyFloor(), kaldi::ApproxEqual(), SpMatrix< Real >::ApproxEqual(), kaldi::AssertDiagEqual(), kaldi::AssertEqual(), MatrixExponential< Real >::Backprop(), IvectorExtractorStats::CheckDims(), TpMatrix< Real >::Cholesky(), MatrixExponential< Real >::Compute(), BasisFmllrEstimate::ComputeAmDiagPrecond(), FastNnetCombiner::ComputeCurrentNnet(), Sgmm2Project::ComputeLdaTransform(), kaldi::ComputeNormalizingTransform(), FastNnetCombiner::ComputePreconditioner(), PackedMatrix< Real >::CopyFromPacked(), CuPackedMatrix< Real >::CopyFromPacked(), MatrixBase< Real >::CopyFromSp(), MatrixBase< Real >::CopyFromTp(), VectorBase< Real >::CopyRowFromSp(), CuPackedMatrix< Real >::CopyToPacked(), kaldi::Debias(), CovarianceStats::Dim(), LdaEstimate::Estimate(), FeatureTransformEstimate::EstimateInternal(), kaldi::EstimateSgmm2FmllrSubspace(), CompressedAffineXformStats::ExtractOneG(), FastNnetCombiner::GetInitialParams(), kaldi::GetLogDetNoFailure(), GetLogLikeTest(), IvectorExtractor::GetPriorAuxf(), kaldi::GpsrBasic(), kaldi::GpsrBasicAlpha(), kaldi::GpsrBB(), kaldi::InitRand(), kaldi::InitRandNonsingular(), IvectorExtractor::InvertWithFlooring(), IvectorExtractor::IvectorExtractor(), kaldi::LinearCgd(), main(), MleAmSgmm2Updater::MapUpdateM(), Matrix< BaseFloat >::Matrix(), VectorBase< Real >::MulTp(), kaldi::NonDiagonalness(), kaldi::NonUnitness(), kaldi::nnet2::PerturbTrainingExample(), CompressedAffineXformStats::PrepareOneG(), Sgmm2Project::ProjectVariance(), kaldi::unittest::RandFullGaussFeatures(), AffineXformStats::Read(), PackedMatrix< Real >::Read(), VectorBase< Real >::Solve(), kaldi::SolveDoubleQuadraticMatrixProblem(), kaldi::SolveQuadraticMatrixProblem(), kaldi::SolveQuadraticProblem(), SubVector< Real >::SubVector(), CuMatrixBase< Real >::SymInvertPosDef(), kaldi::TraceMatSpMat(), kaldi::TraceMatSpMatSp(), kaldi::TraceSpMat(), kaldi::TraceSpSp(), kaldi::TraceSpSpLower(), kaldi::UnitTestCuSpMatrixOperator(), kaldi::UnitTestSpInvert(), kaldi::UnitTestSpLogExp(), kaldi::UnitTestTpInvert(), kaldi::UnitTestTransposeScatter(), IvectorExtractorStats::UpdateVariances(), MleAmSgmm2Updater::UpdateVars(), kaldi::VecSpVec(), and FisherComputationClass::~FisherComputationClass().

104 { return num_rows_; }
MatrixIndexT num_rows_
Real operator() ( MatrixIndexT  r,
MatrixIndexT  c 
) const
inline

Definition at line 114 of file packed-matrix.h.

114  {
115  KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(r) <
116  static_cast<UnsignedMatrixIndexT>(num_rows_) &&
117  static_cast<UnsignedMatrixIndexT>(c) <
118  static_cast<UnsignedMatrixIndexT>(num_rows_)
119  && c <= r);
120  return *(data_ + (r * (r + 1)) / 2 + c);
121  }
MatrixIndexT num_rows_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
Real& operator() ( MatrixIndexT  r,
MatrixIndexT  c 
)
inline

Definition at line 124 of file packed-matrix.h.

124  {
125  KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(r) <
126  static_cast<UnsignedMatrixIndexT>(num_rows_) &&
127  static_cast<UnsignedMatrixIndexT>(c) <
128  static_cast<UnsignedMatrixIndexT>(num_rows_)
129  && c <= r);
130  return *(data_ + (r * (r + 1)) / 2 + c);
131  }
MatrixIndexT num_rows_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
PackedMatrix<Real>& operator= ( const PackedMatrix< Real > &  other)
inline

Definition at line 68 of file packed-matrix.h.

Referenced by TpMatrix< BaseFloat >::operator=(), and SpMatrix< BaseFloat >::operator=().

68  {
69  Resize(other.NumRows());
70  CopyFromPacked(other);
71  return *this;
72  }
void Resize(MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
Set packed matrix to a specified size (can be zero).
void CopyFromPacked(const PackedMatrix< OtherReal > &orig)
void Read ( std::istream &  in,
bool  binary,
bool  add = false 
)

Definition at line 298 of file packed-matrix.cc.

References data_, rnnlm::i, KALDI_ASSERT, KALDI_ERR, KALDI_STRCASECMP, KALDI_WARN, PackedMatrix< Real >::NumRows(), kaldi::Peek(), PackedMatrix< Real >::Read(), kaldi::ReadBasicType(), and kaldi::ReadToken().

Referenced by AffineXformStats::Read(), BasisFmllrAccus::Read(), AccumFullGmm::Read(), LdaEstimate::Read(), CuPackedMatrix< Real >::Read(), PackedMatrix< Real >::Read(), Fmpe::Read(), AmSgmm2::Read(), OnlineIvectorEstimationStats::Read(), IvectorExtractorStats::Read(), kaldi::UnitTestIo(), and kaldi::UnitTestIoCross().

298  {
299  if (add) {
300  PackedMatrix<Real> tmp;
301  tmp.Read(is, binary, false); // read without adding.
302  if (this->NumRows() == 0) this->Resize(tmp.NumRows());
303  else {
304  if (this->NumRows() != tmp.NumRows()) {
305  if (tmp.NumRows() == 0) return; // do nothing in this case.
306  else KALDI_ERR << "PackedMatrix::Read, size mismatch " << this->NumRows()
307  << " vs. " << tmp.NumRows();
308  }
309  }
310  this->AddPacked(1.0, tmp);
311  return;
312  } // now assume add == false.
313 
314  std::ostringstream specific_error;
315  MatrixIndexT pos_at_start = is.tellg();
316  int peekval = Peek(is, binary);
317  const char *my_token = (sizeof(Real) == 4 ? "FP" : "DP");
318  const char *new_format_token = "[";
319  bool is_new_format = false;//added by hxu
320  char other_token_start = (sizeof(Real) == 4 ? 'D' : 'F');
321  int32 size;
322  MatrixIndexT num_elems;
323 
324  if (peekval == other_token_start) { // need to instantiate the other type to read it.
325  typedef typename OtherReal<Real>::Real OtherType; // if Real == float, OtherType == double, and vice versa.
326  PackedMatrix<OtherType> other(this->NumRows());
327  other.Read(is, binary, false); // add is false at this point.
328  this->Resize(other.NumRows());
329  this->CopyFromPacked(other);
330  return;
331  }
332  std::string token;
333  ReadToken(is, binary, &token);
334  if (token != my_token) {
335  if(token != new_format_token) {
336  specific_error << ": Expected token " << my_token << ", got " << token;
337  goto bad;
338  }
339  //new format it is
340  is_new_format = true;
341  }
342  if(!is_new_format) {
343  ReadBasicType(is, binary, &size); // throws on error.
344  if ((MatrixIndexT)size != this->NumRows()) {
345  KALDI_ASSERT(size>=0);
346  this->Resize(size);
347  }
348  num_elems = ((size+1)*(MatrixIndexT)size)/2;
349  if (!binary) {
350  for (MatrixIndexT i = 0; i < num_elems; i++) {
351  ReadBasicType(is, false, data_+i); // will throw on error.
352  }
353  } else {
354  if (num_elems)
355  is.read(reinterpret_cast<char*>(data_), sizeof(Real)*num_elems);
356  }
357  if (is.fail()) goto bad;
358  return;
359  }
360  else {
361  std::vector<Real> data;
362  while(1) {
363  int32 num_lines = 0;
364  int i = is.peek();
365  if (i == -1) { specific_error << "Got EOF while reading matrix data"; goto bad; }
366  else if (static_cast<char>(i) == ']') { // Finished reading matrix.
367  is.get(); // eat the "]".
368  i = is.peek();
369  if (static_cast<char>(i) == '\r') {
370  is.get();
371  is.get(); // get \r\n (must eat what we wrote)
372  }// I don't actually understand what it's doing here
373  else if (static_cast<char>(i) == '\n') { is.get(); } // get \n (must eat what we wrote)
374 
375  if (is.fail()) {
376  KALDI_WARN << "After end of matrix data, read error.";
377  // we got the data we needed, so just warn for this error.
378  }
379  //now process the data:
380  num_lines = int32(sqrt(data.size()*2));
381 
382  KALDI_ASSERT(data.size() == num_lines*(num_lines+1)/2);
383 
384  this->Resize(num_lines);
385 
386  //std::cout<<data.size()<<' '<<num_lines<<'\n';
387 
388  for(int32 i = 0; i < data.size(); i++) {
389  data_[i] = data[i];
390  }
391  return;
392  //std::cout<<"here!!!!!hxu!!!!!"<<std::endl;
393  }
394  else if ( (i >= '0' && i <= '9') || i == '-' ) { // A number...
395  Real r;
396  is >> r;
397  if (is.fail()) {
398  specific_error << "Stream failure/EOF while reading matrix data.";
399  goto bad;
400  }
401  data.push_back(r);
402  }
403  else if (isspace(i)) {
404  is.get(); // eat the space and do nothing.
405  } else { // NaN or inf or error.
406  std::string str;
407  is >> str;
408  if (!KALDI_STRCASECMP(str.c_str(), "inf") ||
409  !KALDI_STRCASECMP(str.c_str(), "infinity")) {
410  data.push_back(std::numeric_limits<Real>::infinity());
411  KALDI_WARN << "Reading infinite value into matrix.";
412  } else if (!KALDI_STRCASECMP(str.c_str(), "nan")) {
413  data.push_back(std::numeric_limits<Real>::quiet_NaN());
414  KALDI_WARN << "Reading NaN value into matrix.";
415  } else {
416  specific_error << "Expecting numeric matrix data, got " << str;
417  goto bad;
418  }
419  }
420  }
421  }
422 bad:
423  KALDI_ERR << "Failed to read packed matrix from stream. " << specific_error.str()
424  << " File position at start is "
425  << pos_at_start << ", currently " << is.tellg();
426 }
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:55
MatrixIndexT NumRows() const
void ReadToken(std::istream &is, bool binary, std::string *str)
ReadToken gets the next token and puts it in str (exception on failure).
Definition: io-funcs.cc:154
int Peek(std::istream &is, bool binary)
Peek consumes whitespace (if binary == false) and then returns the peek() value of the stream...
Definition: io-funcs.cc:145
int32 MatrixIndexT
Definition: matrix-common.h:96
void Resize(MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
Set packed matrix to a specified size (can be zero).
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_WARN
Definition: kaldi-error.h:130
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
#define KALDI_STRCASECMP
Definition: kaldi-utils.h:140
void CopyFromPacked(const PackedMatrix< OtherReal > &orig)
void AddPacked(const Real alpha, const PackedMatrix< Real > &M)
void Resize ( MatrixIndexT  nRows,
MatrixResizeType  resize_type = kSetZero 
)

Set packed matrix to a specified size (can be zero).

The value of the new data depends on resize_type: -if kSetZero, the new data will be zero -if kUndefined, the new data will be undefined -if kCopyData, the new data will be the same as the old data in any shared positions, and zero elsewhere. This function takes time proportional to the number of data elements.

Definition at line 94 of file packed-matrix.cc.

References data_, PackedMatrix< Real >::data_, kaldi::kCopyData, kaldi::kSetZero, kaldi::kUndefined, and PackedMatrix< Real >::Swap().

Referenced by PackedMatrix< BaseFloat >::operator=(), PackedMatrix< BaseFloat >::PackedMatrix(), SpMatrix< BaseFloat >::Resize(), TpMatrix< BaseFloat >::Resize(), and CuPackedMatrix< Real >::Swap().

94  {
95  // the next block uses recursion to handle what we have to do if
96  // resize_type == kCopyData.
97  if (resize_type == kCopyData) {
98  if (this->data_ == NULL || r == 0) resize_type = kSetZero; // nothing to copy.
99  else if (this->num_rows_ == r) { return; } // nothing to do.
100  else {
101  // set tmp to a packed matrix of the desired size.
102  PackedMatrix<Real> tmp(r, kUndefined);
103  size_t r_min = std::min(r, num_rows_);
104  size_t mem_size_min = sizeof(Real) * (r_min*(r_min+1))/2,
105  mem_size_full = sizeof(Real) * (r*(r+1))/2;
106  // Copy the contents to tmp.
107  memcpy(tmp.data_, data_, mem_size_min);
108  char *ptr = static_cast<char*>(static_cast<void*>(tmp.data_));
109  // Set the rest of the contents of tmp to zero.
110  memset(static_cast<void*>(ptr + mem_size_min), 0, mem_size_full-mem_size_min);
111  tmp.Swap(this);
112  return;
113  }
114  }
115  if (data_ != NULL) Destroy();
116  Init(r);
117  if (resize_type == kSetZero) SetZero();
118 }
MatrixIndexT num_rows_
void Init(MatrixIndexT dim)
Init assumes the current contents of the class are is invalid (i.e.
void Scale ( Real  c)

Definition at line 33 of file packed-matrix.cc.

References kaldi::cblas_Xscal(), and data_.

Referenced by SpMatrix< Real >::ApplyFloor(), Fmpe::ComputeC(), kaldi::ComputeFeatureNormalizingTransform(), AmSgmm2::ComputeHsmFromModel(), FastNnetCombiner::ComputePreconditioner(), PldaEstimator::EstimateFromStats(), IvectorExtractor::GetAcousticAuxfVariance(), LdaEstimate::GetStats(), CovarianceStats::GetTotalCovar(), CovarianceStats::GetWithinCovar(), IvectorExtractorStats::IvectorVarianceDiagnostic(), main(), MleAmSgmm2Updater::MapUpdateM(), FullGmm::MergedComponentsLogdet(), kaldi::MleFullGmmUpdate(), OnlineNaturalGradient::PreconditionDirectionsInternal(), OnlinePreconditioner::PreconditionDirectionsInternal(), CompressedAffineXformStats::PrepareOneG(), IvectorExtractorStats::PriorDiagnostics(), MleAmSgmm2Updater::RenormalizeN(), MleAmSgmm2Updater::RenormalizeV(), LdaEstimate::Scale(), OnlineIvectorEstimationStats::Scale(), kaldi::UnitTestCuPackedMatrixScale(), kaldi::UnitTestDeterminantSign(), UnitTestEstimateLda(), kaldi::UnitTestFloorChol(), kaldi::UnitTestIo(), kaldi::UnitTestIoCross(), kaldi::UnitTestMat2Vec(), kaldi::UnitTestNorm(), kaldi::UnitTestSpLogExp(), kaldi::UnitTestTraceSpSpLower(), EbwAmSgmm2Updater::UpdateM(), EbwAmSgmm2Updater::UpdateN(), EbwAmSgmm2Updater::UpdatePhoneVectorsInternal(), PldaUnsupervisedAdaptor::UpdatePlda(), IvectorExtractorStats::UpdatePrior(), EbwAmSgmm2Updater::UpdateU(), IvectorExtractorStats::UpdateVariances(), EbwAmSgmm2Updater::UpdateVars(), and MleAmSgmm2Updater::UpdateVars().

33  {
34  size_t nr = num_rows_,
35  sz = (nr * (nr + 1)) / 2;
36  cblas_Xscal(sz, alpha, data_, 1);
37 }
void cblas_Xscal(const int N, const float alpha, float *data, const int inc)
MatrixIndexT num_rows_
void ScaleDiag ( const Real  alpha)

Definition at line 132 of file packed-matrix.cc.

References data_, and rnnlm::i.

Referenced by FullGmm::LogLikelihoodsPreselect(), kaldi::UnitTestCuPackedMatrixScaleDiag(), kaldi::UnitTestScaleDiag(), and kaldi::UnitTestTraceSpSpLower().

132  {
133  Real *ptr = data_;
134  for (MatrixIndexT i = 2; i <= num_rows_+1; i++) {
135  *ptr *= alpha;
136  ptr += i;
137  }
138 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
void SetDiag ( const Real  alpha)

Definition at line 141 of file packed-matrix.cc.

References data_, and rnnlm::i.

Referenced by kaldi::UnitTestCuSpMatrixConstructor(), and kaldi::UnitTestSetDiag().

141  {
142  Real *ptr = data_;
143  for (MatrixIndexT i = 2; i <= num_rows_+1; i++) {
144  *ptr = alpha;
145  ptr += i;
146  }
147 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
void SetRandn ( )

< Set to unit matrix.

Definition at line 48 of file packed-matrix.cc.

References data_, rnnlm::i, and kaldi::RandGauss().

Referenced by kaldi::UnitTestAddMat2(), kaldi::UnitTestAddMat2Sp(), kaldi::UnitTestAddSp(), kaldi::UnitTestAddToDiag(), kaldi::UnitTestAddVec2Sp(), kaldi::UnitTestCuMatrixAddMatTp(), kaldi::UnitTestCuMatrixAddTpMat(), kaldi::UnitTestCuMatrixCopyFromTp(), kaldi::UnitTestCuPackedMatrixAddToDiag(), kaldi::UnitTestCuPackedMatrixConstructor(), kaldi::UnitTestCuPackedMatrixCopy(), kaldi::UnitTestCuPackedMatrixScale(), kaldi::UnitTestCuPackedMatrixScaleDiag(), kaldi::UnitTestCuPackedMatrixTrace(), kaldi::UnitTestCuSpMatrixAddMat2(), kaldi::UnitTestCuSpMatrixAddSp(), kaldi::UnitTestCuSpMatrixAddToDiag(), kaldi::UnitTestCuSpMatrixAddVec2(), kaldi::UnitTestCuSpMatrixApproxEqual(), kaldi::UnitTestCuSpMatrixOperator(), kaldi::UnitTestCuSpMatrixTraceSpSp(), kaldi::UnitTestCuTpMatrixCholesky(), kaldi::UnitTestCuTpMatrixCopyFromTp(), kaldi::UnitTestCuTpMatrixInvert(), kaldi::UnitTestCuVectorAddTp(), kaldi::UnitTestCuVectorAddTpVec(), kaldi::UnitTestCuVectorMulTp(), kaldi::UnitTestEigSymmetric(), kaldi::UnitTestInnerProd(), kaldi::UnitTestIo(), kaldi::UnitTestIoCross(), kaldi::UnitTestMaxAbsEig(), kaldi::UnitTestMaxMin(), kaldi::UnitTestMul(), kaldi::UnitTestMulTp(), kaldi::UnitTestNorm(), kaldi::UnitTestResize(), kaldi::UnitTestScaleDiag(), kaldi::UnitTestSimpleForMat(), kaldi::UnitTestSpAddDiagVec(), kaldi::UnitTestSpAddVecVec(), kaldi::UnitTestSvdSpeed(), kaldi::UnitTestTp2(), kaldi::UnitTestTp2Sp(), kaldi::UnitTestTrace(), kaldi::UnitTestTraceSpSpLower(), and kaldi::UnitTestTriVecSolver().

48  {
49  Real *data = data_;
50  size_t dim = num_rows_, size = ((dim*(dim+1))/2);
51  for (size_t i = 0; i < size; i++)
52  data[i] = RandGauss();
53 }
float RandGauss(struct RandomState *state=NULL)
Definition: kaldi-math.h:155
MatrixIndexT num_rows_
void SetUnit ( )

< Set to zero

Definition at line 212 of file packed-matrix.cc.

References data_.

Referenced by AmSgmm2::ComputeHsmFromModel(), MleAmSgmm2Updater::ComputeMPrior(), PldaEstimator::InitParameters(), kaldi::NonOrthogonality(), kaldi::NonUnitness(), kaldi::UnitTestCuSpMatrixSetUnit(), and MleAmSgmm2Updater::UpdateVars().

212  {
213  memset(data_, 0, SizeInBytes());
214  for (MatrixIndexT row = 0;row < num_rows_;row++)
215  (*this)(row, row) = 1.0;
216 }
size_t SizeInBytes() const
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
size_t SizeInBytes ( ) const
inline

Definition at line 106 of file packed-matrix.h.

Referenced by SpMatrix< Real >::AddMat2Sp(), and CuPackedMatrix< Real >::CopyFromPacked().

106  {
107  size_t nr = static_cast<size_t>(num_rows_);
108  return ((nr * (nr+1)) / 2) * sizeof(Real);
109  }
MatrixIndexT num_rows_
void Swap ( PackedMatrix< Real > *  other)

Swaps the contents of *this and *other. Shallow swap.

Definition at line 81 of file packed-matrix.cc.

References data_, PackedMatrix< Real >::data_, PackedMatrix< Real >::num_rows_, and kaldi::swap().

Referenced by PackedMatrix< Real >::Resize(), and CuPackedMatrix< Real >::Swap().

81  {
82  std::swap(data_, other->data_);
83  std::swap(num_rows_, other->num_rows_);
84 }
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
MatrixIndexT num_rows_
void Swap ( Matrix< Real > *  other)

Definition at line 87 of file packed-matrix.cc.

References data_, MatrixBase< Real >::data_, MatrixBase< Real >::num_rows_, and kaldi::swap().

87  {
88  std::swap(data_, other->data_);
89  std::swap(num_rows_, other->num_rows_);
90 }
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
MatrixIndexT num_rows_
Real Trace ( ) const

< Set to random values of a normal distribution

Definition at line 219 of file packed-matrix.cc.

Referenced by kaldi::UnitTestCuPackedMatrixTrace(), and kaldi::UnitTestCuTpMatrixCopyFromMat().

219  {
220  Real ans = 0.0;
221  for (MatrixIndexT row = 0;row < num_rows_;row++)
222  ans += (*this)(row, row);
223  return ans;
224 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
void Write ( std::ostream &  out,
bool  binary 
) const

Definition at line 236 of file packed-matrix.cc.

References data_, rnnlm::i, rnnlm::j, KALDI_ASSERT, KALDI_ERR, kaldi::WriteBasicType(), and kaldi::WriteToken().

Referenced by kaldi::UnitTestIoCross(), AffineXformStats::Write(), BasisFmllrAccus::Write(), AccumFullGmm::Write(), LdaEstimate::Write(), CuPackedMatrix< Real >::Write(), Fmpe::Write(), OnlineIvectorEstimationStats::Write(), and IvectorExtractorStats::Write().

236  {
237  if (!os.good()) {
238  KALDI_ERR << "Failed to write vector to stream: stream not good";
239  }
240 
241  int32 size = this->NumRows(); // make the size 32-bit on disk.
242  KALDI_ASSERT(this->NumRows() == (MatrixIndexT) size);
243  MatrixIndexT num_elems = ((size+1)*(MatrixIndexT)size)/2;
244 
245  if(binary) {
246  std::string my_token = (sizeof(Real) == 4 ? "FP" : "DP");
247  WriteToken(os, binary, my_token);
248  WriteBasicType(os, binary, size);
249  // We don't use the built-in Kaldi write routines for the floats, as they are
250  // not efficient enough.
251  os.write((const char*) data_, sizeof(Real) * num_elems);
252  }
253  else {
254  if(size == 0)
255  os<<"[ ]\n";
256  else {
257  os<<"[\n";
258  MatrixIndexT i = 0;
259  for (int32 j = 0; j < size; j++) {
260  for (int32 k = 0; k < j + 1; k++) {
261  WriteBasicType(os, binary, data_[i++]);
262  }
263  os << ( (j==size-1)? "]\n" : "\n");
264  }
265  KALDI_ASSERT(i == num_elems);
266  }
267  }
268  if (os.fail()) {
269  KALDI_ERR << "Failed to write packed matrix to stream";
270  }
271 }
MatrixIndexT NumRows() const
int32 MatrixIndexT
Definition: matrix-common.h:96
#define KALDI_ERR
Definition: kaldi-error.h:127
void WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
Definition: io-funcs.cc:134
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:34

Friends And Related Function Documentation

friend class CuPackedMatrix< Real >
friend

Definition at line 41 of file packed-matrix.h.

std::ostream& operator<< ( std::ostream &  out,
const PackedMatrix< Real > &  m 
)
friend

Definition at line 181 of file packed-matrix.h.

181  {
182  M.Write(os, false);
183  return os;
184 }

Member Data Documentation


The documentation for this class was generated from the following files: