# Help with @distributed with an iterator and speed issue

**URL:** <https://discourse.julialang.org/t/help-with-distributed-with-an-iterator-and-speed-issue/66292>\
**Category:** Performance\
**Tags:** distributed\
**Created:** [August 12, 2021, 8:01pm UTC](https://discourse.julialang.org/t/help-with-distributed-with-an-iterator-and-speed-issue/66292 "2021-08-12T20:01:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![scheinerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scheinerman/32/35303_2.png) [@scheinerman](https://discourse.julialang.org/u/scheinerman)\
**Post date:** [August 12, 2021, 8:01pm UTC](https://discourse.julialang.org/t/help-with-distributed-with-an-iterator-and-speed-issue/66292/1 "2021-08-12T20:01:56Z")

</div>

I have two questions about the following code (in which I’m looking to get the largest eigenvalue of a matrix by permuting its rows).

```julia
using LinearAlgebra, Combinatorics, Distributed

function maxeig(A::Matrix)
    return maximum(real.(eigvals(A)))
end

function maxmaxeig(A::Matrix)
    n, c = size(A)
    f(p) = maxeig(A[p,:])
    return maximum(f(p) for p in permutations(1:n))
end

function maxmaxeig_dist(A::Matrix)
    n, c = size(A)
    f(p) = maxeig(A[p,:])
    @distributed (max) for p in permutations(1:n) 
        f(p)
    end
end

```

**Question 1**

The first function works fine, but this seems to be a good candidate for distributed computing. So I start with `julia -p 8` and include this code with an `@everywhere include("filename.jl")`. The distributed version fails with this error message:

```julia
julia> maxmaxeig_dist(A)
ERROR: MethodError: no method matching firstindex(::Combinatorics.Permutations{Int64})
Closest candidates are:
  firstindex(::Any, ::Any) at abstractarray.jl:366
  firstindex(::Polynomials.LaurentPolynomial) at /Users/ers/.julia/packages/Polynomials/1aa8e/src/polynomials/LaurentPolynomial.jl:225
  firstindex(::Polynomials.SparsePolynomial) at /Users/ers/.julia/packages/Polynomials/1aa8e/src/polynomials/SparsePolynomial.jl:143
  ...
Stacktrace:
 [1] preduce(reducer::Function, f::Function, R::Combinatorics.Permutations{Int64})
   @ Distributed /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.6/Distributed/src/macros.jl:269
 [2] maxmaxeig_dist(A::Matrix{Int64})
   @ Main ~/tmp/maxmaxeig.jl:16
 [3] top-level scope
   @ REPL[37]:1

```

I can make the parallel version work properly by replacing `for p in permutations(1:n) ` with `for p in collect(permutations(1:n))` which expands the iterator `permutations(1:n)` into a very long list.

**Question 2**

The distributed version is a good deal slower than the first, even if I “level the playing field” by wrapping `for p in permutations(1:n)` with `collect`. I’m guessing that I’ve got a lot of data movement involving the passing the matrix `A` (say, 10-by-10) around.

Finally: I’m not an expert, so beginner friendly advice would be most welcome. Thanks!
