QMCPACK
RealSpacePositionsOMPTarget Class Reference

Introduced to handle virtual moves and ratio computations, e.g. More...

+ Inheritance diagram for RealSpacePositionsOMPTarget:
+ Collaboration diagram for RealSpacePositionsOMPTarget:

Classes

struct  MultiWalkerMem
 multi walker shared memory buffer More...
 

Public Member Functions

 RealSpacePositionsOMPTarget ()
 
 RealSpacePositionsOMPTarget (const RealSpacePositionsOMPTarget &in)
 
std::unique_ptr< DynamicCoordinatesmakeClone () override
 
void resize (size_t n) override
 resize internal storages based on the number of particles More...
 
size_t size () const override
 return the number of particles More...
 
void setAllParticlePos (const ParticlePos &R) override
 overwrite the positions of all the particles. More...
 
void setOneParticlePos (const PosType &pos, size_t iat) override
 overwrite the position of one the particle. More...
 
void mw_copyActivePos (const RefVectorWithLeader< DynamicCoordinates > &coords_list, size_t iat, const std::vector< PosType > &new_positions) const override
 copy the active positions of particles with a uniform id in all the walkers to a single internal buffer. More...
 
void mw_acceptParticlePos (const RefVectorWithLeader< DynamicCoordinates > &coords_list, size_t iat, const std::vector< PosType > &new_positions, const std::vector< bool > &isAccepted) const override
 overwrite the positions of particles with a uniform id in all the walkers upon acceptance. More...
 
const PosVectorSoagetAllParticlePos () const override
 all particle position accessor More...
 
PosType getOneParticlePos (size_t iat) const override
 one particle position accessor More...
 
void donePbyP () override
 secure internal data consistency after p-by-p moves More...
 
const RealTypegetDevicePtr () const
 
const auto & getFusedNewPosBuffer () const
 
void createResource (ResourceCollection &collection) const override
 initialize a shared resource and hand it to a collection More...
 
void acquireResource (ResourceCollection &collection, const RefVectorWithLeader< DynamicCoordinates > &coords_list) const override
 acquire a shared resource from a collection More...
 
void releaseResource (ResourceCollection &collection, const RefVectorWithLeader< DynamicCoordinates > &coords_list) const override
 return a shared resource to a collection More...
 
const auto & getMultiWalkerRSoADevicePtrs () const
 
- Public Member Functions inherited from DynamicCoordinates
 DynamicCoordinates (const DynamicCoordinateKind kind_in)
 
 DynamicCoordinates (const DynamicCoordinates &)=default
 
DynamicCoordinatesoperator= (const DynamicCoordinates &)=delete
 
DynamicCoordinateKind getKind () const
 
virtual ~DynamicCoordinates ()=default
 

Private Member Functions

void updateH2D ()
 

Private Attributes

VectorSoaContainer< RealType, QMCTraits::DIM, OMPallocator< RealType, PinnedAlignedAllocator< RealType > > > RSoA
 particle positions in SoA layout More...
 
ResourceHandle< MultiWalkerMemmw_mem_handle_
 
PosVectorSoa RSoA_hostview
 host view of RSoA More...
 
bool is_host_position_changed_
 if true, host position has been changed while the device copy has not been updated. More...
 
bool is_nw_new_pos_prepared
 if true, mw_new_pos has been updated with active positions. More...
 

Additional Inherited Members

- Public Types inherited from DynamicCoordinates
using RealType = QMCTraits::RealType
 
using PosType = QMCTraits::PosType
 
using ParticlePos = PtclOnLatticeTraits::ParticlePos
 
using PosVectorSoa = VectorSoaContainer< RealType, QMCTraits::DIM >
 
- Protected Attributes inherited from DynamicCoordinates
const DynamicCoordinateKind variable_kind_
 type of dynamic coordinates More...
 

Detailed Description

Introduced to handle virtual moves and ratio computations, e.g.

for non-local PP evaluations.

Definition at line 29 of file RealSpacePositionsOMPTarget.h.

Constructor & Destructor Documentation

◆ RealSpacePositionsOMPTarget() [1/2]

Definition at line 32 of file RealSpacePositionsOMPTarget.h.

34  {}
bool is_host_position_changed_
if true, host position has been changed while the device copy has not been updated.
DynamicCoordinates(const DynamicCoordinateKind kind_in)

