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

cblas usage question

utab
Beginner
572 Views
Dear all,

I would like to multiply

A = [ 1 2 6
2 3 1
6 1 4 ]
a symmetrix matrix with a dense matrix
B =[ 2 3
4 5
6 7 ];

and here is the code, but the results are wrong, what could be the problem?

int main()
{
double a[] = {1,2,6,3,1,4};
double b[] = {2,3,4,5,6,7};
double c[] = {0,0,0,0,0,0};
MKL_INT r = 3;
MKL_INT cc= 3;
MKL_INT k = 2;
double alpha = 1.0;
double beta = 0.0;
//
CBLAS_ORDER ord = CblasRowMajor;
CBLAS_SIDE side = CblasLeft;
CBLAS_UPLO uplo = CblasUpper;
cblas_dsymm( ord, side, uplo,
r, cc, alpha,
a, r, b, r,
beta, c, cc );
//
for(int z=0;z<6;z++)
std::cout << c << std::endl;
return 0;
}

Best regards,
Umut
0 Kudos
5 Replies
Chao_Y_Intel
Moderator
572 Views

Umut,

could you have a check on the code bellow? The parameters do not look correct. The output for the following code is: 46 55 22 28 40 51

Thanks,
Chao

int main()
{

//double a[] = {1,2,6,3,1,4};
double a[] = {1,2,6,
2,3,1,
6,1,4};

double b[] = {2,3,4,5,6,7};
double c[] = {0,0,0,0,0,0};

MKL_INT r = 3;
//MKL_INT cc= 3;
MKL_INT cc= 2;
//MKL_INT k = 2;

double alpha = 1.0;
double beta = 0.0;

//

CBLAS_ORDER ord = CblasRowMajor;
CBLAS_SIDE side = CblasLeft;
CBLAS_UPLO uplo = CblasUpper;

/*cblas_dsymm( ord, side, uplo,
r, cc, alpha,
a, r, b, r,beta, c, cc );*/

cblas_dsymm( ord, side, uplo,
r, cc, alpha,
a, r, b, cc,beta, c, cc );

//

for(int z=0;z<6;z++)
std::cout << c << std::endl;

return 0;

}

0 Kudos
utab
Beginner
572 Views
Dear Chao,

Thanks for the reply. The documentation is a bit confusing how the matrix is input was not clear to me. I mean understood to only input the upper triangular part of the A and then I was thinking it is ok, however it gets the whole matrix and internally does the operations I guess,
Thanks.

Umut
0 Kudos
mecej4
Honored Contributor III
572 Views
The matrix arguments must be rectangular arrays; there is no provision for passing triangular arrays, as you can see from the documentation for ?symm.

However, it is sufficient to fill in, i.e., define, only the upper triangle(s) of the input matrix(matrices) when you specify 'U' for uplo. To see this, replace

double a[] = {1,2,6,2,3,1,6,1,4};

by

double a[] = {1,2,6,0,3,1,0,0,4};

and you should see no change in the output. In fact, replacing the '0' values in this line by any other values should not affect the output, either.
0 Kudos
Chao_Y_Intel
Moderator
572 Views

Hello,

yes, it is right that the input A needs to rectangular arrays. but the low triangle in the matrix does not impact the computation.

thanks,
Chao

0 Kudos
Murat_G_Intel
Employee
572 Views
Hello Umut,

As meje and Chao mentioned, we need to allocate memory large enough to store a 3x3 matrix for symmetric A matrix. However, you only need to set lower or upper triangular elements of Adepending on the uplo symm parameter.

In your example code the input paramaters to cblas_dsymm are in error. For symm, m and n are number of rows and number of columns for C matrix respectively. Also, the leading dimensions for A and B matrices should be set considering that we are using the row-major ordering. Please find the code with the required modifications below. Comments describe the paramaters.

[cpp]#include 
#include 
using namespace std;

int main()
{
    /* 3x3 A matrix (symmetric) */
    double a[] = {1,2,6,2,3,1,6,1,4};
    /* 3x2 B Matrix */
    double b[] = {2,3,4,5,6,7};
    /* 3x2 C Matrix */
    double c[] = {0,0,0,0,0,0};

    /* Let's define the SYMM paramaters in terms
     * of row count and coloumn count of C */
    MKL_INT rows_c = 3;
    MKL_INT cols_c= 2;

    double alpha = 1.0;
    double beta  = 0.0;

    CBLAS_ORDER ord  = CblasRowMajor;
    CBLAS_SIDE  side = CblasLeft;
    CBLAS_UPLO  uplo = CblasUpper;

    /* The leading dimension for A, B and C are set considering
     * row-major format.
     *
     * For this particular example:
     * lda = # of columns A (or rows since A is symmetric)
     * ldb = # of columns B
     * ldc = # of columns C */
    cblas_dsymm( ord, side, uplo,
            rows_c, cols_c, alpha,
            a, rows_c, b, cols_c,
            beta, c, cols_c );

    /* This should print 46 55 22 28 40 51 */
    for(int z=0;z<6;z++)
        std::cout << c << std::endl;

    return 0;
}
[/cpp]


The output is:

46 55 22 28 40 51

Best wishes,
Efe
0 Kudos
Reply