about OpenMP

Started by
2 comments, last by Helicobster 11 years, 1 month ago

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?

Advertisement

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

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.

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 <omp.h>
#include <iostream>

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: "<<t2 - t1<<std::endl;
std::cout<<"sum: "<<sum<<std::endl<<std::endl;//new!

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: "<<t3 - t2<<std::endl;
std::cout<<"sum: "<<sum<<std::endl<<std::endl;//new!
std::cout<<"speed improvement: "<<((t2-t1)/(t3-t2))<<"x"<<std::endl;//new!
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.

This topic is closed to new replies.

Advertisement