◆ RealSpacePositionsOMPTarget() [2/2]

Definition at line 35 of file RealSpacePositionsOMPTarget.h.

References VectorSoaContainer< T, D, Alloc >::attachReference(), RealSpacePositionsOMPTarget::RSoA, RealSpacePositionsOMPTarget::RSoA_hostview, and RealSpacePositionsOMPTarget::updateH2D().

37  {
38  RSoA_hostview.attachReference(RSoA.size(), RSoA.capacity(), RSoA.data());
39  updateH2D();
40  }
VectorSoaContainer< RealType, QMCTraits::DIM, OMPallocator< RealType, PinnedAlignedAllocator< RealType > > > RSoA
particle positions in SoA layout
DynamicCoordinates(const DynamicCoordinateKind kind_in)
void attachReference(size_type n, size_type n_padded, T *ptr)
attach to pre-allocated data

Member Function Documentation

◆ acquireResource()

void acquireResource ( ResourceCollection collection,
const RefVectorWithLeader< DynamicCoordinates > &  coords_list 
) const
inlineoverridevirtual

acquire a shared resource from a collection

Reimplemented from DynamicCoordinates.

Definition at line 184 of file RealSpacePositionsOMPTarget.h.

References RefVectorWithLeader< T >::getCastedElement(), RefVectorWithLeader< T >::getCastedLeader(), ResourceCollection::lendResource(), RealSpacePositionsOMPTarget::mw_mem_handle_, RealSpacePositionsOMPTarget::MultiWalkerMem::mw_rsoa_ptrs, and RealSpacePositionsOMPTarget::RSoA.

186  {
187  MultiWalkerMem& mw_mem = coords_list.getCastedLeader<RealSpacePositionsOMPTarget>().mw_mem_handle_ =
188  collection.lendResource<MultiWalkerMem>();
189 
190  auto& mw_rsoa_ptrs(mw_mem.mw_rsoa_ptrs);
191  const auto nw = coords_list.size();
192  mw_rsoa_ptrs.resize(nw);
193  for (int iw = 0; iw < nw; iw++)
194  {
195  auto& coords = coords_list.getCastedElement<RealSpacePositionsOMPTarget>(iw);
196  mw_rsoa_ptrs[iw] = coords.RSoA.device_data();
197  }
198  mw_rsoa_ptrs.updateTo();
199  }

◆ createResource()

void createResource ( ResourceCollection collection) const
inlineoverridevirtual

initialize a shared resource and hand it to a collection

Reimplemented from DynamicCoordinates.

Definition at line 179 of file RealSpacePositionsOMPTarget.h.

References ResourceCollection::addResource().

180  {
181  auto resource_index = collection.addResource(std::make_unique<MultiWalkerMem>());
182  }

◆ donePbyP()

void donePbyP ( )
inlineoverridevirtual

secure internal data consistency after p-by-p moves

Reimplemented from DynamicCoordinates.

Definition at line 165 of file RealSpacePositionsOMPTarget.h.

References RealSpacePositionsOMPTarget::is_host_position_changed_, RealSpacePositionsOMPTarget::is_nw_new_pos_prepared, and RealSpacePositionsOMPTarget::updateH2D().

166  {
167  is_nw_new_pos_prepared = false;
169  {
170  updateH2D();
172  }
173  }
bool is_host_position_changed_
if true, host position has been changed while the device copy has not been updated.
bool is_nw_new_pos_prepared
if true, mw_new_pos has been updated with active positions.

◆ getAllParticlePos()

const PosVectorSoa& getAllParticlePos ( ) const
inlineoverridevirtual

all particle position accessor

Implements DynamicCoordinates.

Definition at line 162 of file RealSpacePositionsOMPTarget.h.

References RealSpacePositionsOMPTarget::RSoA_hostview.

162 { return RSoA_hostview; }

◆ getDevicePtr()

const RealType* getDevicePtr ( ) const
inline

Definition at line 175 of file RealSpacePositionsOMPTarget.h.

References RealSpacePositionsOMPTarget::RSoA.

175 { return RSoA.device_data(); }
VectorSoaContainer< RealType, QMCTraits::DIM, OMPallocator< RealType, PinnedAlignedAllocator< RealType > > > RSoA
particle positions in SoA layout

◆ getFusedNewPosBuffer()

const auto& getFusedNewPosBuffer ( ) const
inline

