Skip to content

Commit

Permalink
Merge pull request #1291 from mmorale3/switch_to_multi
Browse files Browse the repository at this point in the history
Switch to multi array in AFQMC
  • Loading branch information
mmorale3 authored Jan 3, 2019
2 parents 75430a1 + 534bd42 commit 9632a83
Show file tree
Hide file tree
Showing 76 changed files with 1,548 additions and 2,282 deletions.
7 changes: 6 additions & 1 deletion external_codes/boost_multi/multi/array_ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,12 @@ struct layout_t<1u>{
index stride_;
index offset_;
index nelems_;
layout_t() = default;
//layout_t() = default;
constexpr layout_t() :
stride_{1},
offset_{0},
nelems_{0}
{}
constexpr layout_t(index_extension ie, layout_t<0> const&) :
stride_{1},
offset_{0},
Expand Down
2 changes: 1 addition & 1 deletion src/AFQMC/Drivers/DriverFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ bool DriverFactory::executeAFQMCDriver(std::string title, int m_series, xmlNodeP
} else {
auto initial_guess = WfnFac.getInitialGuess(wfn_name);
wset.resize(nWalkers,initial_guess[0],
initial_guess[1][indices[range_t()][range_t(0,NAEB)]]);
initial_guess[1]({0,NMO},{0,NAEB}));
wfn0.Energy(wset);
app_log()<<" Energy of starting determinant - E1, EJ, EXX: "
<<std::setprecision(12) <<wset[0].E1() <<" "
Expand Down
12 changes: 6 additions & 6 deletions src/AFQMC/Estimators/BackPropagatedEstimator.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ namespace afqmc
class BackPropagatedEstimator: public EstimatorBase
{

using CMatrix_ref = boost::multi_array_ref<ComplexType,2>;
using CVector = boost::multi_array<ComplexType,1>;
using CMatrix_ref = boost::multi::array_ref<ComplexType,2>;
using CVector = boost::multi::array<ComplexType,1>;
public:

BackPropagatedEstimator(afqmc::TaskGroup_& tg_, AFQMCInfo& info,
Expand Down Expand Up @@ -64,10 +64,10 @@ class BackPropagatedEstimator: public EstimatorBase
dm_dims = {2*NMO,2*NMO};
}
if(DMBuffer.size() < dm_size) {
DMBuffer.resize(extents[dm_size]);
DMBuffer.reextent(extensions<1u>{dm_size});
}
std::fill(DMBuffer.begin(), DMBuffer.end(), ComplexType(0.0,0.0));
denom.resize(extents[1]);
denom.reextent({1});
}

~BackPropagatedEstimator() {}
Expand All @@ -80,7 +80,7 @@ class BackPropagatedEstimator: public EstimatorBase
// check to see whether we should be accumulating estimates.
bool back_propagate = wset[0].isBMatrixBufferFull();
if(back_propagate) {
CMatrix_ref BackPropDM(DMBuffer.data(), extents[dm_dims.first][dm_dims.second]);
CMatrix_ref BackPropDM(DMBuffer.data(), {dm_dims.first,dm_dims.second});
// Computes GBP(i,j)
denom[0] = ComplexType(0.0,0.0);
wfn0.BackPropagatedDensityMatrix(wset, BackPropDM, denom, path_restoration, !importanceSampling);
Expand All @@ -94,7 +94,7 @@ class BackPropagatedEstimator: public EstimatorBase
}

//template <typename T>
//ComplexType back_propagate_wavefunction(const boost::multi_array<ComplexType,2>& trialSM, SMType& psiBP, T& walker, int nback_prop)
//ComplexType back_propagate_wavefunction(const boost::multi::array<ComplexType,2>& trialSM, SMType& psiBP, T& walker, int nback_prop)
//{
//ComplexType detR = one;
//walker.decrementBMatrix();
Expand Down
10 changes: 4 additions & 6 deletions src/AFQMC/Estimators/EnergyEstimator.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
#include "OhmmsData/libxmldefs.h"
#include "Utilities/NewTimer.h"

#include "boost/multi_array.hpp"

#include "AFQMC/Wavefunctions/Wavefunction.hpp"
#include "AFQMC/Walkers/WalkerSet.hpp"

Expand Down Expand Up @@ -45,9 +43,9 @@ class EnergyEstimator: public EstimatorBase
AFQMCTimers[energy_timer]->start();
size_t nwalk = wset.size();
if(eloc.shape()[0] != nwalk || eloc.shape()[1] != 3)
eloc.resize(boost::extents[nwalk][3]);
eloc.reextent({nwalk,3});
if(ovlp.shape()[0] != nwalk)
ovlp.resize(boost::extents[nwalk]);
ovlp.reextent(extensions<1u>{nwalk});

ComplexType dum, et;
wfn0.Energy(wset,eloc,ovlp);
Expand Down Expand Up @@ -100,8 +98,8 @@ class EnergyEstimator: public EstimatorBase

Wavefunction& wfn0;

boost::multi_array<ComplexType,2> eloc;
boost::multi_array<ComplexType,1> ovlp;
ComplexMatrix<std::allocator<ComplexType>> eloc;
ComplexVector<std::allocator<ComplexType>> ovlp;

std::vector<std::complex<double> > data;

Expand Down
6 changes: 3 additions & 3 deletions src/AFQMC/HamiltonianOperations/HamiltonianOperations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ class dummy_HOps
dummy_HOps() {};

template<class... Args>
boost::multi_array<ComplexType,2> getOneBodyPropagatorMatrix(Args&&... args)
boost::multi::array<ComplexType,2> getOneBodyPropagatorMatrix(Args&&... args)
{
throw std::runtime_error("calling visitor on dummy_HOps object");
return boost::multi_array<ComplexType,2>{};
return boost::multi::array<ComplexType,2>{};
}

template<class... Args>
Expand Down Expand Up @@ -187,7 +187,7 @@ class HamiltonianOperations:
HamiltonianOperations& operator=(HamiltonianOperations&& other) = default;

template<class... Args>
boost::multi_array<ComplexType,2> getOneBodyPropagatorMatrix(Args&&... args) {
boost::multi::array<ComplexType,2> getOneBodyPropagatorMatrix(Args&&... args) {
return boost::apply_visitor(
[&](auto&& a){return a.getOneBodyPropagatorMatrix(std::forward<Args>(args)...);},
*this
Expand Down
64 changes: 32 additions & 32 deletions src/AFQMC/HamiltonianOperations/KP3IndexFactorization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ namespace afqmc
class KP3IndexFactorization
{

using CVector = boost::multi_array<ComplexType,1>;
using SpVector = boost::multi_array<SPComplexType,1>;
using CVector = boost::multi::array<ComplexType,1>;
using SpVector = boost::multi::array<SPComplexType,1>;
using CMatrix = boost::multi::array<ComplexType,2>;
using CMatrix_cref = boost::multi::const_array_ref<ComplexType,2>;
using CMatrix_ref = boost::multi::array_ref<ComplexType,2>;
Expand Down Expand Up @@ -140,17 +140,17 @@ class KP3IndexFactorization
KP3IndexFactorization(KP3IndexFactorization&& other) = default;
KP3IndexFactorization& operator=(KP3IndexFactorization&& other) = default;

boost::multi_array<ComplexType,2> getOneBodyPropagatorMatrix(TaskGroup_& TG, boost::multi_array<ComplexType,1> const& vMF) {
boost::multi::array<ComplexType,2> getOneBodyPropagatorMatrix(TaskGroup_& TG, boost::multi::array<ComplexType,1> const& vMF) {

int nkpts = nopk.size();
int NMO = std::accumulate(nopk.begin(),nopk.end(),0);
// in non-collinear case with SO, keep SO matrix here and add it
// for now, stay collinear
boost::multi_array<ComplexType,2> P1(extents[NMO][NMO]);
boost::multi::array<ComplexType,2> P1({NMO,NMO});

// making a copy of vMF since it will be modified
set_shm_buffer(vMF.shape()[0]);
boost::multi_array_ref<ComplexType,1> vMF_(std::addressof(*SM_TMats.origin()),extents[vMF.shape()[0]]);
boost::multi::array_ref<ComplexType,1> vMF_(std::addressof(*SM_TMats.origin()),{vMF.shape()[0]});

boost::multi::array_ref<ComplexType,1> P1D(std::addressof(*P1.origin()),{NMO*NMO});
std::fill_n(P1D.origin(),P1D.num_elements(),ComplexType(0));
Expand Down Expand Up @@ -290,14 +290,14 @@ class KP3IndexFactorization
{nelpk[nd][K],nopk[K]});
for(int a=0; a<nelpk[nd][K]; ++a)
ma::product(ComplexType(1.),ma::T(G3Da[na+a].sliced(nk,nk+nopk[K])),haj_K[a],
ComplexType(1.),E[indices[range_t()][0]]);
ComplexType(1.),E(E.extension(0),0));
na+=nelpk[nd][K];
if(walker_type==COLLINEAR) {
boost::multi::array_ref<ComplexType,2> haj_Kb(haj_K.origin()+haj_K.num_elements(),
{nelpk[nd][nkpts+K],nopk[K]});
for(int b=0; b<nelpk[nd][nkpts+K]; ++b)
ma::product(ComplexType(1.),ma::T(G3Db[nb+b].sliced(nk,nk+nopk[K])),haj_Kb[b],
ComplexType(1.),E[indices[range_t()][0]]);
ComplexType(1.),E(E.extension(0),0));
nb+=nelpk[nd][nkpts+K];
}
nk+=nopk[K];
Expand All @@ -308,14 +308,14 @@ class KP3IndexFactorization
CVector_ref haj_K(std::addressof(*haj[nd*nkpts+K].origin()),
{na*nk});
SpMatrix_ref Gaj(std::addressof(*GKK[0][K][K].origin()),{nwalk,na*nk});
ma::product(ComplexType(1.),Gaj,haj_K,ComplexType(1.),E[indices[range_t()][0]]);
ma::product(ComplexType(1.),Gaj,haj_K,ComplexType(1.),E(E.extension(0),0));
}
if(walker_type==COLLINEAR) {
na = nelpk[nd][nkpts+K];
CVector_ref haj_K(std::addressof(*haj[nd*nkpts+K].origin())+nelpk[nd][K]*nk,
{na*nk});
SpMatrix_ref Gaj(std::addressof(*GKK[1][K][K].origin()),{nwalk,na*nk});
ma::product(ComplexType(1.),Gaj,haj_K,ComplexType(1.),E[indices[range_t()][0]]);
ma::product(ComplexType(1.),Gaj,haj_K,ComplexType(1.),E(E.extension(0),0));
}
#endif
}
Expand Down Expand Up @@ -570,14 +570,14 @@ class KP3IndexFactorization
{nelpk[nd][K],nopk[K]});
for(int a=0; a<nelpk[nd][K]; ++a)
ma::product(ComplexType(1.),ma::T(G3Da[na+a].sliced(nk,nk+nopk[K])),haj_K[a],
ComplexType(1.),E[indices[range_t()][0]]);
ComplexType(1.),E(E.extension(0),0));
na+=nelpk[nd][K];
if(walker_type==COLLINEAR) {
boost::multi::array_ref<ComplexType,2> haj_Kb(haj_K.origin()+haj_K.num_elements(),
{nelpk[nd][nkpts+K],nopk[K]});
for(int b=0; b<nelpk[nd][nkpts+K]; ++b)
ma::product(ComplexType(1.),ma::T(G3Db[nb+b].sliced(nk,nk+nopk[K])),haj_Kb[b],
ComplexType(1.),E[indices[range_t()][0]]);
ComplexType(1.),E(E.extension(0),0));
nb+=nelpk[nd][nkpts+K];
}
nk+=nopk[K];
Expand All @@ -588,14 +588,14 @@ class KP3IndexFactorization
CVector_ref haj_K(std::addressof(*haj[nd*nkpts+K].origin()),
{na*nk});
SpMatrix_ref Gaj(std::addressof(*GKK[0][K][K].origin()),{nwalk,na*nk});
ma::product(ComplexType(1.),Gaj,haj_K,ComplexType(1.),E[indices[range_t()][0]]);
ma::product(ComplexType(1.),Gaj,haj_K,ComplexType(1.),E(E.extension(0),0));
}
if(walker_type==COLLINEAR) {
na = nelpk[nd][nkpts+K];
CVector_ref haj_K(std::addressof(*haj[nd*nkpts+K].origin())+nelpk[nd][K]*nk,
{na*nk});
SpMatrix_ref Gaj(std::addressof(*GKK[1][K][K].origin()),{nwalk,na*nk});
ma::product(ComplexType(1.),Gaj,haj_K,ComplexType(1.),E[indices[range_t()][0]]);
ma::product(ComplexType(1.),Gaj,haj_K,ComplexType(1.),E(E.extension(0),0));
}
#endif
}
Expand Down Expand Up @@ -838,10 +838,10 @@ class KP3IndexFactorization
void vHS(MatA& X, MatB&& v, double a=1., double c=0.) {
using BType = typename std::decay<MatB>::type::element ;
using AType = typename std::decay<MatA>::type::element ;
boost::multi_array_ref<BType,2> v_(std::addressof(*v.origin()),
extents[v.shape()[0]][1]);
boost::multi_array_ref<AType,2> X_(std::addressof(*X.origin()),
extents[X.shape()[0]][1]);
boost::multi::array_ref<BType,2> v_(std::addressof(*v.origin()),
{v.shape()[0],1});
boost::multi::array_ref<AType,2> X_(std::addressof(*X.origin()),
{X.shape()[0],1});
return vHS(X_,v_,a,c);
}

