What changes should be made while executing cblas_cgemm();

395 Views Asked by At

I tried to do a matrix to matrix multiplication using the function cblas_cgemm(); But the answer I am getting is incorrect compared to manual calculation. I tried to simplify my code without using imaginary terms in input, but the problem remains. What change should I make to get the correct output. This is my code.

#include<stdio.h>
#include<math.h>
#include<complex.h>
#include "cblas.h"

void main()
{
 int i,j;
 double complex A[2][2]={1,2,
                         3,4};
 double complex B[2][2]={4,5,
                         6,7};
 double complex W[2][2]={0,0,
                         0,0};

 const int m1=2;
 const int n1=2;
 const int k1=2;

 const int lda1=2;
 const int ldb1=2;
 const int ldc1=2;
 const double alpha=1.0;
 const double beta=0.0;

 cblas_cgemm(CblasRowMajor,CblasNoTrans,CblasNoTrans,m1,n1,k1,&alpha,A,lda1,B, ldb1 ,&beta,W, ldc1);

 for(i=0;i<m1;++i)
  {
  for(j=0;j<n1;++j)
    printf("%lf %lf\n" ,creal(W[i][j]),cimag(W[i][j]));
  printf("\n");
   }
 }

I got output as

-119296.000000 0.000000
-188416.000000 0.000000 0.000000 0.000000
0.000000 0.000000
I referred to this sitelapack:cblas_cgemm Please help My code using cblas_dgemm() is given below

//Y := alpha*A*X + beta*Y, or   y := alpha*A**T*x + beta*y,
#include<stdio.h>
#include "cblas.h"
const double A[3][1]={
                      1,
                      2,
                      3
                       };
const double X[1][4]={
1,2,3,4,
};
double Y[3][4]={
0,0,0,0,
0,0,0,0,
0,0,0,0
};
int main()
{
 const int m=3;
const int k=1;const int n=4;
const int lda=1;
const int ldb=4;
const int ldc=4;
int incX,incY;
const double alpha=1.0;
const double beta=0.0;
incX=1;incY=1;
int i,j;
for(i=0;i<m;++i)
   {for(j=0;j<k;++j)
    printf("%lf \t" ,A[i][j]);
putchar('\n');
}
cblas_dgemm(CblasRowMajor,CblasNoTrans,CblasNoTrans,m,n,k,alpha,A, lda,X, ldb ,beta,Y, ldc);
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
printf("%lf\t" ,Y[i][j]);
printf("\n");
}
return 0;
}

I got output as

hp@hp-HP-Notebook:~/beamforming/programs/studentprojectdetails$ ./dgemm_trial 1.000000
2.000000
3.000000
1.000000 2.000000 3.000000 4.000000
2.000000 4.000000 6.000000 8.000000
3.000000 6.000000 9.000000 12.000000

2

There are 2 best solutions below

1
On

First issue: Your complex numbers should be declared and used according to the specific complex number layout indicated in cblas.h. Your code indicates you're expecting a 2x2 matrix, and a 2x2 matrix of complex values must be specified with eight total values (four real and four imaginary).

*
 * A note on complex data layouts:
 *
 * In order to allow straightforward interoperation with other libraries and
 * complex types in C and C++, complex data in BLAS is passed through an opaque
 * pointer (void *).  The layout requirements on this complex data are that
 * the real and imaginary parts are stored consecutively in memory, and have
 * the alignment of the corresponding real type (float or double).  The BLAS
 * complex interfaces are compatible with the following types:
 *
 *     - The C complex types, defined in <complex.h>.
 *     - The C++ std::complex types, defined in <complex>.
 *     - The LAPACK complex types, defined in <Accelerate/vecLib/clapack.h>.
 *     - The vDSP types DSPComplex and DSPDoubleComplex, defined in <Accelerate/vecLib/vDSP.h>.
 *     - An array of size two of the corresponding real type.
 *     - A structure containing two elements, each of the corresponding real type.
 * 

Second issue: BLAS routines are not designed to work with two dimensional arrays. Instead you should declare a long one dimensional array. This is the purpose of the LDA parameters. Passing a two dimensional array correctly relies on the assumption that the compiler is going to lay out your two dimensional array in a certain order, which may or may not be true, and results in undefined behavior.

0
On

See the naming conventions of BLAS and Lapack. Since the type of matrices is double complex, cblas_zgemm() should be used instead of cblas_cgemm(). Indeed, z is for double precision complex and c for single precision complex.

In addition, the scalars alpha and beta must also be of type double complex. See the source of the fortran routine zgemm() to check such things: COMPLEX*16 corresponds to double complex.