C ++中用于SSE/AVX的x86 CPU调度 [英] x86 CPU Dispatching for SSE/AVX in C++

查看:115
本文介绍了C ++中用于SSE/AVX的x86 CPU调度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个算法,该算法得益于使用SSE(2)内在函数进行的手部优化.而且,该算法将来还将能够从256位AVX寄存器中受益.

I have an algorithm which benefits from hand optimisation with SSE(2) intrinsics. Moreover, the algorithm will also be able to benefit from the 256-bit AVX registers in the future.

我的问题是什么是最好的方法

My question is what is the best way to

  • 在编译时注册我课程的可用性变量;因此,如果我的类是:FooFooSSE2FooAVX,我需要一种在运行时确定编译哪些类的方法.
  • 确定当前CPU的功能.在最低级别,这将导致cpuid呼叫.
  • 在运行时根据编译后的内容和受支持的内容决定使用什么.
  • Register the availability variants of my class at compile time; so if my classes are, say: Foo, FooSSE2 and FooAVX I require a means of determining at runtime what classes are compiled in.
  • Determine the capabilities of the current CPU. At the lowest level this will result in a cpuid call.
  • Decide at runtime what to use based on what is compiled and what is supported.

虽然我可以破解以上大部分内容,但这似乎已经是一个足够普遍的问题,因此必定会出现一些最佳实践.理想情况下,我试图避免#ifdef混乱

While I can hack most of the above it seems to be a common enough problem that some best practices must have emerged. Ideally I am trying to avoid the #ifdef mess

#ifdef COMPILE_SSE2
    if (sse2_supported)
        // Use the SSE2 class
#endif

推荐答案

只需创建一个工厂"类或函数来创建您的类的适当实例,并隐藏实现工厂的文件中的所有逻辑.

Just create a "factory" class or function to create appropriate instances of your class and hide all the logic in the file that implements the factory.

具有一些类或文件的本地布尔值,例如"isSSE2Supported"或"isAVXSupported".启动时,调用一些函数来初始化这些值.然后,您的工厂逻辑可以根据这些值进行检查,以确定要使用的类.

Have some class or file local boolean values like "isSSE2Supported" or "isAVXSupported". On startup, call some function to initialize these values. Your factory logic can then check against the values to determine which class to use.

由于SSE2始终在x64芯片上可用,因此您实际上并不想避免所有ifdef.您可以避免在x64构建的某些类中进行编译.

Since SSE2 is always available on x64 chips, you don't really want to avoid all of the ifdefs. You can avoid compiling in some of the classes for x64 builds.

这篇关于C ++中用于SSE/AVX的x86 CPU调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