Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion core/test/base/array.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
//
// SPDX-License-Identifier: BSD-3-Clause

Expand Down Expand Up @@ -219,6 +219,16 @@ TYPED_TEST(Array, CanBeCopiedToExecutorlessArray)
}


TYPED_TEST(Array, CanBeCopiedToHost)
{
std::vector<TypeParam> ref{5, 2};

auto vec = this->x.copy_to_host();

ASSERT_EQ(vec, ref);
}


TYPED_TEST(Array, CanBeCopiedFromExecutorlessArray)
{
gko::array<TypeParam> a;
Expand Down
11 changes: 10 additions & 1 deletion cuda/test/base/array.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
//
// SPDX-License-Identifier: BSD-3-Clause

Expand Down Expand Up @@ -58,6 +58,15 @@ TYPED_TEST(Array, CanCopyBackTemporaryCloneOnDifferentExecutor)
}


TYPED_TEST(Array, CanCopyToHost)
{
using T = TypeParam;
auto arr = gko::array<T>(this->exec, I<T>{4, 6});

ASSERT_EQ(arr.copy_to_host(), (std::vector<T>{4, 6}));
}


TYPED_TEST(Array, CanGetValue)
{
using T = TypeParam;
Expand Down
16 changes: 15 additions & 1 deletion include/ginkgo/core/base/array.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
//
// SPDX-License-Identifier: BSD-3-Clause

Expand Down Expand Up @@ -641,6 +641,20 @@ class array {
}
}

/**
* Copies the data into an std::vector.
*
* @return an std::vector containing this array's data.
*/
std::vector<value_type> copy_to_host() const
{
std::vector<value_type> 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.
*
Expand Down
Loading