gmsh-TingyuanDoc
0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
|
Go to the documentation of this file.
9 #include "GmshConfig.h"
16 #if defined(HAVE_EIGEN)
17 #ifdef Success // in X11 header X.h
21 #include <Eigen/Dense>
38 #if defined(HAVE_EIGEN)
39 typedef Eigen::Map<Eigen::Matrix<scalar, Eigen::Dynamic, 1> > EigenVec;
69 inline int size()
const {
return _r; }
88 for(
int i = i0, desti = desti0; i < i0 + ni; i++, desti++)
89 (*
this)(desti) = v(i);
91 inline void set(
int r, scalar v)
94 if(r >=
_r || r < 0) {
95 Msg::Error(
"Invalid index in vector: %i (size = %i)", r,
_r);
103 bool resize(
int r,
bool resetValue =
true)
110 if(resetValue)
setAll(scalar(0.));
114 if(resetValue)
setAll(scalar(0.));
146 #if defined(HAVE_EIGEN)
151 for(
int i = 0; i <
_r; ++i)
_data[i] = scalar(0.);
155 for(
int i = 0; i <
_r; ++i)
_data[i] *= s;
160 #if defined(HAVE_EIGEN)
164 for(
int i = 0; i <
_r; i++)
set(i, m);
168 #if defined(HAVE_EIGEN)
174 #elif !defined(HAVE_BLAS)
183 #if defined(HAVE_EIGEN)
185 scalar s = vv.dot(vother);
189 for(
int i = 0; i <
_r; ++i) s +=
_data[i] * other.
_data[i];
195 #if defined(HAVE_EIGEN)
198 vv.noalias() += alpha * vx;
200 #elif !defined(HAVE_BLAS)
202 for(
int i = 0; i <
_r; i++)
_data[i] += alpha * x.
_data[i];
211 void print(
const std::string name =
"",
const std::string format =
"")
const;
215 if(fread(
_data,
sizeof(scalar),
_r,
f) != (
size_t)
_r)
return;
231 #if defined(HAVE_EIGEN)
232 typedef Eigen::Map<Eigen::Matrix<scalar, Eigen::Dynamic, Eigen::Dynamic> > EigenMat;
233 typedef Eigen::Map<Eigen::Matrix<scalar, Eigen::Dynamic, 1> > EigenVec;
255 if(init0)
setAll(scalar(0.));
276 inline scalar
get(
int r,
int c)
const
279 if(r >=
_r || r < 0 || c >=
_c ||
c < 0) {
280 Msg::Error(
"Invalid index in dense matrix: %i %i (size = %i %i)", r,
c,
285 return (*
this)(r,
c);
289 inline void set(
int r,
int c, scalar v)
292 if(r >=
_r || r < 0 || c >=
_c ||
c < 0) {
293 Msg::Error(
"Invalid index in dense matrix: %i %i (size = %i %i)", r,
c,
303 for(
int i = 0; i <
_r; ++i)
304 for(
int j = 0; j <
_c; ++j) n += (*
this)(i, j) * (*
this)(i, j);
307 bool resize(
int r,
int c,
bool resetValue =
true)
316 if(resetValue)
setAll(scalar(0.));
321 if(resetValue)
setAll(scalar(0.));
326 if(nbRows == -1 && nbColumns != -1) nbRows =
_r *
_c / nbColumns;
327 if(nbRows != -1 && nbColumns == -1) nbColumns =
_r *
_c / nbRows;
328 if(nbRows * nbColumns !=
_r *
_c)
329 Msg::Error(
"Invalid dense matrix reshape: total number of entries must "
330 "be equal (new %i x %i != old %i x %i)",
331 nbRows, nbColumns,
_r,
_c);
366 if(
_r != other.
_r ||
_c != other.
_c) {
367 Msg::Error(
"Cannot sum dense matrices of different sizes");
375 if(i >=
_r || i < 0 || j >=
_c || j < 0) {
376 Msg::Error(
"Invalid index to access dense matrix: %i %i (size = %i %i)",
386 if(i >=
_r || i < 0 || j >=
_c || j < 0) {
387 Msg::Error(
"Invalid index to access dense matrix: %i %i (size = %i %i)",
395 int desti0,
int destj0)
397 for(
int i = i0, desti = desti0; i < i0 + ni; i++, desti++)
398 for(
int j = j0, destj = destj0; j < j0 + nj; j++, destj++)
399 (*
this)(desti, destj) = a(i, j);
404 Msg::Error(
"Dense matrix copy prohibited for proxies, use setAll "
420 for(
int i = 0; i <
_r; i++)
_data[cind + i] = x(i);
424 #if defined(HAVE_EIGEN)
428 for(
int i = 0; i <
_r *
_c; i++)
_data[i] = m;
432 #if defined(HAVE_EIGEN)
437 #elif !defined(HAVE_BLAS)
440 Msg::Error(
"Dense matrix sizes do not match in setAll");
448 #if defined(HAVE_EIGEN)
453 #elif !defined(HAVE_BLAS)
457 for(
int i = 0; i <
_r *
_c; ++i)
_data[i] = scalar(0.);
459 for(
int i = 0; i <
_r *
_c; ++i)
_data[i] *= s;
463 inline void add(
const double &a)
465 for(
int i = 0; i <
_r *
_c; ++i)
_data[i] += a;
469 #if defined(HAVE_EIGEN)
473 for(
int i = 0; i <
_r; i++)
474 for(
int j = 0; j <
_c; j++) (*
this)(i, j) += m(i, j);
479 #if defined(HAVE_EIGEN)
481 ma.noalias() += a * mm;
483 for(
int i = 0; i <
_r; i++)
484 for(
int j = 0; j <
_c; j++) (*
this)(i, j) += a * m(i, j);
488 #if defined(HAVE_EIGEN)
491 EigenVec vx(x.
_data, x.
_r), vy(y._data, y._r);
494 #elif !defined(HAVE_BLAS)
497 for(
int i = 0; i <
_r; i++)
498 for(
int j = 0; j <
_c; j++) y._data[i] += (*
this)(i, j) * x(j);
503 #if defined(HAVE_EIGEN)
506 EigenVec vx(x.
_data, x.
_r), vy(y._data, y._r);
509 #elif !defined(HAVE_BLAS)
511 for(
int i = 0; i <
_r; i++)
512 for(
int j = 0; j <
_c; j++) y._data[i] += (*
this)(i, j) * x(j);
517 #if defined(HAVE_EIGEN)
519 EigenMat ma(
_data,
_r,
_c), mb(b._data, b._r, b._c), mc(
c._data,
c._r,
c._c);
520 mc.noalias() = ma * mb;
522 #elif !defined(HAVE_BLAS)
525 for(
int i = 0; i <
_r; i++)
526 for(
int j = 0; j < b._c; j++)
527 for(
int k = 0; k <
_c; k++)
528 c._data[i +
_r * j] += (*
this)(i, k) * b(k, j);
538 #if defined(HAVE_EIGEN) || !defined(HAVE_BLAS)
541 if(beta != 1)
c.scale(beta);
542 for(
int j = fcol; j < fcol + ncol; j++)
543 for(
int k = 0; k <
_c; k++)
544 c._data[j] += alpha * (*
this)(row, k) * b(k, j);
550 #if defined(HAVE_EIGEN) || !defined(HAVE_BLAS)
553 for(
int j = 0; j <
_c; j++)
554 for(
int i = 0; i <
_r; i++) y._data[j] += alpha * (*
this)(i, j) * x(i);
561 for(
int i = 0; i <
_r; i++)
562 for(
int j = 0; j <
_c; j++) T(j, i) = (*this)(i, j);
568 Msg::Error(
"In-place transposition requires a square matrix "
569 "(size = %d %d)",
_r,
_c);
573 for(
int i = 0; i <
_r; i++)
574 for(
int j = 0; j < i; j++) {
581 scalar alpha = 1., scalar beta = 1.,
bool transposeA =
false,
582 bool transposeB =
false)
583 #if defined(HAVE_EIGEN) || !defined(HAVE_BLAS)
596 #if defined(HAVE_EIGEN) || !defined(HAVE_BLAS)
599 for(
int i = 0; i < n; i++)
_data[i] += alpha * x.
_data[i];
604 #if defined(HAVE_EIGEN)
607 Msg::Error(
"Wrong sizes for dense linear system solve (size = %d %d, "
608 "%d, %d)",
_r,
_c, result.
_r, rhs.
_r);
612 EigenVec vb(rhs.
_data, rhs.
_r);
613 EigenVec vx(result.
_data, result.
_r);
614 vx = ma.colPivHouseholderQr().solve(vb);
617 #elif !defined(HAVE_LAPACK)
619 Msg::Error(
"LU factorization and substitution requires Eigen or LAPACK");
625 #if defined(HAVE_EIGEN) || !defined(HAVE_LAPACK)
627 Msg::Error(
"LU factorization requires LAPACK");
634 #if defined(HAVE_EIGEN) || !defined(HAVE_LAPACK)
636 Msg::Error(
"LU substitution requires LAPACK");
642 #if defined(HAVE_EIGEN)
645 Msg::Error(
"Dense matrix inverse requires square matrix (size = %d %d)",
655 #elif !defined(HAVE_LAPACK)
657 Msg::Error(
"Matrix inversion requires Eigen or LAPACK");
663 #if defined(HAVE_EIGEN)
666 Msg::Error(
"Dense matrix inversion requires square matrix (size = %d %d)",
674 #elif !defined(HAVE_LAPACK)
676 Msg::Error(
"Dense matrix inversion requires LAPACK");
682 #if defined(HAVE_EIGEN)
685 return ma.determinant();
687 #elif !defined(HAVE_LAPACK)
689 Msg::Error(
"Dense matrix inversion requires Eigen or LAPACK");
694 void swap(scalar *a,
int inca, scalar *b,
int incb,
int n)
697 for(
int i = 0; i < n; i++, a += inca, b += incb) {
703 void eigSort(
int n, scalar *wr, scalar *wi, scalar *VL, scalar *VR)
708 for(
int i = 0; i < n - 1; i++) {
712 for(
int j = i + 1; j < n; j++) {
713 const scalar ej = wr[j];
720 swap(&wr[i], 1, &wr[k], 1, 1);
721 swap(&wi[i], 1, &wi[k], 1, 1);
722 swap(&VL[n * i], 1, &VL[n * k], 1, n);
723 swap(&VR[n * i], 1, &VR[n * k], 1, n);
729 bool sortRealPart =
false)
730 #if defined(HAVE_EIGEN)
733 Eigen::EigenSolver<Eigen::Matrix<scalar, Eigen::Dynamic, Eigen::Dynamic> > es(ma);
734 if(es.info() != Eigen::Success) {
735 Msg::Warning(
"Eigen could not compute eigenvalues/eigenvectors");
738 EigenVec vr(eigenValReal.
_data, eigenValReal.
_r);
739 vr = es.eigenvalues().real();
740 EigenVec vi(eigenValImag.
_data, eigenValImag.
_r);
741 vi = es.eigenvalues().imag();
742 EigenMat mr(rightEigenVect.
_data, rightEigenVect.
_r, rightEigenVect.
_c);
743 mr = es.eigenvectors().real();
744 EigenMat ml(leftEigenVect.
_data, leftEigenVect.
_r, leftEigenVect.
_c);
753 #elif !defined(HAVE_LAPACK)
755 Msg::Error(
"Eigenvalue computation of dense matrices requires Eigen or "
762 #if defined(HAVE_EIGEN)
766 EigenVec vs(
S._data,
S._r);
768 <Eigen::Matrix<scalar, Eigen::Dynamic, Eigen::Dynamic> >
769 svd(ma, Eigen::ComputeThinU | Eigen::ComputeThinV);
772 vs =
svd.singularValues();
775 #elif !defined(HAVE_LAPACK)
777 Msg::Error(
"Singular value decomposition of dense matrices requires "
783 void print(
const std::string &name =
"",
784 const std::string &format =
"")
const;
788 if(fread(
_data,
sizeof(scalar),
_r *
_c,
f) != (
size_t)
_r)
return;
void add(const double &a)
void setAsProxy(const fullVector< scalar > &original, int r_start, int r)
void set(int r, scalar v)
void set(int r, int c, scalar v)
void copy(const fullMatrix< scalar > &a)
void add(const fullMatrix< scalar > &m)
void multTByT(const fullVector< scalar > &x)
static double c(int i, int j, fullMatrix< double > &CA, const std::vector< SPoint3 > &P, const std::vector< SPoint3 > &Q)
void setAsProxy(const fullMatrix< scalar > &original, int c_start, int c)
static void Warning(const char *fmt,...)
void setAll(const scalar &m)
static void Error(const char *fmt,...)
void f(int n, double u, double *val)
scalar & operator()(int i, int j)
void scale(const scalar s)
fullMatrix< scalar > transpose() const
void scale(const scalar s)
bool resize(int r, bool resetValue=true)
void binarySave(FILE *f) const
scalar get(int r, int c) const
void setAll(const fullVector< scalar > &m)
void print(const std::string &name="", const std::string &format="") const
void setAsProxy(scalar *data, int r, int c)
bool luFactor(fullVector< int > &ipiv)
fullVector(const fullVector< scalar > &other)
void setOwnData(bool ownData)
void mult(const fullMatrix< scalar > &b, fullMatrix< scalar > &c) const
void setAsProxy(scalar *data, int r)
fullVector< scalar > & operator=(const fullVector< scalar > &other)
bool luSubstitute(const fullVector< scalar > &rhs, fullVector< int > &ipiv, fullVector< scalar > &result)
void multOnBlock(const fullMatrix< scalar > &b, const int ncol, const int fcol, const int alpha, const int beta, fullVector< scalar > &c) const
void copyOneColumn(const fullVector< scalar > &x, const int ind) const
fullMatrix(fullMatrix< scalar > &original, int c_start, int c)
void reshape(int nbRows, int nbColumns)
void multAddy(const fullVector< scalar > &x, fullVector< scalar > &y) const
fullVector(scalar *original, int r)
scalar determinant() const
scalar operator*(const fullVector< scalar > &other)
bool eig(fullVector< double > &eigenValReal, fullVector< double > &eigenValImag, fullMatrix< scalar > &leftEigenVect, fullMatrix< scalar > &rightEigenVect, bool sortRealPart=false)
scalar operator()(int i, int j) const
void binarySave(FILE *f) const
void gemm(const fullMatrix< scalar > &a, const fullMatrix< scalar > &b, scalar alpha=1., scalar beta=1., bool transposeA=false, bool transposeB=false)
void axpy(const fullMatrix< scalar > &x, scalar alpha=1.)
void setAsProxy(const fullMatrix< scalar > &original)
fullMatrix< scalar > & operator=(const fullMatrix< scalar > &other)
void multWithATranspose(const fullVector< scalar > &x, scalar alpha, scalar beta, fullVector< scalar > &y) const
void setAsProxy(const fullMatrix< scalar > &original, int c)
const scalar * getDataPtr() const
bool svd(fullMatrix< scalar > &V, fullVector< scalar > &S)
void setOwnData(bool ownData)
void copy(const fullVector< scalar > &v, int i0, int ni, int desti0)
void copy(const fullMatrix< scalar > &a, int i0, int ni, int j0, int nj, int desti0, int destj0)
const scalar * getDataPtr() const
void operator+=(const fullMatrix< scalar > &other)
bool luSolve(const fullVector< scalar > &rhs, fullVector< scalar > &result)
void eigSort(int n, scalar *wr, scalar *wi, scalar *VL, scalar *VR)
void mult(const fullVector< scalar > &x, fullVector< scalar > &y) const
void multTByT(const fullMatrix< scalar > &a)
void add(const fullMatrix< scalar > &m, const scalar &a)
fullMatrix(int r, int c, scalar *data)
bool resize(int r, int c, bool resetValue=true)
void swap(scalar *a, int inca, scalar *b, int incb, int n)
scalar operator()(int i) const
fullMatrix(int r, int c, bool init0=true)
void setAll(const scalar &m)
void axpy(const fullVector< scalar > &x, scalar alpha=1.)
void setAll(const fullMatrix< scalar > &m)
scalar & operator()(int i)
fullMatrix(const fullMatrix< scalar > &other)
bool invert(fullMatrix< scalar > &result) const
fullMatrix(scalar *original, int r, int c)
void print(const std::string name="", const std::string format="") const