Jump to content

  • Log In with Google      Sign In   
  • Create Account


about OpenMP


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 xiajia   Members   -  Reputation: 163

Like
0Likes
Like

Posted 06 March 2013 - 03:38 AM

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, 06 March 2013 - 03:49 AM.


Sponsor:

#2 Yourself   Crossbones+   -  Reputation: 1047

Like
2Likes
Like

Posted 06 March 2013 - 04:44 AM

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



#3 Kambiz   Members   -  Reputation: 758

Like
1Likes
Like

Posted 06 March 2013 - 07:23 AM

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.



#4 Helicobster   Members   -  Reputation: 195

Like
0Likes
Like

Posted 07 March 2013 - 02:38 AM

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:

 

#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;

}

 

 

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS