2012-01-15 68 views
0

我试图重写一些代码以利用SSE。然而,由于某些原因,我的SSE版本产生了与原始版本不同的结果,例如, 209.1而不是1.47等。与SSE版本不同的结果

为什么?整个功能可以找到here

struct vec_ps 
{ 
    __m128 value; 

    vec_ps(){} 
    vec_ps(float value)   : value(_mm_set1_ps(value)) {} 
    vec_ps(__m128 value)  : value(value)    {} 
    vec_ps(const vec_ps& other) : value(other.value)  {} 

    vec_ps& operator=(const vec_ps& other) 
    { 
     value = other.value; 
     return *this; 
    } 

    vec_ps& operator+=(const vec_ps& other) 
    { 
     value = _mm_add_ps(value, other.value); 
     return *this; 
    } 

    vec_ps& operator-=(const vec_ps& other) 
    { 
     value = _mm_sub_ps(value, other.value); 
     return *this; 
    } 

    vec_ps& operator*=(const vec_ps& other) 
    { 
     value = _mm_mul_ps(value, other.value); 
     return *this; 
    } 

    vec_ps& operator/=(const vec_ps& other) 
    { 
     value = _mm_div_ps(value, other.value); 
     return *this; 
    } 

    static vec_ps load(float* ptr) 
    { 
     return vec_ps(_mm_load_ps(ptr)); 
    } 

    static void stream(float* ptr, const vec_ps& other) 
    { 
     _mm_stream_ps(ptr, other.value); 
    } 

    void stream(float* ptr) 
    { 
     _mm_stream_ps(ptr, value); 
    } 
}; 

vec_ps operator+(const vec_ps& lhs, const vec_ps& rhs) 
{  
    return vec_ps(lhs) += rhs; 
} 

vec_ps operator-(const vec_ps& lhs, const vec_ps& rhs) 
{  
    return vec_ps(lhs) -= rhs; 
} 

vec_ps operator*(const vec_ps& lhs, const vec_ps& rhs) 
{  
    return vec_ps(lhs) *= rhs; 
} 

vec_ps operator/(const vec_ps& lhs, const vec_ps& rhs) 
{  
    return vec_ps(lhs) /= rhs; 
} 

void foo(/*...*/) 
{ 
     std::vector<float, tbb::cache_aligned_allocator<float>> ref_mu(w*h); 
     std::vector<float, tbb::cache_aligned_allocator<float>> cmp_mu(w*h); 
     std::vector<float, tbb::cache_aligned_allocator<float>> ref_sigma_sqd(w*h); 
     std::vector<float, tbb::cache_aligned_allocator<float>> cmp_sigma_sqd(w*h); 
     std::vector<float, tbb::cache_aligned_allocator<float>> sigma_both(w*h); 
     int size = w*h*sizeof(float); 

     /*...*/ 

     float ssim_sum = 0.0; 
     float ssim_sum2 = 0.0; 

     vec_ps ssim_sum_ps(0.0f);  

     for(int n = 0; n < size/16; ++n) 
     { 
      auto ref_mu_ps   = vec_ps::load(ref_mu.data()  + n*4); 
      auto cmp_mu_ps   = vec_ps::load(cmp_mu.data()  + n*4); 
      auto sigma_both_ps  = vec_ps::load(sigma_both.data() + n*4); 
      auto ref_sigma_sqd_ps = vec_ps::load(ref_sigma_sqd.data() + n*4); 
      auto cmp_sigma_sqd_ps = vec_ps::load(cmp_sigma_sqd.data() + n*4); 

      auto numerator = (2.0f * ref_mu_ps * cmp_mu_ps + C1) * (2.0f * sigma_both_ps + C2); 
      auto denominator = (ref_mu_ps*ref_mu_ps + cmp_mu_ps*cmp_mu_ps + C1) * (ref_sigma_sqd_ps + cmp_sigma_sqd_ps + C2); 
      ssim_sum_ps += numerator/denominator; 
     } 

     for(int n = 0; n < 4; ++n) 
      ssim_sum2 += ssim_sum_ps.value.m128_f32[n]; 

     for (int y = 0; y < h; ++y) 
     { 
      int offset = y*w; 
      for (int x = 0; x < w; ++x, ++offset) 
      {   
       float numerator = (2.0f * ref_mu[offset] * cmp_mu[offset] + C1) * (2.0f * sigma_both[offset] + C2); 
       float denominator = (ref_mu[offset]*ref_mu[offset] + cmp_mu[offset]*cmp_mu[offset] + C1) * (ref_sigma_sqd[offset] + cmp_sigma_sqd[offset] + C2); 
       ssim_sum += numerator/denominator;     
      } 
     } 
     assert(ssim_sum2 == ssim_sum); // FAILS! 
} 
+1

你可以也应该自己调试。在调试器中运行它,或添加printf调用以输出中间结果。当你隔离不按预期工作的步骤时,请随时编写一个最小的测试用例并在此处询问。但是“这里是一堆代码,弄清楚什么是错误的”不是一个好问题。 – 2012-01-15 02:04:16

+0

@ BenVoigt; Ofc,你是对的。不过,我已经习惯了在发布之前所建议的内容,而无法弄清楚。 – ronag 2012-01-15 02:12:47

+0

那么,哪一行代码会产生“错误”的结果呢?你可以删除TBB分配器等,并简化一些事情吗? – 2012-01-15 02:14:46

回答

1

只是上面的评论,因为它似乎是问题的答案:是否有任何保证w * h可以被四整除?如果情况并非如此,那么您在SSE版本中的最后一次迭代将基于随机数字。在另一个地方使用sizeof(float),而在另一个地方使用16而不是4 * sizeof(float)`有点令人困惑:为什么不放弃float的大小呢?另外,为什么非SSE版本不是在区域上运行,而是试图跟随矩阵的宽度和高度?

相关问题