• Advertisement
Sign in to follow this  

about OpenMP

This topic is 1783 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Edited by xiajia

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement