C
C
Chestik2017-06-06 16:42:49
C++ / C#
Chestik, 2017-06-06 16:42:49

How to parallelize a loop using thread?

The len variable is always equal to 32, so I divide it into 4 threads, each of which is supposed to perform 8 calculations, but it turns out that the calculation time increases compared to one thread. What am I doing wrong?
The loop itself:

vector<thread> thds;
for (int i = 0; i < len; i = i+4) { // основной цикл умножения
    thds.emplace_back(thread (multi, x, y, ref(res), len, i));
    thds.emplace_back(thread(multi, x, y, ref(res), len, i+1));
    thds.emplace_back(thread(multi, x, y, ref(res), len, i+2));
    thds.emplace_back(thread(multi, x, y, ref(res), len, i+3));
    thds[i].join();
    thds[i+1].join();
    thds[i+2].join();
    thds[i+3].join();
    //cout << i << endl;
  }

Function:
void multi(vector<int>& x, vector<int>& y, vector<int>& res, int len, int i) {
  for (auto j = 0; j < len; ++j)
    res[i + j] += x[i] * y[j];
}

PS I ask you not to scold me much for the implementation, I work with streams for the first time.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
devalone, 2017-06-06
@devalone

Why create threads every time in a loop? It is better to first create the required number of threads, taking into account their number in the system, and give the threads a range for calculations. Maybe you should take something ready-made like en.cppreference.com/w/cpp/thread/async see the example below, there is just a parallel summation of the vector elements. Note that if there are less than 1000 elements, no threads are created at all.

E
exaw, 2017-06-15
@exaw

To organize parallel computing, OpenMP or Intel tbb are usually used. There, planning, balancing, creating threads comes out of the box. Try it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question