 # cublasSgemm wrong return value

I would like to know why the cublasSgemm return wrong result.
I would like to multiply a matrix A of size (2,2)
and an over matrix B of size (2,6) initialized like this
A
0.375 0
0 0.625

B

0.0968 0.1613 0.1935 0.0968 0.3226 0.129
0.0625 0.1875 0.1875 0.2083 0.2083 0.1458

The result C is
0.0363 0.1008 0.0726 0.0605 0.121 0.0806
0.0234 0.1172 0.0703 0.1302 0.0781 0.0911

But I know the result should be
0.0363 0.0605 0.0726 0.0363 0.1210 0.0484
0.0391 0.1172 0.1172 0.1302 0.1302 0.0911

the code is like this:
cublasSgemm(‘n’,‘n’,2, 6, 2,1.0f,A,2,B,2,0.0f,C,2);
I would appreciate any help.
Thank you

I would like to know why the cublasSgemm return wrong result.
I would like to multiply a matrix A of size (2,2)
and an over matrix B of size (2,6) initialized like this
A
0.375 0
0 0.625

B

0.0968 0.1613 0.1935 0.0968 0.3226 0.129
0.0625 0.1875 0.1875 0.2083 0.2083 0.1458

The result C is
0.0363 0.1008 0.0726 0.0605 0.121 0.0806
0.0234 0.1172 0.0703 0.1302 0.0781 0.0911

But I know the result should be
0.0363 0.0605 0.0726 0.0363 0.1210 0.0484
0.0391 0.1172 0.1172 0.1302 0.1302 0.0911

the code is like this:
cublasSgemm(‘n’,‘n’,2, 6, 2,1.0f,A,2,B,2,0.0f,C,2);
I would appreciate any help.
Thank you

CUBLAS is column-major, not row-major, I think that you use row-major to store B and use row-major to interpret C.

in row-major, you B is

0.0968 0.1935 0.3226 …

0.1613 0.0968 0.129 …

and C = A*B is

0.0363 0.0726 0.1210 …

0.1008 0.0605 0.0806

which is column-major format.

but you use row-major to interpret column-major result, hence

C = 0.0363 0.1008 0.0726 0.0605 0.1210 0.0806

``.....``

CUBLAS is column-major, not row-major, I think that you use row-major to store B and use row-major to interpret C.

in row-major, you B is

0.0968 0.1935 0.3226 …

0.1613 0.0968 0.129 …

and C = A*B is

0.0363 0.0726 0.1210 …

0.1008 0.0605 0.0806

which is column-major format.

but you use row-major to interpret column-major result, hence

C = 0.0363 0.1008 0.0726 0.0605 0.1210 0.0806

``.....``

I realize that that B is store in row-major.

I would appreciate if you can tell me how to change

this situation to get it column-major.

Thank you

I realize that that B is store in row-major.

I would appreciate if you can tell me how to change

this situation to get it column-major.

Thank you

The simplest thing is probably to tell [font=“Courier New”]cublasSgemm[/font] to transpose its inputs.

The simplest thing is probably to tell [font=“Courier New”]cublasSgemm[/font] to transpose its inputs.

you need to use clumn-major in your host code,

B(i,j) = B[2*j + i] ;

if you use B[i][j], then C-compiler uses row-major to do translation, say B[i*6+j]

you need to use clumn-major in your host code,

B(i,j) = B[2*j + i] ;

if you use B[i][j], then C-compiler uses row-major to do translation, say B[i*6+j]

I think my matrices were colum-major stored.

I did like this

for (int i = 0; i < row; i++) {

for (int j = 0; j < col; j++) {

``````dMatrix[i * col + j] = (float)value;

}
``````

}

I think my matrices were colum-major stored.

I did like this

for (int i = 0; i < row; i++) {

for (int j = 0; j < col; j++) {

``````dMatrix[i * col + j] = (float)value;

}
``````

}

That is row-major ordering not column major ordering.

That is row-major ordering not column major ordering.

It was a misunderstanding of mine.

May I change like this for col-major?

[i]for (int i = 0; i < row; i++) {

for (int j = 0; j < col; j++) {

dMatrix[j * row + i] = (float)value;

}

}[/i]

It was a misunderstanding of mine.

May I change like this for col-major?

[i]for (int i = 0; i < row; i++) {

for (int j = 0; j < col; j++) {

dMatrix[j * row + i] = (float)value;

}

}[/i]