9 #ifndef OPENVDB_MATH_CONJGRADIENT_HAS_BEEN_INCLUDED 10 #define OPENVDB_MATH_CONJGRADIENT_HAS_BEEN_INCLUDED 17 #include <tbb/parallel_for.h> 18 #include <tbb/parallel_reduce.h> 37 template<
typename ValueType>
class Vector;
55 template<
typename ValueType>
63 s.
absoluteError = std::numeric_limits<ValueType>::epsilon() * 100.0;
90 template<
typename PositiveDefMatrix>
93 const PositiveDefMatrix& A,
97 const State& termination = terminationDefaults<typename PositiveDefMatrix::ValueType>());
122 template<
typename PositiveDefMatrix,
typename Interrupter>
125 const PositiveDefMatrix& A,
129 Interrupter& interrupter,
130 const State& termination = terminationDefaults<typename PositiveDefMatrix::ValueType>());
151 ~Vector() { mSize = 0;
delete[] mData; mData =
nullptr; }
161 bool empty()
const {
return (mSize == 0); }
168 void swap(
Vector& other) { std::swap(mData, other.mData); std::swap(mSize, other.mSize); }
175 template<
typename Scalar>
void scale(
const Scalar& s);
192 template<
typename OtherValueType>
197 std::string str()
const;
209 inline T*
data() {
return mData; }
210 inline const T*
data()
const {
return mData; }
216 template<
typename Scalar>
struct ScaleOp;
217 struct DeterministicDotProductOp;
219 template<
typename OtherValueType>
struct EqOp;
236 template<
typename ValueType_, SizeType STENCIL_SIZE>
244 class ConstValueIter;
248 static inline constexpr
ValueType sZeroValue = zeroVal<ValueType>();
276 ConstRow getConstRow(
SizeType row)
const;
279 RowEditor getRowEditor(
SizeType row);
283 template<
typename Scalar>
void scale(
const Scalar& s);
284 template<
typename Scalar>
291 template<
typename VecValueType>
298 template<
typename VecValueType>
299 void vectorMultiply(
const VecValueType* inVec, VecValueType* resultVec)
const;
303 template<
typename OtherValueType>
311 std::string str()
const;
319 struct ConstRowData {
321 mVals(v), mCols(c), mSize(s) {}
326 template<
typename DataType_ = RowData>
330 using DataType = DataType_;
332 static SizeType capacity() {
return STENCIL_SIZE; }
334 RowBase(
const DataType& data): mData(data) {}
336 bool empty()
const {
return (mData.mSize == 0); }
337 const SizeType& size()
const {
return mData.mSize; }
343 ConstValueIter cbegin()
const;
347 template<
typename OtherDataType>
348 bool eq(
const RowBase<OtherDataType>& other,
354 template<
typename VecValueType>
355 VecValueType dot(
const VecValueType* inVec,
SizeType vecSize)
const;
358 template<
typename VecValueType>
362 std::string str()
const;
365 friend class ConstValueIter;
379 using ConstRowBase = RowBase<ConstRowData>;
388 if (mData.mSize == 0)
return SparseStencilMatrix::sZeroValue;
389 return mData.mVals[mCursor];
396 operator bool()
const {
return (mCursor < mData.mSize); }
402 ConstValueIter(
const RowData& d): mData(d.mVals, d.mCols, d.mSize), mCursor(0) {}
405 const ConstRowData mData;
433 template<
typename Scalar>
void scale(
const Scalar&);
434 template<
typename Scalar>
445 template<
typename VecValueType>
struct VecMultOp;
446 template<
typename Scalar>
struct RowScaleOp;
450 template<
typename OtherValueType>
struct EqOp;
453 std::unique_ptr<ValueType[]> mValueArray;
454 std::unique_ptr<SizeType[]> mColumnIdxArray;
455 std::unique_ptr<SizeType[]> mRowSizeArray;
492 CopyOp(
const T* from_, T* to_): from(from_), to(to_) {}
495 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) to[n] = from[n];
507 FillOp(T* data_,
const T& val_): data(data_), val(val_) {}
510 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) data[n] = val;
522 LinearOp(
const T& a_,
const T* x_,
const T* y_, T* out_): a(a_), x(x_), y(y_), out(out_) {}
526 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) out[n] = x[n] + y[n];
528 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) out[n] = -x[n] + y[n];
530 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) out[n] = a * x[n] + y[n];
547 os << (state.
success ?
"succeeded with " :
"")
572 if (mSize != other.mSize) {
575 mData =
new T[mSize];
607 template<
typename Scalar>
610 ScaleOp(T* data_,
const Scalar& s_):
data(data_), s(s_) {}
612 void operator()(
const SizeRange& range)
const {
613 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n)
data[n] *= s;
622 template<
typename Scalar>
626 tbb::parallel_for(
SizeRange(0, mSize), ScaleOp<Scalar>(mData, s));
635 a(a_), b(b_), binCount(binCount_), arraySize(arraySize_), reducetmp(reducetmp_) {}
639 const SizeType binSize = arraySize / binCount;
642 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) {
644 const SizeType end = (n == binCount-1) ? arraySize : begin + binSize;
647 T sum = zeroVal<T>();
648 for (
SizeType i = begin; i < end; ++i) { sum += a[i] * b[i]; }
666 assert(this->
size() == other.
size());
668 const T* aData = this->
data();
669 const T* bData = other.
data();
673 T result = zeroVal<T>();
675 if (arraySize < 1024) {
679 for (
SizeType n = 0; n < arraySize; ++n) {
680 result += aData[n] * bData[n];
692 tbb::parallel_for(
SizeRange(0, binCount),
695 for (
SizeType n = 0; n < binCount; ++n) {
696 result += partialSums[n];
711 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) {
726 T result = tbb::parallel_reduce(
SizeRange(0, this->
size()), zeroVal<T>(),
740 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) {
741 if (!std::isfinite(
data[n]))
return false;
756 bool finite = tbb::parallel_reduce(
SizeRange(0, this->
size()),
true,
758 [](
bool finite1,
bool finite2) {
return (finite1 && finite2); });
764 template<
typename OtherValueType>
767 EqOp(
const T* a_,
const OtherValueType* b_, T e): a(a_), b(b_), eps(e) {}
769 bool operator()(
const SizeRange& range,
bool equal)
const 772 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) {
780 const OtherValueType* b;
786 template<
typename OtherValueType>
790 if (this->
size() != other.
size())
return false;
791 bool equal = tbb::parallel_reduce(
SizeRange(0, this->
size()),
true,
792 EqOp<OtherValueType>(this->
data(), other.
data(), eps),
793 [](
bool eq1,
bool eq2) {
return (eq1 && eq2); });
802 std::ostringstream ostr;
806 ostr << sep << (*this)[n];
817 template<
typename ValueType, SizeType STENCIL_SIZE>
821 , mValueArray(new
ValueType[mNumRows * STENCIL_SIZE])
822 , mColumnIdxArray(new
SizeType[mNumRows * STENCIL_SIZE])
823 , mRowSizeArray(new
SizeType[mNumRows])
826 tbb::parallel_for(
SizeRange(0, mNumRows),
831 template<
typename ValueType, SizeType STENCIL_SIZE>
835 from(&from_), to(&to_) {}
839 const ValueType* fromVal = from->mValueArray.get();
840 const SizeType* fromCol = from->mColumnIdxArray.get();
841 ValueType* toVal = to->mValueArray.get();
842 SizeType* toCol = to->mColumnIdxArray.get();
843 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) {
844 toVal[n] = fromVal[n];
845 toCol[n] = fromCol[n];
853 template<
typename ValueType, SizeType STENCIL_SIZE>
856 : mNumRows(other.mNumRows)
857 , mValueArray(new
ValueType[mNumRows * STENCIL_SIZE])
858 , mColumnIdxArray(new
SizeType[mNumRows * STENCIL_SIZE])
859 , mRowSizeArray(new
SizeType[mNumRows])
867 tbb::parallel_for(
SizeRange(0, mNumRows),
872 template<
typename ValueType, SizeType STENCIL_SIZE>
877 assert(row < mNumRows);
882 template<
typename ValueType, SizeType STENCIL_SIZE>
886 assert(row < mNumRows);
891 template<
typename ValueType, SizeType STENCIL_SIZE>
899 template<
typename ValueType, SizeType STENCIL_SIZE>
900 template<
typename Scalar>
907 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) {
918 template<
typename ValueType, SizeType STENCIL_SIZE>
919 template<
typename Scalar>
924 tbb::parallel_for(
SizeRange(0, mNumRows), RowScaleOp<Scalar>(*
this, s));
928 template<
typename ValueType, SizeType STENCIL_SIZE>
929 template<
typename VecValueType>
933 mat(&m), in(i), out(o) {}
937 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) {
939 out[n] = row.dot(in, mat->numRows());
944 const VecValueType* in;
949 template<
typename ValueType, SizeType STENCIL_SIZE>
950 template<
typename VecValueType>
955 if (inVec.
size() != mNumRows) {
957 << mNumRows <<
"x" << mNumRows <<
" vs. " << inVec.
size() <<
")");
959 if (resultVec.
size() != mNumRows) {
961 << mNumRows <<
"x" << mNumRows <<
" vs. " << resultVec.
size() <<
")");
968 template<
typename ValueType, SizeType STENCIL_SIZE>
969 template<
typename VecValueType>
972 const VecValueType* inVec, VecValueType* resultVec)
const 975 tbb::parallel_for(
SizeRange(0, mNumRows),
976 VecMultOp<VecValueType>(*
this, inVec, resultVec));
980 template<
typename ValueType, SizeType STENCIL_SIZE>
981 template<
typename OtherValueType>
986 a(&a_), b(&b_), eps(e) {}
991 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) {
992 if (!a->getConstRow(n).eq(b->getConstRow(n), eps))
return false;
1000 const ValueType eps;
1004 template<
typename ValueType, SizeType STENCIL_SIZE>
1005 template<
typename OtherValueType>
1012 EqOp<OtherValueType>(*
this, other, eps),
1013 [](
bool eq1,
bool eq2) {
return (eq1 && eq2); });
1018 template<
typename ValueType, SizeType STENCIL_SIZE>
1026 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) {
1027 const ConstRow row = mat->getConstRow(n);
1029 if (!std::isfinite(*it))
return false;
1040 template<
typename ValueType, SizeType STENCIL_SIZE>
1046 IsFiniteOp(*
this), [](
bool finite1,
bool finite2) {
return (finite1&&finite2); });
1051 template<
typename ValueType, SizeType STENCIL_SIZE>
1055 std::ostringstream ostr;
1057 ostr << n <<
": " << this->
getConstRow(n).str() <<
"\n";
1063 template<
typename ValueType, SizeType STENCIL_SIZE>
1067 assert(i < mNumRows);
1068 const SizeType head = i * STENCIL_SIZE;
1069 return RowEditor(&mValueArray[head], &mColumnIdxArray[head], mRowSizeArray[i], mNumRows);
1073 template<
typename ValueType, SizeType STENCIL_SIZE>
1077 assert(i < mNumRows);
1078 const SizeType head = i * STENCIL_SIZE;
1079 return ConstRow(&mValueArray[head], &mColumnIdxArray[head], mRowSizeArray[i]);
1083 template<
typename ValueType, SizeType STENCIL_SIZE>
1084 template<
typename DataType>
1088 if (this->empty())
return mData.mSize;
1092 const SizeType* colPtr = std::lower_bound(mData.mCols, mData.mCols + mData.mSize, columnIdx);
1094 return static_cast<SizeType>(colPtr - mData.mCols);
1098 template<
typename ValueType, SizeType STENCIL_SIZE>
1099 template<
typename DataType>
1100 inline const ValueType&
1102 SizeType columnIdx,
bool& active)
const 1105 SizeType idx = this->find(columnIdx);
1106 if (idx < this->
size() && this->column(idx) == columnIdx) {
1108 return this->value(idx);
1113 template<
typename ValueType, SizeType STENCIL_SIZE>
1114 template<
typename DataType>
1115 inline const ValueType&
1118 SizeType idx = this->find(columnIdx);
1119 if (idx < this->
size() && this->column(idx) == columnIdx) {
1120 return this->value(idx);
1126 template<
typename ValueType, SizeType STENCIL_SIZE>
1127 template<
typename DataType>
1135 template<
typename ValueType, SizeType STENCIL_SIZE>
1136 template<
typename DataType>
1137 template<
typename OtherDataType>
1140 const RowBase<OtherDataType>& other, ValueType eps)
const 1142 if (this->
size() != other.size())
return false;
1143 for (
ConstValueIter it = cbegin(), oit = other.cbegin(); it || oit; ++it, ++oit) {
1144 if (it.column() != oit.column())
return false;
1151 template<
typename ValueType, SizeType STENCIL_SIZE>
1152 template<
typename DataType>
1153 template<
typename VecValueType>
1156 const VecValueType* inVec,
SizeType vecSize)
const 1158 VecValueType result = zeroVal<VecValueType>();
1160 result +=
static_cast<VecValueType
>(this->value(idx) * inVec[this->column(idx)]);
1165 template<
typename ValueType, SizeType STENCIL_SIZE>
1166 template<
typename DataType>
1167 template<
typename VecValueType>
1172 return dot(inVec.
data(), inVec.
size());
1176 template<
typename ValueType, SizeType STENCIL_SIZE>
1177 template<
typename DataType>
1181 std::ostringstream ostr;
1184 ostr << sep <<
"(" << this->column(n) <<
", " << this->value(n) <<
")";
1191 template<
typename ValueType, SizeType STENCIL_SIZE>
1194 const ValueType* valueHead,
const SizeType* columnHead,
const SizeType& rowSize):
1195 ConstRowBase(ConstRowData(const_cast<ValueType*>(valueHead),
1201 template<
typename ValueType, SizeType STENCIL_SIZE>
1205 RowBase<>(RowData(valueHead, columnHead, rowSize)), mNumColumns(colSize)
1210 template<
typename ValueType, SizeType STENCIL_SIZE>
1215 RowBase<>::mData.mSize = 0;
1219 template<
typename ValueType, SizeType STENCIL_SIZE>
1222 SizeType column,
const ValueType& value)
1224 assert(column < mNumColumns);
1226 RowData& data = RowBase<>::mData;
1230 SizeType offset = this->find(column);
1232 if (offset < data.mSize && data.mCols[offset] == column) {
1234 data.mVals[offset] = value;
1239 assert(data.mSize < this->capacity());
1241 if (offset >= data.mSize) {
1243 data.mVals[data.mSize] = value;
1244 data.mCols[data.mSize] = column;
1247 for (
SizeType i = data.mSize; i > offset; --i) {
1248 data.mVals[i] = data.mVals[i - 1];
1249 data.mCols[i] = data.mCols[i - 1];
1251 data.mVals[offset] = value;
1252 data.mCols[offset] = column;
1260 template<
typename ValueType, SizeType STENCIL_SIZE>
1261 template<
typename Scalar>
1265 for (
int idx = 0, N = this->size(); idx < N; ++idx) {
1266 RowBase<>::mData.mVals[idx] *= s;
1275 template<
typename MatrixType>
1283 using ValueType =
typename MatrixType::ValueType;
1291 tbb::parallel_for(
SizeRange(0, A.numRows()), InitOp(A, mDiag.data()));
1298 const SizeType size = mDiag.size();
1301 assert(r.
size() == size);
1303 tbb::parallel_for(
SizeRange(0, size), ApplyOp(mDiag.data(), r.
data(), z.
data()));
1313 InitOp(
const MatrixType& m, ValueType* v): mat(&m), vec(v) {}
1315 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) {
1316 const ValueType val = mat->
getValue(n, n);
1318 vec[n] =
static_cast<ValueType
>(1.0 / val);
1321 const MatrixType* mat; ValueType* vec;
1327 ApplyOp(
const ValueType* x_,
const ValueType* y_, ValueType* out_):
1328 x(x_), y(y_), out(out_) {}
1330 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) out[n] = x[n] * y[n];
1332 const ValueType *x, *y; ValueType* out;
1341 template<
typename MatrixType>
1345 struct CopyToLowerOp;
1349 using ValueType =
typename MatrixType::ValueType;
1359 , mLowerTriangular(matrix.
numRows())
1360 , mUpperTriangular(matrix.
numRows())
1367 tbb::parallel_for(
SizeRange(0, numRows), CopyToLowerOp(matrix, mLowerTriangular));
1387 mPassedCompatibilityCondition =
true;
1392 ValueType diagonalValue = crow_k.
getValue(k);
1395 if (diagonalValue < 1.e-5) {
1396 mPassedCompatibilityCondition =
false;
1400 diagonalValue =
Sqrt(diagonalValue);
1403 row_k.setValue(k, diagonalValue);
1406 typename MatrixType::ConstRow srcRow = matrix.getConstRow(k);
1407 typename MatrixType::ConstValueIter citer = srcRow.cbegin();
1408 for ( ; citer; ++citer) {
1410 if (ii < k+1)
continue;
1414 row_ii.setValue(k, *citer / diagonalValue);
1419 for ( ; citer; ++citer) {
1421 if (j < k+1)
continue;
1424 ValueType a_jk = row_j.
getValue(k);
1428 typename MatrixType::ConstRow mask = matrix.getConstRow(j);
1429 typename MatrixType::ConstValueIter maskIter = mask.cbegin();
1430 for ( ; maskIter; ++maskIter) {
1432 if (i < j)
continue;
1435 ValueType a_ij = crow_i.
getValue(j);
1436 ValueType a_ik = crow_i.
getValue(k);
1438 a_ij -= a_ik * a_jk;
1440 row_i.setValue(j, a_ij);
1446 tbb::parallel_for(
SizeRange(0, numRows),
1447 TransposeOp(matrix, mLowerTriangular, mUpperTriangular));
1452 bool isValid()
const override {
return mPassedCompatibilityCondition; }
1456 if (!mPassedCompatibilityCondition) {
1462 SizeType size = mLowerTriangular.numRows();
1464 zVec.
fill(zeroVal<ValueType>());
1465 ValueType* zData = zVec.
data();
1467 if (size == 0)
return;
1469 assert(rVec.
size() == size);
1470 assert(zVec.
size() == size);
1473 mTempVec.fill(zeroVal<ValueType>());
1474 ValueType* tmpData = mTempVec.data();
1475 const ValueType* rData = rVec.
data();
1478 for (
SizeType i = 0; i < size; ++i) {
1479 typename TriangularMatrix::ConstRow row = mLowerTriangular.getConstRow(i);
1480 ValueType diagonal = row.
getValue(i);
1481 ValueType dot = row.dot(mTempVec);
1482 tmpData[i] = (rData[i] - dot) / diagonal;
1483 if (!std::isfinite(tmpData[i])) {
1490 for (
SizeType ii = 0; ii < size; ++ii) {
1492 typename TriangularMatrix::ConstRow row = mUpperTriangular.getConstRow(i);
1493 ValueType diagonal = row.
getValue(i);
1494 ValueType dot = row.dot(zVec);
1495 zData[i] = (tmpData[i] - dot) / diagonal;
1496 if (!std::isfinite(zData[i])) {
1507 struct CopyToLowerOp
1509 CopyToLowerOp(
const MatrixType& m,
TriangularMatrix& l): mat(&m), lower(&l) {}
1511 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) {
1512 typename TriangularMatrix::RowEditor outRow = lower->getRowEditor(n);
1514 typename MatrixType::ConstRow inRow = mat->getConstRow(n);
1515 for (
typename MatrixType::ConstValueIter it = inRow.cbegin(); it; ++it) {
1516 if (it.column() > n)
continue;
1517 outRow.setValue(it.column(), *it);
1528 mat(&m), lower(&l), upper(&u) {}
1530 for (
SizeType n = range.begin(), N = range.end(); n < N; ++n) {
1531 typename TriangularMatrix::RowEditor outRow = upper->getRowEditor(n);
1534 typename MatrixType::ConstRow inRow = mat->getConstRow(n);
1535 for (
typename MatrixType::ConstValueIter it = inRow.cbegin(); it; ++it) {
1536 const SizeType column = it.column();
1537 if (column < n)
continue;
1538 outRow.setValue(column, lower->getValue(column, n));
1548 bool mPassedCompatibilityCondition;
1555 namespace internal {
1558 template<
typename T>
1560 axpy(
const T& a,
const T* xVec,
const T* yVec, T* resultVec,
SizeType size)
1566 template<
typename T>
1570 assert(xVec.
size() == yVec.
size());
1571 assert(xVec.
size() == result.
size());
1577 template<
typename MatrixOperator,
typename VecValueType>
1580 const VecValueType* b, VecValueType* r)
1583 A.vectorMultiply(x, r);
1589 template<
typename MatrixOperator,
typename T>
1595 assert(x.
size() == A.numRows());
1606 template<
typename PositiveDefMatrix>
1609 const PositiveDefMatrix& Amat,
1613 const State& termination)
1616 return solve(Amat, bVec, xVec, precond, interrupter, termination);
1620 template<
typename PositiveDefMatrix,
typename Interrupter>
1623 const PositiveDefMatrix& Amat,
1627 Interrupter& interrupter,
1628 const State& termination)
1630 using ValueType =
typename PositiveDefMatrix::ValueType;
1639 const SizeType size = Amat.numRows();
1644 if (size != bVec.
size()) {
1646 << size <<
"x" << size <<
" vs. " << bVec.
size() <<
")");
1648 if (size != xVec.
size()) {
1650 << size <<
"x" << size <<
" vs. " << xVec.
size() <<
")");
1659 const ValueType tmp = bVec.
infNorm();
1667 assert(rVec.isFinite());
1679 ValueType rDotZPrev(1);
1686 for ( ; iteration < termination.
iterations; ++iteration) {
1688 if (interrupter.wasInterrupted()) {
1698 precond.
apply(rVec, zVec);
1701 const ValueType rDotZ = rVec.dot(zVec);
1702 assert(std::isfinite(rDotZ));
1704 if (0 == iteration) {
1708 const ValueType beta = rDotZ / rDotZPrev;
1714 Amat.vectorMultiply(pVec, qVec);
1717 const ValueType pAp = pVec.dot(qVec);
1718 assert(std::isfinite(pAp));
1720 const ValueType alpha = rDotZ / pAp;
1730 l2Error = rVec.l2Norm();
1731 minL2Error =
Min(l2Error, minL2Error);
1736 if (l2Error > 2 * minL2Error) {
1767 #endif // OPENVDB_MATH_CONJGRADIENT_HAS_BEEN_INCLUDED bool isApproxZero(const Type &x)
Return true if x is equal to zero to within the default floating-point comparison tolerance...
Definition: Math.h:349
void increment()
Definition: ConjGradient.h:394
bool operator()(const SizeRange &range, bool finite) const
Definition: ConjGradient.h:1023
ValueType l2Norm() const
Return the L2 norm of this vector.
Definition: ConjGradient.h:185
typename TriangularMatrix::RowEditor TriangleRowEditor
Definition: ConjGradient.h:1355
Definition: ConjGradient.h:1019
T * reducetmp
Definition: ConjGradient.h:659
int iterations
Definition: ConjGradient.h:48
bool isFinite() const
Return true if every element of this matrix has a finite value.
Definition: ConjGradient.h:1042
Vector(SizeType n)
Construct a vector of n elements, with uninitialized values.
Definition: ConjGradient.h:147
const SparseStencilMatrix * mat
Definition: ConjGradient.h:1036
SizeType column() const
Definition: ConjGradient.h:392
SharedPtr< Vector > Ptr
Definition: ConjGradient.h:142
static constexpr ValueType sZeroValue
Definition: ConjGradient.h:248
virtual bool isValid() const
Definition: ConjGradient.h:473
RowEditor getRowEditor(SizeType row)
Return a read/write view onto the given row of this matrix.
Definition: ConjGradient.h:1065
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
RowEditor(ValueType *valueHead, SizeType *columnHead, SizeType &rowSize, SizeType colSize)
Definition: ConjGradient.h:1203
float Sqrt(float x)
Return the square root of a floating-point value.
Definition: Math.h:761
bool isFinite(const float x)
Return true if x is finite.
Definition: Math.h:375
void setValue(SizeType row, SizeType col, const ValueType &)
Set the value at the given coordinates.
Definition: ConjGradient.h:874
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
void resize(SizeType n)
Reset this vector to have n elements, with uninitialized values.
Definition: ConjGradient.h:588
Diagonal preconditioner.
Definition: ConjGradient.h:42
bool eq(const Vector< OtherValueType > &other, ValueType eps=Tolerance< ValueType >::value()) const
Return true if this vector is equivalent to the given vector to within the specified tolerance...
Definition: ConjGradient.h:788
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:443
#define OPENVDB_LOG_DEBUG_RUNTIME(message)
Log a debugging message in both debug and optimized builds.
Definition: logging.h:270
SparseStencilMatrix & operator*=(const Scalar &s)
Multiply all elements in the matrix by s;.
Definition: ConjGradient.h:285
Read-only accessor to a row of this matrix.
Definition: ConjGradient.h:411
Preconditioner(const SparseStencilMatrix< T, STENCIL_SIZE > &)
Definition: ConjGradient.h:470
DeterministicDotProductOp(const T *a_, const T *b_, const SizeType binCount_, const SizeType arraySize_, T *reducetmp_)
Definition: ConjGradient.h:633
Definition: Exceptions.h:56
void computeResidual(const MatrixOperator &A, const VecValueType *x, const VecValueType *b, VecValueType *r)
Compute r = b − Ax.
Definition: ConjGradient.h:1579
void vectorMultiply(const Vector< VecValueType > &inVec, Vector< VecValueType > &resultVec) const
Multiply this matrix by inVec and return the result in resultVec.
Definition: ConjGradient.h:952
MatType scale(const Vec3< typename MatType::value_type > &s)
Return a matrix that scales by s.
Definition: Mat.h:615
std::shared_ptr< T > SharedPtr
Definition: Types.h:114
Definition: ConjGradient.h:705
const T * from
Definition: ConjGradient.h:498
void computeResidual(const MatrixOperator &A, const Vector< T > &x, const Vector< T > &b, Vector< T > &r)
Compute r = b − Ax.
Definition: ConjGradient.h:1591
Read/write accessor to a row of this matrix.
Definition: ConjGradient.h:419
const ValueType & getValue(SizeType row, SizeType col) const
Return the value at the given coordinates.
Definition: ConjGradient.h:884
const T * y
Definition: ConjGradient.h:534
Preconditioner using incomplete Cholesky factorization.
Definition: ConjGradient.h:43
Base class for interrupters.
Definition: NullInterrupter.h:25
ValueType ValueType
Definition: ConjGradient.h:141
bool isFinite() const
Return true if all values along the diagonal are finite.
Definition: ConjGradient.h:1307
MatrixCopyOp(const SparseStencilMatrix &from_, SparseStencilMatrix &to_)
Definition: ConjGradient.h:834
SizeType size() const
Return the number of elements in this vector.
Definition: ConjGradient.h:159
ConstValueIter & operator++()
Definition: ConjGradient.h:395
const T val
Definition: ConjGradient.h:514
JacobiPreconditioner(const MatrixType &A)
Definition: ConjGradient.h:1288
bool operator()(const SizeRange &range, bool finite) const
Definition: ConjGradient.h:737
std::ostream & operator<<(std::ostream &os, const State &state)
Definition: ConjGradient.h:545
SizeType numRows() const
Return the number of rows in this matrix.
Definition: ConjGradient.h:258
const SizeType arraySize
Definition: ConjGradient.h:658
void operator()(const SizeRange &range) const
Definition: ConjGradient.h:524
void scale(const Scalar &s)
Multiply all elements in the matrix by s;.
Definition: ConjGradient.h:921
Information about the state of a conjugate gradient solution.
Definition: ConjGradient.h:46
T operator()(const SizeRange &range, T maxValue) const
Definition: ConjGradient.h:709
bool empty() const
Return true if this vector has no elements.
Definition: ConjGradient.h:161
Base class for conjugate gradient preconditioners.
Definition: ConjGradient.h:41
ValueType infNorm() const
Return the infinity norm of this vector.
Definition: ConjGradient.h:723
const T * data() const
Return a pointer to this vector's elements.
Definition: ConjGradient.h:210
Vector()
Construct an empty vector.
Definition: ConjGradient.h:145
Coord Abs(const Coord &xyz)
Definition: Coord.h:517
const T * a
Definition: ConjGradient.h:655
std::string str() const
Return a string representation of this matrix.
Definition: ConjGradient.h:1053
std::string str() const
Return a string representation of this vector.
Definition: ConjGradient.h:800
const ValueType & operator()(SizeType row, SizeType col) const
Return the value at the given coordinates.
Definition: ConjGradient.h:893
Vector & operator*=(const Scalar &s)
Multiply each element of this vector by s.
Definition: ConjGradient.h:176
IncompleteCholeskyPreconditioner(const MatrixType &matrix)
Definition: ConjGradient.h:1357
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:337
Vector< ValueType > VectorType
Definition: ConjGradient.h:241
SharedPtr< SparseStencilMatrix > Ptr
Definition: ConjGradient.h:242
SharedPtr< Preconditioner > Ptr
Definition: ConjGradient.h:468
const TriangularMatrix & upperMatrix() const
Definition: ConjGradient.h:1503
const Type & Min(const Type &a, const Type &b)
Return the minimum of two values.
Definition: Math.h:656
void axpy(const T &a, const T *xVec, const T *yVec, T *resultVec, SizeType size)
Compute ax + y.
Definition: ConjGradient.h:1560
void swap(Vector &other)
Swap internal storage with another vector, which need not be the same size.
Definition: ConjGradient.h:168
ValueType dot(const Vector &) const
Return the dot product of this vector with the given vector, which must be the same size...
Definition: ConjGradient.h:664
void operator()(const SizeRange &range) const
Definition: ConjGradient.h:637
double absoluteError
Definition: ConjGradient.h:50
LinearOp(const T &a_, const T *x_, const T *y_, T *out_)
Definition: ConjGradient.h:522
bool eq(const SparseStencilMatrix< OtherValueType, STENCIL_SIZE > &other, ValueType eps=Tolerance< ValueType >::value()) const
Return true if this matrix is equivalent to the given matrix to within the specified tolerance...
Definition: ConjGradient.h:1007
SparseStencilMatrix * to
Definition: ConjGradient.h:849
Definition: ConjGradient.h:733
typename TriangularMatrix::ConstRow TriangleConstRow
Definition: ConjGradient.h:1354
void reset()
Definition: ConjGradient.h:398
~Vector()
Definition: ConjGradient.h:151
SharedPtr< IncompleteCholeskyPreconditioner > Ptr
Definition: ConjGradient.h:1352
FillOp(T *data_, const T &val_)
Definition: ConjGradient.h:507
T * out
Definition: ConjGradient.h:535
bool isApproxEqual(const Type &a, const Type &b, const Type &tolerance)
Return true if a is equal to b to within the given tolerance.
Definition: Math.h:406
Iterator over the stored values in a row of this matrix.
Definition: ConjGradient.h:383
const T * data
Definition: ConjGradient.h:747
Definition: Exceptions.h:13
InfNormOp(const T *data_)
Definition: ConjGradient.h:707
Vector & operator=(const Vector &)
Deep copy the given vector.
Definition: ConjGradient.h:568
CopyOp(const T *from_, T *to_)
Definition: ConjGradient.h:492
ConstRow(const ValueType *valueHead, const SizeType *columnHead, const SizeType &rowSize)
Definition: ConjGradient.h:1193
Definition: ConjGradient.h:520
Sparse, square matrix representing a 3D stencil operator of size STENCIL_SIZE.
Definition: ConjGradient.h:39
const Type & Max(const Type &a, const Type &b)
Return the maximum of two values.
Definition: Math.h:595
const T & at(SizeType i) const
Return the value of this vector's ith element.
Definition: ConjGradient.h:202
void operator()(const SizeRange &range) const
Definition: ConjGradient.h:509
bool success
Definition: ConjGradient.h:47
Definition: ConjGradient.h:631
bool isFinite() const
Return true if every element of this vector has a finite value.
Definition: ConjGradient.h:753
IsFiniteOp(const SparseStencilMatrix &m)
Definition: ConjGradient.h:1021
void apply(const Vector< ValueType > &rVec, Vector< ValueType > &zVec) override
Apply this preconditioner to a residue vector: z = M−1r
Definition: ConjGradient.h:1454
SharedPtr< JacobiPreconditioner > Ptr
Definition: ConjGradient.h:1286
void axpy(const T &a, const Vector< T > &xVec, const Vector< T > &yVec, Vector< T > &result)
Compute ax + y.
Definition: ConjGradient.h:1568
const T * data
Definition: ConjGradient.h:717
void operator()(const SizeRange &range) const
Definition: ConjGradient.h:837
ConstRow getConstRow(SizeType row) const
Return a read-only view onto the given row of this matrix.
Definition: ConjGradient.h:1075
State solve(const PositiveDefMatrix &A, const Vector< typename PositiveDefMatrix::ValueType > &b, Vector< typename PositiveDefMatrix::ValueType > &x, Preconditioner< typename PositiveDefMatrix::ValueType > &preconditioner, const State &termination=terminationDefaults< typename PositiveDefMatrix::ValueType >())
Solve Ax = b via the preconditioned conjugate gradient method.
Definition: ConjGradient.h:1608
void clear()
Set the number of entries in this row to zero.
Definition: ConjGradient.h:1212
void fill(const ValueType &value)
Set all elements of this vector to value.
Definition: ConjGradient.h:600
T * data()
Return a pointer to this vector's elements.
Definition: ConjGradient.h:209
Definition: ConjGradient.h:832
SizeType size() const
Return the number of rows in this matrix.
Definition: ConjGradient.h:259
#define OPENVDB_LOG_WARN(message)
Log a warning message of the form 'someVar << "some text" << ...'.
Definition: logging.h:256
const TriangularMatrix & lowerMatrix() const
Definition: ConjGradient.h:1502
double relativeError
Definition: ConjGradient.h:49
IsFiniteOp(const T *data_)
Definition: ConjGradient.h:735
const T * constData() const
Return a pointer to this vector's elements.
Definition: ConjGradient.h:211
MatrixType::ValueType ValueType
Definition: ConjGradient.h:467
State terminationDefaults()
Return default termination conditions for a conjugate gradient solver.
Definition: ConjGradient.h:57
Definition: ConjGradient.h:490
const T & operator[](SizeType i) const
Return the value of this vector's ith element.
Definition: ConjGradient.h:204
void scale(const Scalar &)
Scale all of the entries in this row.
Definition: ConjGradient.h:1263
T & at(SizeType i)
Return the value of this vector's ith element.
Definition: ConjGradient.h:201
tbb::blocked_range< SizeType > SizeRange
Definition: ConjGradient.h:35
Lightweight, variable-length vector.
Definition: ConjGradient.h:37
OtherValueType ValueType
Definition: ConjGradient.h:240
bool isValid() const override
Definition: ConjGradient.h:1452
Tolerance for floating-point comparison.
Definition: Math.h:148
const SizeType binCount
Definition: ConjGradient.h:657
void operator()(const SizeRange &range) const
Definition: ConjGradient.h:494
void scale(const Scalar &s)
Multiply each element of this vector by s.
Definition: ConjGradient.h:624
Index32 SizeType
Definition: ConjGradient.h:33
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
T & operator[](SizeType i)
Return the value of this vector's ith element.
Definition: ConjGradient.h:203
Definition: ConjGradient.h:505
const T * b
Definition: ConjGradient.h:656
T * data
Definition: ConjGradient.h:513
State solve(const PositiveDefMatrix &A, const Vector< typename PositiveDefMatrix::ValueType > &b, Vector< typename PositiveDefMatrix::ValueType > &x, Preconditioner< typename PositiveDefMatrix::ValueType > &preconditioner, Interrupter &interrupter, const State &termination=terminationDefaults< typename PositiveDefMatrix::ValueType >())
Solve Ax = b via the preconditioned conjugate gradient method.
Definition: ConjGradient.h:1622
Vector(SizeType n, const ValueType &val)
Construct a vector of n elements and initialize each element to the given value.
Definition: ConjGradient.h:149
SizeType setValue(SizeType column, const ValueType &value)
Set the value of the entry in the specified column.
Definition: ConjGradient.h:1221
const ValueType & operator*() const
Definition: ConjGradient.h:386
RowEditor & operator*=(const Scalar &s)
Scale all of the entries in this row.
Definition: ConjGradient.h:435
T * to
Definition: ConjGradient.h:499
Definition: Exceptions.h:63
virtual void apply(const Vector< T > &r, Vector< T > &z)=0
Apply this preconditioner to a residue vector: z = M−1r
uint32_t Index32
Definition: Types.h:52
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:212
void apply(const Vector< ValueType > &r, Vector< ValueType > &z) override
Apply this preconditioner to a residue vector: z = M−1r
Definition: ConjGradient.h:1296
SparseStencilMatrix(SizeType n)
Construct an n x n matrix with at most STENCIL_SIZE nonzero elements per row.
Definition: ConjGradient.h:819