-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest-ecef_to_geodetic-speed.cpp
More file actions
144 lines (116 loc) · 3.78 KB
/
test-ecef_to_geodetic-speed.cpp
File metadata and controls
144 lines (116 loc) · 3.78 KB
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
139
140
141
142
143
144
// SPDX-FileCopyrightText: Steven Ward
// SPDX-License-Identifier: MPL-2.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)
{
T lat_rad{};
T lon_rad{};
T ht{};
size_t i = 0;
const size_t n = ecef_vec.size();
for (auto _ : BM_state)
{
func(ecef_vec[i].x, ecef_vec[i].y, ecef_vec[i].z, lat_rad, lon_rad, ht);
benchmark::DoNotOptimize(lat_rad);
benchmark::DoNotOptimize(lon_rad);
benchmark::DoNotOptimize(ht);
++i;
if (i == n)
i = 0;
}
}
int
main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
{
// Copied from benchmark.h
benchmark::MaybeReenterWithoutASLR(argc, argv);
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::max(min_threads, std::thread::hardware_concurrency());
// 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 = min_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;
}