From 84e5bc646fdf8d43256c1ddbaf7ec8794464cbc0 Mon Sep 17 00:00:00 2001 From: JordanHanotiaux <103147288+JordanHanotiaux@users.noreply.github.com> Date: Fri, 11 Apr 2025 15:02:17 +0200 Subject: [PATCH] Update distributedmatrix.cpp --- P2/distributedmatrix.cpp | 52 ++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/P2/distributedmatrix.cpp b/P2/distributedmatrix.cpp index 4e7ba44..5c12761 100644 --- a/P2/distributedmatrix.cpp +++ b/P2/distributedmatrix.cpp @@ -214,29 +214,40 @@ DistributedMatrix multiply(const Matrix& left, const DistributedMatrix& right) { } Matrix DistributedMatrix::multiplyTransposed(const DistributedMatrix &other) const { - Matrix local = localData * other.localData.transpose(); - double *resultData = (double*) malloc(globalRows * other.globalRows * sizeof(double)); - - // Effectuer la réduction - MPI_Allreduce(local.getData().data(), resultData, globalRows * other.globalRows, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD); - - // Affichage de resultData avant de le libérer - if (rank == 0) { // Print seulement sur le processus 0 - std::cout << "Contenu de resultData:" << std::endl; - for (int i = 0; i < globalRows; ++i) { - for (int j = 0; j < other.globalRows; ++j) { - int index = i * other.globalRows + j; - std::cout << resultData[index] << " "; - } - std::cout << std::endl; + + Matrix result = (*this).localData * other.getLocalData().transpose(); + int localSize = result.numRows() * result.numCols(); + + std::vector<int> counts(numProcesses); + std::vector<int> displacements(numProcesses); + + MPI_Allgather(&localSize, 1, MPI_INT, counts.data(), 1, MPI_INT, MPI_COMM_WORLD); + + displacements[0] = 0; + for (int i = 1; i < numProcesses; ++i) + displacements[i] = displacements[i - 1] + counts[i - 1]; + + std::vector<double> buffer(this->globalRows * other.globalRows); + + + MPI_Allreduce( + result.getData().data(), + buffer.data(), + globalRows * other.globalRows, + MPI_DOUBLE, + MPI_SUM, + MPI_COMM_WORLD + ); + + Matrix fullMatrix(globalRows, other.globalRows); + + for (int i = 0; i < globalRows; ++i) { + for (int j = 0; j < other.globalRows; ++j) { + fullMatrix.set(i, j, buffer[i * other.globalRows + j]); } } - // Créer le résultat final - Matrix result(globalRows, other.globalRows); - free(resultData); // Libération de la mémoire allouée - - return result; + return fullMatrix; } @@ -247,4 +258,3 @@ Matrix DistributedMatrix::multiplyTransposed(const DistributedMatrix &other) con - -- GitLab