I want to learn how to use LoopVectorization
. It is known that LoopVectorization
cannot deal with complex number directly, and it need StructArray
, then I write a simple loop for practicing. However there are some errors:
Start
val=StructArray(0.0+0.0*im);
rd=rand(100);
function f(v)
@turbo for n in 1:100
v+=StructArray(1.0+im*0.1 - rd[n]);
end
v
end
@time f(val)
0.029814 seconds (71.45 k allocations: 3.591 MiB, 97.51% compilation time)
Warning: #= In[6]:2 =#:
│ `LoopVectorization.check_args` on your inputs failed; running fallback `@inbounds @fastmath` loop instead.
│ Use `warn_check_args=false`, e.g. `@turbo warn_check_args=false ...`, to disable this warning.
└ @ Main C:\Users\.julia\packages\LoopVectorization\FMfT8\src\condense_loopset.jl:1049
End
The error says that argument check is not pass. To solve this, I make a little modifications on loop:
function f1(val2)
@turbo for n in 1:100
v=val2;
val2 =v+StructArray(1.0+im*0.1 - rd[n])
end
end
@time f1(val)
0.034055 seconds (70.56 k allocations: 3.684 MiB, 99.82% compilation time)
function f2(v)
@turbo for n in 1:1000
add=StructArray(1.0+im*0.1 - rd[n]);
v=v+add;
end
v
end
@time f2(val)
0.029021 seconds (71.50 k allocations: 3.598 MiB, 97.83% compilation time)
┌ Warning: #= In[7]:2 =#:
│ `LoopVectorization.check_args` on your inputs failed; running fallback `@inbounds @fastmath` loop instead.
│ Use `warn_check_args=false`, e.g. `@turbo warn_check_args=false ...`, to disable this warning.
└ @ Main C:\Users\.julia\packages\LoopVectorization\FMfT8\src\condense_loopset.jl:1049
In f1
, LoopVectorization
doesn’t throw error, however all three have the same running time. How could I really solve this and how to use LoopVectorization
generally?