本征矩阵乘法比原始SSE慢

时间:2018-06-27 06:23:42

标签: c++ performance-testing eigen sse

我使用特征值来计算两个矩阵的乘法: 左矩阵查询:1 * 128 右矩阵文档:128 * 10000 并希望获得矩阵大小为1 * 10000的结果, 这是性能测试的演示代码

#include <iostream>
#include <Eigen/Eigen.h>

#include "mathlib/cos.h"
#include "eigen_timer.h"

using namespace Eigen;
using namespace std;

int main(int argc, char** argv) {
    const uint32_t dim = 128;
    const uint32_t doc_num = 100000;
    cout << ">>>> Params:\n     vec_dim[" << dim << \
            "] vec_num[" << doc_num << "] >>>> Result:\n";

    EigenTimer timer;

    // generate matrix data
    MatrixXf query = MatrixXf::Random(1, dim);
    MatrixXf doc = MatrixXf::Random(dim, doc_num);

    timer.restart();
    // Eigen calc
    const MatrixXf& res = query * doc; // multiplication
    long time = timer.elapsed<std::chrono::nanoseconds>();
    cout << "     Eigen Time: " << time << "ns\n";

    const float* query_s = query.data();
    float* doc_s[doc_num];
    for (int i = 0; i < doc_num; i++) {
        doc_s[i] = doc.col(i).data();
    }
    // sse calc
    timer.restart();
    for (int i = 0; i < doc_num; i++) {
        float a = mathlib::inner_product_sse_full(query_s, doc_s[i], dim);
    }
    time = timer.elapsed<std::chrono::nanoseconds>();
    cout << "       SSE Time: " << time << "ns\n";
    return 0;
}

下面是编译标志:

#Preprocessor flags.
CPPFLAGS(r'-D_GNU_SOURCE -D__STDC_LIMIT_MACROS -DNDEBUG -DEIGEN_DONT_PARALLELIZE -DEIGEN_DONT_VECTORIZE')
CPPFLAGS(r'-DVERSION=\"%s\"' % REPO_REVISION())

#C flags.
CFLAGS('-g -pipe -W -Wall -fPIC -msse3 -ldl -lutil -lssl -lcrypto -lpthread -finline-functions -mavx')

#C++ flags.
CXXFLAGS('-g -pipe -fPIC -O2 -std=gnu++11 -lpthread -lssl -lcrypto -ldl')

运行结果是:

>>>> Params:
 vec_dim[128] vec_num[100000] >>>> Result:
 Eigen Time: 21126516ns
   SSE Time: 8024619ns

Eigen使用内部SSE,但是为什么我的演示中Eigen的速度是SSE的两倍?

===== 要么: 我只想实现(vector * matrix)的矢量结果:

  • query_vec,大小为[1 * 128]
  • 1w ad_vec明智的总和大小[128 * 1w]

特征值有什么有效的实现方法?

0 个答案:

没有答案
相关问题