Vince's CSV Parser
Loading...
Searching...
No Matches
data_frame_executor.hpp
1#pragma once
2
3#include <utility>
4
5#include "../common.hpp"
6#include "../parallel/indexed_task_pool.hpp"
7
8#if CSV_ENABLE_THREADS
9#include <thread>
10#endif
11
12namespace csv {
14 public:
15 explicit DataFrameExecutor(size_t worker_count = default_worker_count())
16 : task_pool_(worker_count) {}
17
18 DataFrameExecutor(const DataFrameExecutor&) = delete;
19 DataFrameExecutor& operator=(const DataFrameExecutor&) = delete;
20
22
23 size_t worker_count() const noexcept {
24 return this->task_pool_.worker_count();
25 }
26
27 template<typename Fn>
28 void parallel_for(size_t task_count, Fn&& fn) {
29 if (task_count == 0) {
30 return;
31 }
32
33#if CSV_ENABLE_THREADS
34 const size_t workers = this->task_pool_.worker_count();
35 if (workers <= 1 || task_count <= workers) {
36 this->run_serial(task_count, std::forward<Fn>(fn));
37 return;
38 }
39
40 this->task_pool_.parallel_for(task_count, [&fn](size_t, size_t task_index) {
41 fn(task_index);
42 });
43#else
44 this->run_serial(task_count, std::forward<Fn>(fn));
45#endif
46 }
47
48 private:
49 template<typename Fn>
50 void run_serial(size_t task_count, Fn&& fn) {
51 for (size_t i = 0; i < task_count; ++i) {
52 fn(i);
53 }
54 }
55
56 static size_t default_worker_count() {
57#if CSV_ENABLE_THREADS
58 const unsigned int hw = std::thread::hardware_concurrency();
59 return hw > 0 ? static_cast<size_t>(hw) : 1;
60#else
61 return 0;
62#endif
63 }
64
66 };
67}
The all encompassing namespace.