Giter Site home page Giter Site logo

Comments (6)

CaseyCarter avatar CaseyCarter commented on August 19, 2024 1

Should generate and generate_n also be parallelized?

Your sample program is a great demonstration of why generate and generate_n can't be meaningfully parallelized: the function object is almost always stateful, and evaluations almost always order-dependent. I suspect the writer of this program would be very unhappy if the library spawned 16 threads with their own copies of the lambda resulting in all elements of v_c being equal to v_a[0] + v_b[0].

from stl.

AlexGuteniev avatar AlexGuteniev commented on August 19, 2024

Should generate and generate_n also be parallelized?

Here's how I try to compensate lack of transform with unseq

#include <vector>
#include <execution>

std::vector<int> v_a(16);
std::vector<int> v_b(16);
std::vector<int> v_c(16);

void sum(int* c, int* a, int* b)
{
    std::generate_n(std::execution::unseq, c, 16, [=]() mutable {
        return *(a++) + *(b++);
    });
}

int main()
{
    sum(v_c.data(), v_a.data(), v_b.data());
}

from stl.

AlexGuteniev avatar AlexGuteniev commented on August 19, 2024

Why does execution policy parameter even exist for them?

from stl.

BillyONeal avatar BillyONeal commented on August 19, 2024

Why does execution policy parameter even exist for them?

Because the 'design' process for the parallel algorithms was 'if nobody can come up with a reason why it can't be parallelized, without looking at real implementations, in 10 minutes, it gets a parallel one'. Note that partial_sort has such an overload too even though it is a heap algorithm and none of the other heap algorithms got parallel overloads.

from stl.

AlexGuteniev avatar AlexGuteniev commented on August 19, 2024

I'm trying to make this work without #pragma in my code:

#include <vector>

std::vector<int> v_a(16);
std::vector<int> v_b(16);
std::vector<int> v_c(16);

void sum(int c[], int a[], int b[])
{
#pragma loop(ivdep)
	for (int i = 0; i < 16; i++)
	{
		c[i] = a[i] + b[i];
	}
}

int main()
{
	sum(v_c.data(), v_a.data(), v_b.data());
}

Note that without a pragma the compilers still emits SIMD version, but goes to it conditionally at runtime.

Now that this is useless:

void sum(int * c, int* a, int * b)
{
   std::transform(std::execution::unseq, a, a + 16, b, c, std::plus{});
}

As well as this:

void sum(int* c, int* a, int* b)
{
    std::for_each(std::execution::unseq, c, c + 16, [=](int& v) {
        v = a[&v - c] + b[&v - c];
    });
}

How else I can say it?

from stl.

BillyONeal avatar BillyONeal commented on August 19, 2024

I don't believe we have a way to say that without a pragma at this time. It's really on the optimizer team if they want to consume the unseq signal and they have declined to do so as of yet.

from stl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.