Commit 21bbdab1 authored by Wei Shi's avatar Wei Shi
Browse files

Adding testing code for CompressedMatrix

git-svn-id: https://svn.code.sf.net/p/kaldi/code/trunk@4601 5e6a8d80-dfce-4ca6-a32a-6e07a63d50c8
parent 8351871c
// matrix/compressed-matrix.cc
// Copyright 2012 Johns Hopkins University (author: Daniel Povey)
// Frantisek Skala
// Frantisek Skala, Wei Shi
// See ../../COPYING for clarification regarding multiple authors
//
......@@ -95,28 +95,27 @@ void CompressedMatrix::CopyFromMat(const MatrixBase<float> &mat);
template
void CompressedMatrix::CopyFromMat(const MatrixBase<double> &mat);
void CompressedMatrix::ExtractFromCompressedMat(
const CompressedMatrix &mat,
CompressedMatrix::CompressedMatrix(
const CompressedMatrix &cmat,
const MatrixIndexT row_offset,
const MatrixIndexT num_rows,
const MatrixIndexT col_offset,
const MatrixIndexT num_cols) {
KALDI_PARANOID_ASSERT(row_offset < mat.NumRows());
KALDI_PARANOID_ASSERT(column_offset < mat.NumCols());
KALDI_PARANOID_ASSERT(row_offset >= 0);
KALDI_PARANOID_ASSERT(column_offset >= 0);
KALDI_ASSERT(row_offset+num_rows < mat.NumRows());
KALDI_ASSERT(col_offset+num_cols < mat.NumCols());
if (data_ != NULL) {
delete [] static_cast<float*>(data_); // call delete [] because was allocated with new float[]
data_ = NULL;
}
if (mat.NumRows() == 0) { return; } // Zero-size matrix stored as zero pointer.
const MatrixIndexT num_cols): data_(NULL) {
KALDI_ASSERT(row_offset < cmat.NumRows());
KALDI_ASSERT(col_offset < cmat.NumCols());
KALDI_ASSERT(row_offset >= 0);
KALDI_ASSERT(col_offset >= 0);
KALDI_ASSERT(row_offset+num_rows <= cmat.NumRows());
KALDI_ASSERT(col_offset+num_cols <= cmat.NumCols());
if (cmat.NumRows() == 0) { return; } // Zero-size matrix stored as zero pointer.
if (num_rows == 0 || num_cols == 0) { return; }
GlobalHeader new_global_header;
KALDI_COMPILE_TIME_ASSERT(sizeof(new_global_header) == 16);
GlobalHeader *old_global_header = reinterpret_cast<GlobalHeader*>(mat.Data());
GlobalHeader *old_global_header = reinterpret_cast<GlobalHeader*>(cmat.Data());
PerColHeader *old_per_col_header =
reinterpret_cast<PerColHeader*>(old_global_header+1);
unsigned char *old_byte_data =
......@@ -142,10 +141,11 @@ void CompressedMatrix::ExtractFromCompressedMat(
for (int32 i = 0; i < num_cols; i++) {
memcpy(new_start_of_col, old_start_of_subcol, num_rows);
new_start_of_col += num_rows;
old_start_of_subcol += num_rows;
old_start_of_subcol += cmat.NumRows();
}
}
template<typename Real>
CompressedMatrix &CompressedMatrix::operator =(const MatrixBase<Real> &mat) {
this->CopyFromMat(mat);
......
// matrix/compressed-matrix.h
// Copyright 2012 Johns Hopkins University (author: Daniel Povey)
// Frantisek Skala
// Frantisek Skala, Wei Shi
// See ../../COPYING for clarification regarding multiple authors
//
......@@ -53,19 +53,10 @@ class CompressedMatrix {
const MatrixIndexT row_offset,
const MatrixIndexT num_rows,
const MatrixIndexT col_offset,
const MatrixIndexT num_cols): data_(NULL) {
ExtractFromCompressedMat(mat, row_offset, num_rows, col_offset, num_cols);
}
const MatrixIndexT num_cols);
void *Data() const { return this->data_; }
void ExtractFromCompressedMat(
const CompressedMatrix &mat,
const MatrixIndexT row_offset,
const MatrixIndexT num_rows,
const MatrixIndexT col_offset,
const MatrixIndexT num_cols);
/// This will resize *this and copy the contents of mat to *this.
template<typename Real>
void CopyFromMat(const MatrixBase<Real> &mat);
......
......@@ -4101,6 +4101,41 @@ template<typename Real> static void UnitTestCompressedMatrix() {
}
template<typename Real>
static void UnitTestExtractCompressedMatrix() {
for (int32 i = 0; i < 30; i++) {
MatrixIndexT num_rows = Rand() % 99, num_cols = Rand() % 35;
if (num_rows * num_cols == 0) {
num_rows = 0;
num_cols = 0;
}
Matrix<Real> mat(num_rows, num_cols);
CompressedMatrix cmat(mat);
MatrixIndexT row_offset = Rand() % num_rows, col_offset = Rand() % num_cols;
MatrixIndexT sub_num_rows = Rand() % (num_rows - row_offset) + 1,
sub_num_cols = Rand() % (num_cols - col_offset) + 1;
KALDI_VLOG(3) << "Whole matrix size: " << num_rows << "," << num_cols;
KALDI_VLOG(4) << "Sub-matrix size: " << sub_num_rows << "," << sub_num_cols
<< " with offsets " << row_offset << "," << col_offset;
CompressedMatrix cmat2(cmat, row_offset, sub_num_rows, //take a subset of
col_offset, sub_num_cols); // the compressed matrix
Matrix<Real> mat2(sub_num_rows, sub_num_cols);
cmat2.CopyToMat(&mat2); // uncompress the subset of the compressed matrix
Matrix<Real> mat3(cmat); // uncompress the whole compressed matrix
SubMatrix<Real> sub_mat(mat3, row_offset, sub_num_rows, col_offset, sub_num_cols);
if(!sub_mat.ApproxEqual(mat2)) {
KALDI_LOG << "sub_mat is: ";
sub_mat.Write(std::cout, false);
KALDI_LOG << "mat2 is: ";
mat2.Write(std::cout, false);
KALDI_ERR << "Matrices differ " << sub_mat << " vs. " << mat2;
}
}
}
template<typename Real>
static void UnitTestTridiag() {
SpMatrix<Real> A(3);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment