Question about ldb in mkl_?omatcopy for transpose of column-major matrices

Question about ldb in mkl_?omatcopy for transpose of column-major matrices

Dear all,

I need to do out-of-place transpose of the matrix (actually I have plenty of them if various sizes/shapes). I have a question about ldb parameter in mkl_?omatcopy. Documentation says that if a matrix is in column major format and I am doing transpose, then ldb must be at least equal to number of columns in B. Is it indeed the case or it’s just a typo?

My matrices are stored with minimum leading dimension — number of rows for column major format. So that means I am out of luck using this function if number of columns in B is more than number of rows? That is unfortunate as I would expect this function to handle such cases internally in the best possible way.

Denis.

5 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

On the related note, I have issues using this function on macOS for anything but square matrices (it simply does not work). Given that I store everything in column major format and the leading dimension is number of rows, I can not see where I could mis-use this function, see below, compiled with:

clang++ -std=c++11  -m64 -I/opt/intel/mkl/include example.cc -o example -L/opt/intel/mkl/lib -Wl,-rpath,/opt/intel/mkl/lib -lmkl_intel_lp64 -lmkl_sequential -lmkl_core -lpthread -lm -ldl

// compile with:
// clang++ -std=c++11  -m64 -I/opt/intel/mkl/include example.cc -o example -L/opt/intel/mkl/lib -Wl,-rpath,/opt/intel/mkl/lib -lmkl_intel_lp64 -lmkl_sequential -lmkl_core -lpthread -lm -ldl

#include <iostream>
#include <vector>

#include <mkl.h>

void print(const std::vector<double> &A, const unsigned int m, const unsigned int n)
{
  for (unsigned int i = 0; i < m; ++i)
    {
      for (unsigned int j = 0; j < n; ++j)
        std::cout << A[i+j*m] << " ";

      std::cout << std::endl;
    }
}

bool compare(const std::vector<double> & A, const std::vector<double> &B, const unsigned int Am, const unsigned int An)
{
  bool ret = true;
  for (unsigned int i = 0; i < Am; ++i)
    for (unsigned int j = 0; j < An; ++j)
      ret &= (A[i+j*Am] == B[j+i*An]);

  return ret;
}

void test(const unsigned int m, const unsigned int n)
{
  std::cout << "Test " << m << "x" << n << std::endl;
  const auto &Am = n;
  const auto &An = m;
  const auto &Bm = m;
  const auto &Bn = n;
  std::vector<double> A(Am*An,0.);
  std::vector<double> B(Bm*Bn,0.);

  for (unsigned int i = 0; i < Am*An; ++i)
    A[i] = 1+i;

  mkl_domatcopy('C', 'T', Bm, Bn, 1., A.data(), Am, B.data(), Bm);

  std::cout << "Input:" << std::endl;
  print(A,Am,An);
  std::cout << "Output:" << std::endl;
  print(B,Bm,Bn);
  std::cout << "Correct: " << compare(A,B,Am,An) << std::endl<< std::endl;
}

int main(int argc, char **argv)
{
  test(11,27);
  test(15,4);
  test(10,10);
}

 

We would  recommend you take a look at the domatcopy.c example (mklroot\examples\transc\source\ dir)

Hi Gennady, 

Thanks for the prompt reply. I quickly translated your example to C++ and noticed the following. The mkl_domatcopy is being called with rows=2 and cols=4 (3rd and 4th argument). However, the matrix B (which is clear both from in-code comments and its dst_stride=2) is 4x2. That is, it has 4 rows and 2 columns and is stored in row-major format  (stride=2).

However the MKL documentation states:

rows    The number of rows in matrix B (the destination matrix).

cols       The number of columns in matrix B (the destination matrix).

To me this indicates that MKL's documentation is confusing and not consistent with what is actually meant by rows/cols. Probably, what is actually meant is the number of rows/cols which define a submatrix of A, on which the operation will be applied.

As another proof, If I swap rows/cols in my code above, everything works for all cases!

I would recommend you to reword the documentation to avoid confusion for other users.

Regards, Denis.

I have the same question.

I think the manual is wrong, the rows is the row number of destination matrix B without operation.

But I am not sure.

 

Quote:

davydden wrote:

Hi Gennady, 

Thanks for the prompt reply. I quickly translated your example to C++ and noticed the following. The mkl_domatcopy is being called with rows=2 and cols=4 (3rd and 4th argument). However, the matrix B (which is clear both from in-code comments and its dst_stride=2) is 4x2. That is, it has 4 rows and 2 columns and is stored in row-major format  (stride=2).

However the MKL documentation states:

rows    The number of rows in matrix B (the destination matrix).

cols       The number of columns in matrix B (the destination matrix).

To me this indicates that MKL's documentation is confusing and not consistent with what is actually meant by rows/cols. Probably, what is actually meant is the number of rows/cols which define a submatrix of A, on which the operation will be applied.

As another proof, If I swap rows/cols in my code above, everything works for all cases!

I would recommend you to reword the documentation to avoid confusion for other users.

Regards, Denis.

Leave a Comment

Please sign in to add a comment. Not a member? Join today