# Dynamically schedule work onto pmap worker pool?

**URL:** https://discourse.julialang.org/t/dynamically-schedule-work-onto-pmap-worker-pool/53701
**Category:** General Usage
**Created:** [January 21, 2021, 2:38am UTC](https://discourse.julialang.org/t/dynamically-schedule-work-onto-pmap-worker-pool/53701 "2021-01-21T02:38:49Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [January 21, 2021, 2:38am UTC](https://discourse.julialang.org/t/dynamically-schedule-work-onto-pmap-worker-pool/53701/1 "2021-01-21T02:38:49Z")

</div>

I’ve got `N` chunks of equally long-running work I want to compute across `<N` processes, but one chunk is very different code / results than the others. So code-wise, I’d like to schedule that chunk alone, then `pmap` over the remaining `N-1` chunks. Something like:

```julia
N = 4
addprocs(N) # as an example, in reality N > # of workers
@spawnat :any begin
    println("starting unique chunk")
    sleep(2)
end
pmap(1:N-1) do i
    println("starting chunk $i")
    sleep(2)
end

```

The problem is that this seems to sometime double up on a worker, rather use all (in this case) 4 workers. E.g. here’s a typical output:

```julia
      From worker 4:	starting unique chunk
      From worker 4:	starting chunk 1
      From worker 3:	starting chunk 2
      From worker 2:	starting chunk 3

```

I’ve figured out that it seems to work if I use `pmap` in both cases:

```julia
@sync begin
    @async pmap(1:1) do _
        println("starting unique chunk")
        sleep(2)
    end
    pmap(1:3) do i
        println("starting chunk $i")
        sleep(2)
    end
end

```

But I feel like there must be a cleaner way to do this. Any ideas? Thanks.

---

<div class="post-metadata">

### Author: ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)
#### Post date: [January 21, 2021, 4:00am UTC](https://discourse.julialang.org/t/dynamically-schedule-work-onto-pmap-worker-pool/53701/2 "2021-01-21T04:00:28Z")

</div>

Maybe you could use [WorkerPools](https://docs.julialang.org/en/v1/stdlib/Distributed/#Distributed.WorkerPool)?

```julia
using Distributed
N = 12 # chunks
M = 4 # workers
addprocs(M)
wpids = workers()
uniquePool = WorkerPool(wpids[1:1])
standardPool = WorkerPool(wpids[2:end])

pmap(uniquePool, 1:1) do i
    println("starting unique chunk")
    sleep(2)
end

pmap(standardPool, 2:N) do i
    println("starting chunk $i")
    sleep(2)
end

```

---

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [January 21, 2021, 5:40am UTC](https://discourse.julialang.org/t/dynamically-schedule-work-onto-pmap-worker-pool/53701/3 "2021-01-21T05:40:58Z")

</div>

Thanks for the suggestion. The problem with this though is that even if I launch these with `@async`, after finishing the unique chunk, `wpids[1]` sits idle when what I want is that its helping finish the other chunks.

---

<div class="post-metadata">

### Author: ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)
#### Post date: [January 21, 2021, 7:31am UTC](https://discourse.julialang.org/t/dynamically-schedule-work-onto-pmap-worker-pool/53701/4 "2021-01-21T07:31:47Z")

</div>

Well, I think that’s a little easier:

```julia
using Distributed
N = 12 # chunks
M = 4 # workers
addprocs(M)

pmap(1:N) do i
    if i == 1
        println("starting unique chunk")
        sleep(2)
    else
        println("starting chunk $i")
        sleep(2)
    end
end

```

---

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [January 21, 2021, 8:00am UTC](https://discourse.julialang.org/t/dynamically-schedule-work-onto-pmap-worker-pool/53701/5 "2021-01-21T08:00:51Z")

</div>

Yea, its definitely not impossible, just that code organization wise its awkward to do in my case. Was hoping to find a way to just queue up one work chunk in a different part of the code from all the rest.

---

<div class="post-metadata">

### Author: ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)
#### Post date: [January 21, 2021, 10:44am UTC](https://discourse.julialang.org/t/dynamically-schedule-work-onto-pmap-worker-pool/53701/6 "2021-01-21T10:44:11Z")

</div>

It seems you can push idle/new workers to an existing pool and they will be used by an ongoing computation. No idea if this is ‘safe’ or ‘correct’ though.

---

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [January 22, 2021, 1:13am UTC](https://discourse.julialang.org/t/dynamically-schedule-work-onto-pmap-worker-pool/53701/7 "2021-01-22T01:13:24Z")

</div>

Thanks, yea it looks there’s a form of `remotecall*(::AbstractWorkerPool)` and this is exactly what it does. So I think this is the “cleanest” way to do what I was after:

```julia
pool = default_worker_pool()
@sync begin
    @async remotecall_fetch(pool) do
        println("starting unique chunk")
        sleep(2)
    end
    @async pmap(pool, 1:N-1) do i
        println("starting chunk $i")
        sleep(2)
    end
end

```