Definition at line 177 of file RealSpacePositionsOMPTarget.h.

References RealSpacePositionsOMPTarget::mw_mem_handle_.

177 { return mw_mem_handle_.getResource().mw_new_pos; }

◆ getMultiWalkerRSoADevicePtrs()

const auto& getMultiWalkerRSoADevicePtrs ( ) const
inline

◆ getOneParticlePos()

PosType getOneParticlePos ( size_t  iat) const
inlineoverridevirtual

one particle position accessor

Implements DynamicCoordinates.

Definition at line 163 of file RealSpacePositionsOMPTarget.h.

References RealSpacePositionsOMPTarget::RSoA_hostview.

163 { return RSoA_hostview[iat]; }

◆ makeClone()

std::unique_ptr<DynamicCoordinates> makeClone ( )
inlineoverridevirtual

Implements DynamicCoordinates.

Definition at line 42 of file RealSpacePositionsOMPTarget.h.

43  {
44  return std::make_unique<RealSpacePositionsOMPTarget>(*this);
45  }

◆ mw_acceptParticlePos()

void mw_acceptParticlePos ( const RefVectorWithLeader< DynamicCoordinates > &  coords_list,
size_t  iat,
const std::vector< PosType > &  new_positions,
const std::vector< bool > &  isAccepted 
) const
inlineoverridevirtual

overwrite the positions of particles with a uniform id in all the walkers upon acceptance.

Parameters
coords_lista batch of DynamicCoordinates
iatparicle id, uniform across coords_list
new_positionsproposed positions
isAcceptedaccept/reject info

Implements DynamicCoordinates.

Definition at line 106 of file RealSpacePositionsOMPTarget.h.

References qmcplusplus::app_warning(), VectorSoaContainer< T, D, Alloc >::data(), QMCTraits::DIM, RefVectorWithLeader< T >::getCastedElement(), RefVectorWithLeader< T >::getCastedLeader(), RefVectorWithLeader< T >::getLeader(), RealSpacePositionsOMPTarget::is_nw_new_pos_prepared, RealSpacePositionsOMPTarget::mw_copyActivePos(), RealSpacePositionsOMPTarget::MultiWalkerMem::mw_new_pos, RealSpacePositionsOMPTarget::RSoA, and RealSpacePositionsOMPTarget::RSoA_hostview.

