Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28863 Discussions

How to assign thread ID in OpenMP parallel DO

david47
Novice
247 Views

I am trying to parallelize a DO loop such as,

Nthread = OMP_get_max_threads()

allocate (A(N,Nthread))

A = 0.

!$OMP PARALLEL NUM_THREADS(Nthread) DEFAULT(shared) PRIVATE (i, ithread)

ithread = OMP_get_thread_num()+1 ! ... does this line belong here?

!$OMP DO SCHEDULE (static)

    do i = 1,N

        A(i,ithread) = ' ... results of long calculations'

    end do

!$OMP END DO

!$OMP END PARALLEL

 

I want to allocate memory on my own and not have OpenMP make copies of the array A.  Is the location of my call to ithread correct?  I thought that after the call to the parallel, OpenMP spawns Nthread jobs but if I call ithread the way I have it, OpenMP will run the DO loop Nthread times which is not what I wanted.

Could someone please help me to understand how I can specify the thread ID's (i.e., ithread) to load the array A?

Thank you all so much for your help!

Sincerely,

David

0 Kudos
2 Replies
TobiasK
Moderator
147 Views

@david47 
I am not sure if I get your question / intention right.

First !$omp parallel is not a call to parallel, it instructs the compiler to create a parallel region and create threads according to the value of OMP_NUM_THREADS.

The array A is shared among threads as you do not declare it as private. There are no additional copies of array A.

!$omp do

Is a work sharing construct, the following iteration space of the do loop is distributed among threads created by the parallel region.
e.g. OMP_NUM_THREADS=2, N=10, iteration 1-5 will be executed by thread 0, 6-10 will be executed by thread 1, (assuming static scheduling).

With your usage of omp_get_thread_num() you will see:

A(1:5,1)=some data
A(5:10,1)=0
A(1:5,2)=0
A(5:10,2)=some data

However, the do loop is for sure not executed nthread times.

 

Best
Tobias

 

0 Kudos
david47
Novice
114 Views

Thank you Tobias for your reply!

0 Kudos
Reply