Skip to content
Snippets Groups Projects
Commit 1690e21c authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Add copy_to utilities

While copy returns a new Array/Table from copied values, copy_to deeps
copies values from an Array/Table to an existing one.

Source and destination must have the same size
parent f4332146
No related branches found
No related tags found
1 merge request!89Add missing compatibility check when affecting lists to R^d or R^dxd
...@@ -36,6 +36,13 @@ class [[nodiscard]] Array ...@@ -36,6 +36,13 @@ class [[nodiscard]] Array
return image; return image;
} }
friend PUGS_INLINE void copy_to(const Array<DataType>& source,
const Array<std::remove_const_t<DataType>>& destination)
{
Assert(source.size() == destination.size());
Kokkos::deep_copy(destination.m_values, source.m_values);
}
template <typename DataType2, typename... RT> template <typename DataType2, typename... RT>
friend PUGS_INLINE Array<DataType2> encapsulate(const Kokkos::View<DataType2*, RT...>& values); friend PUGS_INLINE Array<DataType2> encapsulate(const Kokkos::View<DataType2*, RT...>& values);
......
...@@ -46,6 +46,14 @@ class [[nodiscard]] Table ...@@ -46,6 +46,14 @@ class [[nodiscard]] Table
return image; return image;
} }
friend PUGS_INLINE void copy_to(const Table<DataType>& source,
const Table<std::remove_const_t<DataType>>& destination)
{
Assert(source.nbRows() == destination.nbRows());
Assert(source.nbColumns() == destination.nbColumns());
Kokkos::deep_copy(destination.m_values, source.m_values);
}
template <typename DataType2, typename... RT> template <typename DataType2, typename... RT>
friend PUGS_INLINE Table<DataType2> encapsulate(const Kokkos::View<DataType2**, RT...>& values); friend PUGS_INLINE Table<DataType2> encapsulate(const Kokkos::View<DataType2**, RT...>& values);
......
...@@ -105,6 +105,23 @@ TEST_CASE("Array", "[utils]") ...@@ -105,6 +105,23 @@ TEST_CASE("Array", "[utils]")
REQUIRE(((c[0] == 2) and (c[1] == 2) and (c[2] == 2) and (c[3] == 2) and (c[4] == 2) and (c[5] == 2) and REQUIRE(((c[0] == 2) and (c[1] == 2) and (c[2] == 2) and (c[3] == 2) and (c[4] == 2) and (c[5] == 2) and
(c[6] == 2) and (c[7] == 2) and (c[8] == 2) and (c[9] == 2))); (c[6] == 2) and (c[7] == 2) and (c[8] == 2) and (c[9] == 2)));
Array<int> d{a.size()};
copy_to(a, d);
REQUIRE(((d[0] == 0) and (d[1] == 2) and (d[2] == 4) and (d[3] == 6) and (d[4] == 8) and (d[5] == 10) and
(d[6] == 12) and (d[7] == 14) and (d[8] == 16) and (d[9] == 18)));
REQUIRE(((c[0] == 2) and (c[1] == 2) and (c[2] == 2) and (c[3] == 2) and (c[4] == 2) and (c[5] == 2) and
(c[6] == 2) and (c[7] == 2) and (c[8] == 2) and (c[9] == 2)));
copy_to(c, d);
REQUIRE(((a[0] == 0) and (a[1] == 2) and (a[2] == 4) and (a[3] == 6) and (a[4] == 8) and (a[5] == 10) and
(a[6] == 12) and (a[7] == 14) and (a[8] == 16) and (a[9] == 18)));
REQUIRE(((d[0] == 2) and (d[1] == 2) and (d[2] == 2) and (d[3] == 2) and (d[4] == 2) and (d[5] == 2) and
(d[6] == 2) and (d[7] == 2) and (d[8] == 2) and (d[9] == 2)));
} }
SECTION("checking for std container conversion") SECTION("checking for std container conversion")
...@@ -220,5 +237,11 @@ TEST_CASE("Array", "[utils]") ...@@ -220,5 +237,11 @@ TEST_CASE("Array", "[utils]")
{ {
REQUIRE_THROWS_AS(a[10], AssertError); REQUIRE_THROWS_AS(a[10], AssertError);
} }
SECTION("invalid copy_to")
{
Array<int> b{2 * a.size()};
REQUIRE_THROWS_AS(copy_to(a, b), AssertError);
}
#endif // NDEBUG #endif // NDEBUG
} }
...@@ -137,6 +137,23 @@ TEST_CASE("Table", "[utils]") ...@@ -137,6 +137,23 @@ TEST_CASE("Table", "[utils]")
REQUIRE(((c(0, 0) == 2) and (c(1, 0) == 2) and (c(2, 0) == 2) and (c(3, 0) == 2) and // REQUIRE(((c(0, 0) == 2) and (c(1, 0) == 2) and (c(2, 0) == 2) and (c(3, 0) == 2) and //
(c(0, 1) == 2) and (c(1, 1) == 2) and (c(2, 1) == 2) and (c(3, 1) == 2) and // (c(0, 1) == 2) and (c(1, 1) == 2) and (c(2, 1) == 2) and (c(3, 1) == 2) and //
(c(0, 2) == 2) and (c(1, 2) == 2) and (c(2, 2) == 2) and (c(3, 2) == 2))); (c(0, 2) == 2) and (c(1, 2) == 2) and (c(2, 2) == 2) and (c(3, 2) == 2)));
Table<int> d{a.nbRows(), a.nbColumns()};
copy_to(a, d);
REQUIRE(((a(0, 0) == 0) and (a(1, 0) == 2) and (a(2, 0) == 4) and (a(3, 0) == 6) and //
(a(0, 1) == 1) and (a(1, 1) == 3) and (a(2, 1) == 5) and (a(3, 1) == 7) and //
(a(0, 2) == 2) and (a(1, 2) == 4) and (a(2, 2) == 6) and (a(3, 2) == 8)));
REQUIRE(((d(0, 0) == 0) and (d(1, 0) == 2) and (d(2, 0) == 4) and (d(3, 0) == 6) and //
(d(0, 1) == 1) and (d(1, 1) == 3) and (d(2, 1) == 5) and (d(3, 1) == 7) and //
(d(0, 2) == 2) and (d(1, 2) == 4) and (d(2, 2) == 6) and (d(3, 2) == 8)));
copy_to(c, d);
REQUIRE(((d(0, 0) == 2) and (d(1, 0) == 2) and (d(2, 0) == 2) and (d(3, 0) == 2) and //
(d(0, 1) == 2) and (d(1, 1) == 2) and (d(2, 1) == 2) and (d(3, 1) == 2) and //
(d(0, 2) == 2) and (d(1, 2) == 2) and (d(2, 2) == 2) and (d(3, 2) == 2)));
} }
SECTION("checking for Kokkos::View encaspulation") SECTION("checking for Kokkos::View encaspulation")
...@@ -167,5 +184,20 @@ TEST_CASE("Table", "[utils]") ...@@ -167,5 +184,20 @@ TEST_CASE("Table", "[utils]")
REQUIRE_THROWS_AS(a(4, 0), AssertError); REQUIRE_THROWS_AS(a(4, 0), AssertError);
REQUIRE_THROWS_AS(a(0, 3), AssertError); REQUIRE_THROWS_AS(a(0, 3), AssertError);
} }
SECTION("invalid copy_to")
{
SECTION("wrong row number")
{
Table<int> b{2 * a.nbRows(), a.nbColumns()};
REQUIRE_THROWS_AS(copy_to(a, b), AssertError);
}
SECTION("wrong column number")
{
Table<int> c{a.nbRows(), 2 * a.nbColumns()};
REQUIRE_THROWS_AS(copy_to(a, c), AssertError);
}
}
#endif // NDEBUG #endif // NDEBUG
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment