MKL或BLAS例程将矢量乘以标量不合适的位置

Edi*_*enz 7 c c++ performance blas intel-mkl

我在模拟软件中工作,在阵列上完成的许多操作之一是按数字缩放矢量.

我有这样的代码:

//Just some initialization code, don't bother about this part
int n = 10000;
std::vector<double> input(n, 42.0);
std::vector<double> output(input.size());

double alpha = 69.0;

//the actual calculation:
for (size_t i = 0; i < n; ++i) {
    output[i] = input[i] * alpha;
}
Run Code Online (Sandbox Code Playgroud)

我有MKL库,所以如果我的计算是"就地"完成的,可以写下面的内容:

cblas_dscal(n, alpha, &input[0], 1);
Run Code Online (Sandbox Code Playgroud)

但是,这会改变input变量,这不是我想要的.

我试过使用mkl_domatcopy()但是这个操作非常慢.

Edi*_*enz 1

我想出的解决方案就是打电话给cblas_dcopy()当时的人cblas_dscal()

它不是最好的,但它仍然比原始循环更快。

  • 您是否尝试过使用 0 数组进行 cblas_daxpy ? (3认同)