Skip to content

Commit

Permalink
Merge branch 'develop' into matrix_update_engines_direct_inversion
Browse files Browse the repository at this point in the history
  • Loading branch information
PDoakORNL authored Sep 27, 2021
2 parents 78253d2 + 9304f60 commit 555d3d8
Show file tree
Hide file tree
Showing 17 changed files with 371 additions and 400 deletions.
2 changes: 2 additions & 0 deletions src/Optimize/NRCOptimization.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ struct NRCOptimization
Return_t quadstep;
Return_t quadoffset;
Return_t largeQuarticStep;
Return_t stepsize;
bool validFuncVal;
//if tol is absolute, not a percent
bool AbsFuncTol;
Expand All @@ -89,6 +90,7 @@ struct NRCOptimization
quadstep = 0.01;
quadoffset = 0.0;
largeQuarticStep = 2.0;
stepsize = 0.25;
validFuncVal = true;
AbsFuncTol = false;
}
Expand Down
59 changes: 23 additions & 36 deletions src/Particle/DistanceTableData.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,18 @@ class DistanceTableData
using DisplRow = VectorSoaContainer<RealType, DIM>;

protected:
const ParticleSet* Origin;
// FIXME. once DT takes only DynamicCoordinates, change this type as well.
const ParticleSet& origin_;

const int N_sources;
const int N_targets;
const size_t num_sources_;
const size_t num_targets_;

///name of the table
const std::string name_;

/**defgroup SoA data */
/*@{*/
/** distances_[i][j] , [N_targets][N_sources]
/** distances_[i][j] , [num_targets_][num_sources_]
* Note: Derived classes decide if it is a memory view or the actual storage
* For derived AA, only the lower triangle (j<i) data can be accessed safely.
* There is no bound check to protect j>=i terms as the nature of operator[].
Expand All @@ -64,7 +68,7 @@ class DistanceTableData
*/
std::vector<DistRow> distances_;

/** displacements_[N_targets]x[3][N_sources]
/** displacements_[num_targets_]x[3][num_sources_]
* Note: Derived classes decide if it is a memory view or the actual storage
* displacements_[i][j] = r_A2[j] - r_A1[i], the opposite sign of AoS dr
* For derived AA, A1=A2=A, only the lower triangle (j<i) is defined. See the note of distances_
Expand All @@ -82,25 +86,19 @@ class DistanceTableData
///operation modes defined by DTModes
DTModes modes_;

/** set to particle id after move() with prepare_old = true. -1 means not prepared.
* It is intended only for safety checks, not for codepath selection.
*/
int old_prepared_elec_id;

///name of the table
const std::string name_;

public:
///constructor using source and target ParticleSet
DistanceTableData(const ParticleSet& source, const ParticleSet& target, DTModes modes)
: Origin(&source),
N_sources(source.getTotalNum()),
N_targets(target.getTotalNum()),
modes_(modes),
old_prepared_elec_id(-1),
name_(source.getName() + "_" + target.getName())
: origin_(source),
num_sources_(source.getTotalNum()),
num_targets_(target.getTotalNum()),
name_(source.getName() + "_" + target.getName()),
modes_(modes)
{}

/// copy constructor. deleted
DistanceTableData(const DistanceTableData&) = delete;

///virutal destructor
virtual ~DistanceTableData() = default;

Expand All @@ -114,16 +112,16 @@ class DistanceTableData
inline const std::string& getName() const { return name_; }

///returns the reference the origin particleset
const ParticleSet& origin() const { return *Origin; }
const ParticleSet& get_origin() const { return origin_; }

///returns the number of centers
inline IndexType centers() const { return Origin->getTotalNum(); }
inline size_t centers() const { return origin_.getTotalNum(); }

///returns the number of centers
inline IndexType targets() const { return N_targets; }
inline size_t targets() const { return num_targets_; }

///returns the number of source particles
inline IndexType sources() const { return N_sources; }
inline size_t sources() const { return num_sources_; }

