Skip to content

Commit

Permalink
Introduce shorter numeric type aliases and resolve implicit conversio…
Browse files Browse the repository at this point in the history
…n issues
  • Loading branch information
ewarchul committed Jun 29, 2024
1 parent 3aca2bf commit ce49dcf
Show file tree
Hide file tree
Showing 40 changed files with 237 additions and 161 deletions.
2 changes: 1 addition & 1 deletion include/cecxx/benchmark/detail/cec2017/consts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "cecxx/benchmark/detail/types.hpp"

namespace cecxx::benchmark::detail::cec2017 {
const auto CEC2017_OFFSET = std::unordered_map<detail::fn_num, double>{
const auto CEC2017_OFFSET = std::unordered_map<detail::fn_num, f64>{
{1, 100.0}, {2, 200.0}, {3, 300.0}, {4, 400.0}, {5, 500.0}, {6, 600.0}, {7, 700.0}, {8, 800.0},
{9, 900.0}, {10, 1000.0}, {11, 1100.0}, {12, 1200.0}, {13, 1300.0}, {14, 1400.0}, {15, 1500.0}, {16, 1600.0},
{17, 1700.0}, {18, 1800.0}, {19, 1900.0}, {20, 2000.0}, {21, 2100.0}, {22, 2200.0}, {23, 2300.0}, {24, 2400.0},
Expand Down
6 changes: 3 additions & 3 deletions include/cecxx/benchmark/detail/cec2017/evaluate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace cecxx::benchmark::detail::cec2017 {

auto dispatch_problem(problem_context_view ctx, const int fn, const auto& input) {
auto dispatch_problem(problem_context_view ctx, const u8 fn, const auto& input) {
switch (fn) {
case 1:
return bent_cigar(input, ctx);
Expand Down Expand Up @@ -75,14 +75,14 @@ auto dispatch_problem(problem_context_view ctx, const int fn, const auto& input)
throw std::runtime_error{"Unknown CEC problem"};
}

auto evaluate(problem_context_view ctx, const int fn_num, const auto& input) {
auto evaluate(problem_context_view ctx, const u8 fn_num, const auto& input) {
const auto ncol = input.size();
const auto nrow = input.at(0).size();
if (not cec2017::VALID_DIMENSIONS.contains(nrow)) {
throw std::runtime_error{"Invalid problem dimension."};
}

auto output = std::vector<double>(ncol);
auto output = std::vector<f64>(ncol);
for (auto col{0u}; col < output.size(); ++col) {
output[col] = dispatch_problem(ctx, fn_num, input.at(col)) + cec2017::CEC2017_OFFSET.at(fn_num);
}
Expand Down
23 changes: 12 additions & 11 deletions include/cecxx/benchmark/detail/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,32 @@

#include "cecxx/benchmark/detail/types.hpp"
#include "cecxx/benchmark/edition.hpp"
#include "cecxx/types.hpp"

namespace cecxx::benchmark::detail {
template <typename Number> using context_table_t = std::unordered_map<fn_num, table_data<Number>>;

struct problem_context_view {
std::span<const double> shift{};
std::span<const double> rotate{};
std::span<const int> shuffle{};
std::span<const f64> shift{};
std::span<const f64> rotate{};
std::span<const i64> shuffle{};
};

class context_t {
public:
context_t(const cec_edition_t edition, const std::filesystem::path& storage, const int dim);
context_t(const cec_edition_t edition, const std::filesystem::path& storage, const u8 dim);

auto shift(const int fn) const { return shift_.at(fn); }
auto rotate(const int fn) const { return rotate_.at(fn); }
auto shuffle(const int fn) const { return shuffle_.at(fn); }
auto problem_context(const int fn) const {
auto shift(const u8 fn) const { return shift_.at(fn); }
auto rotate(const u8 fn) const { return rotate_.at(fn); }
auto shuffle(const u8 fn) const { return shuffle_.at(fn); }
auto problem_context(const u8 fn) const {
return problem_context_view{shift_.at(fn), rotate_.at(fn), shuffle_.at(fn)};
}

private:
context_table_t<double> rotate_{};
context_table_t<int> shuffle_{};
context_table_t<double> shift_{};
context_table_t<f64> rotate_{};
context_table_t<i64> shuffle_{};
context_table_t<f64> shift_{};
};

} // namespace cecxx::benchmark::detail
8 changes: 4 additions & 4 deletions include/cecxx/benchmark/detail/func_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

namespace cecxx::benchmark::detail {

using stateless_eval_func = double (*)(std::span<const double>);
using local_statefull_eval_func = double (*)(std::span<const double>, problem_context_view, affine_mask_t);
using nonlocal_statefull_eval_func = double (*)(std::span<const double>, problem_context_view, affine_mask_t,
std::vector<double>);
using stateless_eval_func = double (*)(std::span<const f64>);
using local_statefull_eval_func = double (*)(std::span<const f64>, problem_context_view, affine_mask_t);
using nonlocal_statefull_eval_func = double (*)(std::span<const f64>, problem_context_view, affine_mask_t,
std::vector<f64>);

} // namespace cecxx::benchmark::detail
4 changes: 2 additions & 2 deletions include/cecxx/benchmark/detail/geom.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

namespace cecxx::benchmark::detail {

inline auto apply_geom_transformations(std::span<const double> input, problem_context_view ctx, affine_mask_t mask,
double scale_mul = 1.0) {
inline auto apply_geom_transformations(std::span<const f64> input, problem_context_view ctx, affine_mask_t mask,
f64 scale_mul = 1.0) {
const auto nrow = input.size();
auto y_out = std::vector<double>(nrow);
auto z_out = std::vector<double>(nrow);
Expand Down
16 changes: 8 additions & 8 deletions include/cecxx/benchmark/detail/legacy/affine_transformation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
#include "cecxx/benchmark/detail/types.hpp"

namespace cecxx::benchmark::detail {
void shufflefunc(std::span<const double> input, std::span<double> output, std::span<const int> shuffle_vec);
void shiftfunc(std::span<const double> input, std::span<double> output, std::span<const double> shift_vec);
void rotatefunc(std::span<const double> input, std::span<double> output, std::span<const double> rotate_mat);
void sr_func(std::span<const double> input, std::span<double> sr_x, std::span<const double> shit_vec,
std::span<const double> rot_mat, const double sh_rate, const do_affine_trans shift,
const do_affine_trans rotate, std::span<double> output);
void cf_cal(std::span<const double> input, std::span<double> output, std::span<const double> shift_vec,
std::span<const double> delta, std::span<const double> bias, std::span<double> fit, int cf_num);
void shufflefunc(std::span<const f64> input, std::span<f64> output, std::span<const i64> shuffle_vec);
void shiftfunc(std::span<const f64> input, std::span<f64> output, std::span<const f64> shift_vec);
void rotatefunc(std::span<const f64> input, std::span<f64> output, std::span<const f64> rotate_mat);
void sr_func(std::span<const f64> input, std::span<f64> sr_x, std::span<const f64> shit_vec,
std::span<const f64> rot_mat, const f64 sh_rate, const do_affine_trans shift, const do_affine_trans rotate,
std::span<f64> output);
void cf_cal(std::span<const f64> input, std::span<f64> output, std::span<const f64> shift_vec,
std::span<const f64> delta, std::span<const f64> bias, std::span<f64> fit, u8 cf_num);
} // namespace cecxx::benchmark::detail
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

namespace cecxx::functions::multimodal::legacy {

inline auto bi_rastrigin(std::span<const double> input, benchmark::detail::problem_context_view ctx,
benchmark::detail::affine_mask_t mask) -> double {
inline auto bi_rastrigin(std::span<const f64> input, benchmark::detail::problem_context_view ctx,
benchmark::detail::affine_mask_t mask) -> f64 {
using namespace cecxx::benchmark;
const auto nrow = input.size();
auto y = std::vector<double>(nrow);
auto z = std::vector<double>(nrow);
auto tmpx = std::vector<double>(nrow);
auto y = std::vector<f64>(nrow);
auto z = std::vector<f64>(nrow);
auto tmpx = std::vector<f64>(nrow);

if (to_underlying(mask.shift) == 1) {
detail::shiftfunc(input, y, ctx.shift);
Expand All @@ -41,11 +41,11 @@ inline auto bi_rastrigin(std::span<const double> input, benchmark::detail::probl
tmpx[i] += mu0;
}

double tmp{};
double tmp1{};
double tmp2{};
f64 tmp{};
f64 tmp1{};
f64 tmp2{};
constexpr auto d = 1.0;
auto s = 1.0 - 1.0 / (2.0 * std::pow(static_cast<double>(nrow) + 20.0, 0.5) - 8.2);
auto s = 1.0 - 1.0 / (2.0 * std::pow(static_cast<f64>(nrow) + 20.0, 0.5) - 8.2);
auto mu1 = -std::pow((mu0 * mu0 - d) / s, 0.5);
for (auto i = 0u; i < nrow; i++) {
tmp = tmpx[i] - mu0;
Expand All @@ -54,7 +54,7 @@ inline auto bi_rastrigin(std::span<const double> input, benchmark::detail::probl
tmp2 += tmp * tmp;
}
tmp2 *= s;
tmp2 += d * static_cast<double>(nrow);
tmp2 += d * static_cast<f64>(nrow);
tmp = 0.0;
auto output{0.0};

Expand All @@ -68,7 +68,7 @@ inline auto bi_rastrigin(std::span<const double> input, benchmark::detail::probl
} else {
output = tmp2;
}
output += 10.0 * (static_cast<double>(nrow) - tmp);
output += 10.0 * (static_cast<f64>(nrow) - tmp);
} else {
for (auto i = 0u; i < nrow; i++) {
tmp += std::cos(2.0 * M_PI * z[i]);
Expand All @@ -78,10 +78,10 @@ inline auto bi_rastrigin(std::span<const double> input, benchmark::detail::probl
} else {
output = tmp2;
}
output += 10.0 * (static_cast<double>(nrow) - tmp);
output += 10.0 * (static_cast<f64>(nrow) - tmp);
}

return output;
}

} // namespace cecxx::problem::multimodal::legacy
} // namespace cecxx::functions::multimodal::legacy
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@
#include "cecxx/benchmark/detail/legacy/affine_transformation.hpp"

namespace cecxx::functions::multimodal::legacy {
inline auto schaffer(std::span<const double> input, benchmark::detail::problem_context_view ctx,
benchmark::detail::affine_mask_t mask, std::vector<double> acc = {}) -> double {
inline auto schaffer(std::span<const f64> input, benchmark::detail::problem_context_view ctx,
benchmark::detail::affine_mask_t mask, std::vector<f64> acc = {}) -> f64 {
const auto nrow = input.size();
std::vector<double> ys{};
std::vector<f64> ys{};
if (acc.empty()) {
ys.resize(nrow);
} else {
ys = acc;
}
auto z = std::vector<double>(nrow);
auto z = std::vector<f64>(nrow);

auto output{0.0};
sr_func(input, z, ctx.shift, ctx.rotate, 1.0, mask.shift, mask.rot, ys);
double tmp{};
f64 tmp{};
for (auto i = 0u; i < nrow - 1; ++i) {
z[i] = std::pow(ys[i] * ys[i] + ys[i + 1] * ys[i + 1], 0.5);
tmp = std::sin(50.0 * std::pow(z[i], 0.2));
output += std::pow(z[i], 0.5) + std::pow(z[i], 0.5) * tmp * tmp;
}

return output * output / (static_cast<double>(nrow) - 1) / (static_cast<double>(nrow) - 1);
return output * output / (static_cast<f64>(nrow) - 1) / (static_cast<f64>(nrow) - 1);
}

} // namespace cecxx::problem::multimodal::legacy
} // namespace cecxx::functions::multimodal::legacy
4 changes: 2 additions & 2 deletions include/cecxx/benchmark/detail/problem/hybrid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ auto calc_hybrid_chunks(std::ranges::range auto&& mix_ratios, const std::integra
auto chunk_size = std::vector<double>(fn_num);
double acc{};
for (auto i = 0u; i < fn_num - 1; ++i) {
chunk_size[i] = std::ceil(mix_ratios[i] * dim);
chunk_size[i] = std::ceil(mix_ratios[i] * static_cast<double>(dim));
acc += chunk_size[i];
}
chunk_size[fn_num - 1] = dim - acc;
chunk_size[fn_num - 1] = static_cast<double>(dim) - acc;
auto chunk_offset = std::vector<double>(fn_num);
for (auto i = 1u; i < fn_num; i++) {
chunk_offset[i] = chunk_offset[i - 1] + chunk_size[i - 1];
Expand Down
7 changes: 5 additions & 2 deletions include/cecxx/benchmark/detail/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
#include <type_traits>
#include <vector>

#include "cecxx/types.hpp"

namespace cecxx::benchmark::detail {

template <typename Enum> constexpr auto to_underlying(Enum e) noexcept -> std::underlying_type_t<Enum> {
return static_cast<std::underlying_type_t<Enum>>(e);
}

using fn_num = int;
using fn_num = u8;

template <typename T> using table_data = std::vector<T>;

enum class table_type_t { rotate, shift, shuffle };
Expand All @@ -20,7 +23,7 @@ struct affine_mask_t {
do_affine_trans rot{};
do_affine_trans shift{};
do_affine_trans shuffle{};
double rate{1.0};
f64 rate{1.0};
};

} // namespace cecxx::benchmark::detail
2 changes: 1 addition & 1 deletion include/cecxx/benchmark/detail/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "cecxx/benchmark/detail/cec2017/evaluate.hpp"

namespace cecxx::benchmark::detail {
auto dispatch_cec(cec_edition_t edition, problem_context_view ctx, const int fn_num, const auto& input) {
auto dispatch_cec(cec_edition_t edition, problem_context_view ctx, const u8 fn_num, const auto& input) {
switch (edition) {
case cec_edition_t::cec2017:
return cec2017::evaluate(ctx, fn_num, input);
Expand Down
6 changes: 3 additions & 3 deletions include/cecxx/benchmark/evaluator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ namespace cecxx::benchmark {

class evaluator {
public:
evaluator(const cec_edition_t edition, const int dimension,
evaluator(const cec_edition_t edition, const u8 dimension,
const std::filesystem::path& storage = detail::default_benchmark_datadir());

auto operator()(const int fn, const matrix<double> auto& input) const -> std::vector<double> {
auto operator()(const u8 fn, const matrix<double> auto& input) const -> std::vector<double> {
return detail::dispatch_cec(edition_, ctx_.problem_context(fn), fn, input);
}

private:
int dim_{};
u8 dim_{};
detail::context_t ctx_;
cec_edition_t edition_{};
};
Expand Down
10 changes: 10 additions & 0 deletions include/cecxx/executor/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
//
// auto main() -> int {
//
//
// auto cec17_runner = cecxx::benchmark::runner(cec_2017,
//
//
// return EXIT_SUCCESS;
// }
12 changes: 7 additions & 5 deletions include/cecxx/functions/multimodal/ackley.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
#include <cmath>
#include <span>

#include "cecxx/types.hpp"

#define E 2.7182818284590452353602874713526625

namespace cecxx::functions::multimodal {
constexpr auto ackley(std::span<const double> input) -> double {
constexpr auto ackley(std::span<const f64> input) -> f64 {
const auto nrow = input.size();
double sum1{};
double sum2{};
f64 sum1{};
f64 sum2{};
for (auto i = 0u; i < nrow; ++i) {
sum1 += input[i] * input[i];
sum2 += std::cos(2.0 * M_PI * input[i]);
}
sum1 = -0.2 * std::sqrt(sum1 / static_cast<double>(nrow));
sum2 /= static_cast<double>(nrow);
sum1 = -0.2 * std::sqrt(sum1 / static_cast<f64>(nrow));
sum2 /= static_cast<f64>(nrow);
return E - 20.0 * std::exp(sum1) - std::exp(sum2) + 20.0;
}
} // namespace cecxx::functions::multimodal
4 changes: 3 additions & 1 deletion include/cecxx/functions/multimodal/diff_powers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
#include <cmath>
#include <span>

#include "cecxx/types.hpp"

namespace cecxx::functions::multimodal {
constexpr auto diff_powers(std::span<const double> input) -> double {
constexpr auto diff_powers(std::span<const f64> input) -> f64 {
auto output{0.0};
for (auto i = 0u; i < input.size(); i++) {
output += std::pow(std::fabs(input[i]), 2 + 4 * i / (input.size() - 1));
Expand Down
4 changes: 3 additions & 1 deletion include/cecxx/functions/multimodal/discus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
#include <cmath>
#include <span>

#include "cecxx/types.hpp"

namespace cecxx::functions::multimodal {
inline auto discus(std::span<const double> input) -> double {
inline auto discus(std::span<const f64> input) -> f64 {
auto output = std::pow(10.0, 6.0) * input[0] * input[0];
for (auto i = 1u; i < input.size(); i++) {
output += input[i] * input[i];
Expand Down
6 changes: 4 additions & 2 deletions include/cecxx/functions/multimodal/escaffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
#include <cmath>
#include <span>

#include "cecxx/types.hpp"

namespace cecxx::functions::multimodal {
constexpr auto escaffer(std::span<const double> input) -> double {
constexpr auto escaffer(std::span<const f64> input) -> f64 {
const auto nrow = input.size();
auto output{0.0};
double temp1{}, temp2{};
f64 temp1{}, temp2{};
for (auto i = 0u; i < nrow - 1; i++) {
temp1 = std::sin(sqrt(input[i] * input[i] + input[i + 1] * input[i + 1]));
temp1 = temp1 * temp1;
Expand Down
8 changes: 5 additions & 3 deletions include/cecxx/functions/multimodal/grie_rosen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
#include <cmath>
#include <span>

#include "cecxx/types.hpp"

namespace cecxx::functions::multimodal {
constexpr auto grie_rosen(std::span<const double> input) -> double {
constexpr auto grie_rosen(std::span<const f64> input) -> f64 {
const auto nrow = input.size();
double temp{}, tmp1{};
f64 temp{}, tmp1{};
auto output{0.0};
for (auto i = 0u; i < nrow - 1; i++) {
tmp1 = (input[i] + 1.0) * (input[i] + 1.0) - (input[i + 1] + 1.0);
temp = 100.0 * tmp1 * tmp1 + input[i] * input[i];
output += (temp * temp) / 4000.0 - std::cos(temp) + 1.0;
}
tmp1 = (input[nrow - 1] + 1.0) * (input[nrow - 1] + 1.0) - (input[0] + 1.0);
temp = 100.0 * tmp1 * tmp1 + input[nrow-1] * input[nrow -1];
temp = 100.0 * tmp1 * tmp1 + input[nrow - 1] * input[nrow - 1];
output += (temp * temp) / 4000.0 - std::cos(temp) + 1.0;

return output;
Expand Down
Loading

0 comments on commit ce49dcf

Please sign in to comment.