Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for local lambdas with (value_type&) signatures for arr… #133

Merged
merged 4 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions include/ygm/container/detail/array_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <vector>
#include <ygm/comm.hpp>
#include <ygm/detail/ygm_ptr.hpp>
#include <ygm/detail/ygm_traits.hpp>

namespace ygm::container::detail {

Expand Down Expand Up @@ -121,9 +122,23 @@ class array_impl {
template <typename Function>
void for_all(Function fn) {
m_comm.barrier();
for (int i = 0; i < m_local_vec.size(); ++i) {
index_type g_index = global_index(i);
fn(g_index, m_local_vec[i]);
local_for_all(fn);
}

template <typename Function>
void local_for_all(Function fn) {
if constexpr (std::is_invocable<decltype(fn), const index_type,
value_type &>()) {
for (int i = 0; i < m_local_vec.size(); ++i) {
index_type g_index = global_index(i);
fn(g_index, m_local_vec[i]);
}
} else if constexpr (std::is_invocable<decltype(fn), value_type &>()) {
std::for_each(std::begin(m_local_vec), std::end(m_local_vec), fn);
} else {
static_assert(ygm::detail::always_false<>,
"local array lambda must be invocable with (const "
"index_type, value_type &) or (value_type &) signatures");
}
}

Expand Down
23 changes: 23 additions & 0 deletions test/test_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,28 @@ int main(int argc, char **argv) {
});
}

// Test value-only for_all
{
int size = 64;

ygm::container::array<int> arr(world, size);

if (world.rank0()) {
for (int i = 0; i < size; ++i) {
arr.async_set(i, 1);
}
}

world.barrier();

for (int i = 0; i < size; ++i) {
arr.async_increment(i);
}

arr.for_all([&world](const auto value) {
ASSERT_RELEASE(value == world.size() + 1);
});
}

return 0;
}