# Parallel program with external software works from REPL but not from command line

**URL:** <https://discourse.julialang.org/t/parallel-program-with-external-software-works-from-repl-but-not-from-command-line/42757>\
**Category:** General Usage\
**Created:** [July 8, 2020, 10:13pm UTC](https://discourse.julialang.org/t/parallel-program-with-external-software-works-from-repl-but-not-from-command-line/42757 "2020-07-08T22:13:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![PlasmaJoe](https://avatars.discourse-cdn.com/v4/letter/p/a9a28c/32.png) [@PlasmaJoe](https://discourse.julialang.org/u/PlasmaJoe)\
**Post date:** [July 8, 2020, 10:13pm UTC](https://discourse.julialang.org/t/parallel-program-with-external-software-works-from-repl-but-not-from-command-line/42757/1 "2020-07-08T22:13:30Z")

</div>

I’m trying to set up Julia to interact with an external program. I need to run the external program _a lot_ and I have access to a cluster so since the external program only uses one core I think it makes sense to distribute the work by running separate instances on several cores at once.

I’m using “@spawnat” to call a Julia function that launches the external program. The main program starts the workers:

```julia
### mre.jl main program
using Distributed
using HDF5

nprocs = 3

if nprocs() < NP
    addprocs(NP-nprocs())
end

@everywhere include("/Users/me/demo.jl")
Threads.@threads for i = 1:100
    f = @spawnat :any demo(i)
end

```

The secondary function launches the external program (bolsigminus) and saves the data:

```julia
function demo(x)
    input = "/Users/me/new_input_deck.dat"
    run(`./bolsigminus $input`)
    h5write("/Users/me/demo"*string(x)*".h5", "Data/xs", x)
end

```

When I run the main program from the REPL it works perfectly. When I run the main program from the command line using `julia -p 3 mre.jl` the external program launches on each worker but just stops and exits partway through on the first execution without any error messages. I want it to work from the command line because I’m going to have to submit these to the queue on the cluster.

I’m a newbie so I apologize if the answer is obvious, but I’m baffled. Any suggestions are greatly appreciated!

---

<div class="post-metadata">

**Author:** ![ppalmes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ppalmes/32/6838_2.png) [@ppalmes](https://discourse.julialang.org/u/ppalmes)\
**Post date:** [July 8, 2020, 10:25pm UTC](https://discourse.julialang.org/t/parallel-program-with-external-software-works-from-repl-but-not-from-command-line/42757/2 "2020-07-08T22:25:53Z")

</div>

no need to spawn because anything inside the thread is already run in a separate thread.

---

<div class="post-metadata">

**Author:** ![ppalmes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ppalmes/32/6838_2.png) [@ppalmes](https://discourse.julialang.org/u/ppalmes)\
**Post date:** [July 8, 2020, 10:26pm UTC](https://discourse.julialang.org/t/parallel-program-with-external-software-works-from-repl-but-not-from-command-line/42757/3 "2020-07-08T22:26:49Z")

</div>

you may be referring to @distributed instead of @threads. in any case, no need to use spawn.

---

<div class="post-metadata">

**Author:** ![PlasmaJoe](https://avatars.discourse-cdn.com/v4/letter/p/a9a28c/32.png) [@PlasmaJoe](https://discourse.julialang.org/u/PlasmaJoe)\
**Post date:** [July 8, 2020, 11:54pm UTC](https://discourse.julialang.org/t/parallel-program-with-external-software-works-from-repl-but-not-from-command-line/42757/4 "2020-07-08T23:54:48Z")

</div>

Thank you for the coaching! I’ve never worked with any real distributed code before and I’m still at the start of the learning curve. I tried changing the main call to

```julia
@distributed for i = 1:NP-1
    demo(i)
end

```

Still works perfectly from REPL and fails from the command line.

---

<div class="post-metadata">

**Author:** ![ppalmes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ppalmes/32/6838_2.png) [@ppalmes](https://discourse.julialang.org/u/ppalmes)\
**Post date:** [July 9, 2020, 5:02am UTC](https://discourse.julialang.org/t/parallel-program-with-external-software-works-from-repl-but-not-from-command-line/42757/5 "2020-07-09T05:02:55Z")

</div>

try running serially if it works. maybe the error is not in the parallelism but the code inside the loop has some issues. try ordinary for loop first.

---

<div class="post-metadata">

**Author:** ![PlasmaJoe](https://avatars.discourse-cdn.com/v4/letter/p/a9a28c/32.png) [@PlasmaJoe](https://discourse.julialang.org/u/PlasmaJoe)\
**Post date:** [July 9, 2020, 5:06pm UTC](https://discourse.julialang.org/t/parallel-program-with-external-software-works-from-repl-but-not-from-command-line/42757/6 "2020-07-09T17:06:09Z")

</div>

Using a normal loop (just removing “@distributed”) from in front of the for loop works fine in both REPL and command line.

---

<div class="post-metadata">

**Author:** ![ppalmes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ppalmes/32/6838_2.png) [@ppalmes](https://discourse.julialang.org/u/ppalmes)\
**Post date:** [July 9, 2020, 5:46pm UTC](https://discourse.julialang.org/t/parallel-program-with-external-software-works-from-repl-but-not-from-command-line/42757/7 "2020-07-09T17:46:50Z")

</div>

try  
@everywhere function demo(x)  
…  
end

also  
@everywhere using HDF5

instead of including the demo function, place it in the same file with your mre.jl

---

<div class="post-metadata">

**Author:** ![PlasmaJoe](https://avatars.discourse-cdn.com/v4/letter/p/a9a28c/32.png) [@PlasmaJoe](https://discourse.julialang.org/u/PlasmaJoe)\
**Post date:** [July 10, 2020, 5:13pm UTC](https://discourse.julialang.org/t/parallel-program-with-external-software-works-from-repl-but-not-from-command-line/42757/8 "2020-07-10T17:13:28Z")

</div>

Adding “@sync” before “@distributed” resolved the issue. I’m happy that it is running but still at a loss to understand why “@sync” was needed from the command line and not from the REPL.
