Taking advantage of SSE and other CPU extensions

后端 未结 5 482
南笙
南笙 2021-02-04 06:41

Theres are couple of places in my code base where the same operation is repeated a very large number of times for a large data set. In some cases it\'s taking a considerable tim

相关标签:
5条回答
  • 2021-02-04 07:05

    Good reading on the subject: Stop the instruction set war

    Short overview: Sorry, it is not possible to solve your problem in simple and most compatible (Intel vs. AMD) way.

    0 讨论(0)
  • 2021-02-04 07:05

    The SSE intrinsics work with visual c++, GCC and the intel compiler. There is no problem to use them these days.

    Note that you should always keep a version of your code that does not use SSE and constantly check it against your SSE implementation.

    This helps not only for debugging, it is also usefull if you want to support CPUs or architectures that don't support your required SSE versions.

    0 讨论(0)
  • 2021-02-04 07:12

    For your second point there are several solutions as long as you can separate out the differences into different functions:

    • plain old C function pointers
    • dynamic linking (which generally relies on C function pointers)
    • if you're using C++, having different classes that represent the support for different architectures and using virtual functions can help immensely with this.

    Note that because you'd be relying on indirect function calls, the functions that abstract the different operations generally need to represent somewhat higher level functionality or you may lose whatever gains you get from the optimized instruction in the call overhead (in other words don't abstract the individual SSE operations - abstract the work you're doing).

    Here's an example using function pointers:

    typedef int (*scale_func_ptr)( int scalar, int* pData, int count);
    
    
    int non_sse_scale( int scalar, int* pData, int count)
    {
        // do whatever work needs done, without SSE so it'll work on older CPUs
    
        return 0;
    }
    
    int sse_scale( int scalar, in pData, int count)
    {
        // equivalent code, but uses SSE
    
        return 0;
    }
    
    
    // at initialization
    
    scale_func_ptr scale_func = non_sse_scale;
    
    if (useSSE) {
        scale_func = sse_scale;
    }
    
    
    // now, when you want to do the work:
    
    scale_func( 12, theData_ptr, 512);  // this will call the routine that tailored to SSE 
                                        // if the CPU supports it, otherwise calls the non-SSE
                                        // version of the function
    
    0 讨论(0)
  • 2021-02-04 07:15

    Rather than hand-coding an alternative SSE implementation to your scalar code, I strongly suggest you have a look at OpenCL. It is a vendor-neutral portable, cross-platform system for computationally intensive applications (and is highly buzzword-compliant!). You can write your algorithm in a subset of C99 designed for vectorised operations, which is much easier than hand-coding SSE. And best of all, OpenCL will generate the best implementation at runtime, to execute either on the GPU or on the CPU. So basically you get the SSE code written for you.

    Theres are couple of places in my code base where the same operation is repeated a very large number of times for a large data set. In some cases it's taking a considerable time to process these.

    Your application sounds like just the kind of problem that OpenCL is designed to address. Writing alternative functions in SSE would certainly improve the execution speed, but it is a great deal of work to write and debug.

    Is there a compiler and OS independent way writing the code to take advantage of SSE instructions? I like the VC++ intrinsics, which include SSE operations, but I haven't found any cross compiler solutions.

    Yes. The SSE intrinsics have been essentially standardised by Intel, so the same functions work the same between Windows, Linux and Mac (specifically with Visual C++ and GNU g++).

    I still need to support some CPU's that either have no or limited SSE support (eg Intel Celeron). Is there some way to avoid having to make different versions of the program, like having some kind of "run time linker" that links in either the basic or SSE optimised code based on the CPU running it when the process is started?

    You could do that (eg. using dlopen()) but it is a very complex solution. Much simpler would be (in C) to define a function interface and call the appropriate version of the optimised function via function pointer, or in C++ to use different implementation classes, depending on the CPU detected.

    With OpenCL it is not necessary to do this, as the code is generated at runtime for the given architecture.

    What about other CPU extensions, looking at the instruction sets of various Intel and AMD CPU's shows there are a few of them?

    Within the SSE instruction set, there are many flavours. It can be quite difficult to code the same algorithm in different subsets of SSE when certain instructions are not present. I suggest (at least to begin with) that you choose a minimum supported level, such as SSE2, and fall back to the scalar implementation on older machines.

    This is also an ideal situation for unit/regression testing, which is very important to ensure your different implementations produce the same results. Have a test suite of input data and known good output data, and run the same data through both versions of the processing function. You may need to have a precision test for passing (ie. the difference epsilon between the result and the correct answer is below 1e6, for example). This will greatly aid in debugging, and if you build in high-resolution timing to your testing framework, you can compare the performance improvements at the same time.

    0 讨论(0)
  • 2021-02-04 07:22

    In answer to your comment:

    So effectively, as long as I don't try to actually execute code containing unsupported instructions I'm fine, and I could get away with an "if(see2Supported){...}else{...}" type switch?

    Depends. It's fine for SSE instructions to exist in the binary as long as they're not executed. The CPU has no problem with that.

    However, if you enable SSE support in the compiler, it will most likely swap a number of "normal" instructions for their SSE equivalents (scalar floating-point ops, for example), so even chunks of your regular non-SSE code will blow up on a CPU that doesn't support it.

    So what you'll have to do is most likely compile on or two files separately, with SSE enabled, and let them contain all your SSE routines. Then link that with the rest of the app, which is compiled without SSE support.

    0 讨论(0)
提交回复
热议问题