Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix viewtype tohost #3

Merged
merged 1 commit into from
Feb 14, 2024
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
40 changes: 24 additions & 16 deletions MParT/Utilities/ArrayConversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,19 +226,27 @@ namespace mpart{
@param[in] inview A kokkos array in device memory.
@return A kokkos array in host memory. Note that the layout (row-major or col-major) might be different than the default on the Host. The layout will match the device's default layout.
*/
template<typename ScalarType, typename DeviceMemorySpace>
typename Kokkos::View<ScalarType, Kokkos::HostSpace>::HostMirror ToHost(Kokkos::View<ScalarType, DeviceMemorySpace> const& inview){
typename Kokkos::View<ScalarType, Kokkos::HostSpace>::HostMirror outview = Kokkos::create_mirror_view(inview);
// template<typename ScalarType, typename DeviceMemorySpace>
// typename Kokkos::View<ScalarType, Kokkos::HostSpace>::HostMirror ToHost(Kokkos::View<ScalarType, DeviceMemorySpace> const& inview){
// typename Kokkos::View<ScalarType, Kokkos::HostSpace>::HostMirror outview = Kokkos::create_mirror_view(inview);
// Kokkos::deep_copy (outview, inview);
// return outview;
// }

template<class ViewType>
typename ViewType::HostMirror ToHost(ViewType const& inview){
typename ViewType::HostMirror outview = Kokkos::create_mirror_view(inview);
Kokkos::deep_copy (outview, inview);
return outview;
}

template<typename DeviceMemoryType,typename ScalarType>
StridedMatrix<ScalarType, Kokkos::HostSpace> ToHost(StridedMatrix<ScalarType,DeviceMemoryType> const& inview){
typename StridedMatrix<ScalarType,DeviceMemoryType>::HostMirror outview = Kokkos::create_mirror_view(inview);
Kokkos::deep_copy (outview, inview);
return outview;
}

// template<typename DeviceMemoryType,typename ScalarType>
// StridedMatrix<ScalarType, Kokkos::HostSpace> ToHost(StridedMatrix<ScalarType,DeviceMemoryType> const& inview){
// typename StridedMatrix<ScalarType,DeviceMemoryType>::HostMirror outview = Kokkos::create_mirror_view(inview);
// Kokkos::deep_copy (outview, inview);
// return outview;
// }

/**
@brief Copies a range of elements from a Kokkos array in device to host memory
Expand Down Expand Up @@ -272,13 +280,13 @@ namespace mpart{
@tparam ScalarType The type and dimension of the Kokkos::View (e.g., double*, double**, or int*)
@tparam SliceTypes A variadic parameter pack containing options for constructing a Kokkos::subview of the device view.
*/
template<typename DeviceMemoryType, typename ScalarType, class... SliceTypes>
Kokkos::View<ScalarType, Kokkos::HostSpace> ToHost(Kokkos::View<ScalarType,DeviceMemoryType> const& inview, SliceTypes... sliceParams){
auto subview = Kokkos::subview(inview, sliceParams...); // Construct the subview
typename Kokkos::View<ScalarType>::HostMirror outview = Kokkos::create_mirror_view(subview);
Kokkos::deep_copy (outview, subview);
return outview;
}
// template<typename DeviceMemoryType, typename ScalarType, class... SliceTypes>
// Kokkos::View<ScalarType, Kokkos::HostSpace> ToHost(Kokkos::View<ScalarType,DeviceMemoryType> const& inview, SliceTypes... sliceParams){
// auto subview = Kokkos::subview(inview, sliceParams...); // Construct the subview
// typename Kokkos::View<ScalarType>::HostMirror outview = Kokkos::create_mirror_view(subview);
// Kokkos::deep_copy (outview, subview);
// return outview;
// }


#if defined(MPART_ENABLE_GPU)
Expand Down
12 changes: 6 additions & 6 deletions tests/Test_LinearAlgebra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ TEST_CASE( "Testing LU Factorization on Device", "LinearAlgebra_LUDevice" ) {
}
}
SECTION("Compute Determinant") {
PartialPivLU<Kokkos::HostSpace> Alu_d(constA_d);
PartialPivLU<mpart::DeviceSpace> Alu_d(constA_d);
CHECK(Alu_d.determinant() == Approx(eigA.determinant()).epsilon(1e-14).margin(1e-14));
}
}
Expand All @@ -424,12 +424,12 @@ TEST_CASE( "Testing Cholesky Factorization", "LinearAlgebra_Cholesky" ) {
Eigen::MatrixXd eigY = eigA.llt().matrixL().solve(eigB);

SECTION("Compute Cholesky") {
Cholesky<Kokkos::HostSpace> Achol_d(constA_d);
Cholesky<mpart::DeviceSpace> Achol_d(constA_d);
}
SECTION("Solve Cholesky inplace") {
Kokkos::View<double**, Kokkos::LayoutLeft, mpart::DeviceSpace> C_d("C", 3, 2);
Kokkos::deep_copy(C_d, B_d);
Cholesky<Kokkos::HostSpace> Achol_d(constA_d);
Cholesky<mpart::DeviceSpace> Achol_d(constA_d);
Achol_d.solveInPlace(C_d);
auto C_h = ToHost(C_d);
for(unsigned int j=0; j<C_h.extent(1); ++j){
Expand All @@ -441,7 +441,7 @@ TEST_CASE( "Testing Cholesky Factorization", "LinearAlgebra_Cholesky" ) {
SECTION("Solve Cholesky L inplace") {
Kokkos::View<double**, Kokkos::LayoutLeft, mpart::DeviceSpace> C_d("C", 3, 2);
Kokkos::deep_copy(C_d, B_d);
Cholesky<Kokkos::HostSpace> Achol_d(constA_d);
Cholesky<mpart::DeviceSpace> Achol_d(constA_d);
Achol_d.solveInPlaceL(C_d);
auto C_h = ToHost(C_d);
for(unsigned int j=0; j<C_h.extent(1); ++j){
Expand All @@ -451,7 +451,7 @@ TEST_CASE( "Testing Cholesky Factorization", "LinearAlgebra_Cholesky" ) {
}
}
SECTION("Solve Cholesky out of place") {
Cholesky<Kokkos::HostSpace> Achol_d(constA_d);
Cholesky<mpart::DeviceSpace> Achol_d(constA_d);
auto C_d = Achol_d.solve(B_d);
auto C_h = ToHost(C_d);
for(unsigned int j=0; j<C_h.extent(1); ++j){
Expand All @@ -461,7 +461,7 @@ TEST_CASE( "Testing Cholesky Factorization", "LinearAlgebra_Cholesky" ) {
}
}
SECTION("Compute Determinant") {
Cholesky<Kokkos::HostSpace> Achol_d(constA_d);
Cholesky<mpart::DeviceSpace> Achol_d(constA_d);
CHECK(Achol_d.determinant() == Approx(eigA.determinant()).epsilon(1e-14).margin(1e-14));
}
}
Expand Down