Skip to content
Open
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
21 changes: 21 additions & 0 deletions include/pcms/backends/omega_h/omega_h.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "pcms/backends/omega_h/geometry.hpp"
#include "pcms/field/field.hpp"
#include "pcms/geometry/distributed_geometry.hpp"
#include "pcms/geometry/geometry.hpp"
#include "pcms/utility/arrays.hpp"
#include "pcms/utility/assert.hpp"
Expand Down Expand Up @@ -48,6 +49,26 @@ namespace pcms::omega_h
std::shared_ptr<const CoordinateSystem> system,
OmegaHGeometryOptions options = {});

/**
* The geometry of an Omega_h mesh partitioned over ranks, on the geometry of
* this rank's part. Binds query points anywhere in the decomposition; see
* CreateDistributedEvaluator. Collective.
* \param local an Omega_h geometry (from CreateGeometry)
* \param grid_divisions cells per axis of the grid routing points to ranks;
* 0 derives it from the global element count. The same on every rank.
* \throws pcms_error if `local` is not an Omega_h geometry
*/
[[nodiscard]] std::shared_ptr<const DistributedGeometry>
CreateDistributedGeometry(std::shared_ptr<const Geometry> local,
LO grid_divisions = 0);

/** As above, on a new geometry of `mesh` (see CreateGeometry). Collective. */
[[nodiscard]] std::shared_ptr<const DistributedGeometry>
CreateDistributedGeometry(Omega_h::Mesh& mesh,
std::shared_ptr<const CoordinateSystem> system,
OmegaHGeometryOptions options = {},
LO grid_divisions = 0);

/// A point-cloud geometry whose sites are the vertices (`entity_dim` 0) or the
/// element centroids (the mesh dimension) of the mesh of an Omega_h geometry;
/// the support search of its neighbourhoods is the mesh's adjacency search.
Expand Down
29 changes: 29 additions & 0 deletions include/pcms/field/distributed_evaluator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef PCMS_FIELD_DISTRIBUTED_EVALUATOR_HPP
#define PCMS_FIELD_DISTRIBUTED_EVALUATOR_HPP

#include "pcms/field/point_evaluator_factory.hpp"
#include "pcms/geometry/distributed_geometry.hpp"

#include <memory>

namespace pcms
{

/**
* Evaluates `local_source` over the whole decomposition of `geometry`: bind
* the query points on `geometry` (collective) and evaluate fields of the local
* source as usual; evaluation is collective too. The fields' ghost values must
* be synchronized.
* \param local_source an evaluator source on geometry's local geometry, e.g.
* a space created on it
* \throws pcms_error if either is null or `local_source` is not on the local
* geometry
*/
[[nodiscard]] std::shared_ptr<const PointEvaluatorFactory>
CreateDistributedEvaluator(
std::shared_ptr<const DistributedGeometry> geometry,
std::shared_ptr<const PointEvaluatorFactory> local_source);

} // namespace pcms

#endif // PCMS_FIELD_DISTRIBUTED_EVALUATOR_HPP
95 changes: 95 additions & 0 deletions include/pcms/geometry/distributed_geometry.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#ifndef PCMS_GEOMETRY_DISTRIBUTED_GEOMETRY_HPP
#define PCMS_GEOMETRY_DISTRIBUTED_GEOMETRY_HPP

#include "pcms/geometry/bound_points.hpp"
#include "pcms/geometry/geometry.hpp"
#include "pcms/utility/memory_spaces.hpp"
#include "pcms/utility/types.hpp"

#include <Kokkos_Core.hpp>
#include <mpi.h>

#include <memory>
#include <vector>

