用cuBlas将一个矩阵和它的转置相乘
Multiplying a matrix with its transpose using cuBlas
我正试图将矩阵与其转置相乘,但我无法进行正确的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);
}
我错过了什么?如何使正确的sgemm调用矩阵*矩阵转置?
下面的设置为我工作,如果我错过了什么请警告我。我希望它会对某人有用。
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);
}
相关文章:
- 转置矩阵:交换元素不会更改值
- 使用 Eigen 3 库编写一个带有转置作为参数的函数
- 犰狳(C++)中的快速阵列置换(广义张量转置)
- 为什么转置这个 std::vector<std::vector<std::string> > 这么慢?
- 我想重置一个C++结构统计,我可以以某种方式使用 stat() 语法吗?
- 转置结构容器
- 我的转置矩阵代码有什么问题?
- 数组的转置和乘法
- 在C++中使用矢量转置 2D 矩阵
- 特征:块转置
- MKL矩形矩阵Inplace转置:不使用多个核心
- 并行转置不同的矩阵
- 关于次级对角线的转置(翻转)矩阵
- 输出是从您输入的矩阵中打印出矩阵的转置,但我的代码只是打印出您输入的第一个矩阵
- 如何在阵列火中避免翻转和转置的memcpy?
- CUDA矩阵与共享内存转置
- C 阵列的复合物共轭转置
- 这是将一个大而稀疏的矩阵与它的转置相乘的最好方法
- 用cuBlas将一个矩阵和它的转置相乘
- 将非二次矩阵转置到另一个