diff --git a/include/LinearAlgebra/Matrix/coo_matrix.h b/include/LinearAlgebra/Matrix/coo_matrix.h index d7ebd462..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; } 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;