namespace pcms
{

/** Where a query point lies in a partitioned domain. */
struct PointOwner
{
/** The rank that answers for the point, -1 if no rank contains it. */
int rank = -1;
/**
* The entity of `rank`'s local geometry containing the point, in that
* rank's numbering, or -1 if unknown.
*/
LO entity = -1;
};

/**
* \brief The geometry of a partitioned domain: binds query points anywhere in
* the whole decomposition, not only in this rank's part.
*
* Wraps the geometry of this rank's part. A kind of partition supplies only
* LocateOwners: which rank owns each query point. Its Bind is collective:
* every rank calls it, with its own points (possibly none).
*
* Fields are evaluated over it by wrapping the evaluator source of a space on
* the local geometry, see CreateDistributedEvaluator.
*/
class DistributedGeometry : public Geometry
{
public:
[[nodiscard]] const std::shared_ptr<const CoordinateSystem>&
GetCoordinateSystem() const noexcept override
{
return local_->GetCoordinateSystem();
}

/** The geometry of this rank's part. */
[[nodiscard]] const std::shared_ptr<const Geometry>& GetLocalGeometry()
const noexcept
{
return local_;
}

/** The communicator of the partition. */
[[nodiscard]] MPI_Comm GetComm() const noexcept { return comm_; }

/** Hints naming the local geometry: its entities are the ones bound. */
[[nodiscard]] bool TakesHints(const OwnerHints& owners,
LO num_points) const noexcept override
{
return owners.Names(*local_, num_points);
}

/**
* The owner of each of `points` (one row per point, in the local
* geometry's coordinate system). Collective.
* \param owners claimed owners of `points` naming the local geometry, or
* empty; a kind may start its search from them (verified)
* \return one owner per point
*/
[[nodiscard]] virtual std::vector<PointOwner> LocateOwners(
Kokkos::View<const Real**, DeviceMemorySpace> points,
const OwnerHints& owners) const = 0;

/** The dimension of the entities LocateOwners returns. */
[[nodiscard]] virtual int OwnerEntityDim() const noexcept = 0;

protected:
/** \throws pcms_error if `local` is null */
DistributedGeometry(std::shared_ptr<const Geometry> local, MPI_Comm comm);

[[nodiscard]] std::shared_ptr<BoundPoints> BindImpl(
const BindRequest& request) const override;

private:
std::shared_ptr<const Geometry> local_;
MPI_Comm comm_;
};

} // namespace pcms

#endif // PCMS_GEOMETRY_DISTRIBUTED_GEOMETRY_HPP
72 changes: 72 additions & 0 deletions include/pcms/utility/detail/arrays.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "pcms/utility/arrays.hpp"

#include <vector>

namespace pcms::detail
{

Expand Down Expand Up @@ -301,6 +303,76 @@ void ScatterRows(Rank2View<const T, DeviceMemorySpace> src,
KOKKOS_LAMBDA(size_t i, size_t c) { dest(rows(i), c) = src(i, c); });
}

// ---- std::vector <-> device copies ----
//
// Each performs at most one host/device transfer. A row-major `std::vector`
// is the layout MPI buffers use; the device side uses the default layout for
// the device memory space, which may be column-major.

// `host` copied to a new device view.
template <typename T>
Kokkos::View<T*, DeviceMemorySpace> CopyToDevice(const std::vector<T>& host)
{
Kokkos::View<T*, DeviceMemorySpace> device("CopyToDevice", host.size());
const Kokkos::View<const T*, HostMemorySpace, Kokkos::MemoryUnmanaged>
host_view(host.data(), host.size());
Kokkos::deep_copy(device, host_view);
return device;
}

// A device matrix as a row-major host vector, `extent(0) * extent(1)` long.
template <typename T, typename... Properties>
std::vector<std::remove_const_t<T>> CopyToHostRowMajor(
const Kokkos::View<T**, Properties...>& device)
{
using U = std::remove_const_t<T>;
std::vector<U> host(device.extent(0) * device.extent(1));
Kokkos::View<U**, Kokkos::LayoutRight, HostMemorySpace,
Kokkos::MemoryUnmanaged>
host_view(host.data(), device.extent(0), device.extent(1));
DeepCopyMismatchLayouts(host_view, device);
return host;
}

