1

I am just starting on CUBLAS/CUDA programming. I mainly use this of matrix and vector operations. I am quite confusing on the orientation of the vector used in CUBLAS. It seems that there is no difference between row and column vector. So if I using level-2 function to multiply a matrix with a vector, how can I specify the orientation of the vector? Will it be always treated as column vector? If I want to multiply a column vector (nx1) to a row vector (1xm) to make a matrix (nxm), should I always treat them as matrix and use level 3 function for multiplication?

Also, I am using thrust to generate the vector, so if I pass the thrust vector (n elements) to cublasCgemm to form a 1xn or nx1 matrix (i.e. row or column vector). Will that vector be treated as 1xn or nx1 vector if I set the cublasOperation_t as CUBLAS_OP_N?

Thanks.

user1285419
  • 2,183
  • 7
  • 48
  • 70

1 Answers1

1

All data are stored in single pointers i.e. double*. They are stored in memory sequentially. There is no difference between row and column vectors. Single pointers are used for 2D arrays, too. CUBLAS gives you an easy define to locate elements in the matrix

#define IDX2F(i,j,ld) ((((j)-1)*(ld))+((i)-1))

where i is row, j is column and ld is leading dimension of the matrix. ld is used when you want to use a submatrix of a full matrix in the operation.

The multiplication (nx1)(1xm)=(nxm) is performed by cublasDger function.

cublasStatus_t cublasDger(cublasHandle_t handle, int m, int n, const double *alpha, const double *x, int incx, const double *y, int incy, double *A, int lda) If for example y is part of an (kxm) matrix then use incy=k.

ztik
  • 3,482
  • 16
  • 34
  • It helps. I think cublasDger is what I needed. But I am very confusing on the storage format. I am reading some introduction on using CUBLAS. It said C has different storage format as fortran. So if to multiply C(mxk) = A(mxn) x B(nxk) which store in C format, so what's the leading dimensions I should put into the cublasDgemm? – user1285419 Aug 22 '13 at 15:47
  • You might be interested in my answer [here](http://stackoverflow.com/questions/18252526/matrix-multiplication-giving-wrong-output/18292753#18292753) which provides a completely worked example of using cublas gemm with C-style 2D arrays. – Robert Crovella Aug 22 '13 at 19:30
  • The _CUDA Toolkit Documentation_ in [CUBLAS/Data layout](http://docs.nvidia.com/cuda/cublas/index.html#data-layout) states that CUBLAS library uses column-major storage for maximum compatibility with existing Fortran environments. Also it is suggested: _For Fortran code ported to C in mechanical fashion, one may chose to retain 1-based indexing to avoid the need to transform loops_. So regarding BLAS choosing the column-major storage (Fortran) format is the wise choice. Otherwise you need to transform all matrices from C to Fortran before calling cublasDgemm. – ztik Aug 22 '13 at 19:33
  • This [link](http://peterwittek.com/2013/06/cublas-matrix-c-style/) discusses how to pass C-style (row-major) arrays to cublas (which expects column-major) by re-ordering the parameters without doing any transpose operations. – Robert Crovella Aug 22 '13 at 19:35
  • You may also be interested in my answer [here](http://stackoverflow.com/questions/18254070/how-to-configure-cublastsymm-function-arguments/18282137#18282137), which discusses row-major vs. column-major storage briefly, and gives a small example. – Robert Crovella Aug 22 '13 at 21:39