Thursday, January 13, 2011

Open MP C++ in Visual Studio

To set this compiler option in the Visual Studio development environment

   1. Open the project's Property Pages dialog box. For details, see How to: Open Project Property Pages.
   2. Expand the Configuration Properties node.
   3. Expand the C/C++ node.
   4. Select the Language property page.
   5. Modify the OpenMP Support property.

Verify that _OPENMP directive is set:


#ifdef _OPENMP  
fn();  
#endif
 





Sample of program that displays the number of procesors


#include "stdafx.h"
#include "time.h"
#include < iostream >
#include < memory >
#include < omp.h >
  
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{


    time_t start,end;
    time(&start);

    #pragma omp parallel
     printf("Hello from thread %d, nthreads %d\n",  omp_get_thread_num(), omp_get_num_threads());

    time(&end);

    double dif = difftime(end, start);
    cout<
   
}




// sample using loops


#include "stdafx.h"
#include "time.h"
#include < iostream >
#include < memory >
#include < omp.h >
  
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
   time_t start,end;
   time(&start);
   #pragma omp parallel for default(shared)
   for(int i=0;i<2000000000;i++)
    {
            int a = 3;
            int b = 4;
            int aux;
            aux = a; a=b;b=aux;
            for(int t=0;t<20;t++)
            {
                int a2 = 3;
                int b3 = 4;
                aux = a2; a2=b;b=aux;
            }
     }

    time(&end);
    double dif = difftime(end, start);
    cout<
   
return 0;

}


 

No comments:

Post a Comment