我试图将矩阵与其转置相乘,但我无法设法正确进行sgemm调用。 Sgemm需要很多参数。像lda,ldb这样的一些人对我来说很困惑。如果我使用方形矩阵调用下面的函数,则无法正常工作。
/*param inMatrix: contains the matrix data in major order like [1 2 3 1 2 3]
param rowNum: Number of rows in a matrix eg if matrix is
|1 1|
|2 2|
|3 3| than rowNum should be 3*/
void matrixtTransposeMult(std::vector<float>& inMatrix, int rowNum)
{
cublasHandle_t handle;
cublasCreate(&handle);
int colNum = (int)inMatrix.size() / rowNum;
thrust::device_vector<float> d_InMatrix(inMatrix);
thrust::device_vector<float> d_outputMatrix(rowNum*rowNum);
float alpha = 1.0f;
float beta = 0.0f;
cublasSgemm(handle, CUBLAS_OP_N, CUBLAS_OP_T, rowNum, rowNum, colNum, &alpha,
thrust::raw_pointer_cast(d_InMatrix.data()), colNum, thrust::raw_pointer_cast(d_InMatrix.data()), colNum, &beta,
thrust::raw_pointer_cast(d_outputMatrix.data()), rowNum);
thrust::host_vector<float> result = d_outputMatrix;
for (auto elem : result)
std::cout << elem << ",";
std::cout << std::endl;
cublasDestroy(handle);
}
我错过了什么?如何为矩阵* matrixTranspose进行正确的sgemm调用?
答案 0 :(得分:1)
下面的设置对我有用,如果我遗失了什么,请警告我。我希望它对某些人有用
void matrixtTransposeMult(std::vector<float>& inMatrix, int rowNum)
{
cublasHandle_t handle;
cublasCreate(&handle);
int colNum = (int)inMatrix.size() / rowNum;
thrust::device_vector<float> d_InMatrix(inMatrix);
thrust::device_vector<float> d_outputMatrix(rowNum*rowNum);
float alpha = 1.0f;
float beta = 0.0f;
cublasSgemm(handle, CUBLAS_OP_N, CUBLAS_OP_T, rowNum, rowNum, colNum, &alpha,
thrust::raw_pointer_cast(d_InMatrix.data()), rowNum, thrust::raw_pointer_cast(d_InMatrix.data()), rowNum, &beta,
thrust::raw_pointer_cast(d_outputMatrix.data()), rowNum);
thrust::host_vector<float> result = d_outputMatrix;
for (auto elem : result)
std::cout << elem << ",";
std::cout << std::endl;
cublasDestroy(handle);
}