/// return multi walker temporary pair distance table data pointer
virtual const RealType* getMultiWalkerTempDataPtr() const
Expand Down Expand Up @@ -312,20 +310,9 @@ class DistanceTableData
* if false, use the data in distance_[iat] and displacements_[iat]
* @return the id of the nearest particle, -1 not found
*/
virtual int get_first_neighbor(IndexType iat, RealType& r, PosType& dr, bool newpos) const
{
throw std::runtime_error("DistanceTableData::get_first_neighbor is not implemented in calling base class");
return 0;
}
virtual int get_first_neighbor(IndexType iat, RealType& r, PosType& dr, bool newpos) const = 0;

inline void print(std::ostream& os)
{
throw std::runtime_error("DistanceTableData::print is not supported");
//os << "Table " << Origin->getName() << std::endl;
//for (int i = 0; i < r_m.size(); i++)
// os << r_m[i] << " ";
//os << std::endl;
}
inline void print(std::ostream& os) { throw std::runtime_error("DistanceTableData::print is not supported"); }

/// initialize a shared resource and hand it to a collection
virtual void createResource(ResourceCollection& collection) const {}
Expand Down
2 changes: 1 addition & 1 deletion src/Particle/ParticleSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ ParticleSet::ParticleSet(const ParticleSet& p)
Collectables = p.Collectables;
//construct the distance tables with the same order
for (int i = 0; i < p.DistTables.size(); ++i)
addTable(p.DistTables[i]->origin(), p.DistTables[i]->getModes());
addTable(p.DistTables[i]->get_origin(), p.DistTables[i]->getModes());
if (p.SK)
{
LRBox = p.LRBox; //copy LRBox
Expand Down
56 changes: 33 additions & 23 deletions src/Particle/SoaDistanceTableAA.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ struct SoaDistanceTableAA : public DTD_BConds<T, D, SC>, public DistanceTableDat
SoaDistanceTableAA(ParticleSet& target)
: DTD_BConds<T, D, SC>(target.Lattice),
DistanceTableData(target, target, DTModes::NEED_TEMP_DATA_ON_HOST),
Ntargets_padded(getAlignedSize<T>(N_targets)),
num_targets_padded_(getAlignedSize<T>(num_targets_)),
#if !defined(NDEBUG)
old_prepared_elec_id_(-1),
#endif
evaluate_timer_(*timer_manager.createTimer(std::string("SoaDistanceTableAA::evaluate_") + target.getName() +
"_" + target.getName(),
timer_level_fine)),
Expand All @@ -57,28 +60,28 @@ struct SoaDistanceTableAA : public DTD_BConds<T, D, SC>, public DistanceTableDat

size_t compute_size(int N) const
{
const size_t N_padded = getAlignedSize<T>(N);
const size_t Alignment = getAlignment<T>();
return (N_padded * (2 * N - N_padded + 1) + (Alignment - 1) * N_padded) / 2;
const size_t num_padded = getAlignedSize<T>(N);
const size_t Alignment = getAlignment<T>();
return (num_padded * (2 * N - num_padded + 1) + (Alignment - 1) * num_padded) / 2;
}

void resize()
{
// initialize memory containers and views
const size_t total_size = compute_size(N_targets);
const size_t total_size = compute_size(num_targets_);
memory_pool_.resize(total_size * (1 + D));
distances_.resize(N_targets);
displacements_.resize(N_targets);
for (int i = 0; i < N_targets; ++i)
distances_.resize(num_targets_);
displacements_.resize(num_targets_);
for (int i = 0; i < num_targets_; ++i)
{
distances_[i].attachReference(memory_pool_.data() + compute_size(i), i);
displacements_[i].attachReference(i, total_size, memory_pool_.data() + total_size + compute_size(i));
}

old_r_.resize(N_targets);
old_dr_.resize(N_targets);
temp_r_.resize(N_targets);
temp_dr_.resize(N_targets);
old_r_.resize(num_targets_);
old_dr_.resize(num_targets_);
temp_r_.resize(num_targets_);
temp_dr_.resize(num_targets_);
}

const DistRow& getOldDists() const override { return old_r_; }
Expand All @@ -88,7 +91,7 @@ struct SoaDistanceTableAA : public DTD_BConds<T, D, SC>, public DistanceTableDat
{
ScopedTimer local_timer(evaluate_timer_);
constexpr T BigR = std::numeric_limits<T>::max();
for (int iat = 1; iat < N_targets; ++iat)
for (int iat = 1; iat < num_targets_; ++iat)
DTD_BConds<T, D, SC>::computeDistances(P.R[iat], P.getCoordinates().getAllParticlePos(), distances_[iat].data(),
displacements_[iat], 0, iat, iat);
}
Expand All @@ -98,29 +101,30 @@ struct SoaDistanceTableAA : public DTD_BConds<T, D, SC>, public DistanceTableDat
{
ScopedTimer local_timer(move_timer_);

old_prepared_elec_id = prepare_old ? iat : -1;

#if !defined(NDEBUG)
old_prepared_elec_id_ = prepare_old ? iat : -1;
#endif
DTD_BConds<T, D, SC>::computeDistances(rnew, P.getCoordinates().getAllParticlePos(), temp_r_.data(), temp_dr_, 0,
N_targets, iat);
num_targets_, iat);
// set up old_r_ and old_dr_ for moves may get accepted.
if (prepare_old)
{
//recompute from scratch
DTD_BConds<T, D, SC>::computeDistances(P.R[iat], P.getCoordinates().getAllParticlePos(), old_r_.data(), old_dr_,
0, N_targets, iat);
0, num_targets_, iat);
old_r_[iat] = std::numeric_limits<T>::max(); //assign a big number
}
}

