Skip to content

[pstl] std::exclusive_scan with execution policy does not work in-place #59810

@lahwaacz

Description

@lahwaacz

The exclusive.scan#8 section of the standard explicitly says:

Remarks: result may be equal to first.

So there should be no problem with using std::exclusive_scan in "in-place" mode overwriting the storage. However, with the libstdc++ implementation that comes with GCC 12.2.0, it does not work with the overloads that use an execution policy, even if it is std::execution::seq. Consider this example:

#include <algorithm>
#include <numeric>
#include <execution>
#include <vector>
#include <cassert>

int main()
{
    const int size = 10;
    std::vector<int> vec(size);

    // without execution policy
    std::fill(vec.begin(), vec.end(), 1);
    std::exclusive_scan(vec.begin(), vec.end(), vec.begin(), 0);
    assert(vec[0] == 0); // the first element should be 0
    assert(vec[size-1] == size-1); // the last element should be the sum

    // sequential execution policy
    std::fill(vec.begin(), vec.end(), 1);
    std::exclusive_scan(std::execution::seq, vec.begin(), vec.end(), vec.begin(), 0);
    assert(vec[0] == 0); // the first element should be 0
    assert(vec[size-1] == size-1); // the last element should be the sum
}

Note that the code fails on the last of the asserts. The std::exclusive_scan overload without a policy works and there is also no problem with using std::inclusive_scan in-place with any policy.

See also https://stackoverflow.com/q/74932677/4180822 where I asked about this behavior.

Bug report copy-pasted from https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108236 because they use LLVM's PSTL headers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    pstlIssues related to the C++17 Parallel STL

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions