Skip to content

Commit

Permalink
Merge pull request #3092 from STEllAR-GROUP/cxx14_lambda_captures
Browse files Browse the repository at this point in the history
Take advantage of C++14 lambda capture initialization syntax, where possible
  • Loading branch information
hkaiser committed Jan 6, 2018
2 parents 75729c1 + ff2817d commit 6cf7ba5
Show file tree
Hide file tree
Showing 45 changed files with 411 additions and 325 deletions.
3 changes: 2 additions & 1 deletion cmake/HPX_AddConfigTest.cmake
Expand Up @@ -575,7 +575,8 @@ endmacro()
macro(hpx_check_for_cxx14_lambdas)
add_hpx_config_test(HPX_WITH_CXX14_LAMBDAS
SOURCE cmake/tests/cxx14_lambdas.cpp
FILE ${ARGN})
FILE ${ARGN}
CMAKECXXFEATURE cxx_lambda_init_captures)
endmacro()

###############################################################################
Expand Down
3 changes: 2 additions & 1 deletion hpx/config.hpp
@@ -1,4 +1,4 @@
// Copyright (c) 2007-2017 Hartmut Kaiser
// Copyright (c) 2007-2018 Hartmut Kaiser
// Copyright (c) 2011 Bryce Lelbach
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
Expand All @@ -22,6 +22,7 @@
#include <hpx/config/emulate_deleted.hpp>
#include <hpx/config/export_definitions.hpp>
#include <hpx/config/forceinline.hpp>
#include <hpx/config/lambda_capture.hpp>
#include <hpx/config/manual_profiling.hpp>
#include <hpx/config/version.hpp>

Expand Down
21 changes: 21 additions & 0 deletions hpx/config/lambda_capture.hpp
@@ -0,0 +1,21 @@
// Copyright (c) 2018 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)

#ifndef HPX_CONFIG_LAMBDA_CAPTURE_HPP
#define HPX_CONFIG_LAMBDA_CAPTURE_HPP

#include <hpx/config/defines.hpp>

#include <utility>

#if defined(HPX_HAVE_CXX14_LAMBDAS)
#define HPX_CAPTURE_FORWARD(var) var = std::forward<decltype(var)>(var)
#define HPX_CAPTURE_MOVE(var) var = std::move(var)
#else
#define HPX_CAPTURE_FORWARD(var) var
#define HPX_CAPTURE_MOVE(var) var
#endif

