On 24 October 2012 15:39, Paulo Pinto <span dir="ltr"><<a href="mailto:pjmlp@progtools.org" target="_blank">pjmlp@progtools.org</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5">On Wednesday, 24 October 2012 at 02:41:53 UTC, bearophile wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I have found a nice paper, "Extending a C-like Language for Portable SIMD Programming", (2012), by Roland L., Sebastian Hack and Ingo Wald:<br>
<br>
<a href="http://www.cdl.uni-saarland.de/projects/vecimp/vecimp_tr.pdf" target="_blank">http://www.cdl.uni-saarland.<u></u>de/projects/vecimp/vecimp_tr.<u></u>pdf</a><br>
<br>
SIMD programming is necessary in a system language, or in any language that wants to use the modern CPUs well. So languages like C, C++, D (and Mono-C#) support such wider registers.<br>
<br>
The authors of this paper have understood that it's also important to make SIMD programming easy, almost as easy as scalar code, so most programmers are able to write such kind of correct code.<br>
<br>
So this this paper presents ideas to better express SIMD semantics in a C-like language. They introduce few new constructs in a large subset of C language, with few ideas. The result coding patterns seem easy enough (they are surely look simpler than most multi-core coding patterns I've seen, including Cilk+).<br>

<br>
<br>
They present a simple scalar program in C:<br>
<br>
struct data_t {<br>
    int key;<br>
    int other;<br>
};<br>
<br>
int search(data_t* data , int N) {<br>
    for (int i = 0; i < N; i++) {<br>
        int x = data[i].key;<br>
        if (4 < x & x <= 8) return x;<br>
    }<br>
    return -1;<br>
}<br>
<br>
<br>
Then they explain the three most common ways to represent an array of structs, here a struct that contains 3 values:<br>
<br>
x0 y0 z0 x1 y1 z1 x2 y2 z2 x3 y3 z3 x4 y4 z4 x5 y5 z5 x6 y6 z6 x7 y7 z7<br>
(a) Array of Structures (AoS)<br>
<br>
x0 x1 x2 x3 x4 x5 x6 x7   y0 y1 y2 y3 y4 y5 y6 y7   z0 z1 z2 z3 z4 z5 z6 z7<br>
(b) Structure of Arrays (SoA)<br>
<br>
x0 x1 x2 x3 y0 y1 y2 y3 z0 z1 z2 z3 x4 x5 x6 x7 y4 y5 y6 y7 z4 z5 z6 z7<br>
(c) Hybrid Structure of Arrays (Hybrid SoA)<br>
<br>
They explain how the (c) is the preferred pattern in SIMD programming.<br>
<br>
<br>
Using the (c) data pattern they show how in C with (nice) SIMD intrinsics you write vectorized code (a simd_data_t struct instance contains 8 int values):<br>
<br>
struct simd_data_t {<br>
    simd_int key;<br>
    simd_int other;<br>
};<br>
<br>
int search(simd_data_t* data , int N) {<br>
    for (int i = 0; i < N/L; ++i) {<br>
        simd_int x = load(data[i].key);<br>
        simd_int cmp = simd_and(simd_lt(4, x),<br>
        simd_le(x, 8));<br>
        int mask = simd_to_mask(cmp);<br>
        if (mask != 0) {<br>
            simd_int result = simd_and(mask , x);<br>
            for (int j = 0; j < log2(L); j++)<br>
                result = simd_or(result ,<br>
                whole_reg_shr(result , 1 << j));<br>
                return simd_extract(result , 0);<br>
            }<br>
        }<br>
    return -1;<br>
}<br>
<br>
<br>
D should do become able to do this (that is not too much bad), or better.<br>
<br>
<br>
Their C language extensions allow to write nicer code like:<br>
<br>
struct data_t {<br>
    int key;<br>
    int other;<br>
};<br>
<br>
int search(data_t *scalar data , int scalar N) {<br>
    int L = lengthof(*data);<br>
    for (int i = 0; i < N/L; ++i) {<br>
        int x = data[i].key;<br>
        if (4 < x & x <= 8)<br>
            int block[L] result = [x, 0];<br>
        scalar {<br>
            for (int j = 0; j < log2(L); ++j)<br>
                result |= whole_reg_shr(result , 1 << j);<br>
            return get(x, 0);<br>
        }<br>
    }<br>
    return -1;<br>
}<br>
<br>
<br>
This is based on just few simple ideas, explained in the paper (they are interesting, but quoting here those parts of the paper is not a good idea). Such ideas are not directly portable to D (unless the front-end is changed. Their compiler works by lowering, and emits regular C++ code with intrinsics).<br>

<br>
<br>
Near the end of the paper they also propose some C++ library code:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
the C++ template mechanism would allow to define a hybrid SoA container class: Similar to std::vector which abstracts a traditional C array, one could implement a wrapper around a T block[N]*:<<br>
</blockquote>
<br>
<br>
// scalar context throughout this example<br>
struct vec3 { float x, y, z; };<br>
// vec3 block[N]* pointing to ceil(n/N) elements<br>
hsoa <vec3 > vecs(n);<br>
// preferred vector length of vec3 automatically derived<br>
static const int N = hsoa <vec3 >::vector_length;<br>
int i = /*...*/<br>
hsoa <vec3 >::block_index ii = /*...*/<br>
vec3 v = vecs[i]; // gather<br>
vecs[i] = v; // scatter<br>
vec3 block[N] w = vecs[ii]; // fetch whole block<br>
hsoa <vec3 >::ref r = vecs[i]; // get proxy to a scalar<br>
r = v; // pipe through proxy<br>
// for each element<br>
vecs.foreach([](vec3& scalar v) { /*...*/ });<br>
<br>
<br>
Regardless of the other ideas of their C-like language, a similar struct should be added to Phobos once a bit higher level SIMD support is in better shape in D. Supporting Hybrid-SoA and few operations on it will be an important but probably quite short and simple addition to Phobos collections (it's essentially an struct that acts like an array, with few simple extra operations).<br>

<br>
I think no commonly used language allows both very simple and quite efficient SIMD programming (Scala, CUDA, C, C++, C#, Java, Go, and currently Rust too, are not able to support SIMD programming well. I think currently Haskell too is not supporting it well, but Haskell is very flexible, and it's compiled by a native compiler, so such things are maybe possible to add). So supporting it well in D will be an interesting selling point of D. (Supporting a very simple SIMD coding in D will make D more widespread, but such kind of programming will probably keep being a small niche).<br>

<br>
Bye,<br>
bearophile<br>
</blockquote>
<br>
<br></div></div>
Actually, I am yet to see any language that has SIMD as part of the language standard and not as an extension where each vendor does its own way.<br></blockquote><div><br></div><div>HLSL, GLSL, Cg? :)</div><div>I don't think it's possible considering that D is designed to plug on to various backends.</div>
<div>D already has what's required to do some fairly nice (by comparison) simd stuff with good supporting libraries.</div><div><br></div><div>One thing I can think of that would really improve simd (and not only simd) would be a way to define compound operators.</div>
<div>If the library could detect/hook sequences of operations and implement them more efficiently as a compound, that would make some very powerful optimisations available.</div><div><br></div><div>Simple example:</div><div>
  T opCompound(string seq)(T a, T b, T c) if(seq == "* +") { return _madd(a, b, c); }</div><div><div>  T opCompound(string seq)(T a, T b, T c) if(seq == "+ *") { return _madd(b, c, a); }</div></div><div>
<br></div></div>