J'essaie de paralléliser une opération de transposition matricielle en utilisant la bibliothèque CUBLAS (avec la fonction cublasSgeam
). Les données de sortie sont correctes, mais il prend en moyenne 150 fois plus de temps que ma version CPU. Pourquoi?Opération de transposition matricielle très lente avec CUBLAS
code CPU (pour la transposition d'une matrice de N = 5000
par M=140
)
// 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
de code GPU (pour la transposition d'une matrice de N = 5000
par M=140
)
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);
des temps écoulés
CUBLAS: 148.461 ms
CPU: 0.986944 ms
PS:Fonctionnant sur GeForce GTX 660 & Intel Core i5 660
Quelle est la taille de N et M? Prenez également en compte que vous incluez le temps de création du contexte cublas dans le timing. – JackOLantern
Que se passe-t-il si vous exécutez deux fois la transformation? La vitesse est-elle la même la deuxième fois? – talonmies