/usr/include/openturns/HMatrixImplementation.hxx is in libopenturns-dev 1.9-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | // -*- C++ -*-
/**
* @file HMatrixImplementation.hxx
* @brief This file supplies support for HMat
*
* Copyright 2005-2017 Airbus-EDF-IMACS-Phimeca
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef OPENTURNS_HMATRIXIMPLEMENTATION_HXX
#define OPENTURNS_HMATRIXIMPLEMENTATION_HXX
#include "openturns/OTconfig.hxx"
#include "openturns/OTprivate.hxx"
#include "openturns/Point.hxx"
#include "openturns/Sample.hxx"
#include "openturns/CovarianceMatrix.hxx"
#include "openturns/CovarianceModel.hxx"
#ifdef OPENTURNS_HAVE_HMAT
# include <hmat/hmat.h>
#endif
BEGIN_NAMESPACE_OPENTURNS
// Forward declaration
class HMatrixFactory;
// In order to create an H-matrix, one has first to generate an interface with
// HMatrixFactory::build, then compute its coefficients, and after that, it is
// possible to factorize it, solve linear systems, etc. As H-matrices are
// designed to solve very large systems, most operations are performed inplace,
// which is an unusual pattern in OpenTURNS.
// There are two ways to compute matrix coefficients:
// 1. The simplest solution is to provide a method which computes (i,j) coefficient.
// This is what HMatrixRealAssemblyFunction is designed for, and such an object
// is then passed to HMatrixImplementation::simpleAssemble. But most of the time,
// this involves heavy computations which could be reused when computing values
// for different coefficients.
// 2. HMat provides a 2nd interface to compute tiled blocks. But it is cumbersome,
// so we defined a more intuitive interface which should be suited for our needs.
// When dimension is greater than 1, HMatrixFactory::build duplicates input vertices
// to have local d x d matrices, and HMatrixTensorRealAssemblyFunction is an interface
// to compute all coefficients of this local matrix, which had already been allocated.
// It will be automatically copied at the right place in HMatrixImplementation.
class OT_API HMatrixRealAssemblyFunction
{
public:
virtual ~HMatrixRealAssemblyFunction() {}
// Compute matrix coefficient for degrees of freedom i and j
virtual Scalar operator() (UnsignedInteger i, UnsignedInteger j) const = 0;
};
class OT_API HMatrixTensorRealAssemblyFunction
{
public:
virtual ~HMatrixTensorRealAssemblyFunction() {}
HMatrixTensorRealAssemblyFunction(const UnsignedInteger outputDimension)
: dimension_(outputDimension) {}
UnsignedInteger getDimension() const
{
return dimension_;
}
// Compute local matrix for input vertices i and j
virtual void compute(UnsignedInteger i, UnsignedInteger j, Matrix* localValues) const = 0;
protected:
UnsignedInteger dimension_;
};
class OT_API HMatrixClusterTree
{
public:
HMatrixClusterTree(void* ptr_cluster_tree, int size) : hmatClusterTree_(ptr_cluster_tree), size_(size) {}
void* get()
{
return hmatClusterTree_;
}
int getSize()
{
return size_;
}
~HMatrixClusterTree();
private:
void* hmatClusterTree_;
int size_;
};
// HMatrixImplementation is not persistent
class OT_API HMatrixImplementation
: public PersistentObject
{
friend class HMatrixFactory;
CLASSNAME;
private:
// Only visible from HMatrixFactory::build
HMatrixImplementation(void* ptr_hmat_interface, void* ptr_cluster_tree, int cluster_size, void* ptr_hmatrix);
public:
/** Default constructor */
HMatrixImplementation();
/** Copy constructor */
HMatrixImplementation(const HMatrixImplementation& other);
/** Virtual copy constructor */
virtual HMatrixImplementation * clone() const;
// Destructor
virtual ~HMatrixImplementation();
/** Get the dimensions of the matrix */
/** Number of rows */
UnsignedInteger getNbRows() const;
/** Number of columns */
UnsignedInteger getNbColumns() const;
void assemble(const HMatrixRealAssemblyFunction& f, char symmetry);
void assemble(const HMatrixTensorRealAssemblyFunction& f, char symmetry);
void factorize(const String& method);
/** Compute this <- alpha * this */
void scale(Scalar alpha);
/** Compute y <- alpha op(this) * x + beta * y */
void gemv(char trans, Scalar alpha, const Point& x, Scalar beta, Point& y) const;
/** Compute this <- alpha op(A) * p(B) + beta * this */
void gemm(char transA, char transB, Scalar alpha, const HMatrixImplementation& a, const HMatrixImplementation& b, Scalar beta);
/** Transpose matrix */
void transpose();
/** Get the Frobenius norm */
Scalar norm() const;
/** Get the diagonal */
Point getDiagonal() const;
/** Solve system op(A)*X = b */
Point solve(const Point& b, Bool trans) const;
/** Solve system op(A)*X = m */
Matrix solve(const Matrix& m, Bool trans) const;
/** Solve system op(L)*X = b */
Point solveLower(const Point& b, Bool trans) const;
/** Solve system op(L)*X = m */
Matrix solveLower(const Matrix& m, Bool trans) const;
/** Get number of HMatrix elements in compressed and uncompressed forms */
std::pair<size_t, size_t> compressionRatio() const;
/** Get number of HMatrix elements in full blocks and low rank blocks */
std::pair<size_t, size_t> fullrkRatio() const;
/** Dump HMatrix onto file */
void dump(const String & name) const;
/** Change HMatrix settings */
Bool setKey(const String & name, const String & value);
/** String converter */
virtual String __repr__() const;
/** String converter */
virtual String __str__(const String & offset = "") const;
private:
// DO NOT USE
void * hmatInterface_;
Pointer<HMatrixClusterTree> hmatClusterTree_;
void * hmat_;
};
// First implementation, by using HMatrixRealAssemblyFunction
// This is going to be very slow, because each local
// covariance matrix of size 3x3 is computed, but a single
// coefficient is stored.
class CovarianceAssemblyFunction : public HMatrixRealAssemblyFunction
{
private:
const CovarianceModel covarianceModel_;
const Sample vertices_;
const UnsignedInteger covarianceDimension_;
const double epsilon_;
public:
CovarianceAssemblyFunction(const CovarianceModel & covarianceModel, const Sample & vertices, double epsilon)
: HMatrixRealAssemblyFunction()
, covarianceModel_(covarianceModel)
, vertices_(vertices)
, covarianceDimension_(covarianceModel.getDimension())
, epsilon_(epsilon) {}
Scalar operator()(UnsignedInteger i, UnsignedInteger j) const
{
const UnsignedInteger rowIndex = i / covarianceDimension_;
const UnsignedInteger columnIndex = j / covarianceDimension_;
const CovarianceMatrix localCovarianceMatrix(covarianceModel_( vertices_[rowIndex], vertices_[columnIndex] ));
const UnsignedInteger rowIndexLocal = i % covarianceDimension_;
const UnsignedInteger columnIndexLocal = j % covarianceDimension_;
return localCovarianceMatrix(rowIndexLocal, columnIndexLocal) + (i != j ? 0.0 : epsilon_);
}
};
// Second implementation, by using HMatrixTensorRealAssemblyFunction
// Each local covariance matrix is built only once, and its components
// are dispatched into the global covariance matrix
class CovarianceBlockAssemblyFunction : public HMatrixTensorRealAssemblyFunction
{
private:
const CovarianceModel covarianceModel_;
const Sample vertices_;
const double epsilon_;
CovarianceMatrix epsilonId_;
public:
CovarianceBlockAssemblyFunction(const CovarianceModel & covarianceModel, const Sample & vertices, double epsilon)
: HMatrixTensorRealAssemblyFunction(covarianceModel.getDimension())
, covarianceModel_(covarianceModel)
, vertices_(vertices)
, epsilon_(epsilon)
{
Matrix eps = epsilon_ * IdentityMatrix(covarianceModel.getDimension());
Pointer<MatrixImplementation> impl = eps.getImplementation();
epsilonId_ = CovarianceMatrix(covarianceModel.getDimension(), *impl.get());
}
void compute(UnsignedInteger i, UnsignedInteger j, Matrix* localValues) const
{
CovarianceMatrix localResult(covarianceModel_( vertices_[i], vertices_[j] ));
if (i == j && epsilon_ != 0.0)
localResult = localResult + epsilonId_;
memcpy( &localValues->getImplementation()->operator[](0), &localResult.getImplementation()->operator[](0), dimension_ * dimension_ * sizeof(Scalar) );
}
};
END_NAMESPACE_OPENTURNS
#endif /* OPENTURNS_HMATRIXIMPLEMENTATION_HXX */
|