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.