Iso_c_binding: interface to a C function is not thread safe for openmp

I am trying to use openmp to parallelize a fortran loop that has calls to c functions. The current implementation uses Iso_c_binding and I cant find any reference on how to make this interface thread safe. I tried using !$OMP THREADPRIVATE command and the compiler doesn’t like it. Any input would be much appreciated.

use, intrinsic :: ISO_C_BINDING
interface
logical function addffd(ssd,ffd) BIND ( C,name = “addffd”//postfix)
import
character, dimension (*) :: ssd
integer (C_INT) :: ffd
end function
end interface

Thanks

Hi, @AlexR205, thanks for the question. It’s hard to give a good answer based on that interface block alone. I think at minimum, we’d need to see

  • The C function signature
  • A few words about the C function, especially which arguments are modified and which ones are not
  • The loop you are trying to parallelize, including the OpenMP directives
  • The error message you are getting

It’s possible that we’d need to see even more context, but the above should be enough to get started. In principle, there’s no reason a C function cannot be called inside an OpenMP-parallelized do loop.

2 Likes

I can point you to a brief primer video I put together on the topic, but as @nshaffer pointed out, there is no reason you can’t call a C function from inside a parallel loop so long as you’re not modifying a global variable.