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

How can a parllel Fortran loop (OpenMp) call an external function or subroutine correctly?

vahid_a_
Novice
825 Views

Greetings, community!

 

The code below calls a simple scalar-valued function from a parallel loop. As can be seen from the test result, the parallel mode result of the function call (array a) is 0. There's something wrong with this! In serial mode, the code works fine.

 

Could you please suggest a clean/easy workaround for this type of call from a parallel loop? Searching for an answer didn't yield any results...

 

I would also appreciate if you could point out a good reference book that discusses this type of problems/tutorials.

 

The code:

 

 

module init

	integer, parameter :: nx = 5
	real, dimension(1:nx) :: order_parameter,a,b

	
	contains
	
	real function h(phi) !result(h)
        real :: phi
        h = phi**3*(10.0 - 15.0*phi + 6.0*phi**2)
    end function

end module 


program main

	use omp_lib
	use init
	
	implicit none
	integer :: i
	
	order_parameter = [ 0.0,0.25,0.5,0.75,1.0 ]
	
	!$OMP PARALLEL default(private) &
	!$OMP shared(a,b) 

    !$OMP do   
	do i=1,nx
	
		a(i) = h(order_parameter(i))
		b(i) = 1.0 
		
	end do
    !$OMP end do   

    !$OMP end parallel
    
	write(*,*) order_parameter
	write(*,*) 'a:',a
	write(*,*) 'b:',b

		

end program main

 

 

 

PARALLEL run output of "ifort -O2 -qopenmp openmp_func_call_test.f90" on Mac OS:

 

 

  0.0000000E+00  0.2500000      0.5000000      0.7500000       1.000000    
 a: -7.6655056E-26  0.0000000E+00  0.0000000E+00  0.0000000E+00  0.0000000E+00
 b:   1.000000       1.000000       1.000000       1.000000       1.000000

 

 

 

SERIAL run output of "ifort -O2 openmp_func_call_test.f90" on Mac OS:

 

 

 

  0.0000000E+00  0.2500000      0.5000000      0.7500000       1.000000    
 a:  0.0000000E+00  0.1035156      0.5000000      0.8964844       1.000000    
 b:   1.000000       1.000000       1.000000       1.000000       1.000000

 

 

 

0 Kudos
1 Reply
Arjen_Markus
Honored Contributor I
777 Views

The problem is that the variable order_parameter has the attribute "private", as that is the default. I realised this when I printed the values inside the do-loop. Add it to the list of shared variables and the program works correctly. (BTW: As i is an iteration variable it is automatically made private and handled correctly)

Reply