Skip to content

Commit

Permalink
Merge pull request #2295 from STEllAR-GROUP/fixing_2281
Browse files Browse the repository at this point in the history
Making sure for_loop(par, 0, N, ...) is actually executed in parallel
  • Loading branch information
hkaiser committed Aug 20, 2016
2 parents e309cf6 + d652de4 commit be2845b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
6 changes: 5 additions & 1 deletion hpx/parallel/algorithms/for_loop.hpp
Expand Up @@ -232,9 +232,13 @@ namespace hpx { namespace parallel { HPX_INLINE_NAMESPACE(v2)
hpx::traits::is_bidirectional_iterator<E>::value);
}

// the for_loop should be executed sequentially either if the
// execution policy enforces sequential execution of if the
// loop boundaries are input or output iterators
typedef std::integral_constant<bool,
is_sequential_execution_policy<ExPolicy>::value ||
!hpx::traits::is_forward_iterator<B>::value
(!std::is_integral<B>::value &&
!hpx::traits::is_forward_iterator<B>::value)
> is_seq;

std::size_t size = parallel::v1::detail::distance(first, last);
Expand Down
1 change: 1 addition & 0 deletions tests/regressions/parallel/CMakeLists.txt
Expand Up @@ -4,6 +4,7 @@
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

set(tests
for_loop_2281
minimal_findend
scan_different_inits
scan_non_commutative
Expand Down
45 changes: 45 additions & 0 deletions tests/regressions/parallel/for_loop_2281.cpp
@@ -0,0 +1,45 @@
// Copyright (c) 2016 Hartmut Kaiser
//
// 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)

#include <hpx/hpx_init.hpp>
#include <hpx/hpx.hpp>
#include <hpx/include/parallel_for_loop.hpp>
#include <hpx/util/lightweight_test.hpp>

#include <mutex>
#include <set>
#include <string>
#include <vector>

///////////////////////////////////////////////////////////////////////////////
int hpx_main()
{
hpx::lcos::local::spinlock mtx;
std::set<hpx::thread::id> thread_ids;

hpx::parallel::for_loop(
hpx::parallel::par, 0, 100,
[&](int i)
{
std::lock_guard<hpx::lcos::local::spinlock> l(mtx);
thread_ids.insert(hpx::this_thread::get_id());
});

HPX_TEST(thread_ids.size() > std::size_t(1));

return hpx::finalize();
}

int main(int argc, char* argv[])
{
std::vector<std::string> const cfg = {
"hpx.os_threads=4"
};

HPX_TEST_EQ_MSG(hpx::init(argc, argv, cfg), 0,
"HPX main exited with non-zero status");

return hpx::util::report_errors();
}
2 changes: 1 addition & 1 deletion tests/regressions/parallel/minimal_findend.cpp
Expand Up @@ -101,7 +101,7 @@ int main(int argc, char* argv[])
};

HPX_TEST_EQ_MSG(hpx::init(argc, argv, cfg), 0,
"HPX main exted with non-zero status");
"HPX main exited with non-zero status");

return hpx::util::report_errors();
}

0 comments on commit be2845b

Please sign in to comment.