【发布时间】:2015-05-16 01:29:46
【问题描述】:
我正在尝试将列向量与行向量相乘。我可以使用 dgemm 吗?
换句话说 D = A * B 其中 D 是矩阵,A 是列向量,B 是行向量。
我按照https://software.intel.com/en-us/node/520775 此处的文档进行操作。我似乎无法为 cblas_dgemm 获得正确的参数
这是我的尝试。在我的情况下,m = nRows,n = nCols,k = 1
问题似乎是lda、ldb和ldc。我已将它们分别定义为 nCols、k、nRows。
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <mkl.h>
#define nCols 5
#define nRows 20
#define k 1
void PrintMatrix(double* pMatrix, const size_t nR, const size_t nC, const CBLAS_ORDER Order) {
unsigned int i, j;
if (Order == CblasRowMajor)
{
for (i = 0; i < nR; i++)
{
for (j = 0; j < nC; j++)
{
printf("%f \t ", pMatrix[i * nC + j]); // !!!
}
printf("\n"); // !!!
}
}
else
{
for (i = 0; i < nR; i++) {
for (j = 0; j < nC; j++) {
printf("%f \t ", pMatrix[i + j* nR ]); // !!!
}
printf("\n"); // !!!
}
}
printf("\n"); // !!!
}
int main(void) {
double A[] = { 8, 4, 7, 3, 5, 1, 1, 3, 2, 1, 2, 3, 2, 0, 1, 1 , 2, 3, 4, 1};
double B[] = { -1, 2, -1, 1, 2 };
double alpha = 1.0, beta = 0.0;
int i, lda, ldb, ldc;
double *C, *D;
D = (double*) malloc(nRows * nCols * sizeof(double));
C = (double*) malloc(nRows * nCols * sizeof(double));
for (i = 0; i < nRows*nCols; i++)
D[i] = 0.0;
for (i = 0; i < nRows*nCols; i++)
C[i] = 0.0;
lda = nCols;
ldb = k;
ldc = nRows;
cblas_dger(CblasRowMajor, nRows, nCols, alpha, A, 1, B, 1, C, nCols);
PrintMatrix(C, nRows, nCols,CblasRowMajor);
cblas_dgemm (CblasRowMajor, CblasNoTrans, CblasNoTrans, nRows, nCols, k, alpha, A, lda, B, ldb, beta, D, ldc);
PrintMatrix(D, nRows, nCols, CblasRowMajor);
free(D);
free(C);
return 0;
}
【问题讨论】:
标签: c matrix vector blas intel-mkl