#endif
6 changes: 3 additions & 3 deletions hpx/lcos/future.hpp
@@ -1,4 +1,4 @@
// Copyright (c) 2007-2017 Hartmut Kaiser
// Copyright (c) 2007-2018 Hartmut Kaiser
// Copyright (c) 2013 Agustin Berge
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
Expand Down Expand Up @@ -1125,7 +1125,7 @@ namespace hpx { namespace lcos
convert_future_helper(Future && f, Conv && conv) //-V659
{
return f.then(
[conv](Future && f) -> T
[HPX_CAPTURE_FORWARD(conv)](Future && f) -> T
{
return hpx::util::invoke(conv, f.get());
});
Expand Down Expand Up @@ -1426,7 +1426,7 @@ namespace hpx { namespace lcos
"result type by using the supplied conversion function");

return f.then(
[conv](hpx::shared_future<U> const& f)
[HPX_CAPTURE_FORWARD(conv)](hpx::shared_future<U> const& f)
{
return hpx::util::invoke(conv, f.get());
});
Expand Down
6 changes: 4 additions & 2 deletions hpx/lcos/local/packaged_continuation.hpp
@@ -1,4 +1,4 @@
// Copyright (c) 2007-2017 Hartmut Kaiser
// Copyright (c) 2007-2018 Hartmut Kaiser
// Copyright (c) 2014-2015 Agustin Berge
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
Expand Down Expand Up @@ -497,8 +497,10 @@ namespace hpx { namespace lcos { namespace detail
}

ptr->execute_deferred();

ptr->set_on_completed(util::deferred_call(
[this_](shared_state_ptr && f, launch policy)
[HPX_CAPTURE_MOVE(this_)](
shared_state_ptr && f, launch policy)
{
if (hpx::detail::has_async_policy(policy))
this_->async(std::move(f), policy.priority());
Expand Down
27 changes: 11 additions & 16 deletions hpx/parallel/algorithms/adjacent_difference.hpp
Expand Up @@ -78,22 +78,17 @@ namespace hpx { namespace parallel { inline namespace v1
}

auto f1 =
[op, policy](
zip_iterator part_begin, std::size_t part_size
) mutable
{
HPX_UNUSED(policy);

// VS2015RC bails out when op is captured by ref
using hpx::util::get;
util::loop_n<ExPolicy>(
part_begin, part_size,
[op](zip_iterator it)
{
get<2>(*it) = hpx::util::invoke(
op, get<0>(*it), get<1>(*it));
});
};
[HPX_CAPTURE_FORWARD(op)](
zip_iterator part_begin, std::size_t part_size) mutable
{
// VS2015RC bails out when op is captured by ref
using hpx::util::get;
util::loop_n<ExPolicy>(
part_begin, part_size, [op](zip_iterator it) {
get<2>(*it) =
hpx::util::invoke(op, get<0>(*it), get<1>(*it));
});
};

using hpx::util::make_zip_iterator;
return util::partitioner<ExPolicy, Iter, void>::call(
Expand Down
3 changes: 2 additions & 1 deletion hpx/parallel/algorithms/adjacent_find.hpp
Expand Up @@ -76,7 +76,8 @@ namespace hpx { namespace parallel { inline namespace v1
call_with_index(
std::forward<ExPolicy>(policy),
hpx::util::make_zip_iterator(first, next), count-1, 1,
[op, tok](zip_iterator it, std::size_t part_size,
[HPX_CAPTURE_FORWARD(op), tok](
zip_iterator it, std::size_t part_size,
std::size_t base_idx) mutable
{
util::loop_idx_n(
Expand Down
56 changes: 29 additions & 27 deletions hpx/parallel/algorithms/all_any_none.hpp
@@ -1,4 +1,4 @@
// Copyright (c) 2007-2017 Hartmut Kaiser
// Copyright (c) 2007-2018 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)
Expand Down Expand Up @@ -72,22 +72,22 @@ namespace hpx { namespace parallel { inline namespace v1

util::cancellation_token<> tok;
auto f1 =
[op, tok, policy, proj](
FwdIter part_begin, std::size_t part_count
) mutable -> bool
{
HPX_UNUSED(policy);

util::loop_n<ExPolicy>(
part_begin, part_count, tok,
[&op, &tok, &proj](FwdIter const& curr)
[HPX_CAPTURE_FORWARD(op), tok,
HPX_CAPTURE_FORWARD(proj)
](FwdIter part_begin, std::size_t part_count) mutable -> bool
{
util::loop_n<ExPolicy>(part_begin, part_count, tok,
[&op, &tok, &proj](FwdIter const& curr)
{
if (hpx::util::invoke(op,
hpx::util::invoke(proj, *curr)))
{
if (op(proj(*curr)))
tok.cancel();
});
tok.cancel();
}
});

return !tok.was_cancelled();
};
return !tok.was_cancelled();
};

return util::partitioner<ExPolicy, bool>::call(
std::forward<ExPolicy>(policy),
Expand Down Expand Up @@ -247,18 +247,19 @@ namespace hpx { namespace parallel { inline namespace v1

util::cancellation_token<> tok;
auto f1 =
[op, tok, policy, proj](
FwdIter part_begin, std::size_t part_count
) mutable -> bool
[HPX_CAPTURE_FORWARD(op), tok,
HPX_CAPTURE_FORWARD(proj)
](FwdIter part_begin, std::size_t part_count) mutable -> bool
{
HPX_UNUSED(policy);

util::loop_n<ExPolicy>(
part_begin, part_count, tok,
[&op, &tok, &proj](FwdIter const& curr)
{
if (op(proj(*curr)))
if (hpx::util::invoke(op,
hpx::util::invoke(proj, *curr)))
{
tok.cancel();
}
});

return tok.was_cancelled();
Expand Down Expand Up @@ -422,18 +423,19 @@ namespace hpx { namespace parallel { inline namespace v1

util::cancellation_token<> tok;
auto f1 =
[op, tok, policy, proj](
FwdIter part_begin, std::size_t part_count
) mutable -> bool
[HPX_CAPTURE_FORWARD(op), tok,
HPX_CAPTURE_FORWARD(proj)
](FwdIter part_begin, std::size_t part_count) mutable -> bool
{
HPX_UNUSED(policy);

util::loop_n<ExPolicy>(
part_begin, part_count, tok,
[&op, &tok, &proj](FwdIter const& curr)
{
if (!op(proj(*curr)))
if (!hpx::util::invoke(op,
hpx::util::invoke(proj, *curr)))
{
tok.cancel();
}
});

return !tok.was_cancelled();
Expand Down
22 changes: 9 additions & 13 deletions hpx/parallel/algorithms/copy.hpp
@@ -1,6 +1,6 @@
// Copyright (c) 2014 Grant Mercer
// Copyright (c) 2015 Daniel Bourgeois
// Copyright (c) 2016-2017 Hartmut Kaiser
// Copyright (c) 2016-2018 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)
Expand Down Expand Up @@ -423,23 +423,20 @@ namespace hpx { namespace parallel { inline namespace v1
> scan_partitioner_type;

auto f1 =
[pred, proj, flags, policy]
(
zip_iterator part_begin, std::size_t part_size
) -> std::size_t
[HPX_CAPTURE_FORWARD(pred),
HPX_CAPTURE_FORWARD(proj)
](zip_iterator part_begin, std::size_t part_size)
-> std::size_t
{
HPX_UNUSED(flags);
HPX_UNUSED(policy);

std::size_t curr = 0;

// MSVC complains if proj is captured by ref below
util::loop_n<ExPolicy>(
part_begin, part_size,
[&pred, proj, &curr](zip_iterator it) mutable
{
using hpx::util::invoke;
bool f = invoke(pred, invoke(proj, get<0>(*it)));
bool f = hpx::util::invoke(pred,
hpx::util::invoke(proj, get<0>(*it)));

if ((get<1>(*it) = f))
++curr;
Expand All @@ -448,14 +445,13 @@ namespace hpx { namespace parallel { inline namespace v1
return curr;
};
auto f3 =
[dest, flags, policy](
[dest, flags](
zip_iterator part_begin, std::size_t part_size,
hpx::shared_future<std::size_t> curr,
hpx::shared_future<std::size_t> next
) mutable
{
HPX_UNUSED(flags);
HPX_UNUSED(policy);

next.get(); // rethrow exceptions

Expand All @@ -464,7 +460,7 @@ namespace hpx { namespace parallel { inline namespace v1
part_begin, part_size,
[&dest](zip_iterator it) mutable
{
if(get<1>(*it))
if (get<1>(*it))
*dest++ = get<0>(*it);
});
};
Expand Down
22 changes: 11 additions & 11 deletions hpx/parallel/algorithms/equal.hpp
@@ -1,4 +1,4 @@
// Copyright (c) 2007-2017 Hartmut Kaiser
// Copyright (c) 2007-2018 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)
Expand Down Expand Up @@ -111,20 +111,20 @@ namespace hpx { namespace parallel { inline namespace v1

util::cancellation_token<> tok;
auto f1 =
[f, tok, policy](
[f, tok](
zip_iterator it, std::size_t part_count
) mutable -> bool
{
HPX_UNUSED(policy);

util::loop_n<ExPolicy>(
it, part_count, tok,
[&f, &tok](zip_iterator const& curr)
{
using hpx::util::get;
reference t = *curr;
if (!f(get<0>(t), get<1>(t)))
if (!hpx::util::invoke(f, hpx::util::get<0>(t),
hpx::util::get<1>(t)))
{
tok.cancel();
}
});
return !tok.was_cancelled();
};
Expand Down Expand Up @@ -299,20 +299,20 @@ namespace hpx { namespace parallel { inline namespace v1

util::cancellation_token<> tok;
auto f1 =
[f, tok, policy](
[f, tok](
zip_iterator it, std::size_t part_count
) mutable -> bool
{
HPX_UNUSED(policy);

util::loop_n<ExPolicy>(
it, part_count, tok,
[&f, &tok](zip_iterator const& curr)
{
reference t = *curr;
using hpx::util::get;
if (!f(get<0>(t), get<1>(t)))
if (!hpx::util::invoke(f, hpx::util::get<0>(t),
hpx::util::get<1>(t)))
{
tok.cancel();
}
});
return !tok.was_cancelled();
};
Expand Down
20 changes: 10 additions & 10 deletions hpx/parallel/algorithms/exclusive_scan.hpp
@@ -1,4 +1,4 @@
// Copyright (c) 2014-2017 Hartmut Kaiser
// Copyright (c) 2014-2018 Hartmut Kaiser
// Copyright (c) 2016 Minh-Khanh Do
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
Expand Down Expand Up @@ -124,12 +124,10 @@ namespace hpx { namespace parallel { inline namespace v1
using hpx::util::make_zip_iterator;

auto f3 =
[op, policy](
[op](
zip_iterator part_begin, std::size_t part_size,
hpx::shared_future<T> curr, hpx::shared_future<T> next
)
hpx::shared_future<T> curr, hpx::shared_future<T> next)
{
HPX_UNUSED(policy);

next.get(); // rethrow exceptions

Expand All @@ -150,20 +148,22 @@ namespace hpx { namespace parallel { inline namespace v1
std::forward<ExPolicy>(policy),
make_zip_iterator(first, dest), count, init,
// step 1 performs first part of scan algorithm
[op, conv, last](zip_iterator part_begin,
std::size_t part_size) -> T
[op, HPX_CAPTURE_FORWARD(conv), last](
zip_iterator part_begin, std::size_t part_size) -> T
{
T part_init = hpx::util::invoke(conv, get<0>(*part_begin++));
T part_init = hpx::util::invoke(
conv, get<0>(*part_begin++));

auto iters = part_begin.get_iterator_tuple();
if(get<0>(iters) != last)
{
return sequential_exclusive_scan_n(
get<0>(iters),
part_size - 1,
get<1>(iters),
part_init, op, conv);
else
return part_init;
}
return part_init;
},
// step 2 propagates the partition results from left
// to right
Expand Down

0 comments on commit 6cf7ba5

Please sign in to comment.