Example using the THREADPRIVATE directive and the COPYIN clause

De Wiki de Calcul Québec
Aller à : Navigation, rechercher
Cette page est une traduction de la page Exemple pour l'utilisation de la directive THREADPRIVATE et de la clause COPYIN et la traduction est complétée à 100 % et à jour.

Autres langues :anglais 100% • ‎français 100%
File : omp3.f90
! Description :
!   Example for using the THREADPRIVATE directive, which can
!   be used to declare common blocks that are private for each
!   thread. Normally, the memory used by a common block is
!   shared by all threads. The THREADPRIVATE directive must
!   appear immediately after the common block declaration,
!   and also in every subroutine where this common block is
!   declared.
!
!   In the first parallel region, the /cbl/ common block is
!   shared between threads, whereas in the second parallel region
!   the /cb2/ common block is private for each thread. The
!   omp3sub1 and omp3sub2 subroutines do operations that affect
!   the values of their respective common blocks. Each of those
!   subroutines is within a parallel region, so they are called
!   by each thread within the team of threads.
!
!   The third and fourth parallel regions show the usage of the
!   COPYIN clause, allowing the initialization of common block
!   variables just before the parallel region is entered.
!
! Language: Fortran 95
! Author:   Carol Gauthier, CACPUS, Université de Sherbrooke
! Date:     September 2000
!****************************************************************
 Program omp3
!****************************************************************
! Declaration of variables
 
  integer a1(1000),b1(1000)
  integer a2(1000),b2(1000)
  integer a3(1000),b3(1000)
 
  common /cb1/ a1,b1
  common /cb2/ a2,b2
  common /cb3/ a3,b3
  !$OMP THREADPRIVATE(/cb2/,/cb3/)
 
!****************************************************************
! Demonstration for using the THEADPRIVATE directive
 
 write(6,*)'-------PARALLEL without THREADPRIVATE----------------'  
 !================================================================
 !$OMP PARALLEL
 
  call omp3sub1()
 
 !$OMP END PARALLEL
 !================================================================
 write(6,*)  
 
 write(6,*)'-------PARALLEL with THREADPRIVATE(/cb2/)------------'  
 !================================================================
 !$OMP PARALLEL
 
  call omp3sub2()
 
 !$OMP END PARALLEL
 !================================================================
 write(6,*)  
 
 ! Initialisation of a3 and b3
 a3=1
 b3=1
 
 write(6,*)'--PARALLEL with THREADPRIVATE(/cb3/)------------------'  
 !================================================================
 !$OMP PARALLEL
 
  call omp3sub3()
 
 !$OMP END PARALLEL
 !================================================================
 write(6,*)  
 
 ! Reinitialization of a3 and b3
 a3=1
 b3=1
 
 write(6,*)'--PARALLEL COPYIN(/cb3/) with THREADPRIVATE(/cb3/)----'  
 !================================================================
 !$OMP PARALLEL COPYIN(/cb3/)
 
  call omp3sub3()
 
 !$OMP END PARALLEL
 !================================================================
 write(6,*)  
 
! Program end
end



File : omp3sub1.f90
! Description :
!    Fills the vectors a1 and b1 5000 times with the value of the 
!    thread number (ThNum) and verifies that the vectors' values
!    equal ThNum. If not, add one to the variable memerrors.
!    The /cb1/ common block is shared between threads.
!
! Language: Fortran 95
! Author:   Carol Gauthier, CACPUS, Université de Sherbrooke
! Date :    September 2000
!****************************************************************
subroutine omp3sub1()
!****************************************************************
! Declaration of variables
 
 integer ThNum,OMP_GET_THREAD_NUM
 integer i,memerrors,repetitions
 integer a1(1000),b1(1000)
 
 common /cb1/ a1,b1
 
!****************************************************************
 
 ThNum=OMP_GET_THREAD_NUM()
 
 memerrors=0
 do repetitions=1,5000
   do i=1,1000
    a1(i)=ThNum
    b1(i)=ThNum
   end do
   do i=1,1000
    if( (a1(i).ne.ThNum).or.(b1(i).ne.ThNum) )memerrors=memerrors+1
   end do
 end do
 
 write(6,*)ThNum,':',' memerrors=',memerrors
 
! End of the subroutine
 end



File : omp3sub2.f90
! Description :
!    Fills the vectors a2 and b2 5000 times with the value of the 
!    thread number (ThNum) and verifies that the vectors' values
!    equal ThNum. If not, add one to the variable memerrors.
!    The /cb2/ common block is private for each thread, that is,
!    each thread has its own copy.
!
! Language: Fortran 95
! Author:  Carol Gauthier, CACPUS, Université de Sherbrooke
! Date :    September 2000
!****************************************************************
subroutine omp3sub2()
!****************************************************************
! Declaration of variables
 
 integer ThNum,OMP_GET_THREAD_NUM
 integer i,memerrors,repetitions
 integer a2(1000),b2(1000)
 
 common /cb2/ a2,b2
 !$OMP THREADPRIVATE(/cb2/)
 
!****************************************************************
 
 ThNum=OMP_GET_THREAD_NUM()
 
 memerrors=0
 do repetitions=1,5000
   do i=1,1000
    a2(i)=ThNum
    b2(i)=ThNum
   end do
   do i=1,1000
    if( (a2(i).ne.ThNum).or.(b2(i).ne.ThNum) )memerrors=memerrors+1
   end do
 end do
 
 write(6,*)ThNum,':',' memerrors=',memerrors
 
! End of the subroutine
 end



File : omp3sub3.f90
! Description :
!    Displays the values of the first element of the vectors
!    a3 and b3. These variables belong to the /cb3/ common block
!    which is private to each thread, that is, each thread has its
!    own copy.
!
! Language: Fortran 95
! Author:   Carol Gauthier, CACPUS, Université de Sherbrooke
! Date:     September 2000
!****************************************************************
subroutine omp3sub3()
!****************************************************************
! Declaration of variables
 
 integer ThNum,OMP_GET_THREAD_NUM,i,memerrors
 integer a3(1000),b3(1000)
 
 common /cb3/ a3,b3
 !$OMP THREADPRIVATE(/cb3/)
 
!****************************************************************
 
 ThNum=OMP_GET_THREAD_NUM()
 
 ! Verification of the initial value
 write(6,*)ThNum,':', 'a3(1),b3(1) = ',a3(1),b3(1)
 
! End of the subroutine
 end



File : Makefile
#####################################################
# makefile
 
FFLAGS= -qsmp=omp -bmaxdata:2000000000 -u
 
OBJS = omp3.o omp3sub1.o omp3sub2.o omp3sub3.o
 
 
omp1: $(OBJS)
	xlf95_r $(OBJS) $(FFLAGS) -o omp3
 
.f.o: 
	xlf95_r $(FFLAGS) -c $*.f
 
cl:
	rm -f $(OBJS) omp3


Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Ressources de Calcul Québec
Outils
Partager