c++ - BLAS中的元素 vector vector 乘法?

标签 c++ blas

有没有办法使用 BLAS、GSL 或任何其他高性能库进行逐元素 vector 乘法?

最佳答案

(从字面上理解问题的标题......)

是的,它可以单独使用 BLAS 完成(尽管它可能不是最有效的方式。)

诀窍是将输入 vector 之一视为对角矩阵:

⎡a    ⎤ ⎡x⎤    ⎡ax⎤
⎢  b  ⎥ ⎢y⎥ =  ⎢by⎥
⎣    c⎦ ⎣z⎦    ⎣cz⎦

然后您可以使用其中一个矩阵 vector 乘法函数,该函数可以将对角矩阵作为输入而无需填充,例如SBMV

例子:

void ebeMultiply(const int n, const double *a, const double *x, double *y)
{
    extern void dsbmv_(const char *uplo,
                       const int *n,
                       const int *k,
                       const double *alpha,
                       const double *a,
                       const int *lda,
                       const double *x,
                       const int *incx,
                       const double *beta,
                       double *y,
                       const int *incy);

    static const int k = 0; // Just the diagonal; 0 super-diagonal bands
    static const double alpha = 1.0;
    static const int lda = 1;
    static const int incx = 1;
    static const double beta = 0.0;
    static const int incy = 1;

    dsbmv_("L", &n, &k, &alpha, a, &lda, x, &incx, &beta, y, &incy);
}

// Test
#define N 3
static const double a[N] = {1,3,5};
static const double b[N] = {1,10,100};
static double c[N];

int main(int argc, char **argv)
{
    ebeMultiply(N, a, b, c);
    printf("Result: [%f %f %f]\n", c[0], c[1], c[2]);
    return 0;
}

结果:[1.000000 30.000000 500.000000]

关于c++ - BLAS中的元素 vector vector 乘法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7621520/

相关文章:

java - PCA 失败,ND4J : BLAS not found?

c++ - 有没有一种简单的方法来使用基类的变量?

C++ 线程合并排序速度较慢

matlab - 如何使用MATLAB提供的BLAS库?

c - 如何使用英特尔 MKL (cblas) 中的 "double"函数和 "single"函数

c - 使用 cblas 运行程序

fortran - gfortran 与 MKL 的链接导致 'Intel MKL ERROR: Parameter 10 was incorrect on entry to DGEMM'

c++ - windows EnumProcesses 某些进程名称为 <unknown>

c++ - 将临时对象传递给函数

c++ - 无法成功返回我通过在字符串上调用 c_str() 创建的 const char*。 (C++)