# \`pmap\` with shared arrays in side a custom type?

**URL:** <https://discourse.julialang.org/t/pmap-with-shared-arrays-in-side-a-custom-type/399>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 17, 2016, 10:33pm UTC](https://discourse.julialang.org/t/pmap-with-shared-arrays-in-side-a-custom-type/399 "2016-11-17T22:33:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)\
**Post date:** [November 17, 2016, 10:33pm UTC](https://discourse.julialang.org/t/pmap-with-shared-arrays-in-side-a-custom-type/399/1 "2016-11-17T22:33:14Z")

</div>

Hi all,  
I have got the following example problem:

- I have a custom type with a shared array
- I want to distribute computation among `n` workers
- My computation requires me to know _which_ job I am running (i.e. the first job is different from the second etc)

```julia
module bk
type Foo
    arr::SharedArray
    function Foo(n)
	    this=new()
	    this.arr = SharedArray(Float64,(n,1),pids=workers(),init=S->S[Base.localindexes(S)] = rand())
	    return this
    end
end

function sum(f::Foo,idx::UnitRange)
	# idx is the id of jobs that this worker has to do
	sum(f.arr[idx]) # for example
end
end

```

then I include my code on all workers

```julia
julia> addprocs(2)
2-element Array{Int64,1}:
 2
 3

julia> @everywhere include("src/bk.jl")

julia> f = bk.Foo(1000)
bk.Foo([0.465832; 0.465832; … ; 0.501914; 0.501914])

julia> js = bk.jobdist(length(workers()),1000)
2-element Array{UnitRange{Int64},1}:
 1:500   
 501:1000

julia> pmap(x->bk.sum(f,x),js)
ERROR: On worker 2:
UndefVarError: f not defined

```

I think I understand why this is not working - `f` was never defined on the workers. But how could I achieve this? i want to create a `Foo` only in one place (on the master) - Foo should contain the shared arrays and I want to pass the `Foo` around. any ideas?

---

<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:** [November 18, 2016, 3:33am UTC](https://discourse.julialang.org/t/pmap-with-shared-arrays-in-side-a-custom-type/399/2 "2016-11-18T03:33:39Z")

</div>

Perhaps the discussion here is helpful?

[https://groups.google.com/forum/#!topic/julia-users/9Wml9MfrZQg](https://groups.google.com/forum/#!topic/julia-users/9Wml9MfrZQg)

---

<div class="post-metadata">

**Author:** ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)\
**Post date:** [November 18, 2016, 9:30am UTC](https://discourse.julialang.org/t/pmap-with-shared-arrays-in-side-a-custom-type/399/3 "2016-11-18T09:30:57Z")

</div>

yes - very helpful. in fact, that solved this particular problem for me. I will post another question now about memory allocation.
