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           ,,,,,,,[email protected],,,,|          ,,,,,,,[email protected]@[email protected],,|          ,,,,,[email protected]@MM8888888cccccc,|         ,,,,,[email protected]@aOrOJ [email protected]|        ,,,,,[email protected]@@wQQGa  [email protected]|        ,,,[email protected]@@jjarP      @[email protected]@MMMMM8888cccc|       ,,,[email protected]@@@@[email protected]      [email protected]@@@MMMM88cccc|       ,,[email protected]     [email protected]@@@wjM88ccc|      ,,,[email protected]@.  [email protected]|      ,,[email protected]@@jQQjgrOQQE            p grp8ogg8MM8cc|     ,,[email protected]@@jawGP  c                 O..  rgwMM88c|     ,[email protected]@@jawrgGP                        [email protected]|     ,[email protected]@@@jwOgQo                         [email protected]|    ,[email protected]@@@@@@@@@@jjjawOM                            ,[email protected]|    ,[email protected]                            OOpPjM88|    [email protected]@aoorrwwrOrwwaaawro                                 JjM88|    [email protected]@jwpJJPggPOO8pwwrpp                               [email protected]|   ,[email protected]  @     .Epogp                                [email protected]|   [email protected]        GjOE                                [email protected]|   [email protected]           ,.                                oQjMM88|   [email protected]@@@@jjaGpggP              w                                [email protected]|   [email protected]@@@@[email protected]              a                               [email protected]|   [email protected]@jjrgwawwawpggOJ                                                [email protected]@MM88|   [email protected]@jjrgwawwawpggOJ                                                [email protected]@MM88|   [email protected]@@@@[email protected]              a                               [email protected]|   [email protected]@@@@jjaGpggP              w                                [email protected]|   [email protected]           ,.                                oQjMM88|   [email protected]        GjOE                                [email protected]|   ,[email protected]  @     .Epogp                                [email protected]|    [email protected]@jwpJJPggPOO8pwwrpp                               [email protected]|    [email protected]@aoorrwwrOrwwaaawro                                 JjM88|    ,[email protected]                            OOpPjM88|    ,[email protected]@@@@@@@@@@jjjawOM                            ,[email protected]|     ,[email protected]@@@jwOgQo                         [email protected]|     ,[email protected]@@jawrgGP                        [email protected]|     ,,[email protected]@@jawGP  c                 O..  rgwMM88c|      ,,[email protected]@@jQQjgrOQQE            p grp8ogg8MM8cc|      ,,,[email protected]@.  [email protected]|       ,,[email protected]     [email protected]@@@wjM88ccc|       ,,,[email protected]@@@@[email protected]      [email protected]@@@MMMM88cccc|        ,,,[email protected]@@jjarP      @[email protected]@MMMMM8888cccc|        ,,,,,[email protected]@@wQQGa  [email protected]|         ,,,,,[email protected]@aOrOJ [email protected]|          ,,,,,[email protected]@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.