我想并行化矩阵转置操作使用CUBLAS库(与cublasSgeam
功能)。 输出数据是正确的,但它比我的CPU版本平均要多150多个时间。为什么?非常缓慢的矩阵转置操作与CUBLAS
CPU代码(对于转置由M=140
的N = 5000
矩阵)
// Starting the timer
float *matrixT = (float *) malloc (N * M * sizeof(float));
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
matrixT[(j*N)+i] = matrix[(i*M)+j]; // matrix is obviously filled
//Ending the timer
GPU代码(对于转置由M=140
的N = 5000
矩阵)
float *h_matrixT , *d_matrixT , *d_matrix;
h_matrixT = (float *) malloc (N * M * sizeof(float));
cudaMalloc((void **)&d_matrixT , N * M * sizeof(float)));
cudaMalloc((void**)&d_matrix , N * M * sizeof(float)));
cudaMemcpy(d_matrix , matrix , N * M * sizeof(float) , cudaMemcpyHostToDevice));
//Starting the timer
const float alpha = 1.0;
const float beta = 0.0;
cublasHandle_t handle;
cublasCreate(&handle);
cublasSgeam(handle, CUBLAS_OP_T, CUBLAS_OP_N, N, M, &alpha, d_matrix, M, &beta, d_matrix, N, d_matrixT, N);
cublasDestroy(handle);
//Ending the timer
cudaMemcpy(h_matrixT , d_matrixT , N * M * sizeof(float) , cudaMemcpyDeviceToHost));
cudaFree(d_matrix);
cudaFree(d_matrixT);
经过时间
个CUBLAS:148.461毫秒
CPU:0.986944毫秒
PS:运行在GeForce GTX 660 & Intel酷睿i5 660
N和M有多大?同时考虑到你在时间中包括创建cublas上下文的时间。 – JackOLantern
如果您运行两次转换,会发生什么?第二次速度是否一样? – talonmies