110  {
111  assert(this == &coords_list.getLeader());
112  const size_t nw = coords_list.size();
113  auto& coords_leader = coords_list.getCastedLeader<RealSpacePositionsOMPTarget>();
114  MultiWalkerMem& mw_mem = coords_leader.mw_mem_handle_;
115  auto& mw_new_pos = mw_mem.mw_new_pos;
116  auto& mw_rsoa_ptrs = mw_mem.mw_rsoa_ptrs;
117  auto& mw_accept_indices = mw_mem.mw_accept_indices;
118 
120  {
121  mw_copyActivePos(coords_list, iat, new_positions);
122  app_warning() << "This message only appear in unit tests. Report a bug if seen in production code." << std::endl;
123  }
124 
125  coords_leader.is_nw_new_pos_prepared = false;
126 
127  mw_accept_indices.resize(nw);
128  auto* restrict id_array = mw_accept_indices.data();
129 
130  size_t num_accepted = 0;
131  for (int iw = 0; iw < nw; iw++)
132  if (isAccepted[iw])
133  {
134  auto& coords = coords_list.getCastedElement<RealSpacePositionsOMPTarget>(iw);
135  id_array[num_accepted] = iw;
136  // save new coordinates on host copy
137  coords.RSoA_hostview(iat) = mw_new_pos[iw];
138  num_accepted++;
139  }
140 
141  // early return to avoid OpenMP runtime mishandling of size 0 in transfer/compute.
142  if (num_accepted == 0)
143  return;
144 
145  //offload to GPU
146  auto* restrict mw_pos_ptr = mw_new_pos.data();
147  auto* restrict mw_rosa_ptr = mw_rsoa_ptrs.data();
148  const size_t rsoa_stride = RSoA.capacity();
149  const size_t mw_pos_stride = mw_new_pos.capacity();
150 
151  PRAGMA_OFFLOAD("omp target teams distribute parallel for \
152  map(always, to : id_array[:num_accepted])")
153  for (int i = 0; i < num_accepted; i++)
154  {
155  const int iw = id_array[i];
156  RealType* RSoA_dev_ptr = mw_rosa_ptr[iw];
157  for (int id = 0; id < QMCTraits::DIM; id++)
158  RSoA_dev_ptr[iat + rsoa_stride * id] = mw_pos_ptr[iw + mw_pos_stride * id];
159  }
160  }
std::ostream & app_warning()
Definition: OutputManager.h:69
VectorSoaContainer< RealType, QMCTraits::DIM, OMPallocator< RealType, PinnedAlignedAllocator< RealType > > > RSoA
particle positions in SoA layout
for(int i=0;i< size_test;++i) CHECK(Approx(gauss_random_vals[offset_for_rs+i])
bool is_nw_new_pos_prepared
if true, mw_new_pos has been updated with active positions.
QMCTraits::RealType RealType
void mw_copyActivePos(const RefVectorWithLeader< DynamicCoordinates > &coords_list, size_t iat, const std::vector< PosType > &new_positions) const override
copy the active positions of particles with a uniform id in all the walkers to a single internal buff...

◆ mw_copyActivePos()

void mw_copyActivePos ( const RefVectorWithLeader< DynamicCoordinates > &  coords_list,
size_t  iat,
const std::vector< PosType > &  new_positions 
) const
inlineoverridevirtual

copy the active positions of particles with a uniform id in all the walkers to a single internal buffer.

Parameters
coords_lista batch of DynamicCoordinates
iatparicle id, uniform across coords_list
new_positionsproposed positions

Reimplemented from DynamicCoordinates.

Definition at line 86 of file RealSpacePositionsOMPTarget.h.

References RefVectorWithLeader< T >::getCastedLeader(), and RefVectorWithLeader< T >::getLeader().

Referenced by RealSpacePositionsOMPTarget::mw_acceptParticlePos().

89  {
90  assert(this == &coords_list.getLeader());
91  auto& coords_leader = coords_list.getCastedLeader<RealSpacePositionsOMPTarget>();
92 
93  const auto nw = coords_list.size();
94  auto& mw_new_pos = coords_leader.mw_mem_handle_.getResource().mw_new_pos;
95  mw_new_pos.resize(nw);
96 
97  for (int iw = 0; iw < nw; iw++)
98  mw_new_pos(iw) = new_positions[iw];
99 
100  auto* mw_pos_ptr = mw_new_pos.data();
101  PRAGMA_OFFLOAD("omp target update to(mw_pos_ptr[:QMCTraits::DIM * mw_new_pos.capacity()])")
102 
103  coords_leader.is_nw_new_pos_prepared = true;
104  }
bool is_nw_new_pos_prepared
if true, mw_new_pos has been updated with active positions.

◆ releaseResource()

void releaseResource ( ResourceCollection collection,
const RefVectorWithLeader< DynamicCoordinates > &  coords_list 
) const
inlineoverridevirtual

return a shared resource to a collection

Reimplemented from DynamicCoordinates.

Definition at line 201 of file RealSpacePositionsOMPTarget.h.

References RefVectorWithLeader< T >::getCastedLeader(), RealSpacePositionsOMPTarget::mw_mem_handle_, and ResourceCollection::takebackResource().

203  {
204  collection.takebackResource(coords_list.getCastedLeader<RealSpacePositionsOMPTarget>().mw_mem_handle_);
205  }

◆ resize()

void resize ( size_t  n)
inlineoverridevirtual

resize internal storages based on the number of particles

Parameters
nthe number of particles

Implements DynamicCoordinates.

Definition at line 47 of file RealSpacePositionsOMPTarget.h.

References VectorSoaContainer< T, D, Alloc >::attachReference(), qmcplusplus::n, RealSpacePositionsOMPTarget::RSoA, and RealSpacePositionsOMPTarget::RSoA_hostview.

Referenced by RealSpacePositionsOMPTarget::setAllParticlePos().

48  {
49  if (RSoA.size() != n)
50  {
51  RSoA.resize(n);
52  RSoA_hostview.attachReference(RSoA.size(), RSoA.capacity(), RSoA.data());
53  }
54  }
VectorSoaContainer< RealType, QMCTraits::DIM, OMPallocator< RealType, PinnedAlignedAllocator< RealType > > > RSoA
particle positions in SoA layout
void attachReference(size_type n, size_type n_padded, T *ptr)
attach to pre-allocated data

◆ setAllParticlePos()

void setAllParticlePos ( const ParticlePos R)
inlineoverridevirtual

overwrite the positions of all the particles.

Implements DynamicCoordinates.

Definition at line 58 of file RealSpacePositionsOMPTarget.h.

References VectorSoaContainer< T, D, Alloc >::copyIn(), RealSpacePositionsOMPTarget::is_nw_new_pos_prepared, RealSpacePositionsOMPTarget::resize(), RealSpacePositionsOMPTarget::RSoA_hostview, Vector< T, Alloc >::size(), and RealSpacePositionsOMPTarget::updateH2D().

59  {
60  resize(R.size());
62  updateH2D();
63  is_nw_new_pos_prepared = false;
64  }
bool is_nw_new_pos_prepared
if true, mw_new_pos has been updated with active positions.
void copyIn(const Vector< TinyVector< T, D >> &in)
AoS to SoA : copy from Vector<TinyVector<>>
void resize(size_t n) override
resize internal storages based on the number of particles

◆ setOneParticlePos()

void setOneParticlePos ( const PosType pos,
size_t  iat 
)
inlineoverridevirtual

overwrite the position of one the particle.

Implements DynamicCoordinates.

Definition at line 66 of file RealSpacePositionsOMPTarget.h.

References RealSpacePositionsOMPTarget::is_host_position_changed_, and RealSpacePositionsOMPTarget::RSoA_hostview.

67  {
68  RSoA_hostview(iat) = pos;
70  /* This was too slow due to overhead.
71  RealType x = pos[0];
72  RealType y = pos[1];
73  RealType z = pos[2];
74  RealType* data = RSoA.data();
75  size_t offset = RSoA.capacity();
76 
77  PRAGMA_OFFLOAD("omp target map(to : x, y, z, iat)")
78  {
79  data[iat] = x;
80  data[iat + offset] = y;
81  data[iat + offset * 2] = z;
82  }
83  */
84  }
bool is_host_position_changed_
if true, host position has been changed while the device copy has not been updated.

◆ size()

size_t size ( ) const
inlineoverridevirtual

return the number of particles

Implements DynamicCoordinates.

Definition at line 56 of file RealSpacePositionsOMPTarget.h.

References RealSpacePositionsOMPTarget::RSoA_hostview, and VectorSoaContainer< T, D, Alloc >::size().

56 { return RSoA_hostview.size(); }
size_type size() const
return the physical size

◆ updateH2D()

void updateH2D ( )
inlineprivate

Definition at line 243 of file RealSpacePositionsOMPTarget.h.

References RealSpacePositionsOMPTarget::is_host_position_changed_, and RealSpacePositionsOMPTarget::RSoA.

Referenced by RealSpacePositionsOMPTarget::donePbyP(), RealSpacePositionsOMPTarget::RealSpacePositionsOMPTarget(), and RealSpacePositionsOMPTarget::setAllParticlePos().

244  {
245  RealType* data = RSoA.data();
246  PRAGMA_OFFLOAD("omp target update to(data[0:RSoA.capacity()*QMCTraits::DIM])")
248  }
VectorSoaContainer< RealType, QMCTraits::DIM, OMPallocator< RealType, PinnedAlignedAllocator< RealType > > > RSoA
particle positions in SoA layout
bool is_host_position_changed_
if true, host position has been changed while the device copy has not been updated.
QMCTraits::RealType RealType

Member Data Documentation

◆ is_host_position_changed_

bool is_host_position_changed_
private

if true, host position has been changed while the device copy has not been updated.

Definition at line 238 of file RealSpacePositionsOMPTarget.h.

Referenced by RealSpacePositionsOMPTarget::donePbyP(), RealSpacePositionsOMPTarget::setOneParticlePos(), and RealSpacePositionsOMPTarget::updateH2D().

◆ is_nw_new_pos_prepared

bool is_nw_new_pos_prepared
private

if true, mw_new_pos has been updated with active positions.

Definition at line 241 of file RealSpacePositionsOMPTarget.h.

Referenced by RealSpacePositionsOMPTarget::donePbyP(), RealSpacePositionsOMPTarget::mw_acceptParticlePos(), and RealSpacePositionsOMPTarget::setAllParticlePos().

◆ mw_mem_handle_

◆ RSoA

◆ RSoA_hostview


The documentation for this class was generated from the following file: