我想並行化矩陣轉置操作使用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