簡體   English   中英

xtensor 和 xsimd:提高縮減性能

[英]xtensor and xsimd: improve performance on reduction

我正在嘗試使用xtensor在歸約操作(例如元素總和)上獲得與 NumPy 相同的性能。

我為並行計算啟用了xsimd ,但它沒有效果。

以下是基准代碼:

#include <iostream>

#include "xtensor/xreducer.hpp"
#include "xtensor/xrandom.hpp"
#include <ctime>

using namespace std;


pair<double, double> timeit(int size, int n=30){
    double total_clocks = 0;
    double total_sum = 0;
    for (int i=0;i<n;i++){
        xt::xtensor<double, 1> a = xt::random::rand({size}, 0., 1.);
        int start = clock();

        double s = xt::sum(a, xt::evaluation_strategy::immediate)();

        int end = clock();
        total_sum += s; total_clocks += end-start;
    }
    return pair<double, double>(total_clocks/CLOCKS_PER_SEC/n, total_sum); 
}

int main(int argc, char *argv[])
{
    for (int i=5;i<8;i++){
        int size = pow(10, i);
        pair<double, double> ret = timeit(size);
        cout<<"size: "<<size<< " \t " <<ret.first<<" sec\t"<<ret.second<<endl;

    }
    return 0;
}

並在啟用和不啟用xsimd並啟用所有優化(-O3)的情況下編譯它:

$ g++ -DXTENSOR_USE_XSIMD -O3  -march=native -I/home/--user--/install_path/include "./18. test speed 2.cpp" -o a && ./a
size: 100000     0.0001456 sec     1.49984e+06
size: 1000000    0.0013149 sec     1.50002e+07
size: 10000000   0.0125417 sec     1.49995e+08
$ g++ -O3  -march=native -I/home/--user--/install_path/include "./18. test speed 2.cpp" -o a && ./a 
size: 100000     0.0001433 sec     1.49984e+06
size: 1000000    0.0012621 sec     1.50002e+07
size: 10000000   0.0124868 sec     1.49995e+08

順便說一下,同樣的操作使用 numpy:

$ python bench.py
size: 100000     0.000030 sec
size: 1000000    0.000430 sec
size: 10000000   0.005144 sec

大約快 4 倍!

設置

  • Ubuntu 18.04
  • 酷睿 i7 CPU
  • 最新版本的軟件包

如何提高 xtensor 性能? 提前致謝))

根據我打開的這個github問題
-mavx2-ffast-math標志應該被啟用!

$ g++ -mavx2 -ffast-math -DXTENSOR_USE_XSIMD -O3 -I/home/--user--/install_path/include ./bench.cpp -o a && ./a
size: 100000        3.489e-05 sec   4.99932e+06
size: 1000000       0.00050792 sec  4.99989e+07
size: 10000000      0.00544542 sec  4.99997e+08

感謝鄧邦傑

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM