Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
parallel_for_spawning.cpp
Go to the documentation of this file.
1#ifndef NO_MULTITHREADING
2#include "thread.hpp"
3#include <thread>
4
5namespace bb {
11void parallel_for_spawning(size_t num_iterations, const std::function<void(size_t)>& func)
12{
13 std::atomic<size_t> current_iteration(0);
14
15 auto worker = [&](size_t) {
16 // info("entered worker: ", thread_index);
17 size_t index = 0;
18 while ((index = current_iteration.fetch_add(1, std::memory_order_seq_cst)) < num_iterations) {
19 func(index);
20 }
21 // info("exited worker: ", thread_index);
22 };
23
24 auto num_threads = std::min(num_iterations, get_num_cpus()) - 1;
25 // if (num_threads == 1) {
26 // // info("Executing on main thread as only 1 cpu or iteration. iterations: ", num_iterations);
27 // worker(0);
28 // return;
29 // }
30 // info("Starting ", num_threads, " threads to handle ", num_iterations, " iterations.");
31
32 std::vector<std::thread> threads(num_threads);
33
34 for (size_t i = 0; i < num_threads; ++i) {
35 threads[i] = std::thread(worker, i);
36 }
37
38 worker(num_threads);
39
40 for (auto& thread : threads) {
41 thread.join();
42 }
43 // info("joined!\n\n");
44}
45} // namespace bb
46#endif
Entry point for Barretenberg command-line interface.
size_t get_num_cpus()
Definition thread.hpp:12
void parallel_for_spawning(size_t num_iterations, const std::function< void(size_t)> &func)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13