C++函数优化

7
我有一个函数如下,被频繁调用导致程序运行缓慢。是否有优化方法?例如,使用SIMD指令或其他技术。getray()函数用于从预先计算的查找表中检索一个向量-2查询给定的向量-3。它是在Visual-studio-2013中编译的,目标配置是x64机器。
此外,调用此函数的for循环已经使用OpenMP进行了优化。
非常感谢。
bool warpPlanarHomography(
const Eigen::Matrix3d& H_camera2_camera1
, const cv::Mat& image1
, const cv::Mat& image2
, FisheyeCameraUnified& cam1
, FisheyeCameraUnified& cam2
, const Eigen::Vector2i& patchCenter
, const int patchSize
, Eigen::Matrix<unsigned char, 7, 7>& patch1)
{
const int patchSize_2 = 3;
for (int v = 0; v < patchSize; ++v) // row
{
    for (int u = 0; u < patchSize; ++u)
    {
        Eigen::Vector2i p1 = Eigen::Vector2i(u - patchSize_2, v - patchSize_2).cast<int>() + patchCenter;

        if (p1(0, 0) < 0 || p1(1, 0) < 0 || p1(0, 0) >= image1.cols || p1(1, 0) >= image1.rows) return false;

        Eigen::Vector3d ray1;
        cam1.getRay(p1(1, 0), p1(0, 0), ray1);
        Eigen::Vector2d p2;
        if (!cam2.project(H_camera2_camera1 * ray1, p2))
        {
            return false;
        }
        if (p2.x() < 0.0 || p2.x() >= image2.cols - 1 ||
            p2.y() < 0.0 || p2.y() >= image2.rows - 1)
        {
            return false;
        }
        getInterpolatedPixel(image2, p2, &patch1(v, u));
    }
}
return true;
}

其中,项目函数的形式如下:

bool FisheyeCameraUnified::project(const Eigen::Vector3d& ray, Eigen::Vector2d& pt)
{
    double fx, fy, cx, cy, xi;
    fx = m_K(0, 0);
    fy = m_K(1, 1);
    cx = m_K(0, 2);
    cy = m_K(1, 2);
    xi = m_xi;

    double d = ray.norm();
    double rz = 1.0 / (ray(2) + xi * d);

    // Project the scene point to the normalized plane.
    Eigen::Vector2d m_d(ray(0) * rz, ray(1) * rz);

    // Apply the projection matrix.
    pt(0) = fx * m_d(0) + cx;
    pt(1) = fy * m_d(1) + cy;
    return true;
}

并且getInterpolatedPixel()函数如下:
void getInterpolatedPixel(const cv::Mat& image, const Eigen::Vector2d& coords, unsigned char* pixel)
{
    int ix = static_cast<int>(coords.x());
    int iy = static_cast<int>(coords.y());
    double dx = coords.x() - ix;
    double dy = coords.y() - iy;
    double dxdy = dx * dy;

    const double w00 = 1.0 - dx - dy + dxdy;
    const double w01 = dx - dxdy;
    const double w10 = dy - dxdy;
    const double w11 = dxdy;

    const unsigned char* p00 = image.data + iy * image.step.p[0] + ix * image.channels();
    const unsigned char* p01 = p00 + image.channels();
    const unsigned char* p10 = p00 + image.step.p[0];
    const unsigned char* p11 = p10 + image.channels();

    for (int i = 0; i < image.channels(); ++i)
    {
        double value = w11 * p11[i] + w10 * p10[i] + w01 * p01[i] + w00 * p00[i];
        pixel[i] = cv::saturate_cast<unsigned char>(value);
    }
}

1
正如你所说,getInterpolatedPixel中的循环是瓶颈,你尝试过使用OpenMP吗?对于你来说,OpenMP是否可行?对于简单的SIMD指令使用,请尝试VC - Torbjörn
我没有尝试在getInterpolatedPixel函数中使用OpenMP,但我尝试了WarpPlanarHomography。它并没有给我带来任何优势。我猜想原因是for循环很小,效率提升无法弥补OpenMP引起的开销。所以我认为用SIMD优化这个小函数,并用OpenMP优化外部大的for循环可能是一个好主意。谢谢你提供的库信息,我会尝试一下。 - Peidong
我认为你的 project 函数被复制粘贴搞乱了。 - Simon Kraemer
2个回答

3
  1. 先测量瓶颈在哪里,尝试优化该位置
  2. 您可以使用float代替double吗?
  3. m_K(0, 0)m_K(1, 1)是什么,您能用常量替换它们吗?
  4. 如果图像只有特定数量的通道(1、3、4是典型数字),请展开for (int i = 0; i < image.channels(); ++i)循环
  5. 只调用一次image.channels(),然后在后面使用存储的值
  6. 尝试为小函数添加inline修饰符

现在大多数架构中,双精度浮点数比单精度浮点数更快。 - Tom Tanner
谢谢您的回复。我测量了瓶颈,发现是preojct()函数和getinterpolatedpixel函数。至于其他部分,应该是可以的。我会尝试一下。 - Peidong
我在思考的是是否可以将for循环并行化,因为对于一个7x7的图像块,它会以相同的指令但不同的数据被调用49次。 - Peidong
2
@TomTanner 这是可以讨论的 https://dev59.com/zHA75IYBdhLWcg3wMl8Z - Anton Malyshev
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Peidong
显示剩余3条评论

3

除了其他更广泛的答案,还应考虑这一点。

由于getInterpolatedPixel在紧密循环中使用,因此我专注于减少函数调用:

void getInterpolatedPixel(const cv::Mat& image, const Eigen::Vector2d& coords, unsigned char* pixel)
{
    //save two function calls here
    double dx = coords.x();
    double dy = coords.y();
    int ix = static_cast<int>(dx);
    int iy = static_cast<int>(dy);
    dx -= ix;
    dy -= iy;
    //make this const
    const double dxdy = dx * dy;

    const double w00 = 1.0 - dx - dy + dxdy;
    const double w01 = dx - dxdy;
    const double w10 = dy - dxdy;
    const double w11 = dxdy;

    //cache image.channels()
    const int channels = image.channels();

    const unsigned char* p00 = image.data + iy * image.step.p[0] + ix * channels;
    const unsigned char* p01 = p00 + channels;
    const unsigned char* p10 = p00 + image.step.p[0];
    const unsigned char* p11 = p10 + channels;

    for (int i = 0; i < channels; ++i)
    {
        double value = w11 * p11[i] + w10 * p10[i] + w01 * p01[i] + w00 * p00[i];
        pixel[i] = cv::saturate_cast<unsigned char>(value);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接