You can use `@simd` with the innermost loop. (It really should be extended
to work with loop nests as well, but that hasn't been done yet.) Here is
how `@simd` would look in your case:

```Julia
@inbounds for l = 1:1000, k = 1:20, j = 1:20
    @simd for i = 1:70
        bar = bar + (array1[i, l] - array3[1, i, j, k])^2 + (array2[i, l] -
array3[2, i, j, k])^2
    end
end
```

That is, you split off the innermost loop, and attach the `@simd` there.

-erik


On Wed, Jun 1, 2016 at 7:51 AM, Lutfullah Tomak <[email protected]>
wrote:

> Ok, I revise what I said about @ simd, according to documentation, it only
> works for
> one dimensional ranges and it does not allow this type for-loop and throws
> error.
>
> Here is timings for me
>
> julia> function foo()
>           array1 = rand(70, 1000)
>           array2 = rand(70, 1000)
>           array3 = rand(2, 70, 20, 20)
>           bar = 0
>           @time for l = 1:1000, k = 1:20, j = 1:20, i = 1:70
>                 bar = bar +
>                     (array1[i, l] - array3[1, i, j, k])^2 +
>                     (array2[i, l] - array3[2, i, j, k])^2
>           end
>        end
> foo (generic function with 1 method)
>
>
> julia> foo()
>   1.215454 seconds (84.00 M allocations: 1.252 GB, 4.16% gc time)
>
>
> julia> foo()
>   1.308979 seconds (84.00 M allocations: 1.252 GB, 3.92% gc time)
>
>
> julia> function foo()
>           array1 = rand(70, 1000)
>           array2 = rand(70, 1000)
>           array3 = rand(2, 70, 20, 20)
>           bar = 0.0
>           @time for l = 1:1000, k = 1:20, j = 1:20, i = 1:70
>                 bar = bar +
>                     (array1[i, l] - array3[1, i, j, k])^2 +
>                     (array2[i, l] - array3[2, i, j, k])^2
>           end
>        end
> foo (generic function with 1 method)
>
>
> julia> foo()
>   0.114811 seconds
>
>
> julia> foo()
>   0.150542 seconds
>
>
> julia> function foo()          array1 = rand(70, 1000)
>           array2 = rand(70, 1000)
>           array3 = rand(2, 70, 20, 20)
>           bar = 0.0
>           @time @inbounds for l = 1:1000, k = 1:20, j = 1:20, i = 1:70
>                 bar = bar +
>                     (array1[i, l] - array3[1, i, j, k])^2 +
>                     (array2[i, l] - array3[2, i, j, k])^2
>           end
>
>        end
>
> foo (generic function with 1 method)
>
>
> julia> foo()
>   0.004927 seconds
>
>
>
>


-- 
Erik Schnetter <[email protected]>
http://www.perimeterinstitute.ca/personal/eschnetter/

Reply via email to