C++ Logo

std-proposals

Advanced search

Re: Python-style comprehensive containers

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Mon, 21 Jun 2021 01:35:04 -0400
On Sat, Jun 19, 2021 at 3:56 PM Phil Bouchard via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
>
> On 6/18/21 5:18 AM, Cleiton Santoia via Std-Proposals wrote:
>
>
>
> With C++ ranges today you can do :
>
> auto const ints = {0,1,2,3,4,5,6};
> auto d = ints | views::transform([](int x) { return x*2; });
> https://godbolt.org/z/qszGT8xj6
>
> This is great but it'll need to support 2D / 3D / 4D / ... matrices cleanly also.
>
> auto const mat = {{0,1},{2,3},{4,5},{6,7}};
>
>
> For parallel computation it'll need to either:
>
> - Apply maths in parallel across the entire piping sequence, element by element, row by row, vector by vector or matrix by matrix or etc.:
>
> auto res = mat | /* operation 1 */ views::transform([](int x) { return x*2; }) | /* operation 2 */ views::transform([](int x) { return x-2; }) | /* operation 3 */ views::transform([](int x) { return x^2; });
>
> The final "res" here will be filled up in parallel, like if op 1 + op 2 + op 3 is one giant function running in parallel
>
>
> - Or apply maths in parallel at each piping operation:
>
> auto res = mat | /* operation 1 */ views::transform([](int x) { return x*2; }) | /* operation 2 */ views::transform([](int x) { return x-2; }) | /* operation 3 */ views::transform([](int x) { return x^2; });
>
> The final "res" here will be filled up with 3 sequential parallel functions, or one parallel function at the time.

I see no reason why both of those cases should be supported by the
exact same API like that. A simple sequence of operations should occur
as described, with simple rules as to how it works.

The simple rule for view chaining is that each view does its
modification lazily, for each iterator position in the composite
range. As such, parallelism between views in a chain isn't a thing; in
order to manifest a value on a thread, you have to do all of the
computations needed to produce that value. And that's fine; if you
want the other kind of parallelism... for some reason, then it should
be done through an API meant to do precisely that thing.

Views are for lazy operations, and lazy operations don't allow to
interleave individual lazy operations between threads.

Received on 2021-06-21 00:35:19