Excludos
Excludos

Reputation: 1670

Filling a vector in order using threads in C++

I'm attempting to fill a huge double vector (1929x1341, might even get bigger) with data, which right now takes around 10 seconds to do.

for reference, this is the code so far:

vector<vector<float>> vector1;
for (int x = 0; x < mapWidth; x++) {
    vector<float> vector2;
    for (int y = 0; y < mapHeight; y++) {
        int someNumber = calculateNumber(x,y);
        vector2.push_back(someNumber);
    }
    vector1.push_back(vector2);
}

I'm thinking I should be able to cut down on the work-time by dividing the work over separate threads. Specifically I could separate the second for-loop into each their own thread.

Unfortunately, I am not good with threads. The main issue is that the vectors needs to be filled in order. So I can't just separate the second vector to their own threads and combine them later, as that would put them into a semi-random order. I've looked into mutex and Condition Variables, but I am not able to find a good solution to this specific problem.

Would anyone be willing to help me out here?

Upvotes: 0

Views: 1354

Answers (2)

Galik
Galik

Reputation: 48625

The tricky part here is having a few threads all working at the same time. When one of the threads becomes free another one takes over to create a new vector.

For this std::future is useful because it allows us to synchronize the collection of results shared between threads. We can start one asynchronous task for each thread and collects its results in a std::future object.

For this I used std::async to create the threads:

#include <queue>
#include <vector>
#include <future>
#include <iostream>

int width = 5;
int height = 3;

float calculateNumber(int x, int y)
{
    return x * y;
}

std::vector<float> fill_info(int x, int height)
{
    std::vector<float> v;
    v.reserve(height);

    for(int y = 0; y < height; ++y)
        v.push_back(calculateNumber(x, y));

    return v;
}

int main()
{
    // our thread limit
    const auto number_of_threads = std::thread::hardware_concurrency();

    // our data container
    std::vector<std::vector<float>> v;

    // queue of asynchronous (shared) results
    std::queue<std::future<std::vector<float>>> q;

    for(int x = 0; x < width; x++)
    {
        if(q.size() >= number_of_threads)
        {
            v.push_back(q.front().get()); // blocks until thread is done
            q.pop();
        }

        q.emplace(std::async(std::launch::async, fill_info, x, height));
    }

    // collect uncollected results
    while(!q.empty())
    {
        v.push_back(q.front().get()); // blocks until thread is done
        q.pop();
    }

    std::cout << v.size()<< '\n';

    for(int x = 0; x < width; ++x)
        for(int y = 0; y < height; ++y)
            std::cout << "{" << x << ", " << y << "}: " << v[x][y] << '\n';
}

Output:

{0, 0}: 0
{0, 1}: 0
{0, 2}: 0
{1, 0}: 0
{1, 1}: 1
{1, 2}: 2
{2, 0}: 0
{2, 1}: 2
{2, 2}: 4
{3, 0}: 0
{3, 1}: 3
{3, 2}: 6
{4, 0}: 0
{4, 1}: 4
{4, 2}: 8

Upvotes: 1

Jarod42
Jarod42

Reputation: 217398

You may do something like:

std::vector<std::vector<float>> vector1(mapWidth);
std::vector<std::thread> threads;

for (int x = 0; x < mapWidth; x++) {
    threads.emplace_back([&, x]() {
        for (int y = 0; y < mapHeight; y++) {
            int someNumber = calculateNumber(x, y);
            vector1[x].push_back(someNumber);
        }
    });
}

for (int x = 0; x < mapWidth; x++) {
    threads[x].join();
}

Upvotes: 1

Related Questions