# Writing on external temporary files with MPI.jl

**URL:** https://discourse.julialang.org/t/writing-on-external-temporary-files-with-mpi-jl/137485
**Category:** General Usage
**Created:** [June 7, 2026, 8:13pm UTC](https://discourse.julialang.org/t/writing-on-external-temporary-files-with-mpi-jl/137485 "2026-06-07T20:13:50Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![phx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phx/32/219940_2.png) [@phx](https://discourse.julialang.org/u/phx)
#### Post date: [June 7, 2026, 8:13pm UTC](https://discourse.julialang.org/t/writing-on-external-temporary-files-with-mpi-jl/137485/1 "2026-06-07T20:13:50Z")

</div>

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`:

```julia
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

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

```

but the files are not actually there:

```bash
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?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [June 7, 2026, 9:59pm UTC](https://discourse.julialang.org/t/writing-on-external-temporary-files-with-mpi-jl/137485/2 "2026-06-07T21:59:40Z")

</div>

- 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?)

---

<div class="post-metadata">

### Author: ![phx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phx/32/219940_2.png) [@phx](https://discourse.julialang.org/u/phx)
#### Post date: [June 7, 2026, 11:31pm UTC](https://discourse.julialang.org/t/writing-on-external-temporary-files-with-mpi-jl/137485/3 "2026-06-07T23:31:54Z")

</div>

> - 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 `close`d the file at the end, but the behaviour didn’t change.

---

<div class="post-metadata">

### Author: ![phx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phx/32/219940_2.png) [@phx](https://discourse.julialang.org/u/phx)
#### Post date: [June 7, 2026, 11:34pm UTC](https://discourse.julialang.org/t/writing-on-external-temporary-files-with-mpi-jl/137485/4 "2026-06-07T23:34:37Z")

</div>

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()`.
