Skip to content

Commit

Permalink
allowing gmm::csc_matrix and gmm::csr_matrix to be also templetized b…
Browse files Browse the repository at this point in the history
…y index type. This was necessary to interface them with Intel's MKL, which requires int for indexes.
  • Loading branch information
Andriy.Andreykiv committed Jun 6, 2019
1 parent f222414 commit 0616541
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 70 deletions.
42 changes: 21 additions & 21 deletions src/gmm/gmm_inoutput.h
Expand Up @@ -124,12 +124,12 @@ namespace gmm {
/** open filename and reads header */
void open(const char *filename);
/** read the opened file */
template <typename T, int shift> void read(csc_matrix<T, shift>& A);
template <typename T, typename IND_TYPE, int shift> void read(csc_matrix<T, IND_TYPE, shift>& A);
template <typename MAT> void read(MAT &M) IS_DEPRECATED;
template <typename T, int shift>
static void write(const char *filename, const csc_matrix<T, shift>& A);
template <typename T, int shift>
static void write(const char *filename, const csc_matrix<T, shift>& A,
template <typename T, typename IND_TYPE, int shift>
static void write(const char *filename, const csc_matrix<T, IND_TYPE, shift>& A);
template <typename T, typename IND_TYPE, int shift>
static void write(const char *filename, const csc_matrix<T, IND_TYPE, shift>& A,
const std::vector<T> &rhs);
template <typename T, typename INDI, typename INDJ, int shift>
static void write(const char *filename,
Expand Down Expand Up @@ -324,8 +324,8 @@ namespace gmm {
}

/* only valid for double and complex<double> csc matrices */
template <typename T, int shift> void
HarwellBoeing_IO::read(csc_matrix<T, shift>& A) {
template <typename T, typename IND_TYPE, int shift> void
HarwellBoeing_IO::read(csc_matrix<T, IND_TYPE, shift>& A) {

// typedef typename csc_matrix<T, shift>::IND_TYPE IND_TYPE;

Expand Down Expand Up @@ -531,17 +531,17 @@ namespace gmm {
return 1;
}

template <typename T, int shift> void
template <typename T, typename IND_TYPE, int shift> void
HarwellBoeing_IO::write(const char *filename,
const csc_matrix<T, shift>& A) {
const csc_matrix<T, IND_TYPE, shift>& A) {
write(filename, csc_matrix_ref<const T*, const unsigned*,
const unsigned *, shift>
(&A.pr[0], &A.ir[0], &A.jc[0], A.nr, A.nc));
}

template <typename T, int shift> void
template <typename T, typename IND_TYPE, int shift> void
HarwellBoeing_IO::write(const char *filename,
const csc_matrix<T, shift>& A,
const csc_matrix<T, IND_TYPE, shift>& A,
const std::vector<T> &rhs) {
write(filename, csc_matrix_ref<const T*, const unsigned*,
const unsigned *, shift>
Expand Down Expand Up @@ -593,9 +593,9 @@ namespace gmm {
/** save a "double" or "std::complex<double>" csc matrix into a
HarwellBoeing file
*/
template <typename T, int shift> inline void
template <typename T, typename IND_TYPE, int shift> inline void
Harwell_Boeing_save(const std::string &filename,
const csc_matrix<T, shift>& A)
const csc_matrix<T, IND_TYPE, shift>& A)
{ HarwellBoeing_IO::write(filename.c_str(), A); }

/** save a reference on "double" or "std::complex<double>" csc matrix
Expand Down Expand Up @@ -631,8 +631,8 @@ namespace gmm {
/** load a "double" or "std::complex<double>" csc matrix from a
HarwellBoeing file
*/
template <typename T, int shift> void
Harwell_Boeing_load(const std::string &filename, csc_matrix<T, shift>& A) {
template <typename T, typename IND_TYPE, int shift> void
Harwell_Boeing_load(const std::string &filename, csc_matrix<T, IND_TYPE, shift>& A) {
HarwellBoeing_IO h(filename.c_str()); h.read(A);
}

Expand Down Expand Up @@ -1022,8 +1022,8 @@ namespace gmm {
/* read opened file */
template <typename Matrix> void read(Matrix &A);
/* write a matrix */
template <typename T, int shift> static void
write(const char *filename, const csc_matrix<T, shift>& A);
template <typename T, typename IND_TYPE, int shift> static void
write(const char *filename, const csc_matrix<T, IND_TYPE, shift>& A);
template <typename T, typename INDI, typename INDJ, int shift> static void
write(const char *filename,
const csc_matrix_ref<T*, INDI*, INDJ*, shift>& A);
Expand All @@ -1038,8 +1038,8 @@ namespace gmm {
mm.read(A);
}
/** write a matrix-market file */
template <typename T, int shift> void
MatrixMarket_save(const char *filename, const csc_matrix<T, shift>& A) {
template <typename T, typename IND_TYPE, int shift> void
MatrixMarket_save(const char *filename, const csc_matrix<T, IND_TYPE, shift>& A) {
MatrixMarket_IO mm; mm.write(filename, A);
}

Expand Down Expand Up @@ -1106,8 +1106,8 @@ namespace gmm {
}
}

template <typename T, int shift> void
MatrixMarket_IO::write(const char *filename, const csc_matrix<T, shift>& A) {
template <typename T, typename IND_TYPE, int shift> void
MatrixMarket_IO::write(const char *filename, const csc_matrix<T, IND_TYPE, shift>& A) {
write(filename, csc_matrix_ref<const T*, const unsigned*,
const unsigned*,shift>
(&A.pr[0], &A.ir[0], &A.jc[0], A.nr, A.nc));
Expand Down
94 changes: 45 additions & 49 deletions src/gmm/gmm_matrix.h
Expand Up @@ -497,9 +497,8 @@ namespace gmm
/* */
/* ******************************************************************** */

template <typename T, int shift = 0>
template <typename T, typename IND_TYPE = unsigned int, int shift = 0>
struct csc_matrix {
typedef unsigned int IND_TYPE;

std::vector<T> pr;
std::vector<IND_TYPE> ir;
Expand All @@ -519,7 +518,7 @@ namespace gmm
void init_with(const csc_matrix_ref<PT1,PT2,PT3,cshift>& B)
{ init_with_good_format(B); }
template <typename U, int cshift>
void init_with(const csc_matrix<U, cshift>& B)
void init_with(const csc_matrix<U, IND_TYPE, cshift>& B)
{ init_with_good_format(B); }

void init_with_identity(size_type n);
Expand All @@ -529,7 +528,7 @@ namespace gmm

size_type nrows(void) const { return nr; }
size_type ncols(void) const { return nc; }
void swap(csc_matrix<T, shift> &m) {
void swap(csc_matrix<T, IND_TYPE, shift> &m) {
std::swap(pr, m.pr);
std::swap(ir, m.ir); std::swap(jc, m.jc);
std::swap(nc, m.nc); std::swap(nr, m.nr);
Expand All @@ -538,8 +537,8 @@ namespace gmm
{ return mat_col(*this, j)[i]; }
};

template <typename T, int shift> template<typename Matrix>
void csc_matrix<T, shift>::init_with_good_format(const Matrix &B) {
template <typename T, typename IND_TYPE, int shift> template<typename Matrix>
void csc_matrix<T, IND_TYPE, shift>::init_with_good_format(const Matrix &B) {
typedef typename linalg_traits<Matrix>::const_sub_col_type col_type;
nc = mat_ncols(B); nr = mat_nrows(B);
jc.resize(nc+1);
Expand All @@ -560,33 +559,33 @@ namespace gmm
}
}

template <typename T, int shift> template <typename Matrix>
void csc_matrix<T, shift>::init_with(const Matrix &A) {
template <typename T, typename IND_TYPE, int shift>
template <typename Matrix>
void csc_matrix<T, IND_TYPE, shift>::init_with(const Matrix &A) {
col_matrix<wsvector<T> > B(mat_nrows(A), mat_ncols(A));
copy(A, B);
init_with_good_format(B);
}

template <typename T, int shift>
void csc_matrix<T, shift>::init_with_identity(size_type n) {
template <typename T, typename IND_TYPE, int shift>
void csc_matrix<T, IND_TYPE, shift>::init_with_identity(size_type n) {
nc = nr = n;
pr.resize(nc); ir.resize(nc); jc.resize(nc+1);
for (size_type j = 0; j < nc; ++j)
{ ir[j] = jc[j] = shift + j; pr[j] = T(1); }
jc[nc] = shift + nc;
}

template <typename T, int shift>
csc_matrix<T, shift>::csc_matrix(size_type nnr, size_type nnc)
template <typename T, typename IND_TYPE, int shift>
csc_matrix<T, IND_TYPE, shift>::csc_matrix(size_type nnr, size_type nnc)
: nc(nnc), nr(nnr) {
pr.resize(1); ir.resize(1); jc.resize(nc+1);
for (size_type j = 0; j <= nc; ++j) jc[j] = shift;
}

template <typename T, int shift>
struct linalg_traits<csc_matrix<T, shift> > {
typedef csc_matrix<T, shift> this_type;
typedef typename this_type::IND_TYPE IND_TYPE;
template <typename T, typename IND_TYPE, int shift>
struct linalg_traits<csc_matrix<T, IND_TYPE, shift> > {
typedef csc_matrix<T, IND_TYPE, shift> this_type;
typedef linalg_const is_reference;
typedef abstract_matrix linalg_type;
typedef T value_type;
Expand Down Expand Up @@ -625,17 +624,17 @@ namespace gmm
{ return col(itcol)[j]; }
};

template <typename T, int shift>
template <typename T, typename IND_TYPE, int shift>
std::ostream &operator <<
(std::ostream &o, const csc_matrix<T, shift>& m)
(std::ostream &o, const csc_matrix<T, IND_TYPE, shift>& m)
{ gmm::write(o,m); return o; }

template <typename T, int shift>
inline void copy(const identity_matrix &, csc_matrix<T, shift>& M)
template <typename T, typename IND_TYPE, int shift>
inline void copy(const identity_matrix &, csc_matrix<T, IND_TYPE, shift>& M)
{ M.init_with_identity(mat_nrows(M)); }

template <typename Matrix, typename T, int shift>
inline void copy(const Matrix &A, csc_matrix<T, shift>& M)
template <typename Matrix, typename T, typename IND_TYPE, int shift>
inline void copy(const Matrix &A, csc_matrix<T, IND_TYPE, shift>& M)
{ M.init_with(A); }

/* ******************************************************************** */
Expand All @@ -644,11 +643,9 @@ namespace gmm
/* */
/* ******************************************************************** */

template <typename T, int shift = 0>
template <typename T, typename IND_TYPE = unsigned int, int shift = 0>
struct csr_matrix {

typedef unsigned int IND_TYPE;

std::vector<T> pr; // values.
std::vector<IND_TYPE> ir; // col indices.
std::vector<IND_TYPE> jc; // row repartition on pr and ir.
Expand All @@ -667,7 +664,7 @@ namespace gmm
void init_with(const csr_matrix_ref<PT1,PT2,PT3,cshift>& B)
{ init_with_good_format(B); }
template <typename U, int cshift>
void init_with(const csr_matrix<U, cshift>& B)
void init_with(const csr_matrix<U, IND_TYPE, cshift>& B)
{ init_with_good_format(B); }

template <typename Matrix> void init_with(const Matrix &A);
Expand All @@ -678,7 +675,7 @@ namespace gmm

size_type nrows(void) const { return nr; }
size_type ncols(void) const { return nc; }
void swap(csr_matrix<T, shift> &m) {
void swap(csr_matrix<T, IND_TYPE, shift> &m) {
std::swap(pr, m.pr);
std::swap(ir,m.ir); std::swap(jc, m.jc);
std::swap(nc, m.nc); std::swap(nr,m.nr);
Expand All @@ -688,8 +685,8 @@ namespace gmm
{ return mat_row(*this, i)[j]; }
};

template <typename T, int shift> template <typename Matrix>
void csr_matrix<T, shift>::init_with_good_format(const Matrix &B) {
template <typename T, typename IND_TYPE, int shift> template <typename Matrix>
void csr_matrix<T, IND_TYPE, shift>::init_with_good_format(const Matrix &B) {
typedef typename linalg_traits<Matrix>::const_sub_row_type row_type;
nc = mat_ncols(B); nr = mat_nrows(B);
jc.resize(nr+1);
Expand All @@ -710,35 +707,34 @@ namespace gmm
}
}

template <typename T, int shift> template <typename Matrix>
void csr_matrix<T, shift>::init_with(const Matrix &A) {
template <typename T, typename IND_TYPE, int shift> template <typename Matrix>
void csr_matrix<T, IND_TYPE, shift>::init_with(const Matrix &A) {
row_matrix<wsvector<T> > B(mat_nrows(A), mat_ncols(A));
copy(A, B);
init_with_good_format(B);
}

template <typename T, int shift>
void csr_matrix<T, shift>::init_with_identity(size_type n) {
template <typename T, typename IND_TYPE, int shift>
void csr_matrix<T, IND_TYPE, shift>::init_with_identity(size_type n) {
nc = nr = n;
pr.resize(nr); ir.resize(nr); jc.resize(nr+1);
for (size_type j = 0; j < nr; ++j)
{ ir[j] = jc[j] = shift + j; pr[j] = T(1); }
jc[nr] = shift + nr;
}

template <typename T, int shift>
csr_matrix<T, shift>::csr_matrix(size_type nnr, size_type nnc)
template <typename T, typename IND_TYPE, int shift>
csr_matrix<T, IND_TYPE, shift>::csr_matrix(size_type nnr, size_type nnc)
: nc(nnc), nr(nnr) {
pr.resize(1); ir.resize(1); jc.resize(nr+1);
for (size_type j = 0; j < nr; ++j) jc[j] = shift;
jc[nr] = shift;
}


template <typename T, int shift>
struct linalg_traits<csr_matrix<T, shift> > {
typedef csr_matrix<T, shift> this_type;
typedef typename this_type::IND_TYPE IND_TYPE;
template <typename T, typename IND_TYPE, int shift>
struct linalg_traits<csr_matrix<T, IND_TYPE, shift> > {
typedef csr_matrix<T, IND_TYPE, shift> this_type;
typedef linalg_const is_reference;
typedef abstract_matrix linalg_type;
typedef T value_type;
Expand Down Expand Up @@ -775,17 +771,17 @@ namespace gmm
{ return row(itrow)[j]; }
};

template <typename T, int shift>
template <typename T, typename IND_TYPE, int shift>
std::ostream &operator <<
(std::ostream &o, const csr_matrix<T, shift>& m)
(std::ostream &o, const csr_matrix<T, IND_TYPE, shift>& m)
{ gmm::write(o,m); return o; }

template <typename T, int shift>
inline void copy(const identity_matrix &, csr_matrix<T, shift>& M)
template <typename T, typename IND_TYPE, int shift>
inline void copy(const identity_matrix &, csr_matrix<T, IND_TYPE, shift>& M)
{ M.init_with_identity(mat_nrows(M)); }

template <typename Matrix, typename T, int shift>
inline void copy(const Matrix &A, csr_matrix<T, shift>& M)
template <typename Matrix, typename T, typename IND_TYPE, int shift>
inline void copy(const Matrix &A, csr_matrix<T, IND_TYPE, shift>& M)
{ M.init_with(A); }

/* ******************************************************************** */
Expand Down Expand Up @@ -1185,11 +1181,11 @@ namespace std {
template <typename T>
void swap(gmm::dense_matrix<T> &m1, gmm::dense_matrix<T> &m2)
{ m1.swap(m2); }
template <typename T, int shift> void
swap(gmm::csc_matrix<T,shift> &m1, gmm::csc_matrix<T,shift> &m2)
template <typename T, typename IND_TYPE, int shift> void
swap(gmm::csc_matrix<T, IND_TYPE, shift> &m1, gmm::csc_matrix<T, IND_TYPE, shift> &m2)
{ m1.swap(m2); }
template <typename T, int shift> void
swap(gmm::csr_matrix<T,shift> &m1, gmm::csr_matrix<T,shift> &m2)
template <typename T, typename IND_TYPE, int shift> void
swap(gmm::csr_matrix<T, IND_TYPE, shift> &m1, gmm::csr_matrix<T, IND_TYPE, shift> &m2)
{ m1.swap(m2); }
}

Expand Down

0 comments on commit 0616541

Please sign in to comment.