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

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

问题描述

我正在为我的3D计算进行优化,现在有:

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


  • 一个 code>使用标准C语言库的版本,

  • 一个 SSE $ c> #define USE_SSE

  • 一个 AVX 优化版本, $ c> #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个版本之间切换,可执行文件(例如具有不同的库文件和动态加载正确一个,不知道 inline 函数是否正确)?

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.

推荐答案

有几种解决方案可供选择这个。

There are several solutions for this.

一个基于C ++,你可以创建多个类 - 通常,你实现一个接口类,并使用一个工厂函数给你一个正确的对象类。

One is based on C++, where you'd create multiple classes - typically, you implement a interface class, and use a factory function to give you an object of the correct class.

例如

class Matrix
{
   virtual void Multiply(Matrix &result, Matrix& a, Matrix &b) = 0;
   ... 
};

class MatrixPlain : public Matrix
{
   void Multiply(Matrix &result, Matrix& a, Matrix &b);

};


void MatrixPlain::Multiply(...)
{
   ... implementation goes here...
}

class MatrixSSE: public Matrix
{
   void Multiply(Matrix &result, Matrix& a, Matrix &b);
}

void MatrixSSE::Multiply(...)
{
   ... implementation goes here...
}

... same thing for AVX... 

Matrix* factory()
{
    switch(type_of_math)
    {
       case PlainMath: 
          return new MatrixPlain;

       case SSEMath:
          return new MatrixSSE;

       case AVXMath:
          return new MatrixAVX;

       default:
          cerr << "Error, unknown type of math..." << endl;
          return NULL;
    }
}

或者,如上所述,您可以使用共享库有一个公共接口,并动态加载的库是正确的。

Or, as suggested above, you can use shared libraries that have a common interface, and dynamically load the library that is right.

当然,如果您将Matrix基类实现为纯类,则可以逐步细化并仅实现实际发现的部分是有益的,在基础上实现性能不是很高的crticial的功能。

Of course, if you implement the Matrix base class as your "plain" class, you could do stepwise refinement and implementing only the parts you actually find is beneficial, and rely on the baseclass to implement the functions where performance isn't highly crticial.

编辑:
你说的是内联,我想你正在看错了的函数级别,如果是这样的话。你需要相当大的函数来处理相当多的数据。否则,所有的努力将花费在准备数据到正确的格式,然后做一些计算指令,然后将数据回到内存。

You talk about inline, and I think you are looking at the wrong level of function if that is the case. You want fairly large functions that do something on quite a bit of data. Otherwise, all your effort will be spent on preparing the data into the right format, and then doing a few calculation instructions, and then putting the data back into memory.

我也会考虑如何储存您的资料。你存储的数组有X,Y,Z,W,或者你正在存储大量的X,大量的Y,大量的Z和大量的W在单独的数组[假设我们在做3D计算]?根据你的计算工作原理,你可能会发现,做一个或另一个方法将给你最好的效益。

I would also consider how you store your data. Are you storing sets of an array with X, Y, Z, W, or are you storing lots of X, lots of Y, lots of Z and lots of W in separate arrays [assuming we're doing 3D calculations]? Depending on how your calculation works, you may find that doing one or the other way will give you the best benefit.

我做了一个公平的SSE和3DNow !几年前的优化,而诡计通常更多地关于如何存储数据,所以你可以轻松地抓住一个捆绑一个正确的数据。如果你有错误的数据存储方式,你将浪费大量的时间swizzling数据(移动数据从一种存储方式到另一种)。

I've done a fair bit of SSE and 3DNow! optimisations some years back, and the "trick" is often more about how you store the data so you can easily grab a "bundle" of the right kind of data in one go. If you have the data stored the wrong way, you will be wasting a lot of the time "swizzling data" (moving data from one way of storing to another).

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

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