# Pmap error with closure

**URL:** <https://discourse.julialang.org/t/pmap-error-with-closure/10691>\
**Category:** General Usage\
**Tags:** pmap, closure\
**Created:** [May 3, 2018, 9:53pm UTC](https://discourse.julialang.org/t/pmap-error-with-closure/10691 "2018-05-03T21:53:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![EthanAnderes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ethananderes/32/2028_2.png) [@EthanAnderes](https://discourse.julialang.org/u/EthanAnderes)\
**Post date:** [May 3, 2018, 9:53pm UTC](https://discourse.julialang.org/t/pmap-error-with-closure/10691/1 "2018-05-03T21:53:10Z")

</div>

I’ve been getting an error having to do with `pmap` on closures that I don’t understand. Anyone know what is going on?

```julia
addprocs(2)

function gen_f(a::T) where T
    f = x -> T
    return f
end
f = gen_f(1f0) 

f(1f0) # --> Float32
pmap(f, [1f0, 2f0]) # --> Error deserializing a remote exception from worker 2

```

---

<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:** [May 4, 2018, 12:01am UTC](https://discourse.julialang.org/t/pmap-error-with-closure/10691/2 "2018-05-04T00:01:58Z")

</div>

Not really sure what’s happening here.

But it might be something to do with `f` having a type parameter:

```julia
Julia-0.6.2> typeof(f)
##14#15{Float32}

```

This does work for me:

```julia
function gen_g(a)
    f = x -> typeof(a)
    return f
end
g = gen_g(1f0) 

pmap(g, [1f0, 2f0])

```

---

<div class="post-metadata">

**Author:** ![EthanAnderes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ethananderes/32/2028_2.png) [@EthanAnderes](https://discourse.julialang.org/u/EthanAnderes)\
**Post date:** [May 4, 2018, 12:14am UTC](https://discourse.julialang.org/t/pmap-error-with-closure/10691/3 "2018-05-04T00:14:44Z")

</div>

Thanks for checking up on it. For my actual use case using `typeof(a)` isn’t very natural but it might be workable. Seems like a bug though so I guess I’ll post a report on github.

---

<div class="post-metadata">

**Author:** ![EthanAnderes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ethananderes/32/2028_2.png) [@EthanAnderes](https://discourse.julialang.org/u/EthanAnderes)\
**Post date:** [May 4, 2018, 12:21am UTC](https://discourse.julialang.org/t/pmap-error-with-closure/10691/4 "2018-05-04T00:21:08Z")

</div>

… now I recall the problem with using `typeof(a)`, I loose type stability:

```julia
julia> function gen_f1(a)
           T = typeof(a)
           f = x -> T(x)
           return f
       end
gen_f1 (generic function with 1 method)

julia> function gen_f2(a::T) where T
           f = x -> T(x)
           return f
       end
gen_f2 (generic function with 1 method)

julia> f1 = gen_f1(1f0)
(::#5) (generic function with 1 method)

julia> f2 = gen_f2(1f0)
(::#7) (generic function with 1 method)

julia> @code_warntype f1(1f0)
Variables:
  #self#::##5#6{DataType}
  x::Float32

Body:
  begin
      return ((Core.getfield)(#self#::##5#6{DataType}, :T)::DataType)(x::Float32)::Any
  end::Any

julia> @code_warntype f2(1f0)
Variables:
  #self# <optimized out>
  x::Float32

Body:
  begin
      return x::Float32
  end::Float32

```

---

<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:** [May 4, 2018, 12:26am UTC](https://discourse.julialang.org/t/pmap-error-with-closure/10691/5 "2018-05-04T00:26:47Z")

</div>

Could you use a generated function for `gen_f`?

---

<div class="post-metadata">

**Author:** ![EthanAnderes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ethananderes/32/2028_2.png) [@EthanAnderes](https://discourse.julialang.org/u/EthanAnderes)\
**Post date:** [May 4, 2018, 12:38am UTC](https://discourse.julialang.org/t/pmap-error-with-closure/10691/6 "2018-05-04T00:38:43Z")

</div>

I’m guessing your suggesting something like defining `f` directly as a generated function? Or are you thinking of using `@generated` for `gen_f`?

For my use case I’m defining multiple functions within one closure (where all the functions are sharing some local closure variables that are precomputed) which are all passed to the workers. At least I’m not aware of how to use `@generated` for that. Anyway, thanks for the suggestion.

---

<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:** [May 6, 2018, 8:16am UTC](https://discourse.julialang.org/t/pmap-error-with-closure/10691/7 "2018-05-06T08:16:24Z")

</div>

Glad to see the issue being fixed.  
That’s an amazing response!

Cross-referencing issue and PR for posterity:  
[https://github.com/JuliaLang/julia/issues/26979](https://github.com/JuliaLang/julia/issues/26979)  
[https://github.com/JuliaLang/julia/pull/26986](https://github.com/JuliaLang/julia/pull/26986)

---

<div class="post-metadata">

**Author:** ![EthanAnderes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ethananderes/32/2028_2.png) [@EthanAnderes](https://discourse.julialang.org/u/EthanAnderes)\
**Post date:** [May 6, 2018, 2:01pm UTC](https://discourse.julialang.org/t/pmap-error-with-closure/10691/8 "2018-05-06T14:01:01Z")

</div>

Indeed! Although I’m somewhat embarrassed that I didn’t find that one liner fix. Thanks for posting the cross reference.

(Fingers crossed it gets into 0.6.3 backports.)
