有不同的优化(平原,SSE,AVX)在同一个可执行文件C / C ++ [英] Have different optimizations (plain, SSE, AVX) in the same executable with C/C++

查看:126
本文介绍了有不同的优化(平原,SSE,AVX)在同一个可执行文件C / C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发展优化为我的3D计算,我现在有:

I'm developing optimizations for my 3D calculations and I now have:


  • 一个平原使用标准C语言库版本,

  • SSE 优化的版本,编译使用preprocessor ​​的#define USE_SSE

  • AVX 优化的版本,编译使用preprocessor ​​的#define USE_AVX

  • a "plain" version using the standard C language libraries,
  • an SSE optimized version that compiles using a preprocessor #define USE_SSE,
  • an AVX optimized version that compiles using a preprocessor #define USE_AVX

是否有可能在3个版本之间切换,而无需编写不同的可执行文件(例如,具有不同的库文件和动态加载的正确的,不知道是不是在线功能是为正确)?
我会考虑在有这种开关在软件的性能。

Is it possible to switch between the 3 versions without having to compile different executables (ex. having different library files and loading the "right" one dynamically, don't know if inline functions are "right" for that)? I'd consider also performances in having this kind of switch in the software.

推荐答案

一个办法是实施符合​​相同的接口三个库。对于动态库,你可以交换库文件和可执行文件会使用任何发现。例如在W​​indows上,你可以编译3 DLL文件:

One way is to implement three libraries conforming to the same interface. With dynamic libraries, you can just swap the library file and the executable will use whatever it finds. For example on Windows, you could compile three DLLs:


  • PlainImpl.dll

  • SSEImpl.dll

  • AVXImpl.dll

和然后进行对 Impl.dll 可执行文件的链接。现在只是把三个具体的DLL之一到同一目录 .exe文件,其重命名为 Impl.dll ,它会使用该版本。同样的原则也应该主要适用于类UNIX操作系统。

And then make the executable link against Impl.dll. Now just put one of the three specific DLLs into the same directory as the .exe, rename it to Impl.dll, and it will use that version. The same principle should basically be applicable on a UNIX-like OS.

下一步将是编程方式加载库,这可能是最灵活的,但它是操作系统特定的,需要更多的工作(就像打开库,获取函数指针等)

The next step would be to load the libraries programmatically, which is probably the most flexible, but it is OS specific and requires some more work (like opening the library, obtaining function pointers etc.)

编辑:的但当然,你可以只实现了功能的三倍,并选择一个在运行时,根据一些参数/配置文件的设置等,在其他的答案内衬出

But of course, you could just implement the function three times and select one at runtime, depending on some parameter/config file setting etc., as lined out in the other answers.

这篇关于有不同的优化(平原,SSE,AVX)在同一个可执行文件C / C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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