// A row-major host vector of `n_cols` columns as a new device matrix.
template <typename T>
Kokkos::View<T**, DeviceMemorySpace> CopyRowMajorToDevice(
const std::vector<T>& host, int n_cols)
{
const size_t n_rows =
n_cols > 0 ? host.size() / static_cast<size_t>(n_cols) : 0;
const Kokkos::View<const T**, Kokkos::LayoutRight, HostMemorySpace,
Kokkos::MemoryUnmanaged>
host_view(host.data(), n_rows, n_cols);
Kokkos::View<T**, DeviceMemorySpace> device("CopyRowMajorToDevice", n_rows,
n_cols);
DeepCopyMismatchLayouts(device, host_view);
return device;
}

// The rows `rows(k)` of a rank-2 device view, gathered into a new device
// matrix on the device (no transfer).
template <typename SrcView>
Kokkos::View<typename SrcView::non_const_value_type**, DeviceMemorySpace>
GatherRows(const SrcView& src, Kokkos::View<const LO*, DeviceMemorySpace> rows)
{
static_assert(Kokkos::is_view<SrcView>::value && SrcView::rank() == 2,
"GatherRows takes a rank-2 Kokkos::View");
using T = typename SrcView::non_const_value_type;
const int n_cols = static_cast<int>(src.extent(1));
Kokkos::View<T**, DeviceMemorySpace> out("GatherRows", rows.extent(0),
n_cols);
Kokkos::parallel_for(
"GatherRows",
Kokkos::RangePolicy<DeviceMemorySpace::execution_space>(0, rows.extent(0)),
KOKKOS_LAMBDA(LO k) {
for (int c = 0; c < n_cols; ++c) {
out(k, c) = src(rows(k), c);
}
});
return out;
}

} // namespace pcms::detail

#endif // PCMS_UTILITY_DETAIL_ARRAYS_HPP
13 changes: 8 additions & 5 deletions src/pcms/backends/omega_h/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ if(PCMS_ENABLE_OMEGA_H)
# the grid search over the mesh's elements and the mesh-adjacency
# neighbourhood: part of pcms::localization
target_sources(
pcms_localization PRIVATE point_search.cpp adj_search.cpp point_search.hpp
adj_search.hpp queue_visited.hpp)
pcms_localization
PRIVATE point_search.cpp adj_search.cpp distributed_point_search.cpp
point_search.hpp adj_search.hpp queue_visited.hpp
candidate_rank_table.hpp distributed_point_search.hpp)

