g++ for windows

General OpenMP discussion

g++ for windows

Postby kintaro » Fri Jun 20, 2008 3:19 am

Hi,

I'm using the g++ compiler:
# g++ (GCC) 4.3.1
# 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.

under Windows XP 64 on a dual core processor and it seems that with the sample code attached below, I get this output:

#omp_get_max_threads(): 2
#Hello World from thread = 0
#Number of threads = 1


Can anybody help me?

Cheers

Kintaro

main ()
{
omp_set_dynamic(0);
omp_set_num_threads(2);

#pragma omp parallel private(nthreads, tid)
{
int nthreads, tid;
cout << "omp_get_max_threads(): " << omp_get_max_threads() << endl;

/* Obtain thread number */
tid = omp_get_thread_num();
printf("Hello World from thread = %d\n", tid);
/* Only master thread does this */
#pragma omp barrier
if (tid == 0)
{
nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}
}
}
kintaro
 
Posts: 7
Joined: Wed Jun 18, 2008 5:16 am

Re: g++ for windows

Postby ejd » Fri Jun 20, 2008 6:30 am

First a comment about your code. You have used the private clause and declared the variables within the region. You don't have to do both. In fact, some compilers would consider this an error because they would expect that the variables on the clause would already be declared outside of the region. Putting the declaration within the region makes them private, because each thread will see the declaration and make private copies. So instead of this:

Code: Select all
#pragma omp parallel private(nthreads, tid)
{
  int nthreads, tid;

You can do either this:
Code: Select all
int nthreads, tid;
#pragma omp parallel private(nthreads, tid)

or you can do this:
Code: Select all
#pragma omp parallel
{
  int nthreads, tid;

Also, I assume you are just playing with the barrier because it is not needed.

As for why you are only seeing one thread, my guess is that you didn't compile the program with the OpenMP flag. Did you specify "-fopenmp" on the compile??
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Re: g++ for windows

Postby kintaro » Fri Jun 20, 2008 6:34 am

Many thanx!!!

I forgot to add the "-fopenmp" option in the cmd line!

Now it works!

Cheers

Claudio
kintaro
 
Posts: 7
Joined: Wed Jun 18, 2008 5:16 am


Return to Using OpenMP

Who is online

Users browsing this forum: No registered users and 7 guests