Skip to content

Commit

Permalink
added pmap method for arrays, with type/shape of result the same as map
Browse files Browse the repository at this point in the history
  • Loading branch information
gajomi committed Jan 11, 2016
1 parent b9dc4c9 commit 4e6998a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
35 changes: 35 additions & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,17 @@ function map(f, A::AbstractArray)
return map_to!(f, 2, dest, A)
end

function pmap(f, A::AbstractArray; err_retry=true, err_stop=false, pids = workers())
if isempty(A)
return isa(f,Type) ? similar(A,f) : similar(A)
end
results = pmap(f, rest(A, start(A)), err_retry=err_retry, err_stop=err_stop, pids=pids)
first = results[1]
dest = similar(A, typeof(first))
dest[1] = first
return map_to!(identity, 2, dest, results)
end

## 2 argument
function map!{F}(f::F, dest::AbstractArray, A::AbstractArray, B::AbstractArray)
for i = 1:length(A)
Expand Down Expand Up @@ -1359,6 +1370,18 @@ function map(f, A::AbstractArray, B::AbstractArray)
return map_to!(f, 2, dest, A, B)
end

function pmap(f, A::AbstractArray, B::AbstractArray; err_retry=true, err_stop=false, pids = workers())
shp = promote_shape(size(A),size(B))
if prod(shp) == 0
return similar(A, promote_type(eltype(A),eltype(B)), shp)
end
results = pmap(f, rest(A, start(A)), rest(B, start(B)), err_retry=err_retry, err_stop=err_stop, pids=pids)
first = results[1]
dest = similar(A, typeof(first), shp)
dest[1] = first
return map_to!(identity, 2, dest, results)
end

## N argument

ith_all(i, ::Tuple{}) = ()
Expand Down Expand Up @@ -1401,6 +1424,18 @@ function map(f, As::AbstractArray...)
return map_to_n!(f, 2, dest, As)
end

function pmap(f, As::AbstractArray...; err_retry=true, err_stop=false, pids = workers())
shape = mapreduce(size, promote_shape, As)
if prod(shape) == 0
return similar(As[1], promote_eltype(As...), shape)
end
results = pmap(f, [rest(A,start(A)) for A in As]..., err_retry=err_retry, err_stop=err_stop, pids=pids)
first = results[1]
dest = similar(As[1], typeof(first), shape)
dest[1] = first
return map_to!(identity, 2, dest, results)
end

# multi-item push!, unshift! (built on top of type-specific 1-item version)
# (note: must not cause a dispatch loop when 1-item case is not defined)
push!(A, a, b) = push!(push!(A, a), b)
Expand Down
16 changes: 16 additions & 0 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,22 @@ function test_map(::Type{TestAbstractArray})
@test map(f, Int[], Int[], Complex{Int}[]) == Number[]
end

#test pmap array methods for compatibility with map array methods
A = B = C = 1:8
#single arg
mA,mpA = map(string,A), pmap(string,A)
@test mA == mpA
@test typeof(mA) == typeof(mpA)
#double arg
mAB,mpAB = map(string,A,B), pmap(string,A,B)
@test mAB == mpAB
@test typeof(mAB) == typeof(mpAB)
#n arg
mABC,mpABC = map(string,A,B,C), pmap(string,A,B,C)
@test mABC == mpABC
@test typeof(mABC) == typeof(mpABC)


function test_map_promote(::Type{TestAbstractArray})
A = [1:10...]
f(x) = iseven(x) ? 1.0 : 1
Expand Down

0 comments on commit 4e6998a

Please sign in to comment.