Why OpenMP under ubuntu 12.04 is slower than serial version

前端 未结 3 1163
别跟我提以往
别跟我提以往 2020-12-03 16:13

I\'ve read some other questions on this topic. However, they didn\'t solve my problem anyway.

I wrote the code as following and I got pthread version an

3条回答
  •  猫巷女王i
    2020-12-03 17:07

    WARNING: tho answer is controversial. The trick described below is implementation dependent and can lead to a decrease of performance. Still, it might increase it as well. I strongly recommend to take a look at comments to this answer.


    This doesn't really answer the question, but if you alter the way you parallelize your code, you might get a performance boost. Now you do it like this:

    #pragma omp parallel for
    for(int n = 0; n < sizen; ++n)
        sinTable[n] = std::sin(2 * M_PI * n / sizen);
    

    In this case each thread will compute one item. Since you have 2 cores, OpenMP will create two threads by default. To calculate each value a thread would have to:

    1. Initialize.
    2. Compute values.

    The first step is rather expensive. And both your threads would have to do it sizen/2 times. Try to do the following:

        int workloadPerThread = sizen / NUM_THREADS;
        #pragma omp parallel for
        for (int thread = 0; thread < NUM_THREADS; ++thread)
        {
            int start = thread * workloadPerThread;
            int stop = start + workloadPerThread;
            if (thread == NUM_THREADS - 1)
                    stop += sizen % NUM_THREADS;
            for (int n = start; n < stop; ++n)
                sinTable[n] = std::sin(2 * M_PI * n / sizen);
        }
    

    This way your threads will initialize only once.

提交回复
热议问题