From 8c07af70a901aebe4f52d883b32b0aa288e9a099 Mon Sep 17 00:00:00 2001 From: Bruno Turcksin Date: Mon, 20 Mar 2023 14:49:51 -0400 Subject: [PATCH] Move q_points to Kokkos::View --- .../deal.II/matrix_free/cuda_matrix_free.h | 47 ++++++++++--------- .../matrix_free/cuda_matrix_free.templates.h | 30 ++++++------ 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/include/deal.II/matrix_free/cuda_matrix_free.h b/include/deal.II/matrix_free/cuda_matrix_free.h index 069e98e74a..d6588813f6 100644 --- a/include/deal.II/matrix_free/cuda_matrix_free.h +++ b/include/deal.II/matrix_free/cuda_matrix_free.h @@ -20,6 +20,7 @@ #include #include "deal.II/base/memory_space.h" +#include "deal.II/base/utilities.h" #ifdef DEAL_II_WITH_CUDA @@ -174,9 +175,9 @@ namespace CUDAWrappers struct Data { /** - * Pointer to the quadrature points. + * Kokkos::View of the quadrature points. */ - point_type *q_points; + Kokkos::View q_points; /** * Map the position in the local vector to the position in the global @@ -503,10 +504,11 @@ namespace CUDAWrappers std::vector n_cells; /** - * Vector of pointers to the quadrature points associated to the cells of - * each color. + * Vector of Kokkos::View to the quadrature points associated to the cells + * of each color. */ - std::vector q_points; + std::vector> + q_points; /** * Map the position in the local vector to the position in the global @@ -714,8 +716,7 @@ namespace CUDAWrappers const typename CUDAWrappers::MatrixFree::Data *data, const unsigned int n_q_points_1d) { - return *(data->q_points + data->padding_length * cell + - q_point_id_in_cell(n_q_points_1d)); + return data->q_points(cell, q_point_id_in_cell(n_q_points_1d)); } /** @@ -726,9 +727,11 @@ namespace CUDAWrappers struct DataHost { /** - * Vector of quadrature points. + * Kokkos::View of quadrature points on the host. */ - std::vector> q_points; + typename Kokkos::View **, + MemorySpace::Default::kokkos_space>::HostMirror + q_points; /** * Map the position in the local vector to the position in the global @@ -737,14 +740,17 @@ namespace CUDAWrappers std::vector local_to_global; /** - * Vector of inverse Jacobians. + * Kokkos::View of inverse Jacobians on the host. */ - std::vector inv_jacobian; + typename Kokkos::View::HostMirror + inv_jacobian; /** - * Vector of Jacobian times the weights. + * Kokkos::View of Jacobian times the weights on the host. */ - std::vector JxW; + typename Kokkos::View::HostMirror JxW; /** * ID of the associated MatrixFree object. @@ -805,8 +811,8 @@ namespace CUDAWrappers data_host.n_cells * data_host.padding_length; if (update_flags & update_quadrature_points) { - data_host.q_points.resize(n_elements); - Utilities::CUDA::copy_to_host(data.q_points, data_host.q_points); + data_host.q_points = Kokkos::create_mirror(data.q_points); + Kokkos::deep_copy(data_host.q_points, data.q_points); } data_host.local_to_global.resize(n_elements); @@ -815,15 +821,14 @@ namespace CUDAWrappers if (update_flags & update_gradients) { - data_host.inv_jacobian.resize(n_elements * dim * dim); - Utilities::CUDA::copy_to_host(data.inv_jacobian.data(), - data_host.inv_jacobian); + data_host.inv_jacobian = Kokkos::create_mirror(data.inv_jacobian); + Kokkos::deep_copy(data_host.inv_jacobian, data.inv_jacobian); } if (update_flags & update_JxW_values) { - data_host.JxW.resize(n_elements); - Utilities::CUDA::copy_to_host(data.JxW.data(), data_host.JxW); + data_host.JxW = Kokkos::create_mirror(data.JxW); + Kokkos::deep_copy(data_host.JxW, data.JxW); } data_host.constraint_mask.resize(data_host.n_cells); @@ -865,7 +870,7 @@ namespace CUDAWrappers const DataHost &data, const unsigned int i) { - return data.q_points[data.padding_length * cell + i]; + return data.q_points(cell, i); } diff --git a/include/deal.II/matrix_free/cuda_matrix_free.templates.h b/include/deal.II/matrix_free/cuda_matrix_free.templates.h index c50828c180..d23c1b82bc 100644 --- a/include/deal.II/matrix_free/cuda_matrix_free.templates.h +++ b/include/deal.II/matrix_free/cuda_matrix_free.templates.h @@ -234,7 +234,8 @@ namespace CUDAWrappers MatrixFree *data; // Host data std::vector local_to_global_host; - std::vector> q_points_host; + Kokkos::View **, MemorySpace::Default::kokkos_space> + q_points; Kokkos::View JxW; Kokkos::View inv_jacobian; @@ -350,7 +351,12 @@ namespace CUDAWrappers local_to_global_host.resize(n_cells * padding_length); if (update_flags & update_quadrature_points) - q_points_host.resize(n_cells * padding_length); + q_points = Kokkos::View **, + MemorySpace::Default::kokkos_space>( + Kokkos::view_alloc("q_points_" + std::to_string(color), + Kokkos::WithoutInitializing), + n_cells, + q_points_per_cell); if (update_flags & update_JxW_values) JxW = Kokkos::View( @@ -409,11 +415,14 @@ namespace CUDAWrappers // Quadrature points if (update_flags & update_quadrature_points) { - const std::vector> &q_points = + // FIXME too many deep_copy + auto q_points_host = Kokkos::create_mirror_view_and_copy( + MemorySpace::Host::kokkos_space{}, q_points); + const std::vector> &q_points_vec = fe_values.get_quadrature_points(); - std::copy(q_points.begin(), - q_points.end(), - &q_points_host[cell_id * padding_length]); + for (unsigned int i = 0; i < q_points_per_cell; ++i) + q_points_host(cell_id, i) = q_points_vec[i]; + Kokkos::deep_copy(q_points, q_points_host); } if (update_flags & update_JxW_values) @@ -460,10 +469,7 @@ namespace CUDAWrappers // Quadrature points if (update_flags & update_quadrature_points) { - alloc_and_copy(&data->q_points[color], - ArrayView>( - q_points_host.data(), q_points_host.size()), - n_cells * padding_length); + data->q_points[color] = q_points; } // Jacobian determinants/quadrature weights @@ -706,10 +712,6 @@ namespace CUDAWrappers void MatrixFree::free() { - for (auto &q_points_color_ptr : q_points) - Utilities::CUDA::free(q_points_color_ptr); - q_points.clear(); - for (auto &local_to_global_color_ptr : local_to_global) Utilities::CUDA::free(local_to_global_color_ptr); local_to_global.clear(); -- 2.39.5