Writing on external temporary files with MPI.jl

Hi all. I’m learning how to use MPI with Julia, specifically the MPI.jl package.
I’m trying to understand how to print on external temporary files, and I can’t make it work.

This is my current minimal non-working example script, called mpi_test.jl:

using MPI
MPI.Init()

let
    comm = MPI.COMM_WORLD
    prank = MPI.Comm_rank(comm)
    io_file, _ = mktemp()
    io_file = MPI.bcast(io_file, comm)
    f = MPI.File.open(comm, io_file; write=true)
    MPI.File.write(f, "$prank: hello!")
    
    io_files = MPI.gather(io_file, comm)
    prank == 0 && for p in 0:MPI.Comm_size(comm)-1
        println(p, ": ", io_files[p+1])
    end
end

which I run with mpiexecjl -np 4 julia --project mpi_test.jl.
The output is

0: /tmp/jl_FokE6l
1: /tmp/jl_FokE6l
2: /tmp/jl_FokE6l
3: /tmp/jl_FokE6l

but the files are not actually there:

cat: /tmp/jl_FokE6l: No such file or directory

If I write to an ordinary file, say io_file = "test.txt" instead of using mktemp(), the file is correctly created in the working directory and contains the given string.
With mktemp() instead, no file is created at all in the /tmp folder.

Can someone explain what I’m missing here?

  • Did you mean to call tempname() instead of mktemp(), since you want MPI.File.open to be the function that actually creates the file?
  • Note that there is no point in calling tempname() on anything except for prank == 0, since that is where your are broadcasting (bcast) from.
  • Did you forget to call close(f)?

(I assume you are running on a single node, so that all the MPI processes have access to a single /tmp filesystem?)

  • Did you mean to call tempname() instead of mktemp(), since you want MPI.File.open to be the function that actually creates the file?

Huh, I never realised mktemp() already opened the file. I guess I always used it wrong.

  • Note that there is no point in calling tempname() on anything except for prank == 0, since that is where your are broadcasting (bcast) from.

Yeah I know, it doesn’t matter for this simple example, I just found it simpler to write.

  • Did you forget to call close(f)?

I deliberately left it out, I assumed it didn’t change the outcome.

  • (I assume you are running on a single node, so that all the MPI processes have access to a single /tmp filesystem?)

Yes, I’m running it on a single node, on my laptop.

Anyway I switched from mktemp to tempname and closed the file at the end, but the behaviour didn’t change.

Well, I’m just dumb, I found out why this happens, and it has nothing to do with MPI: the temporary files were deleted by Julia when the script ended because I didn’t supply the cleanup=false argument to mktemp().