Skip to content

Commit

Permalink
For loop replaced with for_each in several places in parallelization.
Browse files Browse the repository at this point in the history
  • Loading branch information
vidanovic committed May 15, 2024
1 parent 589ea31 commit 2799755
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/Common/src/MatrixSeries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ namespace FenestrationCommon
{
FenestrationCommon::executeInParallel<size_t>(0, matrix.size() - 1, [&](size_t i) {
const auto & row = matrix[i];
for(size_t j = 0; j < row.size(); ++j)
{
std::for_each(begin(row), end(row), [&](const auto & elem) {
size_t j = &elem - &row[0];
func(i, j);
}
});
});
}
} // namespace
Expand Down
18 changes: 9 additions & 9 deletions src/Common/src/Parallel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@

#include "Utility.hpp"

namespace FenestrationCommon {
namespace FenestrationCommon
{
template<typename IndexType, typename Function>
void executeInParallel(IndexType start, IndexType end, Function&& func)
void executeInParallel(IndexType start, IndexType end, Function && func)
{
const auto numberOfThreads = getNumberOfThreads(end - start + 1);
const auto chunks = chunkIt(start, end, numberOfThreads);

std::vector<std::thread> workers;
workers.reserve(chunks.size());

for (const auto& chunk : chunks)
for(const auto & chunk : chunks)
{
workers.emplace_back([&, chunk]() {
for (IndexType i = chunk.start; i < chunk.end; ++i)
for(IndexType i = chunk.start; i < chunk.end; ++i)
{
func(i);
}
});
}

for (auto& worker : workers)
{
if (worker.joinable())
std::for_each(workers.begin(), workers.end(), [](std::thread & worker) {
if(worker.joinable())
{
worker.join();
}
}
});
}
}
} // namespace FenestrationCommon

0 comments on commit 2799755

Please sign in to comment.