Need help understanding how to run a for loop in parallel

Hello,

I am writing a huge program and given the amount of time required to perform the whole thing, i would like to use some parallel computation, however, i don’t understand how to implement it.

let’s say i have this piece of code :

function dummy(a)
for i in 1:100
a=a+1
end
return a
end

for i in 1:20
dummy(i)
end

my question is : how can i ask my computer to compute the second for loop in parallel. For example use 4cores/threads to do the for loop 4 steps at a time ?

i tried using @parallel and @thread before the for loop, but i always get an error

Thanks in advance

function dummy(a)
    b = copy(a)
    for i in 1:100
        b=b+1
    end
    return b
end

You optimize it to
function dummy2(a)
    return a + 100
end

Then you do this in parallel
for i in 1:20
    i+100
end

Which gets optimized to
120
# test1.jl
function dummy(a)
    for i in 1:100
      a=a+1
    end
    return a
end

Threads.@threads for i = 1 : 20
    println(Threads.threadid());
    dummy(i);
end
julia> include("test1.jl")
2
6
5
4
2
4
[...]