Best way to fill an array of Struct of Float_vs with an array of Struct of floats in VC
Matthias Kretz
[please enable javascript to see the address]
Wed Aug 26 14:40:12 CEST 2015
Hi,
so first of all the new documentation that I promised:
https://web-docs.gsi.de/~mkretz/Vc-master/group__Simdize.html
Vc::simdize will help you implement the pattern Krishna asked for easily and
conveniently. It's certainly quite a load on the compiler, but not on the
generated code.
On Wednesday 26 August 2015 12:28:30 Jaroslav Malec wrote:
> As far as I know, in the current implementation you can use *scatter* and
> *gather* for these operations.
> Please correct me If this is a misconception.
You are right about *can*. But unless you don't care for efficiency, avoid
gather and scatter as much as possible. Gather and scatter are a crutch for
situations that really cannot be improved to better suited memory patterns. In
this case you have a regular memory pattern. Gather and scatter works for
arbitrary (which includes regular, of course) memory patterns.
For regular memory patterns you want to go for (de)interleaving instead of
gather/scatter. Consider
mem: [a0 b0 a1 b1 a2 b2 a3 b3 a4 b4 ... ]
vector_load(&mem[0]) -> {a0 b0 a1 b1}
vector_load(&mem[4]) -> {a2 b2 a3 b3}
But now you really wanted to have {a0 a1 a2 a3} and {b0 b1 b2 b3}. Via a
*deinterleave* operation (consisting of multiple efficient instructions) you
can transform the two vector loads to the desired vectors.
If you instead do gathers you basically do:
scalar_load(&mem[0]) -> a0
scalar_load(&mem[2]) -> a1
scalar_load(&mem[4]) -> a2
scalar_load(&mem[6]) -> a3
scalar_load(&mem[1]) -> b0
scalar_load(&mem[3]) -> b1
scalar_load(&mem[5]) -> b2
scalar_load(&mem[7]) -> b3
combine(a0, a1, a2, a3) -> {a0 a1 a2 a3}
combine(b0, b1, b2, b3) -> {b0 b1 b2 b3}
Especially the large number of loads is what you want to avoid. Many SIMD
codes are quickly limited on the load or store ports of the CPU.
With AVX2 you get gather instructions. That eases the load on the CPU a little
bit. But the instruction is more expensive that you'd like it to be.
Regarding the *deinterleave* operations. There's lots of work on abstracting
them in Vc. I'm sure the documentation isn't good enough yet... You could help
by making specific request for documentation on
https://github.com/VcDevel/Vc/issues.
Hope this helps!
Cheers,
Matthias
>
> If I'm on the right track on this, could you provide some code examples
> that relate
> to what Krishna has in mind? It could be contained in the documentation
> also.
> We would both appreciate it.
>
> Thanks.
>
> Jaroslav Malec
>
> tel. 731 160 847
>
>[please enable javascript to see the address]>:
> > Am working on 0.7. Any particular exampke where you load such structures
> > into vectors?
> >
>[please enable javascript to see the address]> wrote:
> >> Hello Krishna,
> >>
> >> On Thursday 13 August 2015 16:48:16 Krishna Narasimhan wrote:
> >> > Struct A{
> >> >
> >> > float a,b,c
> >> >
> >> > }
> >> >
> >> > Struct A_v
> >> > {
> >> >
> >> > float_v a_v,b_v,c_v
> >> >
> >> > }
> >> >
> >> > void main()
> >> > {
> >> >
> >> > A a = new A[1000];
> >> > A_v a_v = new A_v[1000/float_v::Size]
> >> > // Assume here there is a code that populates a with values
> >> >
> >> > I want to write a code here which would basically move values of a
> >>
> >> into
> >>
> >> > a_v because deinterleave doesnt work. Is there a form of fill() or
> >>
> >> store()
> >>
> >> > or load() I can use here?
> >>
> >> I have written lots of such code for Vc. I recommend you take a look at
> >> Vc
> >> master. Are you currently working on 0.7 or on master?
> >>
> >> In any case. Your mail prompted me to work on my documentation. I'll
> >> hopefully
> >> have a new section on Vc::simdize<T> up today. I'll let you know.
> >>
> >> Cheers,
> >>
> >> Matthias
> >>
> >> _______________________________________________
> >> Vc mailing list
>[please enable javascript to see the address]
> >> https://compeng.uni-frankfurt.de/mailman/listinfo/vc
> >
> > _______________________________________________
> > Vc mailing list
>[please enable javascript to see the address]
> > https://compeng.uni-frankfurt.de/mailman/listinfo/vc
More information about the Vc
mailing list