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 c37a6c4
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/algorithm/nd_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,10 @@ 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)
{
GAPP_BARRIER();
FitnessVector fvec(popsize);
std::transform(first, last, fvec.begin(), detail::element_at(obj));

Expand All @@ -210,10 +212,13 @@ namespace gapp::algorithm::dtl
}
});
});
GAPP_BARRIER();
});
GAPP_BARRIER();

std::for_each(GAPP_EXEC, detail::iota_iterator(0_sz), detail::iota_iterator(popsize), [&](size_t row) noexcept
{
GAPP_BARRIER();
dmat(row, row).store(NONMAXIMAL, std::memory_order_relaxed); // diagonal is all nonmax

for (size_t col = row + 1; col < popsize; col++)
Expand All @@ -224,7 +229,9 @@ namespace gapp::algorithm::dtl
dmat(col, row).store(NONMAXIMAL, std::memory_order_relaxed);
}
}
GAPP_BARRIER();
});
GAPP_BARRIER();

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

sol_info_.resize(last - first);

GAPP_BARRIER();
std::for_each(GAPP_EXEC, pfirst, plast, [&](const FrontInfo& sol)
{
GAPP_BARRIER();
const FitnessVector fnorm = normalizeFitnessVec(first[sol.idx], ideal_point_, nadir_point_);

auto idistance = [&](const auto& line) { return std::inner_product(fnorm.begin(), fnorm.end(), line.begin(), 0.0); };
Expand All @@ -249,7 +251,9 @@ 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();
});
GAPP_BARRIER();
}

inline bool NSGA3::Impl::nichedCompare(size_t lhs, size_t rhs) const noexcept
Expand Down
4 changes: 4 additions & 0 deletions src/algorithm/reference_lines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,17 @@ 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
{
GAPP_BARRIER();
const double dist = math::euclideanDistanceSq(candidate, points.back());
return std::min(current_min, dist);
GAPP_BARRIER();
});
GAPP_BARRIER();
}

return points;
Expand Down
22 changes: 17 additions & 5 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));
std::for_each(GAPP_EXEC, population_.begin(), population_.end(), [this](Candidate<T>& sol) { evaluate(sol); });
GAPP_BARRIER();
std::for_each(GAPP_EXEC, population_.begin(), population_.end(), [this](Candidate<T>& sol) { GAPP_BARRIER(); evaluate(sol); GAPP_BARRIER(); });
GAPP_BARRIER();
fitness_matrix_ = detail::toFitnessMatrix(population_);
if (keep_all_optimal_sols_) solutions_ = detail::findParetoFront(population_);

Expand Down Expand Up @@ -468,8 +470,10 @@ namespace gapp
sol.fitness = (*fitness_function_)(sol.chromosome);
sol.is_evaluated = true;

std::atomic_ref num_evals{ num_fitness_evals_ };
num_evals.fetch_add(1_sz, std::memory_order_acq_rel);
{
std::atomic_ref num_evals{ num_fitness_evals_ };
num_evals.fetch_add(1_sz, std::memory_order_acq_rel);
}
}

GAPP_ASSERT(hasValidFitness(sol));
Expand Down Expand Up @@ -504,27 +508,35 @@ 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]
{
GAPP_BARRIER();
const auto& parent1 = select();
const auto& parent2 = select();

return crossover(parent1, parent2);
auto children = crossover(parent1, parent2);
GAPP_BARRIER();
return std::move(children);
});
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)
{
GAPP_BARRIER();
mutate(child);
repair(child);
evaluate(child);
GAPP_BARRIER();
});
GAPP_BARRIER();

updatePopulation(std::move(children));
if (keep_all_optimal_sols_) updateOptimalSolutions(solutions_, population_);
Expand Down
4 changes: 4 additions & 0 deletions src/metrics/pop_stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,19 @@ 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)
{
GAPP_BARRIER();
const auto point = front[idx];
const FitnessMatrix rest = { front.begin() + idx + 1, front.end() };

const double exclusive_hypervolume = exclusiveHypervolume(point, rest, ref_point);

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

return hypervolume.load(std::memory_order_acquire);
}
Expand Down
4 changes: 4 additions & 0 deletions src/population/population.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,10 @@ 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
{
GAPP_BARRIER();
for (size_t j = 0; j < rhs.size(); j++)
{
const Dominance rhs_state_j = rhs_state[j].load(std::memory_order_acquire);
Expand Down Expand Up @@ -145,7 +147,9 @@ namespace gapp::detail
}
// comp == 0 --> both are OPTIMAL or DOMINATED, can't know
}
GAPP_BARRIER();
});
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 c37a6c4

Please sign in to comment.