forked from aloistr/swisseph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparabola_wrapper.cpp
More file actions
150 lines (130 loc) · 5.03 KB
/
parabola_wrapper.cpp
File metadata and controls
150 lines (130 loc) · 5.03 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
145
146
147
148
149
150
// parabola_wrapper.cpp
// Thread-safe, batch-optimized Swiss Ephemeris parallel executor
#include "parabola_wrapper.h"
#include "swephexp.h"
#include <vector>
#include <string>
#include <future>
#include <mutex>
#include <thread>
#include <queue>
#include <condition_variable>
#include <cstring>
#include <iostream>
// Public API types for use in the rest of Parabola
PlanetBatchResult compute_batch(const PlanetBatchRequest& batch);
size_t g_parabola_thread_count = 1; // default fallback if no autotune is run
class ThreadPool {
public:
ThreadPool(size_t num_threads) {
for (size_t i = 0; i < num_threads; ++i) {
workers.emplace_back([this] {
while (true) {
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(queue_mutex);
condition.wait(lock, [this] { return stop || !tasks.empty(); });
if (stop && tasks.empty()) return;
task = std::move(tasks.front());
tasks.pop();
}
task();
}
});
}
}
template<class F>
auto enqueue(F&& f) -> std::future<typename std::invoke_result_t<F>> {
using return_type = typename std::invoke_result_t<F>;
auto task = std::make_shared<std::packaged_task<return_type()>>(std::forward<F>(f));
std::future<return_type> res = task->get_future();
{
std::unique_lock<std::mutex> lock(queue_mutex);
tasks.emplace([task]() { (*task)(); });
}
condition.notify_one();
return res;
}
~ThreadPool() {
{
std::unique_lock<std::mutex> lock(queue_mutex);
stop = true;
}
condition.notify_all();
for (std::thread &worker : workers)
worker.join();
}
private:
std::vector<std::thread> workers;
std::queue<std::function<void()>> tasks;
std::mutex queue_mutex;
std::condition_variable condition;
bool stop = false;
};
size_t autotune_threads(const std::vector<PlanetRequest>& requests) {
size_t best = 1;
double best_throughput = 0;
size_t trial = 1;
std::vector<PlanetRequest> slice = requests;
while (true) {
try {
auto start = std::chrono::high_resolution_clock::now();
ThreadPool pool(trial);
std::vector<std::future<void>> futures;
size_t slice_size = std::max<size_t>(1, slice.size() / trial);
for (size_t i = 0; i < slice.size(); i += slice_size) {
size_t end = std::min(slice.size(), i + slice_size);
futures.push_back(pool.enqueue([batch = std::vector<PlanetRequest>(slice.begin() + i, slice.begin() + end)]() {
for (const auto& req : batch) {
double xx[6];
char serr[256] = {0};
swe_calc_ut(req.jd, req.ipl, SEFLG_SPEED, xx, serr);
}
}));
}
for (auto& f : futures) f.get();
auto end = std::chrono::high_resolution_clock::now();
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
double throughput = (requests.size() / (double)ms) * 1000.0;
std::cout << trial << " threads: " << ms << " ms => " << throughput << " planets/sec\n";
if (throughput > best_throughput) {
best_throughput = throughput;
best = trial;
++trial;
} else {
break; // performance dropped, stop here
}
} catch (...) {
break; // thread creation failed, cap reached
}
}
return best;
}
PlanetBatchResult compute_batch(const PlanetBatchRequest& batch) {
g_parabola_thread_count = autotune_threads(batch.requests);
ThreadPool pool(g_parabola_thread_count);
std::vector<std::future<PlanetBatchResult>> futures;
size_t slice_size = std::max<size_t>(1, batch.requests.size() / g_parabola_thread_count);
for (size_t i = 0; i < batch.requests.size(); i += slice_size) {
size_t end = std::min(batch.requests.size(), i + slice_size);
std::vector<PlanetRequest> slice(batch.requests.begin() + i, batch.requests.begin() + end);
futures.emplace_back(pool.enqueue([slice]() -> PlanetBatchResult {
PlanetBatchResult result;
for (const auto& req : slice) {
PlanetResult r = {.ipl = req.ipl};
std::memset(r.serr, 0, sizeof(r.serr));
int ret = swe_calc_ut(req.jd, req.ipl, SEFLG_SPEED, r.xx, r.serr);
r.errcode = ret;
result.results.push_back(r);
}
return result;
}));
}
PlanetBatchResult merged;
for (auto& fut : futures) {
PlanetBatchResult r = fut.get();
merged.results.insert(merged.results.end(), r.results.begin(), r.results.end());
}
swe_close();
return merged;
}