parallel-processingjuliapmap

TypeError using pmap in Julia 1.6.1


I have been working to parallelize a function which reads in an input of polynomials and outputs their roots. The polynomials are in the form of a matrix with columns being each polynomial. It works perfectly fine calling directly, but I get strange behaviour once I use pmap.

using Distributed
@everywhere begin
    using PolynomialRoots
end

addprocs(2)

@everywhere global test = ones(Int64, 10,10)

@everywhere begin
    function polyRootS(n)
        q=test[:,n]
        r = roots(q) # provides poly roots from a vector input
        return r
    end
end

function parPolyRoots()
    pmap(x->polyRootS(x) ? error("failed") : x, 1:10; on_error=identity)
end

for i in 1:10
    r = polyRootS(i)
    println(r)
end 
parPolyRoots()

As far as I have read pmap should in effect just be parallelizing the loop when calling polyRootS yet I instead get the outputs:

ComplexF64[0.30901699437494734 + 0.9510565162951534im, 0.8090169943749473 - 0.587785252292473im, -0.8090169943749473 - 0.587785252292473im, -0.8090169943749475 + 0.587785252292473im, 0.8090169943749475 + 0.5877852522924731im, 0.30901699437494745 - 0.9510565162951535im, -1.0 - 7.671452424696258e-18im, -0.30901699437494734 - 0.9510565162951536im, -0.3090169943749474 + 0.9510565162951536im]

for my test matrix and

TypeError(:if, "", Bool, ComplexF64[0.30901699437494734 + 0.9510565162951534im, 0.8090169943749473 - 0.587785252292473im, -0.8090169943749473 - 0.587785252292473im, -0.8090169943749475 + 0.587785252292473im, 0.8090169943749475 + 0.5877852522924731im, 0.30901699437494745 - 0.9510565162951535im, -1.0 - 7.671452424696258e-18im, -0.30901699437494734 - 0.9510565162951536im, -0.3090169943749474 + 0.9510565162951536im])

for my pmap function call. So I am quite puzzled. If someone could point me to some different documentation for pmap and or a few examples that would be especially helpful.

A different question, can I start an @everything begin end statement and cover my defined functions, data, and packages at once or is it best practice to individually isolate them?

Using Julia 1.6.1 in VSCode with Julia extension. Running on Win10 with Intel i5.


Solution

  • The problem is not to do with pmap, it's because of the anonymous function you've defined to be its first argument: x->polyRootS(x) ? error("failed") : x

    If you look at the TypeError (and refer the docs about TypeError's syntax), you can see that it's complaining that it was expecting a Bool and instead got a ComplexF64 array. The left side of the ? in the ternary operator is equivalent to an if condition, and Julia expects a Boolean value there. Instead, the call to polyRootS is returning a ComplexF64[] value.

    It's not clear what you're trying to check there either. Your polyRootS definition doesn't return any error indicator return value, and any exceptions will be handled by the on_error argument already.

    So, replacing that anonymous function with just polyRootS as the first argument to pmap should work.