From c7ac8bef20f7aa3c23f8a4d2abae4440ac619075 Mon Sep 17 00:00:00 2001 From: JordanHanotiaux <103147288+JordanHanotiaux@users.noreply.github.com> Date: Fri, 11 Apr 2025 15:19:44 +0200 Subject: [PATCH] Update distributedmatrix.cpp --- P2/distributedmatrix.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/P2/distributedmatrix.cpp b/P2/distributedmatrix.cpp index d29e0c0..3d6d08c 100644 --- a/P2/distributedmatrix.cpp +++ b/P2/distributedmatrix.cpp @@ -54,14 +54,14 @@ int DistributedMatrix::numCols() const { double DistributedMatrix::get(int i, int j) const { if (j < startCol || j >= startCol + localCols) { - throw std::out_of_range("Attempt to access non-local column in get()"); + throw std::out_of_range("Out of range."); } return localData.get(i, j - startCol); } void DistributedMatrix::set(int i, int j, double value) { if (j < startCol || j >= startCol + localCols) { - throw std::out_of_range("Attempt to access non-local column in set()"); + throw std::out_of_range("Out of range.)"); } localData.set(i, j - startCol, value); } @@ -79,16 +79,17 @@ int DistributedMatrix::localColIndex(int globalColIndex) const { } int DistributedMatrix::ownerProcess(int globalColIndex) const { - int remainingCols = globalCols % numProcesses; + std::vector<int> localStartProcesses(numProcesses); - for (int p = 0; p < numProcesses; ++p) { - int start = p * localCols + min(p, remainingCols); - int cols = localCols + (p < remainingCols ? 1 : 0); - if (globalColIndex >= start && globalColIndex < start + cols) { - return p; + MPI_Allgather(&startCol, 1, MPI_INT, localStartProcesses.data(), 1, MPI_INT, MPI_COMM_WORLD); + + for (int p = 1; p < numProcesses; ++p) { + if (globalColIndex >= localStartProcesses[p-1] && globalColIndex < localStartProcesses[p]) { + return p - 1; } } - return 0; + return numProcesses - 1; + } const Matrix& DistributedMatrix::getLocalData() const { @@ -240,7 +241,6 @@ Matrix DistributedMatrix::multiplyTransposed(const DistributedMatrix &other) con } void sync_matrix(Matrix *matrix, int rank, int src) { - // Utiliser const_cast pour supprimer le qualificatif const, car MPI_Bcast nécessite un void* MPI_Bcast(const_cast<double*>(matrix->getData().data()), matrix->numRows() * matrix->numCols(), MPI_DOUBLE, src, MPI_COMM_WORLD); } -- GitLab