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

Threading Question

David_DiLaura1
New Contributor I
607 Views

The following sequential code (aside from any typos) uses a module so that repeated subroutine calls in the loop can operate on an array that has size and content dependent on the loop. Importantly (for the question being asked here) the subroutines access the array via the module and so the array does not have to be one of the elements in the subroutine parameter list.

 

Is there a way to thread the loop, keeping  the array thread-safe, but NOT have to list the array as a subroutine parameter? Can modules (or parts of modules) be copied and made private?

 

module PlaceForArray
	real(4), allocatable :: array(:,:)
end module

program testit
	use PlaceForArray
	do j = 1,10
		allocate( array(j,j) )
		array = float(j)
		call foo1(Summation)
		!do something with Summation
		call foo1(Prod)
		!do something with Prod
		deallocate( array )
	end do
end program testit

subroutine foo1(ArraySum)
	use PlaceForArray
	ArraySum = sum(array)
	array = ArraySum
end subroutine foo1
subroutine foo2(ArrayProd)
	use PlaceForArray
	ArrayProd = product(array)
end subroutine foo2	

 

 
Labels (1)
0 Kudos
3 Replies
jimdempseyatthecove
Honored Contributor III
579 Views

If (when) you want the module array "array" to be used exclusively by each thread, then declare the array threadprivate.

module PlaceForArray
        !$omp threadprivate(array)
	real(4), allocatable :: array(:,:)
end module

Doing so, provides separate arrays (same name) to each thread.

0 Kudos
jimdempseyatthecove
Honored Contributor III
575 Views

**** Note, each thread must perform an allocation of their array.

Jim Dempsey

David_DiLaura1
New Contributor I
450 Views
0 Kudos
Reply