5 #include <forward_list>
6 #include <Eigen/Sparse>
15 static std::pair<std::vector<int>, std::vector<int>>
19 std::vector<int> start(nb_threads);
20 std::vector<int> end(nb_threads);
23 unsigned batch_size = nb_elements / nb_threads;
24 unsigned batch_remainder = nb_elements % nb_threads;
27 for (
unsigned i = 0; i < nb_threads; ++i) {
28 if (i < batch_remainder){
29 start[i] = i * batch_size + i;
30 end[i] = start[i] + batch_size + 1;
33 start[i] = i * batch_size + batch_remainder;
34 end[i] = start[i] + batch_size;
38 return std::make_pair(start, end);
43 std::function<
void(
size_t start,
size_t end)> functor,
44 bool use_threads =
true)
46 unsigned nb_threads_hint = std::thread::hardware_concurrency();
47 unsigned nb_threads = nb_threads_hint == 0 ? 8 : (nb_threads_hint);
52 std::vector<std::thread> my_threads(nb_threads);
56 for (
unsigned i = 0; i < nb_threads; ++i) {
57 my_threads[i] = std::thread(functor, start[i], end[i]);
61 for(
unsigned i = 0; i < nb_threads; ++i) {
62 functor(start[i], end[i]);
68 std::for_each(my_threads.begin(), my_threads.end(), std::mem_fn(&std::thread::join));
73 template<
typename FType>
76 std::pair<size_t,size_t> systemSize ,
78 bool use_threads =
true )
80 std::forward_list<Eigen::Triplet<double>> triplets;
83 unsigned nb_threads_hint = std::thread::hardware_concurrency();
84 unsigned nb_threads = nb_threads_hint == 0 ? 8 : (nb_threads_hint);
88 std::vector<std::forward_list<Eigen::Triplet<double>>> tripletsVect(nb_threads);
91 std::vector<std::thread> my_threads(nb_threads);
92 for (
unsigned i = 0; i < nb_threads; ++i) {
93 my_threads[i] = std::thread(localAssembly, start[i], end[i], &tripletsVect[i]);
96 std::for_each(my_threads.begin(), my_threads.end(), std::mem_fn(&std::thread::join));
98 for (
unsigned i = 0; i < nb_threads; ++i) {
99 triplets.splice_after(triplets.cbefore_begin(),tripletsVect[i]);
102 localAssembly(0,nb_elements,&triplets);
105 Eigen::SparseMatrix<double> system(systemSize.first,systemSize.second);
106 system.setFromTriplets(triplets.cbegin(),triplets.cend());
Definition: maxwell.hpp:23
static std::pair< std::vector< int >, std::vector< int > > distributeLoad(size_t nb_elements, unsigned nb_threads)
Function to distribute elements (considered as jobs) over threads. It returns a pair of vectors indic...
Definition: parallel_for.hpp:16
static void parallel_for(unsigned nb_elements, std::function< void(size_t start, size_t end)> functor, bool use_threads=true)
Generic function to execute threaded processes.
Definition: parallel_for.hpp:42
Eigen::SparseMatrix< double > parallel_assembly(size_t nb_elements, std::pair< size_t, size_t > systemSize, FType localAssembly, bool use_threads=true)
Function to assemble a global sparse matrix from a procedure that compute local contributions.
Definition: parallel_for.hpp:74