QMCPACK
J1OrbitalSoA.cpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2022 QMCPACK developers.
6 //
7 // File developed by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
8 //
9 // File created by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
10 //////////////////////////////////////////////////////////////////////////////////////
11 // -*- C++ -*-
12 
13 
14 #include "J1OrbitalSoA.h"
16 #include "ResourceCollection.h"
17 
18 namespace qmcplusplus
19 {
20 
21 template<typename T>
23 {
24  // fused buffer for fast transfer
26  // multi walker result
28  // multi walker -1
30 
31  void resize_minus_one(size_t size)
32  {
33  if (mw_minus_one.size() < size)
34  {
35  mw_minus_one.resize(size, -1);
36  mw_minus_one.updateTo();
37  }
38  }
39 
40  J1OrbitalSoAMultiWalkerMem() : Resource("J1OrbitalSoAMultiWalkerMem") {}
41 
43 
44  std::unique_ptr<Resource> makeClone() const override { return std::make_unique<J1OrbitalSoAMultiWalkerMem>(*this); }
45 };
46 
47 template<typename FT>
48 J1OrbitalSoA<FT>::J1OrbitalSoA(const std::string& obj_name, const ParticleSet& ions, ParticleSet& els, bool use_offload)
49  : WaveFunctionComponent(obj_name),
50  use_offload_(use_offload),
51  myTableID(els.addTable(ions, use_offload ? DTModes::ALL_OFF : DTModes::NEED_VP_FULL_TABLE_ON_HOST)),
52  Nions(ions.getTotalNum()),
53  Nelec(els.getTotalNum()),
54  NumGroups(ions.groups()),
55  Ions(ions)
56 {
57  if (my_name_.empty())
58  throw std::runtime_error("J1OrbitalSoA object name cannot be empty!");
59 
60  if (use_offload_)
62 
63  initialize(els);
64 
65  // set up grp_ids
66  grp_ids.resize(Nions);
67  int count = 0;
68  for (int ig = 0; ig < NumGroups; ig++)
69  for (int j = ions.first(ig); j < ions.last(ig); j++)
70  grp_ids[count++] = ig;
71  assert(count == Nions);
72  grp_ids.updateTo();
73 }
74 
75 template<typename FT>
77 
78 template<typename FT>
80 {
81  if (std::any_of(J1Functors.begin(), J1Functors.end(), [](auto* ptr) { return ptr == nullptr; }))
82  app_warning() << "One-body Jastrow \"" << my_name_ << "\" doesn't cover all the particle pairs. "
83  << "Consider fusing multiple entries if they are of the same type for optimal code performance."
84  << std::endl;
85 }
86 
87 template<typename FT>
89 {
90  collection.addResource(std::make_unique<J1OrbitalSoAMultiWalkerMem<RealType>>());
91 }
92 
93 template<typename FT>
95  const RefVectorWithLeader<WaveFunctionComponent>& wfc_list) const
96 {
97  auto& wfc_leader = wfc_list.getCastedLeader<J1OrbitalSoA<FT>>();
99 }
100 
101 template<typename FT>
103  const RefVectorWithLeader<WaveFunctionComponent>& wfc_list) const
104 {
105  auto& wfc_leader = wfc_list.getCastedLeader<J1OrbitalSoA<FT>>();
106  collection.takebackResource(wfc_leader.mw_mem_handle_);
107 }
108 
109 template<typename FT>
112  std::vector<std::vector<ValueType>>& ratios) const
113 {
114  if (!use_offload_)
115  {
116  WaveFunctionComponent::mw_evaluateRatios(wfc_list, vp_list, ratios);
117  return;
118  }
119 
120  // add early return to prevent from accessing vp_list[0]
121  if (wfc_list.size() == 0)
122  return;
123  auto& wfc_leader = wfc_list.getCastedLeader<J1OrbitalSoA<FT>>();
124  auto& vp_leader = vp_list.getLeader();
125  const auto& mw_refPctls = vp_leader.getMultiWalkerRefPctls();
126  auto& mw_mem = wfc_leader.mw_mem_handle_.getResource();
127  auto& mw_vals = mw_mem.mw_vals;
128  auto& mw_minus_one = mw_mem.mw_minus_one;
129  const int nw = wfc_list.size();
130 
131  const size_t nVPs = mw_refPctls.size();
132  mw_vals.resize(nVPs);
133  mw_mem.resize_minus_one(nVPs);
134 
135  const auto& dt_leader(vp_leader.getDistTableAB(wfc_leader.myTableID));
136 
137  FT::mw_evaluateV(NumGroups, GroupFunctors.data(), wfc_leader.Nions, grp_ids.data(), nVPs, mw_minus_one.data(),
138  dt_leader.getMultiWalkerDataPtr(), dt_leader.getPerTargetPctlStrideSize(), mw_vals.data(),
139  mw_mem.transfer_buffer);
140 
141  size_t ivp = 0;
142  for (int iw = 0; iw < nw; ++iw)
143  {
144  const VirtualParticleSet& vp = vp_list[iw];
145  auto& wfc = wfc_list.getCastedElement<J1OrbitalSoA<FT>>(iw);
146  for (int k = 0; k < vp.getTotalNum(); ++k, ivp++)
147  ratios[iw][k] = std::exp(wfc.Vat[mw_refPctls[ivp]] - mw_vals[ivp]);
148  }
149  assert(ivp == nVPs);
150 }
151 
153 template class J1OrbitalSoA<
159 } // namespace qmcplusplus
void checkSanity() const override
Validate the internal consistency of the object.
size_t addResource(std::unique_ptr< Resource > &&res, bool noprint=false)
std::ostream & app_warning()
Definition: OutputManager.h:69
void takebackResource(ResourceHandle< RS > &res_handle)
const std::string my_name_
Name of the object It is required to be different for objects of the same derived type like multiple ...
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
const int NumGroups
number of ion groups
Definition: J1OrbitalSoA.h:71
Vector< T, OffloadPinnedAllocator< T > > mw_vals
For distance tables of virtual particle (VP) sets constructed based on this table, whether full table is needed on host The corresponding DT of VP need to set MW_EVALUATE_RESULT_NO_TRANSFER_TO_HOST accordingly.
A numerical functor.
A ParticleSet that handles virtual moves of a selected particle of a given physical ParticleSet Virtu...
std::unique_ptr< Resource > makeClone() const override
int first(int igroup) const
return the first index of a group i
Definition: ParticleSet.h:514
const bool use_offload_
if true use offload
Definition: J1OrbitalSoA.h:62
J1OrbitalSoAMultiWalkerMem(const J1OrbitalSoAMultiWalkerMem &)
void releaseResource(ResourceCollection &collection, const RefVectorWithLeader< WaveFunctionComponent > &wfc_list) const override
return a shared resource to a collection
An abstract class for a component of a many-body trial wave function.
ResourceHandle< J1OrbitalSoAMultiWalkerMem< RealType > > mw_mem_handle_
Definition: J1OrbitalSoA.h:105
Specialization for one-body Jastrow function using multiple functors.
Definition: J1OrbitalSoA.h:46
Specialized paritlce class for atomistic simulations.
Definition: ParticleSet.h:55
DynamicCoordinateKind getKind() const
void mw_evaluateRatios(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< const VirtualParticleSet > &vp_list, std::vector< std::vector< ValueType >> &ratios) const override
void createResource(ResourceCollection &collection) const override
initialize a shared resource and hand it to a collection
CASTTYPE & getCastedElement(size_t i) const
int last(int igroup) const
return the last index of a group i
Definition: ParticleSet.h:517
MakeReturn< UnaryNode< FnExp, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t exp(const Vector< T1, C1 > &l)
void initialize(const ParticleSet &els)
Definition: J1OrbitalSoA.h:202
J1OrbitalSoA(const std::string &obj_name, const ParticleSet &ions, ParticleSet &els, bool use_offload)
const int Nions
number of ions
Definition: J1OrbitalSoA.h:67
const DynamicCoordinates & getCoordinates() const
Definition: ParticleSet.h:246
Vector< int, OffloadPinnedAllocator< int > > grp_ids
the group_id of each particle
Definition: J1OrbitalSoA.h:75
void acquireResource(ResourceCollection &collection, const RefVectorWithLeader< WaveFunctionComponent > &wfc_list) const override
acquire a shared resource from a collection
virtual void mw_evaluateRatios(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< const VirtualParticleSet > &vp_list, std::vector< std::vector< ValueType >> &ratios) const
evaluate ratios to evaluate the non-local PP multiple walkers
Vector< char, OffloadPinnedAllocator< char > > transfer_buffer
ResourceHandle< RS > lendResource()
Vector< int, OffloadPinnedAllocator< int > > mw_minus_one