Rajeshwar
Rajeshwar

Reputation: 11651

Best way to parallelize this for loop with multiple threads

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.

  1. *u++ how do I handle that?
  2. Before calling doSomethingElse() all the threads need to .join()

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

Answers (2)

Marek R
Marek R

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

eerorika
eerorika

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

Related Questions