Add this blog to Del.icio.us, Digg or Furl | Create Watchlist for this blog

Intel C++ 10, OpenMP & OpenSuSE Linux

OpenMP on Linux with Intel iccAs promised yesterday, I’ll try the OpenMP features of Intel C++ 10.0 for Linux today. Cause I’m lazy, I use a wide spread “Hello World” for OpenMP example, it is very simple and it’s only purpose is to demonstrate the basics of how to parallelize a code with OpenMP.

/***********************************************************
* FILE: omp_hello.c
* DESCRIPTION:
*   OpenMP Example - Hello World - C/C++ Version
*   In this simple example, the master thread
*   forks a parallel region. All threads in the team obtain
*   their unique thread number and print it. The master
*   thread only prints the total number of threads.  Two OpenMP
*   library routines are used to obtain the number of threads
*   and each thread's number.
* AUTHOR: Blaise Barney  5/99
* LAST REVISED: 04/06/05
************************************************************/
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char **argv) {

int nthreads, tid;

/* Fork a team of threads giving them their own copies of variables */
#pragma omp parallel private(nthreads, tid)
  {

  /* Obtain thread number */
  tid = omp_get_thread_num();
  printf("Hello World from thread = %dn", tid);

  /* Only master thread does this */
  if (tid == 0)
    {
    nthreads = omp_get_num_threads();
    printf("Number of threads = %dn", nthreads);
    }

  }  /* All threads join master thread and disband */

}

To compile this example with icc you need the OpenMP switch turned on:

rainer@novotop:/tmp> icc -openmp omp_hello.c
omp_hello.c(22): (col. 1) remark: OpenMP DEFINED REGION
WAS PARALLELIZED.
rainer@novotop:/tmp> ./a.out
Hello World from thread = 0
Number of threads = 2
Hello World from thread = 1

Ok, that’s pretty cool. We saw how to parallelize a region and giving private variables to it. By the way, you can specify the number of threads OpenMP starts with the OMP_NUM_THREADS environment variable (export OMP_NUM_THREADS=4, then start a.out -> you get four thread outputs).
Next post will handle mutexes, semaphores, thread shared memory etc. in more depth.

About installing Intel C++ 10.0 on OpenSuSE 10.2
A good tutorial

Social Bookmarking:
Add to social bookmarking sites.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • MisterWong
  • Furl
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati

And I've started a Drupal relaunch of my EDV Beratung now.

Leave a Reply

I appreciate all comments that add substantial content to my articles. I do not like "link-building" comments and I do not like "congratulation" comments. Every such like data posted here will go to akismet hell.