Channels ▼

Jack Woehr

Dr. Dobb's Bloggers

Getting Started with OpenMP

May 01, 2009

A couple of quick ideas for getting started with OpenMP.

  1. Compile a cute example
  2. Read a book

Compile a cute example

I am assuming that you are using a garden-variety Unix or Unix-like operating system. This should also work even on MS Windows with Cygwin if you install GCC 4.3.0.

Go to the web page Guide into OpenMP: Easy multithreading programming for C++ and grab the example Calculating the Mandelbrot fractal in parallel and compile it as follows:

$ export PATH=/usr/local/gcc-4.3.2/bin:$PATH Of course, substitute here your own path to a reasonably recent build of gcc which supports OpenMP on your own machine! Ditto below in the LD_LIBRARY_PATH statement. $ which gcc /usr/local/gcc-4.3.2/bin/gcc $ gcc --version gcc (GCC) 4.3.2 Copyright (C) 2008 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. $ gcc -fopenmp mandlebrot_ascii.cpp -lm $ export LD_LIBRARY_PATH=/usr/local/gcc-4.3.2/lib $ ./a.out           ,,,,,,,cccccccccccccccccccccccc888888888MMM@jprajwaM888888cccccc,,,,|          ,,,,,,,ccccccccccccccccccccccc8888888888MMM@@jwoOQrj@M8888888cccccc,,|          ,,,,,cccccccccccccccccccccccc8888888888MMMM@jaroEpaj@MM8888888cccccc,|         ,,,,,ccccccccccccccccccccccc8888888888MMMM@@aOrOJ .raj@MMM888888cccccc|        ,,,,,cccccccccccccccccccccc88888888888MMM@@@wQQGa  MO8w@MMMMM88888ccccc|        ,,,ccccccccccccccccccccccc8888888888MMM@@@jjarP      @wj@@MMMMM8888cccc|       ,,,cccccccccccccccccccccc8888888888MM@@@@@jjawo@      Qwaj@@@@MMMM88cccc|       ,,cccccccccccccccccccccc88888888MMM@aaaraaawwrpQ     Ogrwwaj@@@@wjM88ccc|      ,,,cccccccccccccccccccc8888888MMMMM@jrpjgrro8EQcr@.  oM.PJgEraajapr@M88cc|      ,,ccccccccccccccccccc888888MMMMMM@@@jQQjgrOQQE            p grp8ogg8MM8cc|     ,,ccccccccccccccccccc8888MMMMMMMM@@@jawGP  c                 O..  rgwMM88c|     ,ccccccccccccccccc8888MMMMMMMMMM@@@jawrgGP                        crj@MM88|     ,ccccccccccccccc888MMMMMMMMMMM@@@@jwOgQo                         Epaj@MM88|    ,cccccccccccc88888Mja@@@@@@@@@@@jjjawOM                            ,waj@M88|    ,cccccccc8888888MM@jwaajjjaoajjjjjaarPP                            OOpPjM88|    cccccc88888888MMM@@aoorrwwrOrwwaaawro                                 JjM88|    cccc88888888MMMMM@@jwpJJPggPOO8pwwrpp                               jpa@M88|   ,cc888888888MMMMMM@jjwr.  @     .Epogp                                og@M88|   cc888888888MMMMMM@jjapoPg        GjOE                                pww@M88|   c88888888MMMMMM@aaawrgQc           ,.                                oQjMM88|   c8888888M@@@@@jjaGpggP              w                                Oa@MM88|   8MMMMM@j@@@@jjjwwo@wGc              a                               Jwj@MM88|   MM@@jjrgwawwawpggOJ                                                .wj@@MM88|   MM@@jjrgwawwawpggOJ                                                .wj@@MM88|   8MMMMM@j@@@@jjjwwo@wGc              a                               Jwj@MM88|   c8888888M@@@@@jjaGpggP              w                                Oa@MM88|   c88888888MMMMMM@aaawrgQO           ,.                                oQjMM88|   cc888888888MMMMMM@jjapoPg        GjOE                                pww@M88|   ,cc888888888MMMMMM@jjwr.  @     .Epogp                                og@M88|    cccc88888888MMMMM@@jwpJJPggPOO8pwwrpp                               rpa@M88|    cccccc88888888MMM@@aoorrwwrOrwwaaawro                                 JjM88|    ,cccccccc8888888MM@jwaajjjaoajjjjjaarPP                            OOpPjM88|    ,cccccccccccc88888Mja@@@@@@@@@@@jjjawOM                            ,waj@M88|     ,ccccccccccccccc888MMMMMMMMMMM@@@@jwOgQo                         Epaj@MM88|     ,ccccccccccccccccc8888MMMMMMMMMM@@@jawrgGP                        crj@MM88|     ,,ccccccccccccccccccc8888MMMMMMMM@@@jawGP  c                 O..  rgwMM88c|      ,,ccccccccccccccccccc888888MMMMMM@@@jQQjgrOQQE            p grp8ogg8MM8cc|      ,,,cccccccccccccccccccc8888888MMMMM@jrpjgrro8EQcr@.  oM.PJgEraajapr@M88cc|       ,,cccccccccccccccccccccc88888888MMM@aaaraaawwrpQ     Ogrwwaj@@@@wjM88ccc|       ,,,cccccccccccccccccccccc8888888888MM@@@@@jjawo@      Qwaj@@@@MMMM88cccc|        ,,,ccccccccccccccccccccccc8888888888MMM@@@jjarP      @wj@@MMMMM8888cccc|        ,,,,,cccccccccccccccccccccc88888888888MMM@@@wQQGa  MO8w@MMMMM88888ccccc|         ,,,,,ccccccccccccccccccccccc8888888888MMMM@@aOrOJ .raj@MMM888888cccccc|          ,,,,,cccccccccccccccccccccccc8888888888MMMM@jaroEpaj@MM8888888cccccc,| $

Read a book

Parallel Programming in OpenMP (Morgan Kaufman [Elsevier], 2000) has never gone beyond its approximately ten-year-old first edition, to the best of my knowledge and minimal research on the Web. But it's still awesome.

The book introduces the topic of parallelism and narrows it down to shared memory parallelism which is the domain of OpenMP. Then it teaches the concepts and their implementation in OpenMP ... for C/C++ programmers, it all comes down to #pragmas and libraries (d'oh).

The things you can do in parallel using OpenMP, why you do them, how you do them, and how you can go wrong are described in an orderly, scientific and concise fashion.

The only pain for the "average" programmer is that the book is essentially in Fortran. Some examples are show also in C/C++ but you're translating on the fly in your mind if C/C++ is your main praxis. It's like reading a French Horn score while playing the Trumpet.

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video