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 64 of file matrix-common.h.

Constructor & Destructor Documentation

◆ PackedMatrix() [1/4]

PackedMatrix ( )
inline

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

45 : data_(NULL), num_rows_(0) {}
MatrixIndexT num_rows_

◆ PackedMatrix() [2/4]

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() [3/4]

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 CopyFromPacked(const PackedMatrix< OtherReal > &orig)
void Resize(MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
Set packed matrix to a specified size (can be zero).

◆ PackedMatrix() [4/4]

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 CopyFromPacked(const PackedMatrix< OtherReal > &orig)
void Resize(MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
Set packed matrix to a specified size (can be zero).

◆ ~PackedMatrix()

~PackedMatrix ( )
inline

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

74  {
75  Destroy();
76  }

Member Function Documentation

◆ AddPacked()

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

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

Referenced by SpMatrix< float >::AddSp(), TpMatrix< float >::AddTp(), and PackedMatrix< float >::Min().

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:185

◆ AddToDiag()

◆ CopyFromPacked()

template void CopyFromPacked ( const PackedMatrix< OtherReal > &  orig)

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

Referenced by PackedMatrix< float >::CopyFromPacked(), SpMatrix< float >::CopyFromSp(), TpMatrix< float >::CopyFromTp(), CuPackedMatrix< Real >::CopyToPacked(), PackedMatrix< float >::operator=(), PackedMatrix< float >::PackedMatrix(), and PackedMatrix< float >::~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 }
MatrixIndexT NumRows() const
size_t SizeInBytes() const
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ CopyFromVec()

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.

Referenced by PackedMatrix< float >::CopyFromVec(), CompressedAffineXformStats::ExtractOneG(), kaldi::UnitTestSpVec(), EbwAmSgmm2Updater::UpdateW(), MleAmSgmm2Updater::UpdateW(), and PackedMatrix< float >::~PackedMatrix().

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:98
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Data() [1/2]

◆ Data() [2/2]

const Real* Data ( ) const
inline

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

103 { return data_; }

◆ Destroy()

void Destroy ( )

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

Referenced by PackedMatrix< float >::Min(), and PackedMatrix< float >::~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 }
MatrixIndexT num_rows_
#define KALDI_MEMALIGN_FREE(x)
Definition: kaldi-utils.h:60

◆ Init()

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.

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 }
MatrixIndexT num_rows_
#define KALDI_MEMALIGN(align, size, pp_orig)
Definition: kaldi-utils.h:58
#define KALDI_WARN
Definition: kaldi-error.h:150

◆ Max()

Real Max ( ) const
inline

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

Referenced by kaldi::ApproxEqual(), OnlineNaturalGradient::ReorthogonalizeRt1(), 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:185

◆ Min()

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:185

◆ NumCols()

◆ NumRows()

MatrixIndexT NumRows ( ) const
inline

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

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

104 { return num_rows_; }
MatrixIndexT num_rows_

◆ operator()() [1/2]

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:185

◆ operator()() [2/2]

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:185

◆ operator=()

PackedMatrix<Real>& operator= ( const PackedMatrix< Real > &  other)
inline

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

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

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

◆ Read()

void Read ( std::istream &  in,
bool  binary,
bool  add = false 
)

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

