[julia-users] Re: Parallel computation of Array of user-defined types

2016-09-09 Thread amiksvi
Thanks German, indeed I have seen that SharedArrays couldn't be used for 
user-defined types...
I tried to compare the approach you suggested with the serial one, and 
results are so awfully slow with the parallel version that I might have 
done something wrong...
Here it is:

@everywhere type T a end
N = Int(1e5)
v = Array(T, N)
refs = Vector{RemoteRef}(N)
for i = 1:N
refs[i] = @spawn begin T(i) end
end
for i = 1:N
v[i] = fetch(refs[i])
end

called with:

time julia -p 4 parallel.jl

takes 0m31.706s, whereas

type T a end
N = Int(1e5)
v = Array(T, N)
for i = 1:N
v[i] = T(i)
end

called with:

time julia serial.jl

takes only 0m0.293s...


[julia-users] Re: Parallel computation of Array of user-defined types

2016-09-09 Thread Germán Aquino
I usually use a combination of @spawn and fetch. @spawn fires a lightweigth 
task that can return a value of any type, which can be collected by the 
main thread with fetch:

refs = Vector{RemoteRef}(N)
for i in 1:N
refs[i] = @spawn begin doSomethingAndReturnValueOfTypeT end
end

for i in 1:N
v[i] = fetch(refs[i])
end

I'm not aware if there is a better way, as far as I know SharedArrays don't 
work with user-defined types.
You need to have worker threads for this to be of any use, by using 
addprocs() or by starting julia with the -p flag, for instance $ julia -p 4.

I hope that helps,
Germán.



On Friday, September 9, 2016 at 7:51:59 AM UTC-3, ami...@gmail.com wrote:
>
> Hello,
>
> What would be the best option to parallelize this code:
>
> type T a end
> f(i) = T(i)
> v = map(f, collect(1:1:100))
>
> This example could sound stupid but the point is that I have a function 
> ```f``` that returns a rather complicated user-defined type ```T```, and I 
> need to store a lot of these types in an ```Array```.
> I've read a bit about the parallel paradigm of Julia and I honestly 
> wouldn't know how to do such a thing.
>
> Any hint?
>
> Many thanks!
>