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/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; 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. *