SLIDE  3.0.0
A simulator for lithium-ion battery pack degradation
Loading...
Searching...
No Matches
parallelisation.hpp
Go to the documentation of this file.
1/*
2 * parallelisation.hpp
3 *
4 * Some utility functions for parallelisation.
5
6 * Created on: 16 Oct 2022
7 * Author(s): Jorn Reniers, Volkan Kumtepeli
8 */
9
10
11#pragma once
12
13#include "../settings/settings.hpp"
14
15#include <thread>
16#include <vector>
17
18namespace slide {
19
20template <typename Tfun> // #TODO change with parallel algorithms.
21void run(Tfun task_indv, int i_end, unsigned int numMaxParallelWorkers = settings::numMaxParallelWorkers)
22{
23
24 auto task_par = [&](int i_begin, int i_end_, int Nth) {
25 while (i_begin < i_end_) {
26 task_indv(i_begin);
27 i_begin += Nth;
28 }
29 };
30
31 if constexpr (settings::isParallel) {
32 if (numMaxParallelWorkers == 1)
33 task_par(0, i_end, 1);
34 else {
35 if (numMaxParallelWorkers < 1)
36 numMaxParallelWorkers = std::thread::hardware_concurrency();
37
38 const unsigned int N_th_max = std::min(numMaxParallelWorkers, std::thread::hardware_concurrency());
39
40 std::vector<std::thread> threads;
41 threads.reserve(N_th_max);
42
43 for (unsigned int i_begin = 0; i_begin < N_th_max; i_begin++)
44 {
46
47 threads.emplace_back(task_par, i_begin, i_end, N_th_max);
48 }
49
50 for (auto &th : threads) {
51 if (th.joinable())
52 th.join();
53 }
54 }
55 } else {
56 task_par(0, i_end, 1);
57 }
58}
59} // namespace slide
constexpr unsigned int numMaxParallelWorkers
Maximum number of threads to use if isParallel true.
Definition: settings.hpp:29
constexpr bool isParallel
Parallelises the code if possible.
Definition: settings.hpp:28
Slide namespace contains all the types, classes, and functions for the simulation framework.
Definition: Cell.hpp:27
void run(Tfun task_indv, int i_end, unsigned int numMaxParallelWorkers=settings::numMaxParallelWorkers)
Definition: parallelisation.hpp:21