阿罗哈!我在C
工作,我在使用for
循环的数组的所有元素上使用基本函数,我想知道是否可以加速这个计算(例如使用cblas
函数)。我正在使用以下库(并且宁愿不添加任何,因为这个项目不完全在我的控制之下):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <gsl/gsl_statistics_double.h>
#include <gsl/gsl_randist.h>
#include <gsl/gsl_matrix.h>
#include <cblas.h>
#include <float.h>
有问题的代码是这样的:
#use the following functions on every element of the array and add them together
for(ii = 0; ii < k1; ii++) {
val += t[ii]*x[ii] - log(1 + exp(x[ii]));
}
其中x
是长度为k1
的双重数组,我用cblas_dgemv
(它进行矩阵向量乘法)计算,而t
是一个相同长度的双重数组,只包含0
和1
(非稀疏)。现在我在这些条目上使用基本功能并将它们添加到一起。
问题:
cblas_dgemv
的这些非线性函数(exp
和log
)加速这个过程?cblas
函数,我可以更快地进行此计算吗?您的计算更像是BLAS 1级操作。第一个短语中ddot
和x
的t
以及日志部分的减少。
如果您想使用BLAS,答案是肯定的。然而,它本质上不如BLAS L3有效。通过它们的一些并行实现,它可以节省您一些时间和精力。