Introduction to OpenMP using Visual C++ 2005

OpenMP is an application programming interface (API) for shared memory multiprocessing programming on multiprocessor/multicore systems. OpenMP consists of a set of compiler directives and a set of support functions. OpenMP works in conjunction with Fortran, C and C++. It is tailored for Shared Memory environments. The Shared Memory model is an abstraction of centralized multiprocessor system consisting of a collection of processors with access to the same shared memory. Processors can interact and synchronize with each other through shared variables.

In shared memory model, the standard model of parallelism is Fork/Join parallelism. In Fork/Join parallelism, there will be only one thread is active at the start and end and is which is called master thread. Master thread executes the sequential portion and at some points where the parallelism is needed, master thread forks (creates or awakens) additional threads. The master thread and child threads executes in parallel through the parallel sections. At the end the created threads die or are suspended. The flow of controls returns to the master threads.

In OpenMP, the directives are expressed as pragmas (pragmatic informations). Following are the compiler directives & functions:

    parallel which preceeds a block to be executed in parallel
    for which preceeds a for block to be executed in parallel
    sections which preceeds section of blocks to be executed in parallel
    critical which preceeds critical section
    single which preceeds block of code that is to be executed by a single thread
      omp_get_num_procs() which returns the number of processors
      omp_get_num_threads() which returns the number of threads
      omp_get_thread_num() which returns the number of thread within group of threads
      omp_set_num_threads() which sets the number of threads for the parallel region

    Visual Studio Professional and above supports OpenMP. The Following program demonstrates a very simple use of OpenMP in Visual C++ 2005.
    #define _USE_MATH_DEFINES // for math defines in math.h


    #define ANGLE_MAX 360

    void main(int argc, char* argv[])
    int nDegree = 0;
    double dSins[ANGLE_MAX];

    #pragma omp parallel for
    for(nDegree=0; nDegree
    In order to compile it properly you have the use the /openmp compiler switch. You can set it from Project Properties->Configuration Properties->C/C++->Language:

    Ompoptions 150x150

    For a detailed list of OpenMP directives and support function please refer to this MSDN Documentation. In next few days I will try to update this post with more examples.

    2 Responses

    Leave a Reply

    Your email address will not be published. Required fields are marked *