Skip to content

Commit

Permalink
Merge pull request #55 from ckormanyos/minor_cleanup
Browse files Browse the repository at this point in the history
Minor cleanup
  • Loading branch information
ckormanyos authored Feb 26, 2023
2 parents f565aca + 2e93a96 commit e1ec59a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 58 deletions.
120 changes: 67 additions & 53 deletions concurrency/parallel_for.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2017 - 2022.
// Copyright Christopher Kormanyos 2017 - 2023.
// Distributed under the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
Expand All @@ -16,91 +16,105 @@
{
template<typename index_type,
typename callable_function_type>
void parallel_for(index_type start,
auto parallel_for(index_type start,
index_type end,
callable_function_type parallel_function)
callable_function_type parallel_function) -> void
{
// Obtain the number of threads available.
static const auto number_of_threads_hint =
static_cast<unsigned>
// Estimate the number of threads available.
const auto number_of_threads_hint =
static_cast<signed>
(
std::thread::hardware_concurrency()
);

static const auto number_of_threads = // NOLINT(altera-id-dependent-backward-branch)
const auto number_of_threads = // NOLINT(altera-id-dependent-backward-branch)
static_cast<unsigned>
(
((number_of_threads_hint > static_cast<unsigned>(1U))
? static_cast<unsigned>(number_of_threads_hint - 1U)
: static_cast<unsigned>(1U))
(std::max)
(
static_cast<signed>(number_of_threads_hint - static_cast<signed>(UINT8_C(1))),
static_cast<signed>(INT8_C(1))
)
);

// Set the size of a slice for the range functions.
const auto n =
static_cast<index_type>
(
static_cast<index_type>(end - start) + static_cast<index_type>(1)
);
using thread_vector_type = std::vector<std::thread>;

const auto slice =
(std::max)
(
static_cast<index_type>(std::round(static_cast<float>(n) / static_cast<float>(number_of_threads))),
static_cast<index_type>(1)
);
// Create the thread pool and launch the jobs.
thread_vector_type pool { };

// Inner loop.
auto launch_range =
[&parallel_function](index_type index_lo, index_type index_hi)
{
for(auto i = index_lo; i < index_hi; ++i) // NOLINT(altera-id-dependent-backward-branch)
{
parallel_function(i);
}
};
pool.reserve(static_cast<typename thread_vector_type::size_type>(number_of_threads));

// Create the thread pool and launch the jobs.
std::vector<std::thread> pool;
{
// Inner loop.
const auto launch_range =
[&parallel_function](index_type index_lo, index_type index_hi)
{
for(auto i = index_lo; i < index_hi; ++i) // NOLINT(altera-id-dependent-backward-branch)
{
parallel_function(i);
}
};

pool.reserve(number_of_threads);
auto i1 = start;

auto i1 = start;
auto i2 = (std::min)(static_cast<index_type>(start + static_cast<index_type>(slice)), end);
{
// Set the size of a slice for the range functions.
const auto n =
static_cast<index_type>
(
static_cast<index_type>(end - start) + static_cast<index_type>(1)
);

const auto slice =
(std::max)
(
static_cast<index_type>(std::round(static_cast<float>(n) / static_cast<float>(number_of_threads))),
static_cast<index_type>(1)
);

auto i2 = (std::min)(static_cast<index_type>(start + slice), end);

for(auto i = static_cast<index_type>(0U);
static_cast<index_type>(i + static_cast<index_type>(INT8_C(1))) < static_cast<index_type>(number_of_threads); // NOLINT(altera-id-dependent-backward-branch)
++i)
{
pool.emplace_back(launch_range, i1, i2);

for(auto i = static_cast<unsigned>(0U);
((static_cast<unsigned>(static_cast<index_type>(i) + 1) < number_of_threads) && (i1 < end)); // NOLINT(altera-id-dependent-backward-branch)
++i)
{
pool.emplace_back(launch_range, i1, i2);
i1 = i2;

i1 = i2;
if(i1 >= end)
{
break;
}

i2 = (std::min)(static_cast<index_type>(i2 + slice), end);
}
i2 = (std::min)(static_cast<index_type>(i2 + slice), end);
}
}

if(i1 < end)
{
pool.emplace_back(launch_range, i1, end);
if(i1 < end)
{
pool.emplace_back(launch_range, i1, end);
}
}

// Wait for the jobs to finish.
for(std::thread& t : pool)
for(auto& thread_in_pool : pool)
{
if(t.joinable())
if(thread_in_pool.joinable())
{
t.join();
thread_in_pool.join();
}
}
}

// Provide a serial version for easy comparison.
template<typename index_type,
typename callable_function_type>
void sequential_for(index_type start,
auto sequential_for(index_type start,
index_type end,
callable_function_type sequential_function)
callable_function_type sequential_function) -> void
{
for(index_type i = start; i < end; i++)
for(auto i = start; i < end; ++i)
{
sequential_function(i);
}
Expand Down
2 changes: 1 addition & 1 deletion mandelbrot/cfg/mandelbrot_cfg.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2022.
// Copyright Christopher Kormanyos 2022 - 2023.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2022.
// Copyright Christopher Kormanyos 2022 - 2023.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
Expand Down
6 changes: 3 additions & 3 deletions test/test_mandelbrot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
// cd ..

// Compile with cpp_dec_float
// g++ -c -finline-functions -march=native -mtune=native -O3 -Wall -Wextra -std=c++11 -I. -I/mnt/c/boost/boost_1_81_0 -Ijpeg/jpeg-6b-2022 -pthread test/test_mandelbrot.cpp -o test_mandelbrot.o
// g++ -c -finline-functions -march=native -mtune=native -O3 -Wall -Wextra -std=c++14 -I. -I/mnt/c/boost/boost_1_81_0 -Ijpeg/jpeg-6b-2022 -pthread test/test_mandelbrot.cpp -o test_mandelbrot.o
// g++ test_mandelbrot.o -lpthread -ljpeg-6b -Ljpeg/jpeg-6b-2022/obj -o test_mandelbrot.exe

// Compile with gmp_dec_float
// g++ -c -finline-functions -march=native -mtune=native -O3 -Wall -Wextra -std=c++2a -DMANDELBROT_USE_GMP_FLOAT -I. -I/mnt/c/boost/boost_1_81_0 -Ijpeg/jpeg-6b-2022 -pthread test/test_mandelbrot.cpp -o test_mandelbrot.o
// Compile with gmp_float
// g++ -c -finline-functions -march=native -mtune=native -O3 -Wall -Wextra -std=c++14 -DMANDELBROT_USE_GMP_FLOAT -I. -I/mnt/c/boost/boost_1_81_0 -Ijpeg/jpeg-6b-2022 -pthread test/test_mandelbrot.cpp -o test_mandelbrot.o
// g++ test_mandelbrot.o -lpthread -ljpeg-6b -Ljpeg/jpeg-6b-2022/obj -lgmp -o test_mandelbrot.exe

auto main() -> int // NOLINT(bugprone-exception-escape)
Expand Down

0 comments on commit e1ec59a

Please sign in to comment.