diff --git a/P2/distributedmatrix.cpp b/P2/distributedmatrix.cpp index 269bd904d870334e97f449e3408114f1a02d768b..4df69bd65c7e30cf883ef744813189984ba0b9bd 100644 --- a/P2/distributedmatrix.cpp +++ b/P2/distributedmatrix.cpp @@ -214,62 +214,29 @@ DistributedMatrix multiply(const Matrix& left, const DistributedMatrix& right) { } Matrix DistributedMatrix::multiplyTransposed(const DistributedMatrix &other) const { - - 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_Allgatherv( - result.getData().data(), - localSize, - MPI_DOUBLE, - buffer.data(), - counts.data(), - displacements.data(), - MPI_DOUBLE, - MPI_COMM_WORLD - ); - - Matrix fullMatrix(globalRows, other.globalRows); - - for (int i = 0; i < globalRows; ++i) { - for (int j = 0; j < other.globalRows; ++j) { - double sum = 0.0; - for (int p = 0; p < numProcesses; p++) - { - try { - sum += buffer[displacements[p] + i * other.globalRows + j]; - } catch (...) { - // Ne fait rien en cas d'exception - } - - } - fullMatrix.set(i, j, sum); - } - } - - if (rank == 0) { // pour éviter que tous les processus n'impriment - std::cout << "Résultat de multiplyTransposed:" << std::endl; - for (int i = 0; i < fullMatrix.numRows(); ++i) { - for (int j = 0; j < fullMatrix.numCols(); ++j) { - std::cout << fullMatrix.get(i, j) << " "; + Matrix local = localData * other.localData.transpose(); + double *resultData = (double*) malloc(globalRows * other.globalRows * sizeof(double)); + + // Effectuer la réduction + MPI_Allreduce(local.getData(), 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; } } - return fullMatrix; + // Créer le résultat final + Matrix result(globalRows, other.globalRows); + free(resultData); // Libération de la mémoire allouée + + return result; } @@ -280,3 +247,4 @@ Matrix DistributedMatrix::multiplyTransposed(const DistributedMatrix &other) con +