Skip to content

fmiReload and fmiLoad not thread-save #186

Open
@AnHeuermann

Description

@AnHeuermann

Issue

I want to simulate a number of FMUs in parallel with FMI.jl and reuse the already loaded FMUs for multiple runs, but encounter an error in FMIImport where cd is used to change the directory while running in parallel.

ERROR: LoadError: IOError: pwd(): no such file or directory (ENOENT)
Stacktrace:
  [1] uv_error
    @ ./libuv.jl:97 [inlined]
  [2] pwd()
    @ Base.Filesystem ./file.jl:63
  [3] loadBinary(fmu::FMICore.FMU2)
    @ FMIImport ~/.julia/packages/FMIImport/Zfd3H/src/FMI2/ext.jl:246
  [4] fmi2Load(pathToFMU::String; unpackPath::String, type::Nothing, cleanup::Bool, logLevel::UInt32)
    @ FMIImport ~/.julia/packages/FMIImport/Zfd3H/src/FMI2/ext.jl:240
  [5] #fmiLoad#168
    @ ~/.julia/packages/FMI/PEsZa/src/FMI.jl:616 [inlined]

I believe the issue is the usage of cd in FMIImport ext.jl#L245-L252
Is cd needed here? But I guess there are other problems with using e.g. ccall in parallel, see https://docs.julialang.org/en/v1/manual/multi-threading/#@threadcall.

If it's not possible to use fmiLoad and fmiReload in parallel, maybe add a check to them to test if they are run inside a parallel region?

How to reproduce

fmuArray = [FMI.fmiLoad(fmuPath) for _ in 1:10]
locks = [ReentrantLock() for _ in 1:10]
Threads.@threads for i in 1:100
    idx = ((i-1)%10+1)
    lock(locks[idx]) do
      fmu = fmuArray[idx]
      # Do something with the FMU
      FMI.fmiInstantiate!(fmu)
      FMI.fmiSetupExperiment(fmu)
      FMI.fmiEnterInitializationMode(fmu)
      FMI.fmiExitInitializationMode(fmu)
      
     # Reload FMU
      FMI.fmiReload(fmu)
    end
end

You can get similar results with

Threads.@threads for i in 1:100
         lastDirectory = pwd()
         mkdir("temp_$i")
         cd("temp_$i")
         sleep(5)
         cd(lastDirectory)
end

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions