Skip to main content
GameDev.net gamedev.net
🔒 Locked

about OpenMP

Started by xiajia Mar 6, 2013 at 9:38 AM 2 replies 1.9k views
Original Post
xiajia
xiajia

I am the beginner of OpenMP.the code below:


#include "windows.h"
#include "math.h"
#include <omp.h>
#include <iostream>

int main(void)
{
    double t1 = omp_get_wtime( );
    for(int i = 0;i < 8;i ++)
    {
        float a = 0;
        for(int j = 0;j < 10000000;j++)
        {
            a += sqrtf(j);
        }
    }
    double t2 = omp_get_wtime( );
    std::cout<<"time: "<<t2 - t1<<std::endl;
#pragma omp parallel for
    for(int i = 0;i < 8;i ++)
    {
        float a = 0;
        for(int j = 0;j < 10000000;j++)
        {
            a += sqrtf(j);
        }
    }
    t1 = omp_get_wtime( );
    std::cout<<"time: "<<t1 - t2<<std::endl;
    system("pause");
    return 0;
}
 

when i release with VS2010,and run,there is no upgrade!In some cases , it will decline.i don't know Y?

Yourself
Yourself

probably because the compiler figures out that you don't use the variable 'a', so it optimizes the code out.

Kambiz
Kambiz

Even if you use 'a', the compile might still optimize the loop away and replace it by a simple assignment. Few compilers out there recognize such simple pattern very well.

Helicobster
Helicobster

OpenMP is supported in Visual C++ 2010, but it isn't enabled by default when you make a new project.

Go to Project->Properties, then Configuration Properties, then C/C++, Language, and set Open MP Support to Yes.

Kambiz and Yourself are correct, too - the compiler is optimising away the code branches that don't go anywhere.


Try switching on OpenMP support in your project and running this:

[source]

#include "windows.h"
#include "math.h"
#include
#include

int main(void)
{
double t1 = omp_get_wtime( );
float sum = 0.0f;//new!
for(int i = 0;i < 8;i ++)
{
float a = 0;
for(int j = 0;j < 10000000;j++)
{
a += sqrtf(j);
}
sum += a;//new!
}
double t2 = omp_get_wtime( );
std::cout<<"time: "< std::cout<<"sum: "<
sum = 0.0f;//new!
#pragma omp parallel for
for(int i = 0;i < 8;i ++)
{
float a = 0;
for(int j = 0;j < 10000000;j++)
{
a += sqrtf(j);
}
sum += a;//new!
}
double t3 = omp_get_wtime( );
std::cout<<"time: "< std::cout<<"sum: "< std::cout<<"speed improvement: "<<((t2-t1)/(t3-t2))<<"x"< system("pause");
return 0;
}

[/source]

You should see a speed improvement that's close to your CPU's core-count. I get around 3.8x on a quad-core.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.