# How to @parallel a CartesianRange?

**URL:** <https://discourse.julialang.org/t/how-to-parallel-a-cartesianrange/924>\
**Category:** General Usage\
**Created:** [December 13, 2016, 4:37pm UTC](https://discourse.julialang.org/t/how-to-parallel-a-cartesianrange/924 "2016-12-13T16:37:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![oatlzzvztd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oatlzzvztd/32/3285_2.png) [@oatlzzvztd](https://discourse.julialang.org/u/oatlzzvztd)\
**Post date:** [December 13, 2016, 4:37pm UTC](https://discourse.julialang.org/t/how-to-parallel-a-cartesianrange/924/1 "2016-12-13T16:37:53Z")

</div>

In serial, the following for loop is just fine:

```julia
A = randn(5,5)

for inds = CartesianRange(size(A))
  i, j = inds.I
  # Some function of A[i, j], i, and j
end

```

However, naively adding @sync @parallel to the for loop throws a MethodError:

```julia
ERROR: LoadError: MethodError: no method matching getindex(::CartesianRange{CartesianIndex{2}}, ::UnitRange{Int64})

```

I assume this is the part of the @parallel macro that subdivides the range into approximately-equal chunklets.

I’ve tried simply adding the relevant getindex definitions, but I can’t settle on a definition that isn’t awful. Thoughts?

I thought about working around the issue by doing something like

```julia
@sync @parallel for idx = eachindex(A)
  i, j = ind2sub(size(A), idx)
  # Some function of A[idx], i, and j
end

```

but in real life `A` is a pretty large matrix of arbitrary dimension and ind2sub seems expensive relative to the rest of this inner loop.