Referenced by PackedMatrix< float >::Min(), AffineXformStats::Read(), AccumFullGmm::Read(), LdaEstimate::Read(), CuPackedMatrix< Real >::Read(), PackedMatrix< float >::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
void AddPacked(const Real alpha, const PackedMatrix< Real > &M)
kaldi::int32 int32
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
MatrixIndexT NumRows() const
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:98
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
void CopyFromPacked(const PackedMatrix< OtherReal > &orig)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
#define KALDI_STRCASECMP
Definition: kaldi-utils.h:147
void Resize(MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
Set packed matrix to a specified size (can be zero).

◆ Resize()

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.

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

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.

◆ Scale()

void Scale ( Real  c)

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

Referenced by SpMatrix< float >::ApplyFloor(), Fmpe::ComputeC(), kaldi::ComputeFeatureNormalizingTransform(), AmSgmm2::ComputeHsmFromModel(), FastNnetCombiner::ComputePreconditioner(), PldaEstimator::EstimateFromStats(), kaldi::EstPca(), IvectorExtractor::GetAcousticAuxfVariance(), LdaEstimate::GetStats(), CovarianceStats::GetTotalCovar(), CovarianceStats::GetWithinCovar(), IvectorExtractorStats::IvectorVarianceDiagnostic(), main(), MleAmSgmm2Updater::MapUpdateM(), FullGmm::MergedComponentsLogdet(), PackedMatrix< float >::Min(), kaldi::MleFullGmmUpdate(), OnlinePreconditioner::PreconditionDirectionsInternal(), OnlineNaturalGradient::PreconditionDirectionsInternal(), CompressedAffineXformStats::PrepareOneG(), IvectorExtractorStats::PriorDiagnostics(), MleAmSgmm2Updater::RenormalizeN(), MleAmSgmm2Updater::RenormalizeV(), LdaEstimate::Scale(), kaldi::UnitTestCuPackedMatrixScale(), kaldi::UnitTestDeterminantSign(), UnitTestEstimateLda(), kaldi::UnitTestFloorChol(), kaldi::UnitTestIo(), kaldi::UnitTestIoCross(), kaldi::UnitTestMat2Vec(), kaldi::UnitTestNorm(), 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 }
MatrixIndexT num_rows_
void cblas_Xscal(const int N, const float alpha, float *data, const int inc)

◆ ScaleDiag()

void ScaleDiag ( const Real  alpha)

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

Referenced by FullGmm::LogLikelihoodsPreselect(), kaldi::UnitTestCuPackedMatrixScaleDiag(), kaldi::UnitTestScaleDiag(), kaldi::UnitTestTraceSpSpLower(), and PackedMatrix< float >::~PackedMatrix().

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

◆ SetDiag()

void SetDiag ( const Real  alpha)

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

Referenced by kaldi::UnitTestCuSpMatrixConstructor(), kaldi::UnitTestSetDiag(), and PackedMatrix< float >::~PackedMatrix().

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

◆ SetRandn()

void SetRandn ( )

< Set to unit matrix.

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

Referenced by PackedMatrix< float >::PackedMatrix(), 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_

◆ SetUnit()

void SetUnit ( )

< Set to zero

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

Referenced by AmSgmm2::ComputeHsmFromModel(), PldaEstimator::InitParameters(), kaldi::NonOrthogonality(), kaldi::NonUnitness(), PackedMatrix< float >::PackedMatrix(), 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 }
MatrixIndexT num_rows_
int32 MatrixIndexT
Definition: matrix-common.h:98
size_t SizeInBytes() const

◆ SetZero()

◆ SizeInBytes()

size_t SizeInBytes ( ) const
inline

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

Referenced by SpMatrix< float >::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_

◆ Swap() [1/2]

void Swap ( PackedMatrix< Real > *  other)

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

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

Referenced by PackedMatrix< float >::Min(), PackedMatrix< float >::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_

◆ Swap() [2/2]

void Swap ( Matrix< Real > *  other)

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

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_

◆ Trace()

Real Trace ( ) const

< Set to random values of a normal distribution

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

Referenced by PackedMatrix< float >::PackedMatrix(), 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 }
MatrixIndexT num_rows_
int32 MatrixIndexT
Definition: matrix-common.h:98

◆ Write()

void Write ( std::ostream &  out,
bool  binary 
) const

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

Referenced by PackedMatrix< float >::Min(), kaldi::UnitTestIoCross(), AffineXformStats::Write(), AccumFullGmm::Write(), LdaEstimate::Write(), CuPackedMatrix< Real >::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 }
kaldi::int32 int32
MatrixIndexT NumRows() const
int32 MatrixIndexT
Definition: matrix-common.h:98
#define KALDI_ERR
Definition: kaldi-error.h:147
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:185
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

◆ CuPackedMatrix< Real >

friend class CuPackedMatrix< Real >
friend

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

◆ operator<<

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

◆ data_

◆ num_rows_


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