Welcome to my tutorial on how to get started with writing OpenMP applications in Visual Studio. First things first, OpenMP is not available for the express or standard versions of Microsoft Visual Studio. Therefore, you will need the professional version or higher if you want to use visual studio to develop OpenMP project. There might be a way to work around this limitation by trying to download vcomp.lib or vcomp.dll, however, the legality of doing that is questionable. For this tutorial, I will be using Visual Studio 2005 professional edition, however, this tutorial will apply to almost all recent versions of Visual Studio.
Step 1: Create your project
The simplest way to test your first OpenMP program would be to create a new project. For this tutorial, we will be creating a Win32 Console Application. If you select Visual C++ -> Win32, you should see the Console Application as an option. On the next dialog, click application settings, then clear the checkbox next to “Precompiled Header”. Click the “Finish” button.
Step 2: Enable OpenMP
OpenMP does a lot of things automatically for us programmers. However, that means that it’s very complicated for the compiler to handle OpenMP. We’ll need to manually enable OpenMP. Right-click on your project, and select properties. Select C/C++ -> Language, and change “OpenMP Support” to ‘Yes’. Click ok, and make sure your application still compiles and runs.
Step 3: Include omp.h
While you may not need it right away, it’s a good idea to go ahead and include omp.h in your source code. Visual Studio will already know the location, so all you have to do is type “#include <omp.h>” at the top of your source code.
Step 4: Try out your program and see if it runs on multiple threads
For this step, you’ll want to download the source code here.
Troubleshooting: Changing the number of threads
If you look at the source code, you can see that the program should print, “Running on multiple threads”, once for each thread. Therefor, if you only see your program print this statement once, that means your program is only running on one thread. By default, Visual studio should detect the total number of processors available in your computer, and start your program with said number of threads. However, if you have a single-core computer, only one thread will be created for your program.
To change this behavior, right-click on your project in the Solution Explorer window, and select properties. In the left-hand column, select “Debugging”. There is a field called “Environment”, which is where we can set environment variables. Try adding this into the environment variables: “OMP_NUM_THREADS=4”, and see if your program now spawns with four threads.
Next Tutorial: The basics of OpenMP