Any usable SIMD implementation?

Manu via Digitalmars-d digitalmars-d at puremagic.com
Wed Apr 6 19:43:09 PDT 2016


On 7 April 2016 at 10:42, Walter Bright via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
> On 4/6/2016 5:36 AM, Manu via Digitalmars-d wrote:
>>
>> But at very least, the important detail is that the version ID's are
>> standardised and shared among all compilers.
>
>
> It's a reasonable suggestion; some points:
>
> 1. This has been characterized as a blocker, it is not, as it does not
> impede writing code that takes advantage of various SIMD code generation at
> compile time.

It's sufficiently blocking that I have not felt like working any
further without this feature present. I can't feel like it 'works' or
it's 'done', until I can demonstrate this functionality.
Perhaps we can call it a psychological blocker, and I am personally
highly susceptible to those.

> 2. I'm not sure these global settings are the best approach, especially if
> one is writing applications that dynamically adjusts based on the CPU the
> user is running on.

They are necessary to provide a baseline. It is typical when building
code that you specify a min-spec. This is what's used by default
throughout the application.
Runtime selection is not practical in a broad sense. Emitting small
fragments of SIMD here and there will probably take a loss if they are
all surrounded by a runtime selector. SIMD is all about pipelining,
and runtime branches on SIMD version are antithesis to good SIMD
usage; they can't be applied for small-scale deployment.
In my experience, runtime selection is desirable for large scale
instantiations at an outer level of the work loop. I've tried to
design this intent in my library, by making each simd API capable of
receiving SIMD version information via template arg, and within the
library, the version is always passed through to dependent calls.
The Idea is, if you follow this pattern; propagating a SIMD version
template arg through to your outer function, then you can instantiate
your higher-level work function for any number of SIMD feature
combinations you feel is appropriate.
Naturally, this process requires a default, otherwise this usage
baggage will cloud the API everywhere (rather than in the few cases
where a developer specifically wants to make use of it), and many
developers in 2015 feel SSE2 is a weak default. I would choose SSE4.1
in my applications, xbox developers would choose AVX1, it's very
application/target-audience specific, but SSE2 is the only reasonable
selection if we are not to accept a hint from the command line.

> The main trouble comes about when different modules are
> compiled with different settings. What happens with template code
> generation, when the templates are pulled from different modules? What
> happens when COMDAT functions are generated? (The linker picks one
> arbitrarily and discards the others.) Which settings wind up in the
> executable will be not easily predictable.

In my library design, the baseline simd version (expected from the
compiler) is mangled into the symbols, just as in the case a user
overrides it when instantiating a code path that may be selected on
runtime branch.
I had imagined this would solve such link related symbol selection
problems. Can you think of cases where this is insufficient?


> I suspect that using a pragma would be a much better approach:
>
>    pragma(SIMD, AFX)
>    {
>         ... code ...
>    }
>
> Doing it on the command line is certainly the traditional way, but it
> strikes me as being bug-prone and as unhygienic and obsolete as the C
> preprocessor is (for similar reasons).

I've done it with a template arg because it can be manually
propagated, and users can extrapolate the pattern into their outer
work functions, which can then easily have multiple versions
instantiated for runtime selection.
I think it's also important to mangle it into the symbol name for the
reasons I mention above.


More information about the Digitalmars-d mailing list