gpt4 book ai didi

c++ - 使用 cuBlas 将矩阵与其转置相乘

转载 作者:行者123 更新时间:2023-11-30 05:39:37 24 4
gpt4 key购买 nike

我正在尝试将矩阵与其转置相乘,但我无法进行正确的 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);
}

我错过了什么?如何正确调用 matrix*matrixTranspose 的 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);
}

关于c++ - 使用 cuBlas 将矩阵与其转置相乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32286599/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com