Skip to content

Commit

Permalink
temp2
Browse files Browse the repository at this point in the history
  • Loading branch information
KRM7 committed Sep 8, 2023
1 parent 5ef1778 commit 52a6245
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/algorithm/nd_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ namespace gapp::algorithm::dtl
const size_t popsize = std::distance(first, last);
DominanceMatrix dmat(popsize, popsize /*, MAXIMAL */);

GAPP_BARRIER();
std::for_each(GAPP_EXEC, detail::iota_iterator(0_sz), detail::iota_iterator(first->size()), [&](size_t obj)
{
FitnessVector fvec(popsize);
Expand All @@ -211,6 +212,7 @@ namespace gapp::algorithm::dtl
});
});
});
GAPP_BARRIER();

std::for_each(GAPP_EXEC, detail::iota_iterator(0_sz), detail::iota_iterator(popsize), [&](size_t row) noexcept
{
Expand All @@ -225,6 +227,7 @@ namespace gapp::algorithm::dtl
}
}
});
GAPP_BARRIER();

return dmat;
}
Expand Down
2 changes: 2 additions & 0 deletions src/algorithm/nsga3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ namespace gapp::algorithm

sol_info_.resize(last - first);

GAPP_BARRIER();
std::for_each(GAPP_EXEC, pfirst, plast, [&](const FrontInfo& sol)
{
const FitnessVector fnorm = normalizeFitnessVec(first[sol.idx], ideal_point_, nadir_point_);
Expand All @@ -250,6 +251,7 @@ namespace gapp::algorithm
sol_info_[sol.idx].ref_idx = std::distance(ref_lines_.begin(), closest);
sol_info_[sol.idx].ref_dist = math::perpendicularDistanceSq(*closest, fnorm);
});
GAPP_BARRIER();
}

inline bool NSGA3::Impl::nichedCompare(size_t lhs, size_t rhs) const noexcept
Expand Down
2 changes: 2 additions & 0 deletions src/algorithm/reference_lines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,15 @@ namespace gapp::algorithm::reflines
std::swap(min_distances[idx], min_distances.back());
min_distances.pop_back();

GAPP_BARRIER();
/* Calc the distance of each candidate to the closest ref point. */
std::transform(GAPP_EXEC, candidate_points.begin(), candidate_points.end(), min_distances.begin(), min_distances.begin(),
[&](const Point& candidate, double current_min) noexcept
{
const double dist = math::euclideanDistanceSq(candidate, points.back());
return std::min(current_min, dist);
});
GAPP_BARRIER();
}

return points;
Expand Down
6 changes: 6 additions & 0 deletions src/core/ga_base.impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,9 @@ namespace gapp
/* Create and evaluate the initial population of the algorithm. */
num_objectives_ = findNumberOfObjectives();
population_ = generatePopulation(population_size_, std::move(initial_population));
GAPP_BARRIER();
std::for_each(GAPP_EXEC, population_.begin(), population_.end(), [this](Candidate<T>& sol) { evaluate(sol); });
GAPP_BARRIER();
fitness_matrix_ = detail::toFitnessMatrix(population_);
if (keep_all_optimal_sols_) solutions_ = detail::findParetoFront(population_);

Expand Down Expand Up @@ -504,6 +506,7 @@ namespace gapp
std::vector<CandidatePair<T>> child_pairs(num_children / 2);

prepareSelections();
GAPP_BARRIER();
std::generate(GAPP_EXEC, child_pairs.begin(), child_pairs.end(),
[this]
{
Expand All @@ -512,19 +515,22 @@ namespace gapp

return crossover(parent1, parent2);
});
GAPP_BARRIER();

auto children = detail::flatten(std::move(child_pairs));

/* If the population size is odd, one too many child candidates were generated by the crossovers. */
if (children.size() > population_size_) children.pop_back();

GAPP_BARRIER();
std::for_each(GAPP_EXEC, children.begin(), children.end(),
[this](Candidate<T>& child)
{
mutate(child);
repair(child);
evaluate(child);
});
GAPP_BARRIER();

updatePopulation(std::move(children));
if (keep_all_optimal_sols_) updateOptimalSolutions(solutions_, population_);
Expand Down
2 changes: 2 additions & 0 deletions src/metrics/pop_stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ namespace gapp::detail
const FitnessMatrix front = uniqueSortedParetoFront(fmat);

std::atomic<double> hypervolume = 0.0;
GAPP_BARRIER();
std::for_each(GAPP_EXEC, detail::iota_iterator(0_sz), detail::iota_iterator(front.size()), [&](size_t idx)
{
const auto point = front[idx];
Expand All @@ -163,6 +164,7 @@ namespace gapp::detail

hypervolume.fetch_add(exclusive_hypervolume, std::memory_order_acq_rel);
});
GAPP_BARRIER();

return hypervolume.load(std::memory_order_acquire);
}
Expand Down
2 changes: 2 additions & 0 deletions src/population/population.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ namespace gapp::detail
std::vector<Dominance> lhs_state(lhs.size());
std::vector<std::atomic<Dominance>> rhs_state(rhs.size());

GAPP_BARRIER();
std::for_each(GAPP_EXEC, iota_iterator(0_sz), iota_iterator(lhs.size()), [&](size_t i) noexcept
{
for (size_t j = 0; j < rhs.size(); j++)
Expand Down Expand Up @@ -146,6 +147,7 @@ namespace gapp::detail
// comp == 0 --> both are OPTIMAL or DOMINATED, can't know
}
});
GAPP_BARRIER();

Candidates<T> optimal_solutions;
optimal_solutions.reserve(lhs.size() + rhs.size());
Expand Down
5 changes: 5 additions & 0 deletions src/utility/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@
#endif


#include <atomic>
inline std::atomic_bool global_barrier;
#define GAPP_BARRIER() std::ignore = global_barrier.exchange(true, std::memory_order_acq_rel)


namespace gapp
{
constexpr std::size_t operator ""_sz(unsigned long long arg) noexcept
Expand Down

0 comments on commit 52a6245

Please sign in to comment.