Skip to content

Commit

Permalink
Merge branch 'pvidal_set_onion_patch_locator_on_gpu' into 'main'
Browse files Browse the repository at this point in the history
Set OnionPatchLocator on GPU.

See merge request gysela-developpers/gyselalibxx!676
  • Loading branch information
EmilyBourne committed Sep 4, 2024
1 parent 8be9417 commit a357a82
Show file tree
Hide file tree
Showing 9 changed files with 384 additions and 57 deletions.
5 changes: 2 additions & 3 deletions src/multipatch/connectivity/edge_transformation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class EdgeTransformation
* @return Boolean stating if there is an equivalent index.
*/
template <class CurrentIdx>
bool const is_match_available(CurrentIdx const& current_idx) const
bool is_match_available(CurrentIdx const& current_idx) const
{
using IdxTarget
= std::conditional_t<std::is_same_v<CurrentIdx, IdxEdge1>, IdxEdge2, IdxEdge1>;
Expand Down Expand Up @@ -209,8 +209,7 @@ class EdgeTransformation
*
*/
template <class CurrentGrid, class TargetGrid>
bool const search_for_match(Idx<TargetGrid>& target_idx, Idx<CurrentGrid> const& current_idx)
const
bool search_for_match(Idx<TargetGrid>& target_idx, Idx<CurrentGrid> const& current_idx) const
{
static_assert(
std::is_same_v<CurrentGrid, EdgeGrid1> || std::is_same_v<CurrentGrid, EdgeGrid2>,
Expand Down
14 changes: 0 additions & 14 deletions src/multipatch/connectivity/ipatch_locator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,10 @@
*
* The physical coordinates are supposed to be global
* and the logical coordinates are supposed to be locally defined.
*
* @tparam X First physical coordinate.
* @tparam Y Second physical coordinate.
*/
template <class X, class Y>
class IPatchLocator
{
public:
/// @brief Default value to define outside domain (not a patch).
static constexpr int outside_domain = -1;

IPatchLocator() = default;
virtual ~IPatchLocator() = default;

/**
* @brief Get the patch index where the physical coordinate is.
* @param coord Physical coordinate we want to identify the associated patch.
* @return[int] Patch index of the associated patch.
*/
virtual int operator()(Coord<X, Y> const& coord) const = 0;
};
69 changes: 48 additions & 21 deletions src/multipatch/connectivity/onion_patch_locator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,20 @@
*
* This operator locates on which patch a given physical coordinate is.
*
* @warning The operator can works on GPU or CPU according to the given
* execution space ExecSpace. The ExecSpace by default is device.
* The constructor will still need to be called from CPU, but the operator()
* needs to be called from the given ExecSpace.
*
* @anchor OnionPatchLocatorImplementation
*
* @tparam MultipatchIdxRanges A MultipatchType type containing the 2D index ranges
* on each patch.
* @tparam Mapping A mapping type for all the patches.
* @tparam ExecSpace The space (CPU/GPU) where the calculations are carried out.
* By default it is on device.
*/
template <class MultipatchIdxRanges, class Mapping>
template <class MultipatchIdxRanges, class Mapping, class ExecSpace = Kokkos::DefaultExecutionSpace>
class OnionPatchLocator;


Expand All @@ -42,12 +49,13 @@ class OnionPatchLocator;
*
* See @ref OnionPatchLocatorImplementation
*
* @tparam ExecSpace The space (CPU/GPU) where the calculations are carried out.
* @tparam Patches Patch types. Their order is important.
* @tparam Mapping A mapping type for all the patches.
*/
template <class... Patches, class Mapping>
class OnionPatchLocator<MultipatchType<IdxRangeOnPatch, Patches...>, Mapping>
: public IPatchLocator<typename Mapping::cartesian_tag_x, typename Mapping::cartesian_tag_y>
template <class... Patches, class Mapping, class ExecSpace>
class OnionPatchLocator<MultipatchType<IdxRangeOnPatch, Patches...>, Mapping, ExecSpace>
: public IPatchLocator
{
using X = typename Mapping::cartesian_tag_x;
using Y = typename Mapping::cartesian_tag_y;
Expand All @@ -69,11 +77,10 @@ class OnionPatchLocator<MultipatchType<IdxRangeOnPatch, Patches...>, Mapping>
&& ...),
"The mappings and the patches have to be defined on the same dimensions.");

Mapping const m_mapping;
MultipatchIdxRanges const m_all_idx_ranges;

Mapping const& m_mapping;
MultipatchIdxRanges const& m_all_idx_ranges;

std::array<Coord<R>, n_patches + 1> m_radii;
Kokkos::View<Coord<R>*, typename ExecSpace::memory_space> m_radii;

public:
/**
Expand All @@ -88,6 +95,7 @@ class OnionPatchLocator<MultipatchType<IdxRangeOnPatch, Patches...>, Mapping>
OnionPatchLocator(MultipatchIdxRanges const& all_idx_ranges, Mapping const& mapping)
: m_mapping(mapping)
, m_all_idx_ranges(all_idx_ranges)
, m_radii("m_radii", n_patches + 1)
{
set_and_check_radii();
}
Expand All @@ -111,23 +119,21 @@ class OnionPatchLocator<MultipatchType<IdxRangeOnPatch, Patches...>, Mapping>
*
* @see IPatchLocator
*/
int operator()(Coord<X, Y> const& coord) const
KOKKOS_INLINE_FUNCTION int operator()(Coord<X, Y> const coord) const
{
// Dichotomy to find the right patch.
int patch_index_min = 0;
int patch_index_max = n_patches - 1;

// Check if the physical coordinate corresponds to rmax of the domain.
Coord<R> r_max = m_radii[patch_index_max + 1];
Coord<R> r_max = m_radii(patch_index_max + 1);
Coord<R> r(m_mapping(coord));
if (r == r_max) {
return patch_index_max;
}

while (patch_index_max >= patch_index_min) {
int patch_index_mid = (patch_index_min + patch_index_max) / 2;
Coord<R> r_min = m_radii[patch_index_mid];
r_max = m_radii[patch_index_mid + 1];
Coord<R> r_min = m_radii(patch_index_mid);
r_max = m_radii(patch_index_mid + 1);

if (r_min <= r && r < r_max) {
return patch_index_mid;
Expand All @@ -137,34 +143,55 @@ class OnionPatchLocator<MultipatchType<IdxRangeOnPatch, Patches...>, Mapping>
patch_index_min = patch_index_mid + 1;
}
};
return IPatchLocator<X, Y>::outside_domain;
return IPatchLocator::outside_domain;
}


/**
* @brief Get the mapping on the given Patch.
* The function can run on device and host.
* @tparam Patch Patch type.
* @return The mapping on the given Patch.
*/
template <class Patch>
KOKKOS_FUNCTION Mapping get_mapping_on_patch() const
{
return m_mapping;
}

/// @brief Get the type of the mapping on the given Patch.
template <class Patch>
using get_mapping_on_patch_t = Mapping;


private:
/** @brief Set the m_radii array containing all the boundary radial coordinates.
* Check if the patches are well ordered by comparing the radii.
*/
void set_and_check_radii()
{
Kokkos::View<Coord<R>*, Kokkos::HostSpace> radii_host("radii_host", n_patches + 1);

std::array<Coord<R>, n_patches> r_min {
(Coord<R>(ddc::coordinate(m_all_idx_ranges.template get<Patches>().front())))...};
std::array<Coord<R>, n_patches> r_max {
(Coord<R>(ddc::coordinate(m_all_idx_ranges.template get<Patches>().back())))...};

m_radii[0] = r_min[0];
radii_host(0) = r_min[0];
for (std::size_t i(0); i < n_patches - 1; i++) {
if (abs(double(r_min[i + 1] - r_max[i])) > 1e-14) {
throw std::invalid_argument("The patches listed in PatchOrdering must be ordered.");
}
m_radii[i + 1] = r_max[i];
radii_host(i + 1) = r_max[i];
}
m_radii[n_patches] = r_max[n_patches - 1];
radii_host(n_patches) = r_max[n_patches - 1];

Kokkos::deep_copy(m_radii, radii_host);
}
};



template <class MultipatchIdxRanges, class Mapping>
// To help the template deduction.
template <class MultipatchIdxRanges, class Mapping, class ExecSpace = Kokkos::DefaultExecutionSpace>
OnionPatchLocator(MultipatchIdxRanges const& all_idx_ranges, Mapping const& mapping)
-> OnionPatchLocator<MultipatchIdxRanges, Mapping>;
-> OnionPatchLocator<MultipatchIdxRanges, Mapping, ExecSpace>;
21 changes: 19 additions & 2 deletions src/multipatch/data_types/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,22 @@ DField<Patch3::IdxRange12> field3_from_multipatch = multipatch_field.get<Patch3>
### Types
In `types.hpp` file different aliases are defined to use the MultipatchType class:

* DFieldOnPatch: for storing Field defined on the logical 2D grid of patches.
* IdxRangeOnPatch: for storing 2D IdxRange defined on the logical 2D grid of patches.
* To store 1D grids: `Grid1OnPatch` and `Grid2OnPatch`.
* To store fields and constant fields:
* 2D: (`DFieldMemOnPatch`), `DFieldOnPatch` and `DConstFieldOnPatch`;
* 1D on first dimension: `DField1OnPatch` and `DConstField1OnPatch`.
* To store indices and index ranges:
* 2D: `IdxRangeOnPatch`
* 1D on first dimension: `IdxRange1OnPatch` and `Idx1OnPatch`.
* To store coordinates:
* 2D: `CoordFieldOnPatch`
* 1D on first dimension: `Coord1Field1OnPatch_1D`.
* To store the splines:
* Spline grids: `BSplines1OnPatch` and `BSplines2OnPatch`.
* 2D: 2D spline coefficients `SplineCoeffOnPatch_2D` and `ConstSplineCoeffOnPatch_2D`;
* 2D: spline coefficients on the first dimension `SplineCoeff1OnPatch_2D` and `ConstSplineCoeff1OnPatch_2D`;
* 1D on the fist dimension: spline coefficients `SplineCoeff1OnPatch_1D` and `SplineCoeff2OnPatch_1D`;
* To store derivatives:
* $`\partial_^{(i)} f(x, y_j)`$: `ConstDeriv1_OnPatch_2D`
* $`\partial_^{(i)} f(x_j, y)`$: `ConstDeriv2_OnPatch_2D`
* $`\partial_^{(i)} \partial_y^{(j)} f(x, y)`$: `ConstDeriv12_OnPatch_2D`
6 changes: 3 additions & 3 deletions src/multipatch/data_types/multipatch_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class MultipatchType
* @param other The equivalent MultipatchType being copied.
*/
template <template <typename P> typename OT, class... OPatches>
MultipatchType(MultipatchType<OT, OPatches...> const& other)
KOKKOS_FUNCTION MultipatchType(MultipatchType<OT, OPatches...> const& other)
: m_tuple(std::make_tuple(other.template get<Patches>()...))
{
static_assert(
Expand Down Expand Up @@ -89,7 +89,7 @@ class MultipatchType
"MultipatchTypes are not equivalent");
}

~MultipatchType() = default;
KOKKOS_FUNCTION ~MultipatchType() {}

/**
* Retrieve an object from the patch that it is defined on.
Expand All @@ -98,7 +98,7 @@ class MultipatchType
* @return The object on the given patch.
*/
template <class Patch>
T<Patch> get() const
KOKKOS_FUNCTION T<Patch> get() const
{
return std::get<T<Patch>>(m_tuple);
}
Expand Down
Loading

0 comments on commit a357a82

Please sign in to comment.