SIMD programming languages [closed]
Asked Answered
T

8

10

In the last couple of years, I've been doing a lot of SIMD programming and most of the time I've been relying on compiler intrinsic functions (such as the ones for SSE programming) or on programming assembly to get to the really nifty stuff. However, up until now I've hardly been able to find any programming language with built-in support for SIMD.

Now obviously there are the shader languages such as HLSL, Cg and GLSL that have native support for this kind of stuff however, I'm looking for something that's able to at least compile to SSE without autovectorization but with built-in support for vector operations. Does such a language exist?

This is an example of (part of) a Cg shader that does a spotlight and in terms of syntax this is probably the closest to what I'm looking for.

float4 pixelfunction(
    output_vs IN, 
    uniform sampler2D texture : TEX0, 
    uniform sampler2D normals : TEX1, 
    uniform float3 light, 
    uniform float3 eye ) : COLOR
{
    float4 color    = tex2D( texture, IN.uv );
    float4 normal   = tex2D( normals, IN.uv ) * 2 - 1;

    float3 T = normalize(IN.T);
    float3 B = normalize(IN.B);

    float3 N = 
        normal.b * normalize(IN.normal) +
        normal.r * T +
        normal.g * B;

    float3 V = normalize(eye - IN.pos.xyz);
    float3 L = normalize(light - IN.pos);
    float3 H = normalize(L + V);

    float4 diffuse  = color * saturate( dot(N, L) );
    float4 specular = color * pow(saturate(dot(N, H)), 15);
    float falloff   = dot(L, normalize(light));

    return pow(falloff, 5) * (diffuse + specular);
}

Stuff that would be a real must in this language is:

  • Built in swizzle operators
  • Vector operations (dot, cross, normalize, saturate, reflect et cetera)
  • Support for custom data types (structs)
  • Dynamic branching would be nice (for loops, if statements)
Titmouse answered 13/9, 2009 at 12:50 Comment(0)
T
8

So recently Intel released ISPC which is exactly what I was looking for when asking this question. It's a language that can link with normal C code, has and implicit execution model, and support for all the features mentioned in the start post (swizzle operators, branching, data structs, vector ops, shader like) and compiles for SSE2, SSE4, AVX, AVX2, and Xeon Phi vector instructions.

Titmouse answered 28/3, 2012 at 21:8 Comment(1)
Fixed link: ispc.github.io or github.com/ispc/ispcCorduroys
H
7

Your best bet is probably OpenCL. I know it has mostly been hyped as a way to run code on GPUs, but OpenCL kernels can also be compiled and run on CPUs. OpenCL is basically C with a few restrictions:

  1. No function pointers
  2. No recursion

and a bunch of additions. In particular vector types:

float4 x = float4(1.0f, 2.0f, 3.0f, 4.0f);
float4 y = float4(10.0f, 10.0f, 10.0f, 10.0f);

float4 z = y + x.s3210 // add the vector y with a swizzle of x that reverses the element order

On big caveat is that the code has to be cleanly sperable, OpenCL can't call out to arbitrary libraries, etc. But if your compute kernels are reasonably independent then you basically get a vector enhanced C where you don't need to use intrinsics.

Here is a quick reference/cheatsheet with all of the extensions.

Hermia answered 21/9, 2009 at 14:11 Comment(5)
Can I still link OpenCL libs to a C application and hand it a set of vectors?Titmouse
Comming to think about it, it doesn't need to be able to link, I just need to be able to pass it some data :-)Titmouse
Basically, you compile an OpenCL compute kernel which has a C function as an entry point, then you run tell OpenCL to run the kernel using parameters you specify, which could be vectors, data sets, or even textures.Hermia
This seems to be the best solution for the problem I have at hand, thanks.Titmouse
AFAIU OpenCL requires drivers on the host -- how would an app (say, a game) that uses OpenCL be shipped on the 3 major desktop platforms?Unfasten
S
6

It's not really the language itself, but there is a library for Mono (Mono.Simd) that will expose the vectors to you and optimise the operations on them into SSE whenever possible:

Separative answered 13/9, 2009 at 13:44 Comment(1)
This solution looks nice; looks far better than the C++ intrinsics. However the solution is roughly equivalent and not what I'm looking for. (I was looking for actual languages designed with SIMD built-in instead of bolted on). However, it's definitely something to remember when doing a .Net based solution.Titmouse
T
2

It's a library for C++, rather than built into the language, but Eigen is pretty invisible once your variables are declared.

Tahsildar answered 27/3, 2012 at 16:16 Comment(0)
M
0

That would be Fortran that you are looking for. If memory serves even the open-source compilers (g95, gfortran) will take advantage of SSE if it's implemented on your hardware.

Matroclinous answered 13/9, 2009 at 13:30 Comment(1)
Those Fortran implementations still use automatic vectorization in the same way most C++ compilers support this. The problem I have with this is that it's very hard to predict what code will be vectorized and what code won't. Now I don't know the state of this in Fortran compilers because my background is in C++, so I think I'd prefer a high-level shader-like approach that gives me more control over the final output.Titmouse
T
0

Currently the best solution is to do it myself by creating a back-end for the open-source Cg frontend that Nvidia released, but I'd like to save myself the effort so I'm curious if it's been done before. Preferably I'd start using it right away.

Titmouse answered 17/9, 2009 at 12:40 Comment(2)
Cg isn't open source, it's proprietary to Nvidia. It would be an enormous amount of work to create a back-end that generated SIMD code for a CPU. As Louis answers, you should seriously check out OpenCL. You can write processing kernels in a C-based language (very similar to Cg and GLSL) and run it either on the GPU or CPU (where it will generate SIMD code for you). OpenCL is cross-platform, supported by many vendors (Nvidia, ATI, Apple, etc) and you can get an SDK right away.Virgil
The Cg front-end source code is available at developer.nvidia.com/object/cg_compiler_code.html The code is made available specifically for creating a back-end for the compiler. However, I do prefer existing solutions such as OpenCL.Titmouse
K
0

The D programming language also provides access to SIMD in a similar way than Mono.SIMD.

Kathline answered 2/10, 2012 at 21:43 Comment(0)
C
0

I know this question is a bit old, but I found myself in a similar predicament and decided I should just make my own.

I haven't gotten very far yet in the slightest, but if you're interested in the directions that I'm exploring it might be worth a look. :)

https://github.com/HappMacDonald/MasterBlaster

MasterBlaster is a functional programming language, but it's going to compile down into a bytecode that is ultimately it's own much simpler stack-based language called Crude. Crude then compiles directly into assembly.

My strategy is a SIMD-first one: unoptimized executables will use almost entirely SIMD, and then one of the potential optimizations will be to simplify code that isn't benefiting from SIMD into using only general registers instead.

Crude is up to the turing-complete stage, but only exists as a few dozen GAS macros presently. I'm working towards a self-contained compiler for it, and building out the iterator/generator features that are the stars of the show when it comes to SIMD acceleration.

No vector-matrix-etc support just yet, but that is on the roadmap and I'll probably bear your description in mind when writing up that syntax. :)

Coben answered 28/9, 2022 at 8:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.