# Parallel Programming

**URL:** <https://discourse.julialang.org/t/parallel-programming/82174>\
**Category:** New to Julia\
**Tags:** threads\
**Created:** [June 3, 2022, 9:07am UTC](https://discourse.julialang.org/t/parallel-programming/82174 "2022-06-03T09:07:38Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mahmoud](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mahmoud](https://discourse.julialang.org/u/Mahmoud)\
**Post date:** [June 3, 2022, 9:07am UTC](https://discourse.julialang.org/t/parallel-programming/82174/1 "2022-06-03T09:07:39Z")

</div>

I am new to parallel programming concept and I am trying to follow up with the basics but I have this problem  
when I run  
`julia -t 4`  
`using Base.Threads `  
`nthreads()`  
The result is always 1 so I cannot start applying the concepts.  
I tried it on my machine which is core I5 and on a remote server for high end computing platform.

---

<div class="post-metadata">

**Author:** ![martin.d.maas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martin.d.maas/32/50964_2.png) [@martin.d.maas](https://discourse.julialang.org/u/martin.d.maas)\
**Post date:** [June 3, 2022, 9:34am UTC](https://discourse.julialang.org/t/parallel-programming/82174/2 "2022-06-03T09:34:34Z")

</div>

Now that’s strange, I get 4 if I run that code. Which version of Julia are you running?

---

<div class="post-metadata">

**Author:** ![Mahmoud](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mahmoud](https://discourse.julialang.org/u/Mahmoud)\
**Post date:** [June 3, 2022, 9:44am UTC](https://discourse.julialang.org/t/parallel-programming/82174/3 "2022-06-03T09:44:03Z")

</div>

![juliathreads](https://global.discourse-cdn.com/julialang/original/3X/3/c/3cb3fa4ad17c612d27391ba1c07536f15a6b520b.png)  
1.0.5  
this photo from remote server but its the same on my local machine

---

<div class="post-metadata">

**Author:** ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)\
**Post date:** [June 3, 2022, 9:51am UTC](https://discourse.julialang.org/t/parallel-programming/82174/4 "2022-06-03T09:51:51Z")

</div>

Are you launching Julia with the command `julia --threads=4` / `julia -t 4`? For me this works as expected, although, I haven’t tested it on Julia 1.0.5.

 ![Screenshot 2022-06-03 at 10.48.55](https://global.discourse-cdn.com/julialang/original/3X/a/9/a9c503c71c5c2ac4cab6a9b0f0b3bf9158bf6a85.png)

---

<div class="post-metadata">

**Author:** ![Mahmoud](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mahmoud](https://discourse.julialang.org/u/Mahmoud)\
**Post date:** [June 3, 2022, 9:56am UTC](https://discourse.julialang.org/t/parallel-programming/82174/5 "2022-06-03T09:56:14Z")

</div>

![232g32df2g](https://global.discourse-cdn.com/julialang/original/3X/7/b/7baaea55f3c6eb4d393417b1b124c8e1618a9a31.png)  
I do not know why I receive this error

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [June 3, 2022, 10:13am UTC](https://discourse.julialang.org/t/parallel-programming/82174/6 "2022-06-03T10:13:25Z")

</div>

I think the option `-t` or `--threads` was introduced later. What you want to to is

```nohighlight
export JULIA_NUM_THREADS=4
julia

```

i.e. define `JULIA_NUM_THREADS` as an environment variable, not in a running julia session. Otherwise I can also recommend to use a newer version of Julia, unless you really have to use 1.0.

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [June 3, 2022, 10:26am UTC](https://discourse.julialang.org/t/parallel-programming/82174/7 "2022-06-03T10:26:10Z")

</div>

```bash
$ JULIA_NUM_THREADS=4 julia
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.7.2 (2022-02-06)
 _/ |\ __'_|_|_|\__'_| | Fedora 36 build
|__/ |

julia> Threads.nthreads()
4

```

---

<div class="post-metadata">

**Author:** ![Mahmoud](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mahmoud](https://discourse.julialang.org/u/Mahmoud)\
**Post date:** [June 3, 2022, 10:28am UTC](https://discourse.julialang.org/t/parallel-programming/82174/8 "2022-06-03T10:28:13Z")

</div>

![aaaaaaaaaaaaa](https://global.discourse-cdn.com/julialang/original/3X/7/1/717078fbe12e783f670a6561aa29410340c99223.png)  
I think this works but I do not know the source of this segmentation fault!

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [June 3, 2022, 10:33am UTC](https://discourse.julialang.org/t/parallel-programming/82174/9 "2022-06-03T10:33:22Z")

</div>

This is because [prior to v1.3](https://github.com/JuliaLang/julia/blob/v1.3.0/NEWS.md) IO was not thread-safe. Another reason to update to the latest Julia version!

---

<div class="post-metadata">

**Author:** ![Mahmoud](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mahmoud](https://discourse.julialang.org/u/Mahmoud)\
**Post date:** [June 3, 2022, 10:36am UTC](https://discourse.julialang.org/t/parallel-programming/82174/10 "2022-06-03T10:36:26Z")

</div>

> [@josuagrw](#):
>
> `JULIA_NUM_THREADS=4`

this works also thank you

```julia
#SBATCH -t 00:20:00
#SBATCH -N 4
#SBATCH -A tud01
#SBATCH -p DevQ
#SBATCH --mail-user= ***********@**********
#SBATCH --mail-type=BEGIN,END
# This job's working directory
cd $SLURM_SUBMIT_DIR
module load julia/1.0.5
JULIA_NUM_THREADS=20 
julia /ichec/home/users/JuliaCodes/FCode_ICH.jl

```

do you think this is correct sequence?

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [June 3, 2022, 10:37am UTC](https://discourse.julialang.org/t/parallel-programming/82174/11 "2022-06-03T10:37:28Z")

</div>

No it needs to be on one line:

```julia
$ JULIA_NUM_THREADS=4 julia

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 3, 2022, 11:30am UTC](https://discourse.julialang.org/t/parallel-programming/82174/12 "2022-06-03T11:30:49Z")

</div>

You might have limited control over what’s on the cluster, but fwiw I’d think twice about working with multithreaded code using a three year old version of Julia that predates the introduction of the current threading infrastructure in version 1.3.

If it’s on the cluster because 1.0.5 used to be the LTS version you could let your admin know that the current LTS is 1.6.6.

---

<div class="post-metadata">

**Author:** ![Mahmoud](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mahmoud](https://discourse.julialang.org/u/Mahmoud)\
**Post date:** [June 3, 2022, 3:34pm UTC](https://discourse.julialang.org/t/parallel-programming/82174/13 "2022-06-03T15:34:55Z")

</div>

I sent an email to him  
thank you  
it is really a problem the server only include old versions  
it caused many errors in other codes also  
now I know

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [June 3, 2022, 4:27pm UTC](https://discourse.julialang.org/t/parallel-programming/82174/14 "2022-06-03T16:27:41Z")

</div>

If you install a local version of Julia for yourself you’ll need ~450MB of storage. Maybe that doesn’t eat too much of your quota? And in terms of files it’s ~2500, if there’s a limitation on number of files on your cluster.

---

<div class="post-metadata">

**Author:** ![Mahmoud](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mahmoud](https://discourse.julialang.org/u/Mahmoud)\
**Post date:** [June 3, 2022, 5:03pm UTC](https://discourse.julialang.org/t/parallel-programming/82174/15 "2022-06-03T17:03:51Z")

</div>

I did that  
could you please tell me the correct way to call the new version not the old one  
 ![2222](https://global.discourse-cdn.com/julialang/original/3X/8/5/852ab977eb135c7875ff52d45d373768e0d8fecc.png)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 3, 2022, 5:34pm UTC](https://discourse.julialang.org/t/parallel-programming/82174/16 "2022-06-03T17:34:00Z")

</div>

Depends on your setup, but on Linux the easiest is usually to just create a symlink from somewhere on your path to `julia-1.7.3/bin/julia`

---

<div class="post-metadata">

**Author:** ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)\
**Post date:** [June 7, 2022, 7:51am UTC](https://discourse.julialang.org/t/parallel-programming/82174/17 "2022-06-07T07:51:32Z")

</div>

If you are using a SLURM cluster, you may be interested in taking a look at Distributed.jl (gives you pmap which can parallelise across different processes using @distributed for reductions and pmap for parallel maps - these are the bread and butter of parallel programming). ClusterManagers.jl also provides a way of interacting with SLURM to create the processes in the first place without having to manually create the processes.

Also, you will find a lot of benefit upgrading (or asking the sysadmin to upgrade) the Julia version.

---

<div class="post-metadata">

**Author:** ![Mahmoud](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mahmoud](https://discourse.julialang.org/u/Mahmoud)\
**Post date:** [June 7, 2022, 11:55am UTC](https://discourse.julialang.org/t/parallel-programming/82174/18 "2022-06-07T11:55:09Z")

</div>

IT admin already updated to latest version and I have already ran my first code with threads “@threads”  
I will start apply distributed  
thank you
