Example using the SAVE attribute

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

Autres langues :anglais 100% • ‎français 100%
File : omp4.f90
! Description:
!   This example illustrates the influence of the SAVE attribute
!   on variable declarations. When a variable that is declared in
!   a subroutine has the SAVE attribute, its value is kept between
!   subroutine calls. By default, local variables in a subroutine
!   do not have the SAVE attribute.
!
!   Consequences for parallel regions:
!   1. Variables with the SAVE attribute are shared between threads. 
!      So if two threads call the same subroutine at the same time,
!      a variable with the SAVE attribute will be shared between
!      those threads (i.e. one sole memory address used by all
!      threads).
!
!   2. Variables without the SAVE attribute are not shared. Each
!      thread has its own copy of the variable.
!
!  In the first parallel region, the subroutine omp4sub1 operates
!  on a variable with the SAVE attribute set. Hence its operations
!  affect the values that are at the same memory address for all
!  threads.
!
!  In the second parallel region, the subroutine omp4sub2 operates
!  on a variable without the SAVE attribute set. Hence its operations
!  affect values that are at different memory addresses for each
!  thread.
!
! Language: Fortran 95
! Author :  Carol Gauthier, CACPUS, Université de Sherbrooke
! Date :    September 2000
!****************************************************************
 Program omp4
!****************************************************************
 
 write(6,*)'----------------------------------------------------------------'
 write(6,*)'Calling sub1 in parallel (isave has the SAVE attribute)'
 !=================================================================
 !$OMP PARALLEL
 
  call omp4sub1()
 
 !$OMP END PARALLEL
 !================================================================
 write(6,*)  
 
 write(6,*)'----------------------------------------------------------------'
 write(6,*)'Calling sub2 in parallel (inosave does not have the SAVE attribute)'
 !================================================================
 !$OMP PARALLEL
 
  call omp4sub2()
 
 !$OMP END PARALLEL
 !================================================================
 write(6,*)  
 
! Program end
end



File : omp4sub1.f90
! Description:
!   Puts the thread number (ThNum) into all elements of the
!   vector isave. Then verify the value of each element.
!
! Language: Fortran 95
! Author :  Carol Gauthier, CACPUS, Université de Sherbrooke
! Date :    September 2000
!****************************************************************
subroutine omp4sub1()
!****************************************************************
! Declaration of variables
 
 integer ThNum,OMP_GET_THREAD_NUM
 integer i,memerrors
 integer,dimension(1000000),save :: isave
 
!****************************************************************
 
 ThNum=OMP_GET_THREAD_NUM()
 
 do i=1,1000000
  isave(i)=ThNum
 end do
 
 memerrors=0
 do i=1,1000000
  if( isave(i).ne.ThNum )memerrors=memerrors+1
 end do
 
 write(6,*)ThNum,':',' memerrors = ',memerrors
 
! End of the subroutine
 end



File : omp4sub2.f90
! Description:
!   Puts the thread number (ThNum) into all elements of the
!   vector inosave. Then verify the value of each element.
!
! Language: Fortran 95
! Author :  Carol Gauthier, CACPUS, Université de Sherbrooke
! Date :    September 2000
!****************************************************************
subroutine omp4sub2()
!****************************************************************
! Declaration of variables
 
 integer ThNum,OMP_GET_THREAD_NUM
 integer i,memerrors
 integer,dimension(1000000) :: inosave
 
!****************************************************************
 
 ThNum=OMP_GET_THREAD_NUM()
 
 do i=1,1000000
  inosave(i)=ThNum
 end do
 memerrors=0
 do i=1,1000000
  if( inosave(i).ne.ThNum )memerrors=memerrors+1
 end do
 
 write(6,*)ThNum,':',' memerrors = ',memerrors
 
! End of the subroutine
 end



File : Makefile
#####################################################
# makefile
 
FFLAGS= -qsmp=omp -bmaxdata:2000000000 -u
 
OBJS = omp4.o omp4sub1.o omp4sub2.o
 
 
omp1: $(OBJS)
	xlf95_r $(OBJS) $(FFLAGS) -o omp4
 
.f.o: 
	xlf95_r $(FFLAGS) -c $*.f
 
cl:
	rm -f $(OBJS) omp4


Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Ressources de Calcul Québec
Outils
Partager