与Yeppp一起表演!比本机实现慢

Performance with Yeppp! is slower than native implementation

本文关键字:本机 实现 Yeppp 一起 表演      更新时间:2023-10-16

嗨,我正在尝试使用Yeppp提高代码中向量代数的性能!然而,库的性能实际上越来越差。。。这里有一段Vector类代码:

#include "Vector3.h"
#include <cmath>
#include "yepCore.h"
Vector3::Vector3()
{
    //ctor
}
Vector3::~Vector3()
{
    //dtor
}
Vector3::Vector3(float X, float Y, float Z)
{
    x = X;
    y = Y;
    z = Z;
}

float& Vector3::operator[](int idx)
{
    return (&x)[idx];
}
Vector3& Vector3::normalize()
{
#if USE_YEPPP
    float inf;
    yepCore_SumSquares_V32f_S32f(&x, &inf, 3);
    yepCore_Multiply_IV32fS32f_IV32f(&x, 1.0f / sqrt(inf), 3);
#else
    float inf = 1.0f / sqrt((x * x) + (y * y) + (z * z));
    x *= inf;
    y *= inf;
    z *= inf;
#endif
    return *this;
}
Vector3 Vector3::cross(Vector3& rh)
{
    return Vector3 (
                (y * rh.z) - (z * rh.y),
                (z * rh.x) - (x * rh.z),
                (x * rh.y) - (y * rh.x)
    );
}
float Vector3::dot(Vector3& rh)
{
#if USE_YEPPP
    float ret = 0;
    yepCore_DotProduct_V32fV32f_S32f(&x, &rh.x, &ret, 3);
    return ret;
#else
    return x*rh.x+y*rh.y+z*rh.z;
#endif
}
Vector3 Vector3::operator*(float scalar)
{
#if USE_YEPPP
    Vector3 ret;
    yepCore_Multiply_V32fS32f_V32f(&x, scalar, &ret.x , 3);
    return ret;
#else
    return Vector3(x*scalar, y*scalar,z*scalar);
#endif
}
Vector3 Vector3::operator+(Vector3 rh)
{
#if USE_YEPPP
    Vector3 ret;
    yepCore_Add_V32fV32f_V32f(&x, &rh.x, &ret.x, 3);
    return ret;
#else
    return Vector3(x+rh.x, y+rh.y, z+rh.z);
#endif
}
Vector3 Vector3::operator-(Vector3 rh)
{
#if USE_YEPPP
    Vector3 ret;
    yepCore_Subtract_V32fV32f_V32f(&x, &rh.x, &ret.x, 3);
    return ret;
#else
    return Vector3(x-rh.x, y-rh.y, z-rh.z);
#endif
}
Vector3 operator*(float s, const Vector3& v)
{
#if USE_YEPPP
    Vector3 ret;
    yepCore_Multiply_V32fS32f_V32f(&v.x, s, &ret.x , 3);
    return ret;
#else
    return Vector3(s*v.x,s*v.y,s*v.z);
#endif
}

我正在使用g++编译器。编译器选项:g++-Wall-fexceptions-fPIC-Wl,--无需-std=c++11-pthread-ggdb链接器选项:g++-共享-lpthread-lyeppp-ldl

你知道我做错了什么吗?

Yeppp!针对处理100+个元素的阵列进行了优化。

由于使用SIMD的能力有限以及函数调用、动态调度和参数检查的开销,它在小型阵列(如示例中的长度为3的阵列)上效率不高。