For Programmers: Free Programming Magazines  


Home > Archive > Fortran > February 2007 > OpenMP "Hello World" problems?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author OpenMP "Hello World" problems?
joaquin.casanova@gmail.com

2007-02-24, 7:04 pm

I'm trying to learn OpenMP and I have a question. On an Intel Core 2
Duo, running Suse 10, and Intel Fortran Compiler 9.1, the following
program:

PROGRAM hello

use omp_lib
implicit none
integer :: i, n

call omp_set_num_threads(2)
n = omp_get_num_threads()
write(*,*) 'There are',n,'threads'

!$omp parallel private(i)
i = omp_get_thread_num()
write(*,*) 'Hello World from thread',i
!$omp end parallel

END PROGRAM

ifort -openmp hello.f90

I get this output:

There are 1 threads
Hello World from thread 0
Hello World from thread 1

So my question is, why does it say 1 thread when I specify 2, and then
why does it count up from 0? Is this normal OMP behaviour, or I am I
doing something wrong? Thanks for your help.

blmblm@myrealbox.com

2007-02-24, 7:04 pm

In article <1172342173.141883.109070@s48g2000cws.googlegroups.com>,
<joaquin.casanova@gmail.com> wrote:
> I'm trying to learn OpenMP and I have a question. On an Intel Core 2
> Duo, running Suse 10, and Intel Fortran Compiler 9.1, the following
> program:
>
> PROGRAM hello
>
> use omp_lib
> implicit none
> integer :: i, n
>
> call omp_set_num_threads(2)
> n = omp_get_num_threads()
> write(*,*) 'There are',n,'threads'
>
> !$omp parallel private(i)
> i = omp_get_thread_num()
> write(*,*) 'Hello World from thread',i
> !$omp end parallel
>
> END PROGRAM
>
> ifort -openmp hello.f90
>
> I get this output:
>
> There are 1 threads
> Hello World from thread 0
> Hello World from thread 1
>
> So my question is, why does it say 1 thread when I specify 2,


Because at the point at which you obtain the number of threads,
there is only one. You don't get multiple threads until you enter
a parallel block. (Caveat: This is at the conceptual level, not
at the implementation level. An implementation might try to reduce
overhead due to thread creation/destruction by creating all needed
threads once at the start of the program. But conceptually there's
only one thread at the point at which you call omp_get_num_threads.)

> and then
> why does it count up from 0? Is this normal OMP behaviour, or I am I
> doing something wrong? Thanks for your help.


Here I'm guessing, but this is normal behavior in C-plus-OpenMP and
presumably is done to match C's 0-based array indexing. It makes
less sense in Fortran, but maybe it's not feasible for OpenMP to
make the needed distinction?

--
B. L. Massingill
ObDisclaimer: I don't speak for my employers; they return the favor.
Leif Harcke

2007-02-24, 7:04 pm

On Sat, 24 Feb 2007 10:36:13 -0800, joaquin.casanova wrote:

> So my question is, why does it say 1 thread when I specify 2, and
> then why does it count up from 0? Is this normal OMP behaviour, or I
> am I doing something wrong?


This is normal OpenMP behavior. Try this modified test program:

program hello

use omp_lib
implicit none
integer :: i, n

call omp_set_num_threads(2)

!$omp parallel private(i,n)
n = omp_get_num_threads()
i = omp_get_thread_num()
print *, 'Hello World from thread ', i, ' of ', n
!$omp end parallel

end program hello

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com