C++ + openmp for parallel computing: how to set up in visual studio

copenmpparallel-processing

I have a c++ program that creates an object and then calls 2 functions of this object that are independent from one another. So it looks like this:

Object myobject(arg1, arg2);
double answer1 = myobject.function1();
double answer2 = myobject.function2();

I would like to have those 2 computations run in parallel to save computation time. I've seen that this could be done using openmp, but couldn't figure out how to set it up. The only examples I found were sending the same calculation (i.e. "hello world!" for example) to the different cores and the output was 2 times "hello world!". How can I do it in this situation?

I use Windows XP with Visual Studio 2005.

Best Answer

You should look into the sections construct of OpenMP. It works like this:

#pragma omp parallel sections
{
   #pragma omp section
   {
      ... section 1 block ...
   }
   #pragma omp section
   {
      ... section 2 block ...
   }
}

Both blocks might execute in parallel given that there are at least two threads in the team but it is up to the implementation to decide how and where to execute each section.

There is a cleaner solution using OpenMP tasks, but it requires that your compiler supports OpenMP 3.0. MSVC only supports OpenMP 2.0 (even in VS 11!).

You should explicitly enable OpenMP support in your project's settings. If you are doing compilation from the command line, the option is /openmp.

Related Topic