[mathicgb] 159/393: Made the copying of a row of a matrix faster through using memcpy.
Doug Torrance
dtorrance-guest at moszumanska.debian.org
Fri Apr 3 15:58:53 UTC 2015
This is an automated email from the git hooks/post-receive script.
dtorrance-guest pushed a commit to branch upstream
in repository mathicgb.
commit 8724a90d722714ae8dd647eec1ca3ee6159b929d
Author: Bjarke Hammersholt Roune <bjarkehr.code at gmail.com>
Date: Tue Feb 5 18:05:30 2013 +0100
Made the copying of a row of a matrix faster through using memcpy.
---
src/mathicgb/F4MatrixBuilder2.cpp | 39 ++++++++++++++++++++++++++++++++++++---
src/mathicgb/RawVector.hpp | 12 ++++++++++++
src/mathicgb/SparseMatrix.cpp | 19 ++++++++++++++-----
3 files changed, 62 insertions(+), 8 deletions(-)
diff --git a/src/mathicgb/F4MatrixBuilder2.cpp b/src/mathicgb/F4MatrixBuilder2.cpp
index 0d5fc0a..6879fc2 100755
--- a/src/mathicgb/F4MatrixBuilder2.cpp
+++ b/src/mathicgb/F4MatrixBuilder2.cpp
@@ -309,7 +309,10 @@ namespace {
SparseMatrix& right,
const PolyRing& ring
) const {
- std::vector<std::pair<SparseMatrix::Scalar,F4MatrixBuilder2::F4PreBlock::Row>> from;
+ left.clear();
+ right.clear();
+ const auto modulus = static_cast<SparseMatrix::Scalar>(ring.charac());
+
const auto end = preBlocks.end();
for (auto it = preBlocks.begin(); it != end; ++it) {
auto& block = **it;
@@ -318,10 +321,40 @@ namespace {
const auto row = block.row(r);
if (row.entryCount == 0)
continue;
- from.emplace_back(std::make_pair(1, row));
+ MATHICGB_ASSERT(row.entryCount != 0);
+ MATHICGB_ASSERT(row.scalars == 0 || row.externalScalars == 0);
+
+ if (row.externalScalars != 0) {
+ auto indices = row.indices;
+ auto indicesEnd = row.indices + row.entryCount;
+ auto scalars = row.externalScalars;
+ for (; indices != indicesEnd; ++indices, ++scalars) {
+ const auto scalar = static_cast<SparseMatrix::Scalar>(*scalars);
+ const auto index = *indices;
+ const auto translated = project(index);
+ if (translated.left)
+ left.appendEntry(translated.index, scalar);
+ else
+ right.appendEntry(translated.index, scalar);
+ }
+ } else {
+ auto indices = row.indices;
+ auto indicesEnd = row.indices + row.entryCount;
+ auto scalars = row.scalars;
+ for (; indices != indicesEnd; ++indices, ++scalars) {
+ const auto index = *indices;
+ const auto translated = project(index);
+ if (translated.left)
+ left.appendEntry(translated.index, *scalars);
+ else
+ right.appendEntry(translated.index, *scalars);
+ }
+ }
+ MATHICGB_ASSERT(left.rowCount() == right.rowCount());
+ left.rowDone();
+ right.rowDone();
}
}
- project(from, left, right, ring);
}
void project(
diff --git a/src/mathicgb/RawVector.hpp b/src/mathicgb/RawVector.hpp
index b7b253a..a904127 100755
--- a/src/mathicgb/RawVector.hpp
+++ b/src/mathicgb/RawVector.hpp
@@ -217,6 +217,18 @@ public:
// **** Extra functionality not available on std::vector
+ // memcpy onto the end of the vector.
+ void memcpy(const T* from, size_t countOfT) {
+ MATHICGB_ASSERT(countOfT <= capacityToGo());
+ std::memcpy(mEnd, from, countOfT * sizeof(T));
+ mEnd += countOfT;
+ }
+
+ /// Unused capacity.
+ size_t capacityToGo() const {
+ return mCapacityEnd - mEnd;
+ }
+
/// Returns true if there is no more capacity left. That is, if
/// capacity() == size().
bool atCapacity() const {
diff --git a/src/mathicgb/SparseMatrix.cpp b/src/mathicgb/SparseMatrix.cpp
index 865404f..14171d8 100755
--- a/src/mathicgb/SparseMatrix.cpp
+++ b/src/mathicgb/SparseMatrix.cpp
@@ -171,11 +171,17 @@ void SparseMatrix::appendRowAndNormalize(
}
void SparseMatrix::appendRow(const SparseMatrix& matrix, const RowIndex row) {
- MATHICGB_ASSERT(row < matrix.rowCount());
- auto it = matrix.rowBegin(row);
- const auto end = matrix.rowEnd(row);
- for (; it != end; ++it)
- appendEntry(it.index(), it.scalar());
+ MATHICGB_ASSERT(row < matrix.rowCount());
+
+ const auto size = matrix.entryCountInRow(row);
+ while (mBlock.mScalars.capacityToGo() < size)
+ growEntryCapacity();
+ MATHICGB_ASSERT(mBlock.mScalars.capacityToGo() ==
+ mBlock.mColIndices.capacityToGo());
+
+ auto const data = matrix.mRows[row];
+ mBlock.mScalars.memcpy(data.mScalarsBegin, size);
+ mBlock.mColIndices.memcpy(data.mIndicesBegin, size);
rowDone();
}
@@ -360,6 +366,7 @@ void SparseMatrix::reserveFreeEntries(const size_t freeCount) {
void SparseMatrix::growEntryCapacity() {
MATHICGB_ASSERT(mBlock.mColIndices.size() == mBlock.mScalars.size());
MATHICGB_ASSERT(mBlock.mColIndices.capacity() == mBlock.mScalars.capacity());
+ MATHICGB_ASSERT(mBlock.mColIndices.size() <= mBlock.mColIndices.capacity());
// TODO: handle overflow of arithmetic here
if (mMemoryQuantum != 0 &&
@@ -374,6 +381,8 @@ void SparseMatrix::growEntryCapacity() {
reserveFreeEntries(mBlock.mColIndices.capacity() * 2);
}
+ MATHICGB_ASSERT(mBlock.mColIndices.size() <= mBlock.mColIndices.capacity());
+
MATHICGB_ASSERT(mBlock.mColIndices.size() == mBlock.mScalars.size());
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mathicgb.git
More information about the debian-science-commits
mailing list