并行调用具有参数向量的元素向量的成员函数
Call a member function of a vector of elements with a vector of arguments in parallel
给定这段代码:
struct T
{
void f(int const);
};
void f(std::vector<T> &u, std::vector<int> const &v)
{
for (std::size_t i = 0; i < u.size(); ++i)
u[i].f(v[i]);
}
是否有并行化 void f(std::vector<T> &u, std::vector<int> const &v)
主体的标准方法?
这碰巧有效 (https://godbolt.org/z/gRv9Ze):
void f(std::vector<T> &u, std::vector<int> const &v)
{
auto const indices = std::views::iota(0u, u.size()) | std::views::common;
std::for_each(std::execution::par_unseq, std::begin(indices), std::end(indices),
[&](std::size_t const i) { u[i].f(v[i]); });
}
但据报道依赖这种行为是错误的(见此 bug report
还有这个 answer). Indeed, this doesn't run in parallel (https://godbolt.org/z/MPGdHF):
void f(std::vector<T> &u, std::vector<int> const &v)
{
std::ranges::iota_view<std::size_t, std::size_t> const indices(0u, u.size());
std::for_each(std::execution::par_unseq, std::begin(indices), std::end(indices),
[&](std::size_t const i) { u[i].f(v[i]); });
}
我很确定应该有一个标准的方法来并行创建类似 运行 的函数。我可能错过了一个明显的
algorithm,但是std::transform
在这里好像不太合适,其他的更不合适。
保持在 std
以内,您最好的选择是 std::transform
使用忽略所提供内容的输出迭代器
struct unit_iterator {
using difference_type = std::ptrdiff_t;
using value_type = std::tuple<>;
using pointer = std::tuple<> *;
using const_pointer = const std::tuple<> *;
using reference = std::tuple<> &;
using const reference = const std::tuple<> &;
using iterator_category = std::random_access_iterator_tag;
reference operator*() { return value; }
const_reference operator*() const { return value; }
reference operator[](difference_type) { return value; }
const_reference operator[](difference_type) const { return value; }
pointer operator->() { return &value; }
const_pointer operator->() const { return &value; }
unit_iterator& operator++() { return *this; }
unit_iterator operator++(int) { return *this; }
unit_iterator& operator+=(difference_type) { return *this; }
unit_iterator operator+(difference_type) const { return *this; }
unit_iterator& operator--() { return *this; }
unit_iterator operator--(int) { return *this; }
unit_iterator& operator-=(difference_type) { return *this; }
unit_iterator operator-(difference_type) const { return *this; }
difference_type operator-(unit_iterator) const { return 0; }
bool operator==(unit_iterator) const { return true; }
bool operator!=(unit_iterator) const { return false; }
bool operator<(unit_iterator) const { return false; }
bool operator<=(unit_iterator) const { return true; }
bool operator>(unit_iterator) const { return false; }
bool operator>=(unit_iterator) const { return true; }
private:
static value_type value;
};
void f(std::vector<T> &u, std::vector<int> const &v)
{
std::transform(std::execution::par_unseq, begin(u), end(u), begin(v), unit_iterator{},
[](T & u, int v) { u.f(v); return std::tuple<>{}; });
}
给定这段代码:
struct T
{
void f(int const);
};
void f(std::vector<T> &u, std::vector<int> const &v)
{
for (std::size_t i = 0; i < u.size(); ++i)
u[i].f(v[i]);
}
是否有并行化 void f(std::vector<T> &u, std::vector<int> const &v)
主体的标准方法?
这碰巧有效 (https://godbolt.org/z/gRv9Ze):
void f(std::vector<T> &u, std::vector<int> const &v)
{
auto const indices = std::views::iota(0u, u.size()) | std::views::common;
std::for_each(std::execution::par_unseq, std::begin(indices), std::end(indices),
[&](std::size_t const i) { u[i].f(v[i]); });
}
但据报道依赖这种行为是错误的(见此 bug report 还有这个 answer). Indeed, this doesn't run in parallel (https://godbolt.org/z/MPGdHF):
void f(std::vector<T> &u, std::vector<int> const &v)
{
std::ranges::iota_view<std::size_t, std::size_t> const indices(0u, u.size());
std::for_each(std::execution::par_unseq, std::begin(indices), std::end(indices),
[&](std::size_t const i) { u[i].f(v[i]); });
}
我很确定应该有一个标准的方法来并行创建类似 运行 的函数。我可能错过了一个明显的
algorithm,但是std::transform
在这里好像不太合适,其他的更不合适。
保持在 std
以内,您最好的选择是 std::transform
使用忽略所提供内容的输出迭代器
struct unit_iterator {
using difference_type = std::ptrdiff_t;
using value_type = std::tuple<>;
using pointer = std::tuple<> *;
using const_pointer = const std::tuple<> *;
using reference = std::tuple<> &;
using const reference = const std::tuple<> &;
using iterator_category = std::random_access_iterator_tag;
reference operator*() { return value; }
const_reference operator*() const { return value; }
reference operator[](difference_type) { return value; }
const_reference operator[](difference_type) const { return value; }
pointer operator->() { return &value; }
const_pointer operator->() const { return &value; }
unit_iterator& operator++() { return *this; }
unit_iterator operator++(int) { return *this; }
unit_iterator& operator+=(difference_type) { return *this; }
unit_iterator operator+(difference_type) const { return *this; }
unit_iterator& operator--() { return *this; }
unit_iterator operator--(int) { return *this; }
unit_iterator& operator-=(difference_type) { return *this; }
unit_iterator operator-(difference_type) const { return *this; }
difference_type operator-(unit_iterator) const { return 0; }
bool operator==(unit_iterator) const { return true; }
bool operator!=(unit_iterator) const { return false; }
bool operator<(unit_iterator) const { return false; }
bool operator<=(unit_iterator) const { return true; }
bool operator>(unit_iterator) const { return false; }
bool operator>=(unit_iterator) const { return true; }
private:
static value_type value;
};
void f(std::vector<T> &u, std::vector<int> const &v)
{
std::transform(std::execution::par_unseq, begin(u), end(u), begin(v), unit_iterator{},
[](T & u, int v) { u.f(v); return std::tuple<>{}; });
}