Promote Your Blog

Wednesday, January 18, 2012

Using OpenMP with MS Visual C++

Using OpenMP to exploit multicore/multiprocessor systems in VC++ programs is this simple:

1. Enable OpenMP support in MS Visual Studio (see figure)
2. Add following headers to your C++ program:


3. To know, how many processors (cores are also treated as processors), call this following OpenMP API function:

int iCPU = omp_get_num_procs();

4. Now set number of threads to be created (say for data parallelism in case of loop parallelization)


5. To actually, convert a single thread loop to multithread, insert this OpenMP pragma

double start = omp_get_wtime();

#pragma omp parallel for private (si)

for loop

nested for loop(si=0,si<1000;si++)



double end = omp_get_wtime();

double time_taken = end - start;

6. omp_get_wtime() is called before and after the loop, so as to determine total time taken in execution (wall time).

7. For sure, more number of threads lesser the time in execution

8. To actually confirm this, check under Performance tab of Task Manager (Alt-Ctrl-Del). % CPU Usage is more for more threads meaning better CPU utilization

Caution: Task parallelized should be of greater load than overhead of parallelization. This is very important. Otherwise, you may end up with false notations that OpenMP degrades performance...

No comments:

Post a Comment