# layouts, field data, Lagrange evaluator, the geometry, the uniform-grid
# binary field and the public entry points: part of pcms::field
target_sources(
pcms_field
PRIVATE entity_layout.cpp lagrange_layout.cpp geometry.cpp omega_h.cpp
uniform_grid_binary_field.cpp entity_layout.hpp lagrange_layout.hpp
field_data.hpp lagrange_evaluator.hpp
uniform_grid_binary_field.hpp omega_h_geometry.hpp
uniform_grid_binary_field.cpp distributed_omega_h_geometry.cpp
entity_layout.hpp lagrange_layout.hpp field_data.hpp
lagrange_evaluator.hpp uniform_grid_binary_field.hpp
omega_h_geometry.hpp distributed_omega_h_geometry.hpp
PUBLIC FILE_SET field TYPE HEADERS BASE_DIRS
${PROJECT_SOURCE_DIR}/include/pcms FILES
${PROJECT_SOURCE_DIR}/include/pcms/backends/omega_h/omega_h.hpp
Expand Down
109 changes: 109 additions & 0 deletions src/pcms/backends/omega_h/candidate_rank_table.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#ifndef PCMS_BACKENDS_OMEGA_H_CANDIDATE_RANK_TABLE_HPP
#define PCMS_BACKENDS_OMEGA_H_CANDIDATE_RANK_TABLE_HPP

#include "pcms/backends/omega_h/omega_h.hpp"
#include "pcms/localization/candidate_rank_table.hpp"
#include "pcms/utility/bounding_box.hpp"
#include "pcms/utility/types.hpp"
#include "pcms/utility/detail/uniform_grid.hpp"
#include "pcms/utility/uniform_grid.hpp"

#include <Omega_h_bbox.hpp>
#include <Omega_h_mesh.hpp>

#include <optional>
#include <stdexcept>

namespace pcms::omega_h
{

/**
* Cells per axis giving about one element per cell of the whole
* decomposition, the same on every rank (`nelems()` counts ghosts, so it
* differs between ranks). Collective.
*/
template <int Dim>
[[nodiscard]] LO DivisionsPerAxisForGlobalMesh(Omega_h::Mesh& mesh,
LO min_divisions = 10)
{
return pcms::detail::DivisionsPerAxisForMesh<Dim>(
static_cast<LO>(mesh.nglobal_ents(mesh.dim())), min_divisions);
}

/**
* The shared routing grid for `mesh`, identical on every rank: the globally
* reduced bounding box, about one element per cell of the global element
* count. Collective.
*/
template <int Dim>
[[nodiscard]] UniformGrid<Dim> SharedRoutingGrid(Omega_h::Mesh& mesh,
LO min_divisions = 10)
{
return CreateUniformGridFromMesh<Dim>(
mesh, DivisionsPerAxisForGlobalMesh<Dim>(mesh, min_divisions));
}

/**
* The routing grid for `mesh` with `divisions` cells per axis, or the default
* resolution (`SharedRoutingGrid`) when `divisions` is 0. Collective.
*
* `divisions` must be the same on every rank, or cell ids would name different
* regions of space on different ranks.
*/
template <int Dim>
[[nodiscard]] UniformGrid<Dim> RoutingGrid(Omega_h::Mesh& mesh, LO divisions)
{
if (divisions > 0) {
return CreateUniformGridFromMesh<Dim>(mesh, divisions);
}
return SharedRoutingGrid<Dim>(mesh);
}

/**
* The bounding box of this rank's part of `mesh`, or none when it holds no
* vertices: a rank with no local coordinates owns no elements either, so there
* is nothing for a query to find there.
*/
template <int Dim>
[[nodiscard]] std::optional<AABBox<Dim>> LocalBounds(Omega_h::Mesh& mesh)
{
if (mesh.nverts() == 0) {
return std::nullopt;
}
const auto bbox = Omega_h::find_bounding_box<Dim>(mesh.coords());
AABBox<Dim> box{};
for (int d = 0; d < Dim; ++d) {
box.center[d] = (bbox.min[d] + bbox.max[d]) / 2.0;
box.half_width[d] = (bbox.max[d] - bbox.min[d]) / 2.0;
}
return box;
}

/**
* The candidate rank table of the parts of `mesh` on `grid` (the shared
* routing grid by default). Collective.
* \throws std::invalid_argument if the mesh dimension is not `Dim`
*/
template <int Dim>
[[nodiscard]] CandidateRankTable<Dim> CreateCandidateRankTable(
Omega_h::Mesh& mesh, const UniformGrid<Dim>& grid)
{
if (mesh.dim() != Dim) {
throw std::invalid_argument(
"CreateCandidateRankTable: mesh dimension does not match the table");
}
return CandidateRankTable<Dim>(mesh.comm()->get_impl(), grid,
LocalBounds<Dim>(mesh));
}

/** \see CreateCandidateRankTable */
template <int Dim>
[[nodiscard]] CandidateRankTable<Dim> CreateCandidateRankTable(
Omega_h::Mesh& mesh)
{
return CreateCandidateRankTable<Dim>(mesh, SharedRoutingGrid<Dim>(mesh));
}

} // namespace pcms::omega_h

#endif // PCMS_BACKENDS_OMEGA_H_CANDIDATE_RANK_TABLE_HPP
Loading
Loading