diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 286d56279a2ed..7ba5febb73398 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -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) @@ -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{}) = () @@ -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) diff --git a/test/abstractarray.jl b/test/abstractarray.jl index 363ad822840d1..06fd0d7cbf266 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -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