# Using Distributed computing in JULIA for UNet

**URL:** https://discourse.julialang.org/t/using-distributed-computing-in-julia-for-unet/86483
**Category:** New to Julia
**Tags:** question, package, distributed, pmap
**Created:** [August 29, 2022, 3:51am UTC](https://discourse.julialang.org/t/using-distributed-computing-in-julia-for-unet/86483 "2022-08-29T03:51:39Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![momo1](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@momo1](https://discourse.julialang.org/u/momo1)
#### Post date: [August 29, 2022, 3:51am UTC](https://discourse.julialang.org/t/using-distributed-computing-in-julia-for-unet/86483/1 "2022-08-29T03:51:39Z")

</div>

Hi  
I have a UNet training for Machine Learning which works on my laptop (14 core) but its extremely slow.  
I’m trying to explore ways of speeding it up and came across Distributed library.  
So I’ve only added the following three lines in my code:

```julia
using Distributed

addprocs(exeflags=`--project=$(Base.active_project())`)
..
rmprocs(workers)

```

Also, It doesn’t identify pmap() function which I’ve called like this :

`train_batch_input_files, train_batch_target_files = pmap(grab_random_files, train_dataset, batch_size)`  
I’m not sure If i’m actually using distributed computing correctly or not?

---

<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: [August 29, 2022, 8:18am UTC](https://discourse.julialang.org/t/using-distributed-computing-in-julia-for-unet/86483/2 "2022-08-29T08:18:24Z")

</div>

It looks like you are just trying to load the files in parallel? If you are loading from disk, this will likely be bottlenecked by your disk speed, and not how many cores you have.

`pmap` is likely being defined, but maybe it is called with the wrong types. The main way to use `pmap` is:

```julia
some_func(x) = x^2

results = pmap(some_func, [1, 2, 3])
# results = [1, 4, 9]

```

So that each element in the array is mapped using the supplied function into a result, similarly to how one uses broadcasting.

In your example, the `pmap` should return an array of results, with each element being the return type of your supplied function. In this case it looks like a tuple. This means you will get an array of tuples as the return type, so I don’t think you can simply destructure that. Secondly, if the batch size parameter is fed into your function, but is not an array, you can create an anonymous function which wraps this parameter:

```julia
results = pmap(x->grab_random_files(x, batch_size), train_dataset)

```

If you are trying to get your code to run faster, I would recommend profiling the code first to see which parts are taking the most time, and focus on optimising them first.

EDIT: Use the `@everywhere` macro to load any non base functions used in your mapping functions. I usually have a separate file with all necessary function definitions and have an `@everywhere include("functions.jl")` before I use `pmap`.

---

<div class="post-metadata">

### Author: ![momo1](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@momo1](https://discourse.julialang.org/u/momo1)
#### Post date: [August 30, 2022, 6:47am UTC](https://discourse.julialang.org/t/using-distributed-computing-in-julia-for-unet/86483/5 "2022-08-30T06:47:59Z")

</div>

So I tried  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/3/e3c8cbd9b44832bcaf4a0b2f829caa25d38f5414.png)  
but it throws an error for

> ‘status’ not defined

. How do I use pmap then?

---

<div class="post-metadata">

### Author: ![momo1](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@momo1](https://discourse.julialang.org/u/momo1)
#### Post date: [August 30, 2022, 11:19pm UTC](https://discourse.julialang.org/t/using-distributed-computing-in-julia-for-unet/86483/6 "2022-08-30T23:19:52Z")

</div>

So instead of using pmap, i used @sync and @distributed before the for loop. In the call to environment i used @everywhere for all process to have access to all `using` files and now the run time has reduced. from 145 to 110 seconds.

---

<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: [August 31, 2022, 5:22am UTC](https://discourse.julialang.org/t/using-distributed-computing-in-julia-for-unet/86483/7 "2022-08-31T05:22:21Z")

</div>

Since this is just on a single machine, multithreading is likely a better fit here as it has very little overhead ([Multi-Threading · The Julia Language](https://docs.julialang.org/en/v1/manual/multi-threading/)). Here you would just replace the `pmap` and not need the `@everything`:

```julia
Threads.@threads for file in input_files
    Jaws.transfer(file, pwd())
end

```

Distributed uses different processes with separate memory so one needs to load all the libraries on each process, whereas multithreading uses shared memory. If I am on a single machine, I tend to try multithreading first, and only move to distributed if there is a particular benefit.

Just make sure you have multiple threads available with `Threads.nthreads()`. A sensible value for this is the number of logical cores in your CPU (14). The docs tells you how to change this.
