Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.
7227 Discussions

multiply column vector by row vector using dgemm

hagai_sela
Beginner
1,368 Views

Hi,
I am trying to multiply a 4x1 matrix (a column vector) by a 1x3 matrix (row vector) using dgemm. The code is attached, I can't seem to get the parameters right. The attached version outputs "MKL ERROR: Parameter 11 was incorrect on entry to cblas_dgemm". I tried different values and couldn't get it right.
Any clues?

Thanks,
Hagai.

multcolumnrow.cpp

0 Kudos
6 Replies
barragan_villanueva_
Valued Contributor I
1,368 Views

Hagai,

According to the mkl_cblas.h cblas_gemm function has the follwing interface:

void cblas_dgemm(const CBLAS_ORDER Order, const CBLAS_TRANSPOSE TransA,
const CBLAS_TRANSPOSE TransB, const MKL_INT M, const MKL_INT N,
const MKL_INT K, const double alpha, const double *A,
const MKL_INT lda, const double *B, const MKL_INT ldb,
const double beta, double *C, const MKL_INT ldc);

MKL ERROR: Parameter 11 was incorrect on entry to cblas_dgemm

This means, that ldb argument is wrong in your example.

Please use the following arguments:

cblas_dgemm(
CblasRowMajor,
CblasNoTrans,
CblasNoTrans,
HEIGHT,
WIDTH,
1,
1.0,
ColumnVector,
1, // !!!
RowVector,
WIDTH, // !!!
1.0,
Result,
WIDTH); // !!!

So, the result is:

2 2 2
2 2 2
2 2 2
2 2 2


Thanks
--Victor


0 Kudos
hagai_sela
Beginner
1,368 Views
Worked. Thanks!

Hagai.
0 Kudos
barragan_villanueva_
Valued Contributor I
1,368 Views
Hagai,

BTW,you need tochange PrintMatrix function to see results correctly:
(I used i+1)

void PrintMatrix(double* pMatrix, const size_t nRows, const size_t nCols)
{
for (size_t i=0; i {
for (size_t j=0; j {
cout << pMatrix[i * nCols + j] << 't'; // !!!
}
cout << endl;
}
cout << endl;
}

int main()
{
double ColumnVector[HEIGHT];
double RowVector[WIDTH];
double Result[WIDTH * HEIGHT];

for (int i=0; i ColumnVector = i+1; // !!!
PrintMatrix(ColumnVector, HEIGHT, 1);

for (int i=0; i RowVector = i+1; // !!!
PrintMatrix(RowVector, 1, WIDTH);

cblas_dgemm(
CblasRowMajor,
CblasNoTrans,
CblasNoTrans,
HEIGHT,
WIDTH,
1,
1.0,
ColumnVector,
1,
RowVector,
WIDTH,
1.0,
Result,
WIDTH);

PrintMatrix(Result, HEIGHT, WIDTH);
return 0;
}


Pleaselook at MKL examples/cblas/source/cblas_dgemmx.c file for more details.

Thanks
--Victor
0 Kudos
darkfate
Beginner
1,368 Views

Hagai,

According to the mkl_cblas.h cblas_gemm function has the follwing interface:

void cblas_dgemm(const CBLAS_ORDER Order, const CBLAS_TRANSPOSE TransA,
const CBLAS_TRANSPOSE TransB, const MKL_INT M, const MKL_INT N,
const MKL_INT K, const double alpha, const double *A,
const MKL_INT lda, const double *B, const MKL_INT ldb,
const double beta, double *C, const MKL_INT ldc);

MKL ERROR: Parameter 11 was incorrect on entry to cblas_dgemm

This means, that ldb argument is wrong in your example.

Please use the following arguments:

cblas_dgemm(
CblasRowMajor,
CblasNoTrans,
CblasNoTrans,
HEIGHT,
WIDTH,
1,
1.0,
ColumnVector,
1, // !!!
RowVector,
WIDTH, // !!!
1.0,
Result,
WIDTH); // !!!

So, the result is:

2 2 2
2 2 2
2 2 2
2 2 2


Thanks
--Victor



Hello Victor Pasco,

i am trying to do same with the dgemm(..) but it still does not work for me. I am multiplying an A:= 16x4 with B:= 4x1. alpha=1.0, beta=1.0. a,b are stored in rows so the first parameters have to be "T" for transposed right?

dgemm("T","T",&sixteen,&one,&four,α,a,&sixteen,b,&four,β,c,&sixteen);
0 Kudos
barragan_villanueva_
Valued Contributor I
1,368 Views
Quoting - darkfate

Hello Victor Pasco,

i am trying to do same with the dgemm(..) but it still does not work for me. I am multiplying an A:= 16x4 with B:= 4x1. alpha=1.0, beta=1.0. a,b are stored in rows so the first parameters have to be "T" for transposed right?

dgemm("T","T",&sixteen,&one,&four,α,a,&sixteen,b,&four,β,c,&sixteen);

Hagai,

So, you decided to not use CBLAS while you have alreadyworking sources.

And what's error message now?Or it's another problem.
Did you look at corresponding BLAS examples (on FORTRAN) and MKL docs to correctly pass arguments from C/C++?

Thanks
--Victor
0 Kudos
mecej4
Honored Contributor III
1,368 Views
Note that, in general, you will need to initialize the output matrix C or 'Result' (except when \beta is zero) before calling cblas_dgemm.
0 Kudos
Reply