-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest-ecef_to_geodetic-speed.cpp
138 lines (113 loc) · 3.17 KB
/
test-ecef_to_geodetic-speed.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// SPDX-FileCopyrightText: Steven Ward
// SPDX-License-Identifier: OSL-3.0
// test speed of ECEF-to-Geodetic functions
#include "map_func_name_to_func_info.hpp"
#include "read_coords.hpp"
#include <algorithm>
#include <benchmark/benchmark.h>
#include <concepts>
#include <cstdlib>
#include <fmt/ranges.h>
#include <ranges>
#include <string>
#include <thread>
#include <vector>
template <std::floating_point T>
void BM_do_ecef_to_geodetic_test_speed(
benchmark::State& BM_state,
const ecef_to_geodetic_func<T>& func,
const std::vector<ECEF<T>>& ecef_vec)
{
for (auto _ : BM_state)
{
for (const auto& ecef_given : ecef_vec)
{
T lat_rad{};
T lon_rad{};
T ht{};
func(ecef_given.x, ecef_given.y, ecef_given.z, lat_rad, lon_rad, ht);
benchmark::DoNotOptimize(lat_rad);
benchmark::DoNotOptimize(lon_rad);
benchmark::DoNotOptimize(ht);
}
}
BM_state.SetItemsProcessed(BM_state.iterations() * ecef_vec.size());
}
int main(int argc, char** argv)
{
// Copied from /usr/include/benchmark/benchmark.h
benchmark::Initialize(&argc, argv);
/*
// function names may be given in argv
if (benchmark::ReportUnrecognizedArguments(argc, argv))
return 1;
*/
// {{{ determine num_threads
constexpr unsigned int min_threads = 1;
const unsigned int max_threads = (std::thread::hardware_concurrency() != 0) ? std::thread::hardware_concurrency() : min_threads;
// https://en.wikipedia.org/wiki/Elvis_operator
//const unsigned int max_threads = std::thread::hardware_concurrency() ?: min_threads;
unsigned int num_threads;
try
{
num_threads = static_cast<unsigned int>(std::stoi(std::getenv("NUM_THREADS")));
}
catch (...)
{
num_threads = max_threads;
}
num_threads = std::clamp(num_threads, min_threads, max_threads);
/*
if (num_threads > min_threads)
// Don't use all the cores
--num_threads;
*/
// }}}
std::vector<std::string> func_names;
if (argc > 1)
{
// use given functions
for (int i = 1; i < argc; ++i)
{
func_names.emplace_back(argv[i]);
}
// validate func_names
for (const auto& func_name : func_names)
{
// verify the given function names are valid
if (!map_func_name_to_func_info.contains(func_name))
{
fmt::println(stderr, "Error: \"{}\" is not a valid function name.", func_name);
fmt::println(stderr, "Valid function names are:");
const auto keys = std::views::keys(map_func_name_to_func_info);
fmt::println(stderr, " {}", fmt::join(keys, "\n "));
return EXIT_FAILURE;
}
}
}
else
{
// use all functions
for (const auto& [func_name, ignore] : map_func_name_to_func_info)
{
func_names.push_back(func_name);
}
}
std::vector<ECEF<double>> ecef_vec;
read_coords_ecef(ecef_vec);
for (const auto& func_name : func_names)
{
const auto& func_info = map_func_name_to_func_info.at(func_name);
if (num_threads == 1)
benchmark::RegisterBenchmark(func_info.display_name,
&BM_do_ecef_to_geodetic_test_speed<double>,
func_info.func, ecef_vec);
else
benchmark::RegisterBenchmark(func_info.display_name,
&BM_do_ecef_to_geodetic_test_speed<double>,
func_info.func, ecef_vec)->Threads(num_threads);
}
benchmark::RunSpecifiedBenchmarks();
benchmark::Shutdown();
return 0;
}