Expand Down Expand Up @@ -936,7 +936,7 @@ class KP3IndexFactorization
Sp3Tensor_ref vik3D(TMats.origin(),{nwalk,ni,nk});
SpMatrix_ref Likn(std::addressof(*LQKikn[Q][K].origin()),
{ni*nk,nchol});
ma::product(T(X[indices[range_t(nc0,nc0+nchol)][range_t()]]),
ma::product(T(X.sliced(nc0,nc0+nchol)),
T(Likn),vik);
for(int nw=0; nw<nwalk; nw++)
for(int i=0; i<ni; i++)
Expand All @@ -946,7 +946,7 @@ class KP3IndexFactorization
Sp3Tensor_ref vki3D(TMats.origin(),{nwalk,nk,ni});
SpMatrix_ref Likn(std::addressof(*LQKikn[kminus[Q]][QK].origin()),
{nk*ni,nchol});
ma::product(T(X[indices[range_t(nc0,nc0+nchol)][range_t()]]),
ma::product(T(X.sliced(nc0,nc0+nchol)),
H(Likn),vki);
for(int nw=0; nw<nwalk; nw++) {
auto&& vki_n(vki3D[nw]);
Expand All @@ -968,7 +968,7 @@ class KP3IndexFactorization
Sp3Tensor_ref vik3D(TMats.origin(),{nwalk,ni,nk});
SpMatrix_ref Likn(std::addressof(*LQKikn[Q][kpos].origin()),
{ni*nk,nchol});
ma::product(T(X[indices[range_t(nc0,nc0+nchol)][range_t()]]),
ma::product(T(X.sliced(nc0,nc0+nchol)),
T(Likn),vik);
for(int nw=0; nw<nwalk; nw++)
for(int i=0; i<ni; i++)
Expand All @@ -978,7 +978,7 @@ class KP3IndexFactorization
Sp3Tensor_ref vki3D(TMats.origin(),{nwalk,nk,ni});
SpMatrix_ref Likn(std::addressof(*LQKikn[Q][kpos].origin()),
{nk*ni,nchol});
ma::product(T(X[indices[range_t(nc0,nc0+nchol)][range_t()]]),
ma::product(T(X.sliced(nc0,nc0+nchol)),
H(Likn),vki);
for(int nw=0; nw<nwalk; nw++) {
auto&& vki_n(vki3D[nw]);
Expand Down Expand Up @@ -1011,7 +1011,7 @@ class KP3IndexFactorization
SpMatrix_ref vik(TMats.origin(),{nwalk,ni*nk});
Sp3Tensor_ref vik3D(TMats.origin(),{nwalk,ni,nk});
// v[nw][k(in Q(K))][i(in K)] += sum_n conj(LQK[i][k][n]) X[Q][n-][nw]
ma::product(T(X[indices[range_t(nc0+nchol,nc0+2*nchol)][range_t()]]),
ma::product(T(X.sliced(nc0+nchol,nc0+2*nchol)),
H(Likn),vik);
for(int nw=0; nw<nwalk; nw++) {
auto&& vik3D_n = vik3D[nw];
Expand All @@ -1036,10 +1036,10 @@ class KP3IndexFactorization
void vbias(const MatA& G, MatB&& v, double a=1., double c=0., int k=0) {
using BType = typename std::decay<MatB>::type::element ;
using AType = typename std::decay<MatA>::type::element ;
boost::multi_array_ref<BType,2> v_(std::addressof(*v.origin()),
extents[v.shape()[0]][1]);
boost::const_multi_array_ref<AType,2> G_(std::addressof(*G.origin()),
extents[G.shape()[0]][1]);
boost::multi::array_ref<BType,2> v_(std::addressof(*v.origin()),
{v.shape()[0],1});
boost::multi::const_array_ref<AType,2> G_(std::addressof(*G.origin()),
{G.shape()[0],1});
return vbias(G_,v_,a,c,k);
}

Expand Down Expand Up @@ -1125,18 +1125,18 @@ class KP3IndexFactorization
std::lock_guard<shared_mutex> guard(*mutex[Q]);
int nc0 = 2*std::accumulate(ncholpQ.begin(),ncholpQ.begin()+Q,0);
// v+ = 0.5*a*(v1+v2)
ma::axpy(halfa,vlocal.sliced(0,ncholpQ[Q]),v[indices[range_t(nc0,nc0+ncholpQ[Q])][range_t()]]);
ma::axpy(halfa,vlocal.sliced(0,ncholpQ[Q]),v.sliced(nc0,nc0+ncholpQ[Q]));
// v- = -0.5*a*i*(v1-v2)
ma::axpy(minusimhalfa,vlocal.sliced(0,ncholpQ[Q]),v[indices[range_t(nc0+ncholpQ[Q],nc0+2*ncholpQ[Q])][range_t()]]);
ma::axpy(minusimhalfa,vlocal.sliced(0,ncholpQ[Q]),v.sliced(nc0+ncholpQ[Q],nc0+2*ncholpQ[Q]));
}
int Qm = kminus[Q];
int nc0 = 2*std::accumulate(ncholpQ.begin(),ncholpQ.begin()+Qm,0);
if(Q != Q0) {
std::lock_guard<shared_mutex> guard(*mutex[Qm]);
// v+ = 0.5*a*(v1+v2)
ma::axpy(halfa,vlocal.sliced(0,ncholpQ[Qm]),v[indices[range_t(nc0,nc0+ncholpQ[Qm])][range_t()]]);
ma::axpy(halfa,vlocal.sliced(0,ncholpQ[Qm]),v.sliced(nc0,nc0+ncholpQ[Qm]));
// v- = -0.5*a*i*(v1-v2)
ma::axpy(imhalfa,vlocal.sliced(0,ncholpQ[Qm]),v[indices[range_t(nc0+ncholpQ[Qm],nc0+2*ncholpQ[Qm])][range_t()]]);
ma::axpy(imhalfa,vlocal.sliced(0,ncholpQ[Qm]),v.sliced(nc0+ncholpQ[Qm],nc0+2*ncholpQ[Qm]));
}
} // to release the lock
if(haveV)
Expand Down Expand Up @@ -1174,9 +1174,9 @@ class KP3IndexFactorization
std::lock_guard<shared_mutex> guard(*mutex[Q0]);
int nc0 = 2*std::accumulate(ncholpQ.begin(),ncholpQ.begin()+Q0,0);
// v+ = 0.5*a*(v1+v2)
ma::axpy(halfa,vlocal.sliced(0,ncholpQ[Q0]),v[indices[range_t(nc0,nc0+ncholpQ[Q0])][range_t()]]);
ma::axpy(halfa,vlocal.sliced(0,ncholpQ[Q0]),v.sliced(nc0,nc0+ncholpQ[Q0]));
// v- = -0.5*a*i*(v1-v2)
ma::axpy(imhalfa,vlocal.sliced(0,ncholpQ[Q0]),v[indices[range_t(nc0+ncholpQ[Q0],nc0+2*ncholpQ[Q0])][range_t()]]);
ma::axpy(imhalfa,vlocal.sliced(0,ncholpQ[Q0]),v.sliced(nc0+ncholpQ[Q0],nc0+2*ncholpQ[Q0]));
}
} // to release the lock
if(haveV)
Expand Down
Loading

0 comments on commit 9632a83

Please sign in to comment.