wpkzz
wpkzz

Reputation: 899

Retrievieng SharedArray on Julia 0.4

Edit: I did try using fetch()

I seem to have break something in Julia this week. I had played with the SharedArray type on a computer with 12 threads (6 doble thread cpu), I maneged to get a result and to print it and save it as matrix text file without problem, more or less following the instructions on http://docs.julialang.org/en/release-0.4/manual/parallel-computing/#shared-arrays. I had this routine where I initialized a number of workers and an Array, pass them as argument to a function, and expected to obtain a SharedArray with numerical values in return. It went more or less like this

addprocs(11)
BCero=rand(128,128)
ConjuntoX=Array[]

for j,k=1:128
  push!(ConjuntoX, [j,k])
end


function obtenerKernelParalell(LasB::Array, lasX::Array, jmax::Int)
result=SharedArray(Float64,(jmax,jmax))
@sync @parallel for j=1:jmax
    xj=lasX[j]
     for k=1:j
        xk=lasX[k]
     for l=1:jmax
            xl=lasX[l]
            result[j,k]+= LasB[(xk-xl+xconstante)...]*LasB[(xj-xl+xconstante)...]
    end

    end
end 

end

KSuaveParalel=obtenerKernelParalell(BceroSuave, ConjuntoX,128);

What I did get after runing this for the first time was an array that behaved itself like a normal Array. If I typed KSuaveParalel[3,12] I obtained a value. But now I get the next thing from the REPL: KSuaveParalel

11-element Array{Any,1}:
RemoteRef{Channel{Any}}(2,1,122) 
RemoteRef{Channel{Any}}(3,1,123) 
RemoteRef{Channel{Any}}(4,1,124) 
RemoteRef{Channel{Any}}(5,1,125) 
RemoteRef{Channel{Any}}(6,1,126) 
RemoteRef{Channel{Any}}(7,1,127) 
RemoteRef{Channel{Any}}(8,1,128) 
RemoteRef{Channel{Any}}(9,1,129) 
RemoteRef{Channel{Any}}(10,1,130)
RemoteRef{Channel{Any}}(11,1,131)
RemoteRef{Channel{Any}}(12,1,132)

So I got an array of References... and I do not know how to get its values. Also using fetch() doesn't seem to work. What is going on here?

Upvotes: 0

Views: 113

Answers (2)

Alexander Morley
Alexander Morley

Reputation: 4181

Edit:

You need to make sure you return the array in the function. i.e. return result


You will want to call fetch() on each Remote Reference object to wait and get the value that will be returned.

[fetch(x) for x in KSuaveParalel]

The RemoteRef object is returned immediately (i.e. before the computation has actually been done). See this answer (Julia Parallel macro does not seem to work) and the docs for more info.

http://docs.julialang.org/en/release-0.4/stdlib/parallel/#Base.fetch

Upvotes: 2

wpkzz
wpkzz

Reputation: 899

Okey, sort of figured a way to do it, but is not probably the most efficient way to do it: If I convert INSIDE the function the result from SharredArray to Array, I get the (apparently) correct result:

 function obtenerKernelParalell(LasB::Array, lasX::Array, jmax::Int)
     result=SharedArray(Float64,(jmax,jmax))
      @sync @parallel for j=1:jmax
        xj=lasX[j]
             for k=1:j
               xk=lasX[k]
               for l=1:jmax
                  xl=lasX[l]
                  result[j,k]+= LasB[(xk-xl+xconstante)...]*LasB[(xjxl+xconstante)...]
               end

     end
end 
 result=Array(result)
 return result
end

Is this because the conversion does the fetch() with the right settings automatically?

Upvotes: 0

Related Questions