lyjh
lyjh

Reputation: 21

OpenMP running time

I am running some parallel work using OpenMP, but find that speedup does not scale well. I then found that as the number of threads increases, the time for doing the same amount of work increases as well. Here is the example:

void fillingVec(vector<int>& vec) {
    for (int i = 0; i < 1000000; ++i)
        vec.push_back(i);
}

int main() {
    const int num_thread = omp_get_max_threads();
    vector<int> vec;
    double start;
    #pragma omp parallel \
    private(vec)
    {
        int id = omp_get_thread_num();
        start = omp_get_wtime();
        fillingVec(vec);
    }
    double end = omp_get_wtime();
    printf("Elasped time = %f sec\n", end - start);

    return 0;
}

Since I run same work for each thread, I expect the running time is similar for different thread numbers. However, as I tested the program, the running time seems increases linearly as the number of thread increases. Here are the results:

#     time
1     0.004387
4     0.009015
16    0.034197
32    0.230581

Can someone explain why it is the case? Is it simply due to the overhead of OpenMP?

Upvotes: 2

Views: 612

Answers (1)

Alexey Kukanov
Alexey Kukanov

Reputation: 12764

Most likely, the time increase is due to memory allocation overhead. Try reserving the space for the vector (with vec.reserve(1000000);) before filling it with data.

Also, the way you measure time is strange and has a data race, because each thread in the parallel region writes into the shared start variable.

Upvotes: 1

Related Questions