mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-08-12 11:49:02 +08:00
Speed up parsing of sparse Market file.
This commit is contained in:
parent
684cfc762d
commit
e2f4ee1c2b
@ -12,38 +12,38 @@
|
|||||||
#define EIGEN_SPARSE_MARKET_IO_H
|
#define EIGEN_SPARSE_MARKET_IO_H
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace Eigen {
|
namespace Eigen {
|
||||||
|
|
||||||
namespace internal
|
namespace internal
|
||||||
{
|
{
|
||||||
template <typename Scalar>
|
template <typename Scalar, typename StorageIndex>
|
||||||
inline bool GetMarketLine (std::stringstream& line, Index& M, Index& N, Index& i, Index& j, Scalar& value)
|
inline void GetMarketLine (const char* line, StorageIndex& i, StorageIndex& j, Scalar& value)
|
||||||
{
|
{
|
||||||
line >> i >> j >> value;
|
std::stringstream sline(line);
|
||||||
i--;
|
sline >> i >> j >> value;
|
||||||
j--;
|
|
||||||
if(i>=0 && j>=0 && i<M && j<N)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
template <typename Scalar>
|
|
||||||
inline bool GetMarketLine (std::stringstream& line, Index& M, Index& N, Index& i, Index& j, std::complex<Scalar>& value)
|
template<> inline void GetMarketLine (const char* line, int& i, int& j, float& value)
|
||||||
|
{ std::sscanf(line, "%d %d %g", &i, &j, &value); }
|
||||||
|
|
||||||
|
template<> inline void GetMarketLine (const char* line, int& i, int& j, double& value)
|
||||||
|
{ std::sscanf(line, "%d %d %lg", &i, &j, &value); }
|
||||||
|
|
||||||
|
template<> inline void GetMarketLine (const char* line, int& i, int& j, std::complex<float>& value)
|
||||||
|
{ std::sscanf(line, "%d %d %g %g", &i, &j, &numext::real_ref(value), &numext::imag_ref(value)); }
|
||||||
|
|
||||||
|
template<> inline void GetMarketLine (const char* line, int& i, int& j, std::complex<double>& value)
|
||||||
|
{ std::sscanf(line, "%d %d %lg %lg", &i, &j, &numext::real_ref(value), &numext::imag_ref(value)); }
|
||||||
|
|
||||||
|
template <typename Scalar, typename StorageIndex>
|
||||||
|
inline void GetMarketLine (const char* line, StorageIndex& i, StorageIndex& j, std::complex<Scalar>& value)
|
||||||
{
|
{
|
||||||
|
std::stringstream sline(line);
|
||||||
Scalar valR, valI;
|
Scalar valR, valI;
|
||||||
line >> i >> j >> valR >> valI;
|
sline >> i >> j >> valR >> valI;
|
||||||
i--;
|
value = std::complex<Scalar>(valR,valI);
|
||||||
j--;
|
|
||||||
if(i>=0 && j>=0 && i<M && j<N)
|
|
||||||
{
|
|
||||||
value = std::complex<Scalar>(valR, valI);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename RealScalar>
|
template <typename RealScalar>
|
||||||
@ -81,13 +81,13 @@ namespace internal
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Scalar>
|
template<typename Scalar, typename StorageIndex>
|
||||||
inline void PutMatrixElt(Scalar value, int row, int col, std::ofstream& out)
|
inline void PutMatrixElt(Scalar value, StorageIndex row, StorageIndex col, std::ofstream& out)
|
||||||
{
|
{
|
||||||
out << row << " "<< col << " " << value << "\n";
|
out << row << " "<< col << " " << value << "\n";
|
||||||
}
|
}
|
||||||
template<typename Scalar>
|
template<typename Scalar, typename StorageIndex>
|
||||||
inline void PutMatrixElt(std::complex<Scalar> value, int row, int col, std::ofstream& out)
|
inline void PutMatrixElt(std::complex<Scalar> value, StorageIndex row, StorageIndex col, std::ofstream& out)
|
||||||
{
|
{
|
||||||
out << row << " " << col << " " << value.real() << " " << value.imag() << "\n";
|
out << row << " " << col << " " << value.real() << " " << value.imag() << "\n";
|
||||||
}
|
}
|
||||||
@ -133,17 +133,20 @@ template<typename SparseMatrixType>
|
|||||||
bool loadMarket(SparseMatrixType& mat, const std::string& filename)
|
bool loadMarket(SparseMatrixType& mat, const std::string& filename)
|
||||||
{
|
{
|
||||||
typedef typename SparseMatrixType::Scalar Scalar;
|
typedef typename SparseMatrixType::Scalar Scalar;
|
||||||
typedef typename SparseMatrixType::Index Index;
|
typedef typename SparseMatrixType::StorageIndex StorageIndex;
|
||||||
std::ifstream input(filename.c_str(),std::ios::in);
|
std::ifstream input(filename.c_str(),std::ios::in);
|
||||||
if(!input)
|
if(!input)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
char rdbuffer[4096];
|
||||||
|
input.rdbuf()->pubsetbuf(rdbuffer, 4096);
|
||||||
|
|
||||||
const int maxBuffersize = 2048;
|
const int maxBuffersize = 2048;
|
||||||
char buffer[maxBuffersize];
|
char buffer[maxBuffersize];
|
||||||
|
|
||||||
bool readsizes = false;
|
bool readsizes = false;
|
||||||
|
|
||||||
typedef Triplet<Scalar,Index> T;
|
typedef Triplet<Scalar,StorageIndex> T;
|
||||||
std::vector<T> elements;
|
std::vector<T> elements;
|
||||||
|
|
||||||
Index M(-1), N(-1), NNZ(-1);
|
Index M(-1), N(-1), NNZ(-1);
|
||||||
@ -155,32 +158,35 @@ bool loadMarket(SparseMatrixType& mat, const std::string& filename)
|
|||||||
if(buffer[0]=='%')
|
if(buffer[0]=='%')
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
std::stringstream line(buffer);
|
|
||||||
|
|
||||||
if(!readsizes)
|
if(!readsizes)
|
||||||
{
|
{
|
||||||
|
std::stringstream line(buffer);
|
||||||
line >> M >> N >> NNZ;
|
line >> M >> N >> NNZ;
|
||||||
if(M > 0 && N > 0 && NNZ > 0)
|
if(M > 0 && N > 0 && NNZ > 0)
|
||||||
{
|
{
|
||||||
readsizes = true;
|
readsizes = true;
|
||||||
//std::cout << "sizes: " << M << "," << N << "," << NNZ << "\n";
|
|
||||||
mat.resize(M,N);
|
mat.resize(M,N);
|
||||||
mat.reserve(NNZ);
|
mat.reserve(NNZ);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Index i(-1), j(-1);
|
StorageIndex i(-1), j(-1);
|
||||||
Scalar value;
|
Scalar value;
|
||||||
if( internal::GetMarketLine(line, M, N, i, j, value) )
|
internal::GetMarketLine(buffer, i, j, value);
|
||||||
|
|
||||||
|
i--;
|
||||||
|
j--;
|
||||||
|
if(i>=0 && j>=0 && i<M && j<N)
|
||||||
{
|
{
|
||||||
++ count;
|
++count;
|
||||||
elements.push_back(T(i,j,value));
|
elements.push_back(T(i,j,value));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
std::cerr << "Invalid read: " << i << "," << j << "\n";
|
std::cerr << "Invalid read: " << i << "," << j << "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mat.setFromTriplets(elements.begin(), elements.end());
|
mat.setFromTriplets(elements.begin(), elements.end());
|
||||||
if(count!=NNZ)
|
if(count!=NNZ)
|
||||||
std::cerr << count << "!=" << NNZ << "\n";
|
std::cerr << count << "!=" << NNZ << "\n";
|
||||||
|
@ -129,6 +129,19 @@ template<typename SparseMatrixType> void sparse_extra(const SparseMatrixType& re
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename SparseMatrixType>
|
||||||
|
void check_marketio()
|
||||||
|
{
|
||||||
|
typedef Matrix<typename SparseMatrixType::Scalar, Dynamic, Dynamic> DenseMatrix;
|
||||||
|
Index rows = internal::random<Index>(1,100);
|
||||||
|
Index cols = internal::random<Index>(1,100);
|
||||||
|
SparseMatrixType m1, m2;
|
||||||
|
m1 = DenseMatrix::Random(rows, cols).sparseView();
|
||||||
|
saveMarket(m1, "sparse_extra.mtx");
|
||||||
|
loadMarket(m2, "sparse_extra.mtx");
|
||||||
|
VERIFY_IS_APPROX(m1,m2);
|
||||||
|
}
|
||||||
|
|
||||||
void test_sparse_extra()
|
void test_sparse_extra()
|
||||||
{
|
{
|
||||||
for(int i = 0; i < g_repeat; i++) {
|
for(int i = 0; i < g_repeat; i++) {
|
||||||
@ -143,5 +156,15 @@ void test_sparse_extra()
|
|||||||
|
|
||||||
CALL_SUBTEST_3( (sparse_product<DynamicSparseMatrix<float, ColMajor> >()) );
|
CALL_SUBTEST_3( (sparse_product<DynamicSparseMatrix<float, ColMajor> >()) );
|
||||||
CALL_SUBTEST_3( (sparse_product<DynamicSparseMatrix<float, RowMajor> >()) );
|
CALL_SUBTEST_3( (sparse_product<DynamicSparseMatrix<float, RowMajor> >()) );
|
||||||
|
|
||||||
|
CALL_SUBTEST_4( (check_marketio<SparseMatrix<float,ColMajor,int> >()) );
|
||||||
|
CALL_SUBTEST_4( (check_marketio<SparseMatrix<double,ColMajor,int> >()) );
|
||||||
|
CALL_SUBTEST_4( (check_marketio<SparseMatrix<std::complex<float>,ColMajor,int> >()) );
|
||||||
|
CALL_SUBTEST_4( (check_marketio<SparseMatrix<std::complex<double>,ColMajor,int> >()) );
|
||||||
|
CALL_SUBTEST_4( (check_marketio<SparseMatrix<float,ColMajor,long int> >()) );
|
||||||
|
CALL_SUBTEST_4( (check_marketio<SparseMatrix<double,ColMajor,long int> >()) );
|
||||||
|
CALL_SUBTEST_4( (check_marketio<SparseMatrix<std::complex<float>,ColMajor,long int> >()) );
|
||||||
|
CALL_SUBTEST_4( (check_marketio<SparseMatrix<std::complex<double>,ColMajor,long int> >()) );
|
||||||
|
TEST_SET_BUT_UNUSED_VARIABLE(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user