2

これをできるだけ簡潔にします。

非常に特殊なハードウェアの制約により、Windows に移植する必要があるプロジェクトがあります。Apple DSP ライブラリである Accelerate を使用してベクトル距離計算を実行する小さなユーティリティ クラスがあります。上記のライブラリがなくても機能するようにこれを書き直す必要がありますが、適切な代替品を見つけることができませんでした。私の最善の行動方針は何ですか?

#include <Accelerate/Accelerate.h>

inline float distBetween(float *x, float *y, unsigned int count) {
    float *tmp = (float*)malloc(count * sizeof(float));
    //  float tmp[count];
    //t = y - x
    vDSP_vsub(x, 1, y, 1, tmp, 1, count);
    //t.squared
    vDSP_vsq(tmp, 1, tmp, 1, count);
    //t.sum
    float sum;
    vDSP_sve(tmp, 1, &sum, count);
    delete tmp;
    return sqrt(sum);
}

inline float cosineDistance(float *x, float *y, unsigned int count) {
    float dotProd, magX, magY;
    float *tmp = (float*)malloc(count * sizeof(float));

    vDSP_dotpr(x, 1, y, 1, &dotProd, count);

    vDSP_vsq(x, 1, tmp, 1, count);
    vDSP_sve(tmp, 1, &magX, count);
    magX = sqrt(magX);

    vDSP_vsq(y, 1, tmp, 1, count);
    vDSP_sve(tmp, 1, &magY, count);
    magY = sqrt(magY);

    delete tmp;

    return 1.0 - (dotProd / (magX * magY));
}
4

2 に答える 2

4

ベクトル関数は通常、特定のアセンブリ言語命令によって実装されます。この実装は非常に遅いです。おそらく、SSE 命令を使用するライブラリが必要になるでしょう。

コードでは、すべての引数 stride_x、stride_y、stride_res が 1 に等しいため、関数の引数からそれらを削除することをお勧めします。コードはより高速である必要があります。

//t = y - x    
float
vDSP_vsub(float *x, int stride_x, float *y, int stride_y, float *res, int stride_res, int count)
{
    while(count > 0) 
    {
        // may be *x - *y ?
        *res = *y - *x;
        res += stride_res;
        x += stride_x;
        y += stride_y;
        count--;
    }    
}

//t.squared
float
vDSP_vsq(float *x, int stride_x, float *res, int stride_res, int count)
{
    while(count > 0) 
    {
        *res += (*x) * (*x);
        x += stride_x;
        res += stride_res;
        count--;
    }    
}

//t.sum
float
vDSP_sve(float *x, int stride_x, float *res, int count)
{
    *res = 0.0;
    while(count > 0) 
    {
        *res += *x;
        x += stride_x;
        count--;
    }    
}

float
vDSP_dotpr(float *x, int stride_x, float *y, int stride_y, float *res, int count)
{
    *res = 0.0;
    while(count > 0) 
    {
        *res += (*x) * (*y);
        x += stride_x;
        y += stride_y;
        count--;
    }    
}
于 2012-02-17T04:47:21.223 に答える
2

Intel の IPP ライブラリをご覧ください。

于 2012-02-17T05:45:32.653 に答える