From 8263c156988badfa6197833eeabf8e3b3ddbc514 Mon Sep 17 00:00:00 2001 From: Emily Bourne Date: Fri, 29 May 2026 09:35:38 +0200 Subject: [PATCH 1/3] Matrix printing should be on host --- include/LinearAlgebra/Matrix/coo_matrix.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/LinearAlgebra/Matrix/coo_matrix.h b/include/LinearAlgebra/Matrix/coo_matrix.h index d7ebd462..83f8149a 100644 --- a/include/LinearAlgebra/Matrix/coo_matrix.h +++ b/include/LinearAlgebra/Matrix/coo_matrix.h @@ -67,7 +67,7 @@ class SparseMatrixCOO T* values_data() const; template - friend std::ostream& operator<<(std::ostream& stream, const SparseMatrixCOO& matrix); + friend std::ostream& operator<<(std::ostream& stream, const SparseMatrixCOO& matrix); void write_to_file(const std::string& filename) const; @@ -82,7 +82,7 @@ class SparseMatrixCOO }; template -std::ostream& operator<<(std::ostream& stream, const SparseMatrixCOO& matrix) +std::ostream& operator<<(std::ostream& stream, const SparseMatrixCOO& matrix) { stream << "SparseMatrixCOO: " << matrix.rows_ << " x " << matrix.columns_ << "\n"; stream << "Number of non-zeros (nnz): " << matrix.nnz_ << "\n"; From 886b53ce7a723e42160e5b621172eb4304594cbc Mon Sep 17 00:00:00 2001 From: Emily Bourne Date: Fri, 29 May 2026 13:54:08 +0200 Subject: [PATCH 2/3] Fix operator<< friend definition --- include/LinearAlgebra/Matrix/csr_matrix.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/LinearAlgebra/Matrix/csr_matrix.h b/include/LinearAlgebra/Matrix/csr_matrix.h index 5f7afdae..2b2a8823 100644 --- a/include/LinearAlgebra/Matrix/csr_matrix.h +++ b/include/LinearAlgebra/Matrix/csr_matrix.h @@ -104,8 +104,8 @@ class SparseMatrixCSR KOKKOS_FUNCTION int* column_indices_data() const; KOKKOS_FUNCTION int* row_start_indices_data() const; - template - friend std::ostream& operator<<(std::ostream& stream, const SparseMatrixCSR& matrix); + template + friend std::ostream& operator<<(std::ostream& stream, const SparseMatrixCSR& matrix); template friend class SparseMatrixCSR; From 5ab1c97da07750a2e934a536d3ad1f69076757ce Mon Sep 17 00:00:00 2001 From: Emily Bourne Date: Fri, 29 May 2026 14:44:37 +0200 Subject: [PATCH 3/3] Allow cout for both MemorySpaces --- include/LinearAlgebra/Matrix/coo_matrix.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/include/LinearAlgebra/Matrix/coo_matrix.h b/include/LinearAlgebra/Matrix/coo_matrix.h index 83f8149a..3eb8b60b 100644 --- a/include/LinearAlgebra/Matrix/coo_matrix.h +++ b/include/LinearAlgebra/Matrix/coo_matrix.h @@ -66,8 +66,8 @@ class SparseMatrixCOO int* column_indices_data() const; T* values_data() const; - template - friend std::ostream& operator<<(std::ostream& stream, const SparseMatrixCOO& matrix); + template + friend std::ostream& operator<<(std::ostream& stream, const SparseMatrixCOO& matrix); void write_to_file(const std::string& filename) const; @@ -81,18 +81,22 @@ class SparseMatrixCOO bool is_symmetric_ = false; }; -template -std::ostream& operator<<(std::ostream& stream, const SparseMatrixCOO& matrix) +template +std::ostream& operator<<(std::ostream& stream, const SparseMatrixCOO& matrix) { stream << "SparseMatrixCOO: " << matrix.rows_ << " x " << matrix.columns_ << "\n"; stream << "Number of non-zeros (nnz): " << matrix.nnz_ << "\n"; if (matrix.is_symmetric_) { stream << "Matrix is symmetric.\n"; } + // Create host mirrors and deep_copy from device if necessary. + // If the View is already on host, deep_copy is a no-op. + auto h_values = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, matrix.values_); + auto h_column_indices = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, matrix.column_indices_); + auto h_row_indices = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, matrix.row_indices_); stream << "Non-zero elements (row, column, value):\n"; for (int i = 0; i < matrix.nnz_; ++i) { - stream << "(" << matrix.row_indices_(i) << ", " << matrix.column_indices_(i) << ", " << matrix.values_(i) - << ")\n"; + stream << "(" << h_row_indices(i) << ", " << h_column_indices(i) << ", " << h_values(i) << ")\n"; } return stream; }