13

我需要一些想法如何以某种方式编写一些可并行化问题的 C++ 跨平台实现,以便我可以利用 SIMD(SSE、SPU 等)(如果可用)。以及我希望能够在运行时在 SIMD 和非 SIMD 之间切换。

你会建议我如何解决这个问题? (当然我不想为所有可能的选项多次实施该问题)

我可以看到这对于 C++ 来说可能不是一件容易的事,但我相信我遗漏了一些东西。到目前为止,我的想法看起来像这样......一个类 cStream 将是单个字段的数组。使用多个 cStreams 我可以实现 SoA(数组结构)。然后使用一些 Functor,我可以伪造需要在整个 cStream 上执行的 Lambda 函数。

// just for example I'm not expecting this code to compile
cStream a; // something like float[1024]
cStream b;
cStream c;

void Foo()
{
    for_each(
        AssignSIMD(c, MulSIMD(AddSIMD(a, b), a)));
}

其中 for_each 将负责增加流的当前指针,以及使用 SIMD 和不使用 SIMD 内联函子的主体。

像这样:

// just for example I'm not expecting this code to compile
for_each(functor<T> f)
{
#ifdef USE_SIMD
    if (simdEnabled)
        real_for_each(f<true>()); // true means use SIMD
    else
#endif
        real_for_each(f<false>());
}

请注意,如果 SIMD 已启用,则检查一次并且循环围绕主函子。

4

6 回答 6

3

如果有人感兴趣,这是我用来测试我在阅读 Paul 发布的库时带来的新想法的脏代码。

谢谢保罗!

// This is just a conceptual test
// I haven't profile the code and I haven't verified if the result is correct
#include <xmmintrin.h>


// This class is doing all the math
template <bool SIMD>
class cStreamF32
{
private:
    void*       m_data;
    void*       m_dataEnd;
    __m128*     m_current128;
    float*      m_current32;

public:
    cStreamF32(int size)
    {
        if (SIMD)
            m_data = _mm_malloc(sizeof(float) * size, 16);
        else
            m_data = new float[size];
    }
    ~cStreamF32()
    {
        if (SIMD)
            _mm_free(m_data);
        else
            delete[] (float*)m_data;
    }

    inline void Begin()
    {
        if (SIMD)
            m_current128 = (__m128*)m_data;
        else
            m_current32 = (float*)m_data;
    }

    inline bool Next()
    {
        if (SIMD)
        {
            m_current128++;
            return m_current128 < m_dataEnd;
        }
        else
        {
            m_current32++;
            return m_current32 < m_dataEnd;
        }
    }

    inline void operator=(const __m128 x)
    {
        *m_current128 = x;
    }
    inline void operator=(const float x)
    {
        *m_current32 = x;
    }

    inline __m128 operator+(const cStreamF32<true>& x)
    {
        return _mm_add_ss(*m_current128, *x.m_current128);
    }
    inline float operator+(const cStreamF32<false>& x)
    {
        return *m_current32 + *x.m_current32;
    }

    inline __m128 operator+(const __m128 x)
    {
        return _mm_add_ss(*m_current128, x);
    }
    inline float operator+(const float x)
    {
        return *m_current32 + x;
    }

    inline __m128 operator*(const cStreamF32<true>& x)
    {
        return _mm_mul_ss(*m_current128, *x.m_current128);
    }
    inline float operator*(const cStreamF32<false>& x)
    {
        return *m_current32 * *x.m_current32;
    }

    inline __m128 operator*(const __m128 x)
    {
        return _mm_mul_ss(*m_current128, x);
    }
    inline float operator*(const float x)
    {
        return *m_current32 * x;
    }
};

// Executes both functors
template<class T1, class T2>
void Execute(T1& functor1, T2& functor2)
{
    functor1.Begin();
    do
    {
        functor1.Exec();
    }
    while (functor1.Next());

    functor2.Begin();
    do
    {
        functor2.Exec();
    }
    while (functor2.Next());
}

// This is the implementation of the problem
template <bool SIMD>
class cTestFunctor
{
private:
    cStreamF32<SIMD> a;
    cStreamF32<SIMD> b;
    cStreamF32<SIMD> c;

public:
    cTestFunctor() : a(1024), b(1024), c(1024) { }

    inline void Exec()
    {
        c = a + b * a;
    }

    inline void Begin()
    {
        a.Begin();
        b.Begin();
        c.Begin();
    }

    inline bool Next()
    {
        a.Next();
        b.Next();
        return c.Next();
    }
};


int main (int argc, char * const argv[]) 
{
    cTestFunctor<true> functor1;
    cTestFunctor<false> functor2;

    Execute(functor1, functor2);

    return 0;
}
于 2010-01-23T10:00:30.473 回答
3

您可能想查看 MacSTL 库的源代码以了解该领域的一些想法:www.pixelglow.com/macstl/

于 2010-01-23T08:41:34.947 回答
2

您可能想看一下我对 SIMD/非 SIMD 的尝试:

  • vrep,一个专门针对 SIMD 的模板化基类(请注意它如何区分仅浮点 SSE 和引入整数向量的 SSE2。)。

  • 更有用的 v4fv4i等类(通过中间v4子类化)。

当然,它比 SoA 更适合用于rgba / xyz类型计算的 4 元素向量,因此当 8 路 AVX 出现时将完全失去动力,但一般原则可能有用。

于 2010-01-23T14:28:52.113 回答
2

我见过的最令人印象深刻的 SIMD 缩放方法是 RTFact 光线追踪框架:幻灯片纸张。很值得一看。研究人员与英特尔密切相关(萨尔布吕肯现在是英特尔视觉计算研究所的所在地),因此您可以确定向前扩展至 AVX 和 Larrabee 是他们的想法。

英特尔的Ct “数据并行”模板库看起来也很有前途。

于 2010-01-23T14:39:22.463 回答
1

请注意,给定的示例决定在编译时执行什么(因为您使用的是预处理器),在这种情况下,您可以使用更复杂的技术来决定您实际想要执行的内容;例如,Tag Dispatch:http ://cplusplus.co.il/2010/01/03/tag-dispatching/ 按照那里显示的示例,您可以使用 SIMD 实现快速实现,而没有 SIMD 实现速度慢。

于 2010-01-23T08:16:15.937 回答
0

您是否考虑过使用现有的解决方案,例如liboil?它实现了许多常见的 SIMD 操作,并且可以在运行时决定是否使用 SIMD/非 SIMD 代码(使用由初始化函数分配的函数指针)。

于 2010-01-23T14:57:27.663 回答