#ifndef HPC_GEMATRIX_H #define HPC_GEMATRIX_H 1 #include #include #include #include #include "bench.h" namespace matvec { enum class StorageOrder { ColMajor, RowMajor }; template struct GeMatrixView; template struct GeMatrix { typedef T ElementType; typedef I Index; typedef GeMatrix NoView; typedef GeMatrixView View; GeMatrix(Index m, Index n, StorageOrder order=StorageOrder::ColMajor) : m(m), n(n), incRow(order==StorageOrder::ColMajor ? 1: n), incCol(order==StorageOrder::RowMajor ? 1: m), data(new T[m*n]) { } ~GeMatrix() { delete[] data; } const ElementType & operator()(Index i, Index j) const { assert(i struct GeMatrixView { typedef T ElementType; typedef I Index; typedef GeMatrix NoView; typedef GeMatrixView View; GeMatrixView(Index m, Index n, T *data, Index incRow, Index incCol) : m(m), n(n), incRow(incRow), incCol(incCol), data(data) { } GeMatrixView(const GeMatrixView &rhs) : m(rhs.m), n(rhs.n), incRow(rhs.incRow), incCol(rhs.incCol), data(rhs.data) { } const ElementType & operator()(Index i, Index j) const { assert(i struct GeMatrixCombinedConstView { typedef typename std::common_type::type ElementType; typedef typename std::common_type::type Index; template GeMatrixCombinedConstView(const Matrix1& A, const Matrix2& B, ApplyOperator apply) : m(A.m), n(A.n), A(A), B(B), apply(apply) { assert(A.m == B.m && A.n == B.n); } const ElementType operator()(Index i, Index j) const { return apply(A(i, j), B(i, j)); } const Index m; const Index n; const Matrix1& A; const Matrix2& B; std::function apply; }; // // Interface for bench // template void initGeMatrix(GeMatrix &A, InitValue initValue) { bench::initGeMatrix(A.m, A.n, A.data, A.incRow, A.incCol, initValue); } template void applyGeMatrix(GeMatrix &A, ApplyOperator apply) { for (typename GeMatrix::Index i = 0; i < A.m; ++i) { for (typename GeMatrix::Index j = 0; j < A.n; ++j) { apply(A(i, j), i, j); } } } template typename GeMatrix::ElementType asumDiffGeMatrix(const GeMatrix &A, const GeMatrix &B) { return bench::asumDiffGeMatrix(A.m, A.n, A.data, A.incRow, A.incCol, B.data, B.incRow, B.incCol); } } // namespace matvec #endif // HPC_GEMATRIX_H