Thread: Enabling C++ OpenMP parallel programming for Code::Blocks

  1. #1
    Registered User FortranLevelC++'s Avatar
    Join Date
    May 2013
    Location
    United States
    Posts
    81

    Enabling C++ OpenMP parallel programming for Code::Blocks

    Hello.

    I am beginning to learn OpenMP for C++ for parallel programming (with only one node, shared memory computer). I am using Ubuntu Linux 64 bit, g++ and also Code::Blocks. The CPU is a desktop AMD with 8 cores.

    I have started to study he tutorial site:
    https://computing.llnl.gov/tutorials.../exercise.html

    It seems that it is easy to use g++ to build a program using OpenMP, everything is running perfectly. But in order to enable OpenMP for Code::Blocks I need some help, because I get some errors. Many thanks in advance for your precious time.

    First of all, let me explain how I managed to make g++ work with OpenMP for the test program. When I build the test program below, the following g++ command with "-fopenmp" works:

    g++ -fopenmp omp_hello.c -o omp_hello

    And here is the test program:

    Code:
    /******************************************************************************
    * 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 = %d\n", tid);
    
    
      /* Only master thread does this */
      if (tid == 0) 
        {
        nthreads = omp_get_num_threads();
        printf("Number of threads = %d\n", nthreads);
        }
    
    
      }  /* All threads join master thread and disband */
    
    
    }
    And here is the output of the program (built with g++) at the console. Because this combination of g++ and OpenMP is smart enough to figure out that there are 8 cores in the AMD desktop CPU, it automatically matched the number of threads to the number of cores and it gave this result:
    ------------------------------
    > ./omp_hello
    Hello World from thread = 4
    Hello World from thread = 5
    Hello World from thread = 7
    Hello World from thread = 3
    Hello World from thread = 1
    Hello World from thread = 6
    Hello World from thread = 0
    Number of threads = 8
    Hello World from thread = 2
    ----------------------------

    But when I build the same program with Code::Blocks, I am unable to choose the settings properly, and only one thread is executing when I run the same program I have built with Code::Blocks
    --------------
    Hello World from thread = 0
    Number of threads = 1
    ---------------------

    When I use Code::Blocks, I add "-fopenmp" (without quotation marks) to the menu:

    Compiler and Debugger -> Linker Settings -> Other Linker Options

    and I also added the include search path for the header file omp.h in the appropriate menu:
    /usr/lib/gcc/x86_64-linux-gnu/4.6/include

    so that this prevents error messages when I build the project with Code::Blocks.

    However, I get the warning message which says:

    "warning, ignoring #pragma omp parallel [-Wunknown -pragmas]

    I believe that this warning is an indication that I still need to set up something.

    I will appreciate any suggestions. It seems that the program is not finding the 8 cores of the CPU to run 8 threads with one thread per core.
    Last edited by FortranLevelC++; 09-16-2013 at 10:48 PM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Try adding -fopenmp to the compiler settings.
    Maybe leave it in the linker settings too (?).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User FortranLevelC++'s Avatar
    Join Date
    May 2013
    Location
    United States
    Posts
    81
    Quote Originally Posted by oogabooga View Post
    Try adding -fopenmp to the compiler settings.
    Maybe leave it in the linker settings too (?).

    Many thanks! You are absolutely correct. I have removed -fopenmp from the linker settings of Code::Blocks, and transferred it to the compiler settings, and sure enough, this time there was no warning and the output of the program is perfect:

    ------------
    Hello World from thread = 2
    Hello World from thread = 0
    Number of threads = 8
    Hello World from thread = 6
    Hello World from thread = 1
    Hello World from thread = 7
    Hello World from thread = 3
    Hello World from thread = 5
    Hello World from thread = 4
    ---------------------
    Last edited by FortranLevelC++; 09-16-2013 at 10:45 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parallel programming in C
    By gevni in forum C Programming
    Replies: 6
    Last Post: 06-26-2013, 05:49 PM
  2. only one thread shows on openMP code inside
    By mariola82 in forum C++ Programming
    Replies: 2
    Last Post: 07-15-2011, 08:35 AM
  3. Replies: 1
    Last Post: 06-20-2011, 07:46 AM
  4. Replies: 3
    Last Post: 03-16-2011, 06:31 PM
  5. OpenMP parallel threads for matrix multiplier
    By collymitch in forum C Programming
    Replies: 0
    Last Post: 04-07-2005, 04:38 PM