OpenMP with ifort on MAC

OpenMP with ifort on MAC


I have been having an issue which I have tried to summarise in the following two pieces of code:

file: testCaller.f90


module testCaller
use omp_lib
implicit none


subroutine testCall(a_Matrix, a_Index)

implicit none
double precision, dimension(:,:), allocatable, intent(out):: a_Matrix
integer, intent(in):: a_Index

integer:: k1, k2, id

allocate(a_Matrix(a_Index, a_Index))
id = omp_get_thread_num()
write(*,*) 'I am in', id
do k1 = 1, a_Index
do k2 = 1, a_Index
a_Matrix(k1,k2) = 0.0d0
end do
end do

end subroutine testCall

end module testCaller


file: testCalling.f90


program testCalling

use testCaller

implicit none

double precision, dimension(:,:), allocatable:: M
integer:: I

I = 10

call testCall(M,I)

end program testCalling


compilation instruction:

ifort -openmp -parallel -fpp -c testCaller.f90 

ifort -c testCalling.f90 

ifort -openmp -parallel -fpp testCaller.o testCalling.o -o testCall.out

Upon execution I get the following error:

forrtl: severe (40): recursive I/O operation, unit -1, file unknown

I hope I have been able to recreate the problem using this, I am not really sure why this is happening. Can someone please provide some insights?


6 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Strange? That program should compile and run.
Are you linking in the correct libraries (in particular the OpenMP library supplied with IFV)?
I notice you are using ifort for link only. What happens when you compile and link at the same time

ifort -openmp -parallel -fpp -c testCaller.f90
ifort -openmp -parallel -fpp testCalling.f90 testCaller.o -o testCall.out

As a test for optimization error, insert after the call

write(*,*) M(2,2)

IOW use M such that the compiler does not optimize it out.

Granted, if this is an optimization error, some benign use of M may provide a work around. (e.g. test one element for NaN)

Jim Dempsey


Thanks for your suggestion. I tried it and it works now. Could you explain what was the issue ?

I think the problem is that, as Jim mentioned, you did not compile the main program with -openmp. One of the things this does is tell the run-time library that you are using threads. What then happened was when you did the WRITE to standard output inside the parallel region, you violated the Fortran language rule against starting an I/O operation on a unit while another one is in progress, hence the error. By compiling the main program also with -openmp, this tells the libraries to protect against threaded access and allows the writes to serialize.

Retired 12/31/2016

So in that case if I had multiple functions/subroutines/modules, and only one of them had parallel code in it (but they access each other throughout other segments of the code), I should be using the openmp flag for creating the object files of all the functions/subroutines/modules ?

Yes, absolutely. You should use -openmp to compile all the sources - especially for routines to be called from parallel regions - as otherwise they will not be thread-safe. (There may be other issues preventing thread safety, such as use of global data, that you'll also have to look out for. Unfortunately, our tools that can help detect such issues, Intel Inspector XE and the Static Analysis feature of Intel Fortran Studio XE, aren't supported on OS X.

Retired 12/31/2016

Leave a Comment

Please sign in to add a comment. Not a member? Join today