From b9f92b69bea3e023d664d3b47c2482ed882571d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org> Date: Tue, 30 Apr 2019 12:58:21 +0200 Subject: [PATCH] Dynare++: replace some uses of std::vector by a lighter std::unique_ptr --- dynare++/kord/dynamic_model.cc | 4 ++-- dynare++/kord/first_order.cc | 4 ++-- dynare++/sylv/cc/GeneralMatrix.cc | 13 ++++++------- dynare++/sylv/cc/SchurDecomp.cc | 11 ++++++----- dynare++/sylv/cc/SchurDecompEig.cc | 6 +++--- dynare++/sylv/cc/SylvMatrix.cc | 18 +++++++++--------- dynare++/sylv/cc/SymSchurDecomp.cc | 12 ++++++------ dynare++/tl/cc/twod_matrix.cc | 6 +++--- 8 files changed, 37 insertions(+), 37 deletions(-) diff --git a/dynare++/kord/dynamic_model.cc b/dynare++/kord/dynamic_model.cc index 118e7f1251..80c4914f6a 100644 --- a/dynare++/kord/dynamic_model.cc +++ b/dynare++/kord/dynamic_model.cc @@ -22,7 +22,7 @@ NameList::writeMat(mat_t *fd, const std::string &vname) const if (maxlen == 0) return; - std::vector<char> m(getNum()*maxlen); + auto m = std::make_unique<char[]>(getNum()*maxlen); for (int i = 0; i < getNum(); i++) for (int j = 0; j < maxlen; j++) @@ -35,7 +35,7 @@ NameList::writeMat(mat_t *fd, const std::string &vname) const dims[0] = getNum(); dims[1] = maxlen; - matvar_t *v = Mat_VarCreate(vname.c_str(), MAT_C_CHAR, MAT_T_UINT8, 2, dims, m.data(), 0); + matvar_t *v = Mat_VarCreate(vname.c_str(), MAT_C_CHAR, MAT_T_UINT8, 2, dims, m.get(), 0); Mat_VarWrite(fd, v, MAT_COMPRESSION_NONE); diff --git a/dynare++/kord/first_order.cc b/dynare++/kord/first_order.cc index d567234293..eb5243c6f5 100644 --- a/dynare++/kord/first_order.cc +++ b/dynare++/kord/first_order.cc @@ -163,7 +163,7 @@ FirstOrder::solve(const TwoDMatrix &fd) lapack_int ldvsl = vsl.getLD(), ldvsr = vsr.getLD(); lapack_int lwork = 100*n+16; Vector work(lwork); - std::vector<lapack_int> bwork(n); + auto bwork = std::make_unique<lapack_int[]>(n); lapack_int info; lapack_int sdim2 = sdim; { @@ -172,7 +172,7 @@ FirstOrder::solve(const TwoDMatrix &fd) dgges("N", "V", "S", order_eigs, &n, matE.getData().base(), &lda, matD.getData().base(), &ldb, &sdim2, alphar.base(), alphai.base(), beta.base(), vsl.getData().base(), &ldvsl, vsr.getData().base(), &ldvsr, - work.base(), &lwork, bwork.data(), &info); + work.base(), &lwork, bwork.get(), &info); } if (info) throw KordException(__FILE__, __LINE__, diff --git a/dynare++/sylv/cc/GeneralMatrix.cc b/dynare++/sylv/cc/GeneralMatrix.cc index 61317d9331..4b8c098bc5 100644 --- a/dynare++/sylv/cc/GeneralMatrix.cc +++ b/dynare++/sylv/cc/GeneralMatrix.cc @@ -13,7 +13,6 @@ #include <cstdlib> #include <cmath> #include <limits> -#include <vector> GeneralMatrix::GeneralMatrix(const GeneralMatrix &m) : data(m.rows*m.cols), rows(m.rows), cols(m.cols), ld(m.rows) @@ -431,11 +430,11 @@ ConstGeneralMatrix::multInvLeft(const std::string &trans, int mrows, int mcols, if (rows > 0) { GeneralMatrix inv(*this); - std::vector<lapack_int> ipiv(rows); + auto ipiv = std::make_unique<lapack_int[]>(rows); lapack_int info; lapack_int rows2 = rows, mcols2 = mcols, mld2 = mld, lda = inv.ld; - dgetrf(&rows2, &rows2, inv.getData().base(), &lda, ipiv.data(), &info); - dgetrs(trans.c_str(), &rows2, &mcols2, inv.base(), &lda, ipiv.data(), d, + dgetrf(&rows2, &rows2, inv.getData().base(), &lda, ipiv.get(), &info); + dgetrs(trans.c_str(), &rows2, &mcols2, inv.base(), &lda, ipiv.get(), d, &mld2, &info); } } @@ -538,15 +537,15 @@ SVDDecomp::construct(const GeneralMatrix &A) lapack_int lwork = -1; lapack_int info; - std::vector<lapack_int> iwork(8*minmn); + auto iwork = std::make_unique<lapack_int[]>(8*minmn); // query for optimal lwork dgesdd("A", &m, &n, a, &lda, s, u, &ldu, vt, &ldvt, &tmpwork, - &lwork, iwork.data(), &info); + &lwork, iwork.get(), &info); lwork = static_cast<lapack_int>(tmpwork); Vector work(lwork); // do the decomposition dgesdd("A", &m, &n, a, &lda, s, u, &ldu, vt, &ldvt, work.base(), - &lwork, iwork.data(), &info); + &lwork, iwork.get(), &info); if (info < 0) throw SYLV_MES_EXCEPTION("Internal error in SVDDecomp constructor"); if (info == 0) diff --git a/dynare++/sylv/cc/SchurDecomp.cc b/dynare++/sylv/cc/SchurDecomp.cc index 6d9779e024..acafa9a357 100644 --- a/dynare++/sylv/cc/SchurDecomp.cc +++ b/dynare++/sylv/cc/SchurDecomp.cc @@ -4,7 +4,7 @@ #include "SchurDecomp.hh" -#include <vector> +#include <memory> #include <dynlapack.h> @@ -15,13 +15,14 @@ SchurDecomp::SchurDecomp(const SqSylvMatrix &m) SqSylvMatrix auxt(m); lapack_int lda = auxt.getLD(), ldvs = q.getLD(); lapack_int sdim; - std::vector<double> wr(rows), wi(rows); + auto wr = std::make_unique<double []>(rows); + auto wi = std::make_unique<double []>(rows); lapack_int lwork = 6*rows; - std::vector<double> work(lwork); + auto work = std::make_unique<double []>(lwork); lapack_int info; dgees("V", "N", nullptr, &rows, auxt.base(), &lda, &sdim, - wr.data(), wi.data(), q.base(), &ldvs, - work.data(), &lwork, nullptr, &info); + wr.get(), wi.get(), q.base(), &ldvs, + work.get(), &lwork, nullptr, &info); t_storage = std::make_unique<QuasiTriangular>(auxt.getData(), rows); t = t_storage.get(); } diff --git a/dynare++/sylv/cc/SchurDecompEig.cc b/dynare++/sylv/cc/SchurDecompEig.cc index 68eeca8749..c491feaef3 100644 --- a/dynare++/sylv/cc/SchurDecompEig.cc +++ b/dynare++/sylv/cc/SchurDecompEig.cc @@ -3,7 +3,7 @@ #include <dynlapack.h> -#include <vector> +#include <memory> /* Bubble diagonal 1×1 or 2×2 block from position ‘from’ to position ‘to’. If an eigenvalue cannot be swapped with its neighbour, the @@ -55,9 +55,9 @@ SchurDecompEig::tryToSwap(diag_iter &it, diag_iter &itadd) lapack_int n = getDim(), ldt = getT().getLD(), ldq = getQ().getLD(); lapack_int ifst = it->getIndex() + 1; lapack_int ilst = itadd->getIndex() + 1; - std::vector<double> work(n); + auto work = std::make_unique<double[]>(n); lapack_int info; - dtrexc("V", &n, getT().base(), &ldt, getQ().base(), &ldq, &ifst, &ilst, work.data(), + dtrexc("V", &n, getT().base(), &ldt, getQ().base(), &ldq, &ifst, &ilst, work.get(), &info); if (info < 0) throw SYLV_MES_EXCEPTION("Wrong argument to dtrexc."); diff --git a/dynare++/sylv/cc/SylvMatrix.cc b/dynare++/sylv/cc/SylvMatrix.cc index b9326e5ac3..a66ede20cf 100644 --- a/dynare++/sylv/cc/SylvMatrix.cc +++ b/dynare++/sylv/cc/SylvMatrix.cc @@ -10,7 +10,7 @@ #include <dynlapack.h> #include <cmath> -#include <vector> +#include <memory> void SylvMatrix::multLeftI(const SqSylvMatrix &m) @@ -220,30 +220,30 @@ SqSylvMatrix::multInvLeft2(GeneralMatrix &a, GeneralMatrix &b, // PLU factorization Vector inv(data); - std::vector<lapack_int> ipiv(rows); + auto ipiv = std::make_unique<lapack_int[]>(rows); lapack_int info; lapack_int rows2 = rows, lda = ld; - dgetrf(&rows2, &rows2, inv.base(), &lda, ipiv.data(), &info); + dgetrf(&rows2, &rows2, inv.base(), &lda, ipiv.get(), &info); // solve a lapack_int acols = a.ncols(); double *abase = a.base(); - dgetrs("N", &rows2, &acols, inv.base(), &lda, ipiv.data(), + dgetrs("N", &rows2, &acols, inv.base(), &lda, ipiv.get(), abase, &rows2, &info); // solve b lapack_int bcols = b.ncols(); double *bbase = b.base(); - dgetrs("N", &rows2, &bcols, inv.base(), &lda, ipiv.data(), + dgetrs("N", &rows2, &bcols, inv.base(), &lda, ipiv.get(), bbase, &rows2, &info); // condition numbers - std::vector<double> work(4*rows); - std::vector<lapack_int> iwork(rows); + auto work = std::make_unique<double[]>(4*rows); + auto iwork = std::make_unique<lapack_int[]>(rows); double norm1 = getNorm1(); dgecon("1", &rows2, inv.base(), &lda, &norm1, &rcond1, - work.data(), iwork.data(), &info); + work.get(), iwork.get(), &info); double norminf = getNormInf(); dgecon("I", &rows2, inv.base(), &lda, &norminf, &rcondinf, - work.data(), iwork.data(), &info); + work.get(), iwork.get(), &info); } void diff --git a/dynare++/sylv/cc/SymSchurDecomp.cc b/dynare++/sylv/cc/SymSchurDecomp.cc index 238f53d584..e6df543752 100644 --- a/dynare++/sylv/cc/SymSchurDecomp.cc +++ b/dynare++/sylv/cc/SymSchurDecomp.cc @@ -9,7 +9,7 @@ #include <algorithm> #include <cmath> -#include <vector> +#include <memory> SymSchurDecomp::SymSchurDecomp(const ConstGeneralMatrix &mata) : lambda(mata.nrows()), q(mata.nrows()) @@ -34,7 +34,7 @@ SymSchurDecomp::SymSchurDecomp(const ConstGeneralMatrix &mata) double *w = lambda.base(); double *z = q.base(); lapack_int ldz = q.getLD(); - std::vector<lapack_int> isuppz(2*std::max(1, static_cast<int>(m))); + auto isuppz = std::make_unique<lapack_int[]>(2*std::max(1, static_cast<int>(m))); double tmpwork; lapack_int lwork = -1; lapack_int tmpiwork; @@ -43,16 +43,16 @@ SymSchurDecomp::SymSchurDecomp(const ConstGeneralMatrix &mata) // query for lwork and liwork dsyevr("V", "A", "U", &n, a, &lda, vl, vu, il, iu, &abstol, - &m, w, z, &ldz, isuppz.data(), &tmpwork, &lwork, &tmpiwork, &liwork, &info); + &m, w, z, &ldz, isuppz.get(), &tmpwork, &lwork, &tmpiwork, &liwork, &info); lwork = static_cast<lapack_int>(tmpwork); liwork = tmpiwork; // allocate work arrays - std::vector<double> work(lwork); - std::vector<lapack_int> iwork(liwork); + auto work = std::make_unique<double[]>(lwork); + auto iwork = std::make_unique<lapack_int[]>(liwork); // do the calculation dsyevr("V", "A", "U", &n, a, &lda, vl, vu, il, iu, &abstol, - &m, w, z, &ldz, isuppz.data(), work.data(), &lwork, iwork.data(), &liwork, &info); + &m, w, z, &ldz, isuppz.get(), work.get(), &lwork, iwork.get(), &liwork, &info); if (info < 0) throw SYLV_MES_EXCEPTION("Internal error in SymSchurDecomp constructor"); diff --git a/dynare++/tl/cc/twod_matrix.cc b/dynare++/tl/cc/twod_matrix.cc index 00969cc2be..afac960de6 100644 --- a/dynare++/tl/cc/twod_matrix.cc +++ b/dynare++/tl/cc/twod_matrix.cc @@ -3,7 +3,7 @@ #include "twod_matrix.hh" #include "tl_exception.hh" -#include <vector> +#include <memory> #include <fstream> #include <iomanip> #include <limits> @@ -39,13 +39,13 @@ ConstTwoDMatrix::writeMat(mat_t *fd, const std::string &vname) const size_t dims[2]; dims[0] = nrows(); dims[1] = ncols(); - std::vector<double> data(nrows()*ncols()); + auto data = std::make_unique<double[]>(nrows()*ncols()); for (int j = 0; j < ncols(); j++) for (int i = 0; i < nrows(); i++) data[j*nrows()+i] = get(i, j); - matvar_t *v = Mat_VarCreate(vname.c_str(), MAT_C_DOUBLE, MAT_T_DOUBLE, 2, dims, data.data(), 0); + matvar_t *v = Mat_VarCreate(vname.c_str(), MAT_C_DOUBLE, MAT_T_DOUBLE, 2, dims, data.get(), 0); Mat_VarWrite(fd, v, MAT_COMPRESSION_NONE); -- GitLab