int get_first_neighbor(IndexType iat, RealType& r, PosType& dr, bool newpos) const override
{
//ensure there are neighbors
assert(N_targets > 1);
assert(num_targets_ > 1);
RealType min_dist = std::numeric_limits<RealType>::max();
int index = -1;
if (newpos)
{
for (int jat = 0; jat < N_targets; ++jat)
for (int jat = 0; jat < num_targets_; ++jat)
if (temp_r_[jat] < min_dist && jat != iat)
{
min_dist = temp_r_[jat];
Expand All @@ -137,7 +141,7 @@ struct SoaDistanceTableAA : public DTD_BConds<T, D, SC>, public DistanceTableDat
min_dist = distances_[iat][jat];
index = jat;
}
for (int jat = iat + 1; jat < N_targets; ++jat)
for (int jat = iat + 1; jat < num_targets_; ++jat)
if (distances_[jat][iat] < min_dist)
{
min_dist = distances_[jat][iat];
Expand Down Expand Up @@ -167,7 +171,7 @@ struct SoaDistanceTableAA : public DTD_BConds<T, D, SC>, public DistanceTableDat
for (int idim = 0; idim < D; ++idim)
std::copy_n(temp_dr_.data(idim), nupdate, displacements_[iat].data(idim));
//copy column
for (size_t i = iat + 1; i < N_targets; ++i)
for (size_t i = iat + 1; i < num_targets_; ++i)
{
distances_[i][iat] = temp_r_[i];
displacements_[i](iat) = -temp_dr_[i];
Expand All @@ -189,7 +193,7 @@ struct SoaDistanceTableAA : public DTD_BConds<T, D, SC>, public DistanceTableDat
}
else
{
assert(old_prepared_elec_id == jat);
assert(old_prepared_elec_id_ == jat);
//copy row
assert(nupdate <= old_r_.size());
std::copy_n(old_r_.data(), nupdate, distances_[jat].data());
Expand All @@ -200,7 +204,13 @@ struct SoaDistanceTableAA : public DTD_BConds<T, D, SC>, public DistanceTableDat

private:
///number of targets with padding
const int Ntargets_padded;
const size_t num_targets_padded_;
#if !defined(NDEBUG)
/** set to particle id after move() with prepare_old = true. -1 means not prepared.
* It is intended only for safety checks, not for codepath selection.
*/
int old_prepared_elec_id_;
#endif
/// timer for evaluate()
NewTimer& evaluate_timer_;
/// timer for move()
Expand Down
Loading

0 comments on commit 555d3d8

Please sign in to comment.