Reputation: 11651
I currently have a code block like this
UINT8* u = getResult();
for (UINT64 counter = 0; counter < MaxCount; counter++)
{
for (UINT64 index = 0; index < c_uOneMB; ++index)
{
*u++ = genValue();
}
}
Now in order to make this run faster. I am doing something like this. Basically splitting the inner thread into a method. However I have two concerns which I am not sure how to tackle.
Any suggestions on how to accomplish that?
void doSomething(UINT8* u)
{
for (UINT64 index = 0; index < c_uOneMB; ++index)
{
*u++ = genValue();
}
}
UINT8* u = getResult();
for (UINT64 counter = 0; counter < MaxCount; counter++)
{
std::thread t(doSomething,u);
}
doSomethingElse();
Upvotes: 2
Views: 288
Reputation: 37697
With little details you have provided I can give only this:
std::generate_n(std::execution::par, getResult(), MaxCount * c_uOneMB, genValue);
Upvotes: 3
Reputation: 238341
Best way to parallize this for loop with multiple threads
Best way depends on many factors and is subjective. In fact, sometimes (perhaps most of the time) non-parallelised code is faster. If speed is most important, then the best way is whatever you have measured to be fastest.
Using the standard library algorithms is usually straightforward:
std::generate_n(
std::execution::par_unseq,
u,
MaxCount * c_uOneMB,
genValue);
Upvotes: 2