From dd0424fb5460a514e22874d2c8bd1151b7d08344 Mon Sep 17 00:00:00 2001 From: Tobias Ribizel Date: Sat, 19 Apr 2025 19:22:20 +0200 Subject: [PATCH 1/2] add copy_to_host function --- core/test/base/array.cpp | 12 +++++++++++- include/ginkgo/core/base/array.hpp | 16 +++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/core/test/base/array.cpp b/core/test/base/array.cpp index f7e03855d06..7da2eaa03ac 100644 --- a/core/test/base/array.cpp +++ b/core/test/base/array.cpp @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors +// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors // // SPDX-License-Identifier: BSD-3-Clause @@ -219,6 +219,16 @@ TYPED_TEST(Array, CanBeCopiedToExecutorlessArray) } +TYPED_TEST(Array, CanBeCopiedToHost) +{ + std::vector ref{5, 2}; + + auto vec = this->x.copy_to_host(); + + ASSERT_EQ(vec, ref); +} + + TYPED_TEST(Array, CanBeCopiedFromExecutorlessArray) { gko::array a; diff --git a/include/ginkgo/core/base/array.hpp b/include/ginkgo/core/base/array.hpp index e0cf8c22ab3..4e4ee9c5f51 100644 --- a/include/ginkgo/core/base/array.hpp +++ b/include/ginkgo/core/base/array.hpp @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors +// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors // // SPDX-License-Identifier: BSD-3-Clause @@ -641,6 +641,20 @@ class array { } } + /** + * Copies the data into an std::vector. + * + * @return an std::vector containing this array's data. + */ + std::vector copy_to_host() const + { + std::vector result(this->get_size()); + auto view = make_array_view(this->get_executor()->get_master(), + this->get_size(), result.data()); + view = *this; + return result; + } + /** * Fill the array with the given value. * From 2b152025584b61e62608dd84d4e82f4a620d6465 Mon Sep 17 00:00:00 2001 From: Tobias Ribizel Date: Tue, 22 Apr 2025 14:07:46 +0200 Subject: [PATCH 2/2] add device copy_to_host test --- cuda/test/base/array.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/cuda/test/base/array.cpp b/cuda/test/base/array.cpp index db7d4c54536..b2346a91c72 100644 --- a/cuda/test/base/array.cpp +++ b/cuda/test/base/array.cpp @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors +// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors // // SPDX-License-Identifier: BSD-3-Clause @@ -58,6 +58,15 @@ TYPED_TEST(Array, CanCopyBackTemporaryCloneOnDifferentExecutor) } +TYPED_TEST(Array, CanCopyToHost) +{ + using T = TypeParam; + auto arr = gko::array(this->exec, I{4, 6}); + + ASSERT_EQ(arr.copy_to_host(), (std::vector{4, 6})); +} + + TYPED_TEST(Array, CanGetValue) { using T = TypeParam;