Skip to content
21 changes: 21 additions & 0 deletions include/pcms/field/detail/evaluate_transpose.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Part of pcms/field/point_evaluator.hpp, which includes it after declaring
// PointEvaluator and before defining it.
#include "pcms/field/point_evaluator.hpp"
#ifndef PCMS_FIELD_DETAIL_EVALUATE_TRANSPOSE_HPP
#define PCMS_FIELD_DETAIL_EVALUATE_TRANSPOSE_HPP

namespace pcms::detail
{

/// @brief functional = E^T values + beta functional, with the checks of
/// PointEvaluator::EvaluateTranspose; beta == 0 does not read the
/// functional (collective)
template <typename T, typename LayoutPolicy>
void EvaluateTranspose(
const PointEvaluator<T, LayoutPolicy>& evaluator,
ValueView<const T, DeviceMemorySpace, LayoutPolicy> values,
Field<T>& functional, T beta);

} // namespace pcms::detail

#endif // PCMS_FIELD_DETAIL_EVALUATE_TRANSPOSE_HPP
83 changes: 83 additions & 0 deletions include/pcms/field/detail/field_data.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Part of pcms/field/field_data.hpp, which includes it after declaring
// FieldData and before defining it.
#include "pcms/field/field_data.hpp"
#ifndef PCMS_FIELD_DETAIL_FIELD_DATA_HPP
#define PCMS_FIELD_DETAIL_FIELD_DATA_HPP

#include <span>

namespace pcms::detail
{

// The FieldData coefficient operations, for the coefficient algebra of
// pcms/field/detail/field_ops.hpp, which checks the operands.

/// @brief z = a x + b y
template <typename T>
void LinearSum(T a, const FieldData<T>& x, T b, const FieldData<T>& y,
FieldData<T>& z)
{
z.LinearSum(a, x, b, y);
}

/// @brief z = c x
template <typename T>
void Scale(T c, const FieldData<T>& x, FieldData<T>& z)
{
z.Scale(c, x);
}

/// @brief z = c
template <typename T>
void Const(T c, FieldData<T>& z)
{
z.Const(c);
}

/// @brief z = x / y, entry by entry
template <typename T>
void Div(const FieldData<T>& x, const FieldData<T>& y, FieldData<T>& z)
{
z.Div(x, y);
}

/// @return the sum of x_i y_i over the coefficients this rank owns
template <typename T>
[[nodiscard]] T DotProdLocal(const FieldData<T>& x, const FieldData<T>& y)
{
return x.DotProdLocal(y);
}

/// @return the smallest coefficient this rank owns
template <typename T>
[[nodiscard]] T MinLocal(const FieldData<T>& x)
{
return x.MinLocal();
}

/// @return the largest coefficient magnitude this rank owns
template <typename T>
[[nodiscard]] T MaxNormLocal(const FieldData<T>& x)
{
return x.MaxNormLocal();
}

/// @brief Replaces each value by its sum over the ranks `data` is
/// partitioned over. Collective.
template <typename T>
void SumOverRanks(const FieldData<T>& data, std::span<T> values)
{
data.SumOverRanks(values);
}

/// @brief Replaces each value by its maximum over the ranks `data` is
/// partitioned over. Collective.
template <typename T>
void MaxOverRanks(const FieldData<T>& data, std::span<T> values)
{
data.MaxOverRanks(values);
}

} // namespace pcms::detail

#endif // PCMS_FIELD_DETAIL_FIELD_DATA_HPP
144 changes: 144 additions & 0 deletions include/pcms/field/detail/field_data_ops.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#ifndef PCMS_FIELD_DETAIL_FIELD_DATA_OPS_HPP
#define PCMS_FIELD_DETAIL_FIELD_DATA_OPS_HPP

#include "pcms/field/field_data.hpp"
#include "pcms/utility/assert.hpp"
#include "pcms/utility/memory_spaces.hpp"
#include <Kokkos_Core.hpp>
#include <cstdint>

namespace pcms::detail
{

// The kernels of the FieldData coefficient operations, in place on a kind's
// [dof_holder][component] storage.

template <typename T>
using CoefficientView = Kokkos::View<T**, DeviceMemorySpace>;

/// @return `x` as the kind `Kind`
/// @throws pcms_error if `x` is field data of another kind
template <typename Kind, typename T>
const Kind& OperandOfKind(const FieldData<T>& x)
{
const auto* kind = dynamic_cast<const Kind*>(&x);
if (kind == nullptr) {
throw pcms_error("FieldData: the operand is field data of another kind");
}
return *kind;
}

template <typename T>
void CheckSameShape(const CoefficientView<T>& a, const CoefficientView<T>& b)
{
if (a.extent(0) != b.extent(0) || a.extent(1) != b.extent(1)) {
throw pcms_error("FieldData: the operands have different numbers of "
"coefficients");
}
}

template <typename T>
auto CoefficientPolicy(const CoefficientView<T>& out)
{
return Kokkos::MDRangePolicy<DeviceMemorySpace::execution_space,
Kokkos::Rank<2>>(
{0, 0},
{static_cast<int64_t>(out.extent(0)), static_cast<int64_t>(out.extent(1))});
}

/// @brief out = a x + b y
template <typename T>
void LinearSumValues(const CoefficientView<T>& out, T a,
const CoefficientView<T>& x, T b,
const CoefficientView<T>& y)
{
CheckSameShape(out, x);
CheckSameShape(out, y);
Kokkos::parallel_for(
"pcms::FieldData::LinearSum", CoefficientPolicy(out),
KOKKOS_LAMBDA(const int64_t i, const int64_t c) {
out(i, c) = a * x(i, c) + b * y(i, c);
});
}

/// @brief out = c x
template <typename T>
void ScaleValues(const CoefficientView<T>& out, T c,
const CoefficientView<T>& x)
{
CheckSameShape(out, x);
Kokkos::parallel_for(
"pcms::FieldData::Scale", CoefficientPolicy(out),
KOKKOS_LAMBDA(const int64_t i, const int64_t k) {
out(i, k) = c * x(i, k);
});
}

/// @brief out = c
template <typename T>
void ConstValues(const CoefficientView<T>& out, T c)
{
Kokkos::deep_copy(out, c);
}

/// @brief out = x / y, entry by entry
template <typename T>
void DivValues(const CoefficientView<T>& out, const CoefficientView<T>& x,
const CoefficientView<T>& y)
{
CheckSameShape(out, x);
CheckSameShape(out, y);
Kokkos::parallel_for(
"pcms::FieldData::Div", CoefficientPolicy(out),
KOKKOS_LAMBDA(const int64_t i, const int64_t c) {
out(i, c) = x(i, c) / y(i, c);
});
}

/// @return the sum of x_i y_i
template <typename T>
T DotProdValues(const CoefficientView<T>& x, const CoefficientView<T>& y)
{
CheckSameShape(x, y);
T sum = T{};
Kokkos::parallel_reduce(
"pcms::FieldData::DotProdLocal", CoefficientPolicy(x),
KOKKOS_LAMBDA(const int64_t i, const int64_t c, T& partial) {
partial += x(i, c) * y(i, c);
},
sum);
return sum;
}

/// @return the smallest x_i; the largest T for no coefficients
template <typename T>
T MinValues(const CoefficientView<T>& x)
{
T min{};
Kokkos::parallel_reduce(
"pcms::FieldData::MinLocal", CoefficientPolicy(x),
KOKKOS_LAMBDA(const int64_t i, const int64_t c, T& partial) {
partial = x(i, c) < partial ? x(i, c) : partial;
},
Kokkos::Min<T>(min));
return min;
}

/// @return the largest |x_i|; 0 for no coefficients
template <typename T>
T MaxNormValues(const CoefficientView<T>& x)
{
T max{};
Kokkos::parallel_reduce(
"pcms::FieldData::MaxNormLocal", CoefficientPolicy(x),
KOKKOS_LAMBDA(const int64_t i, const int64_t c, T& partial) {
const T a = Kokkos::abs(x(i, c));
partial = a > partial ? a : partial;
},
Kokkos::Max<T>(max));
return max;
}

} // namespace pcms::detail

#endif // PCMS_FIELD_DETAIL_FIELD_DATA_OPS_HPP
128 changes: 128 additions & 0 deletions include/pcms/field/detail/field_ops.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#ifndef PCMS_FIELD_DETAIL_FIELD_OPS_HPP
#define PCMS_FIELD_DETAIL_FIELD_OPS_HPP

#include "pcms/field/field.hpp"
#include "pcms/utility/assert.hpp"
#include <cmath>
#include <span>
#include <string>
#include <typeinfo>

namespace pcms::detail
{

// The coefficient algebra of the solvers and projections over Fields: each
// function checks its operands, then calls the FieldData coefficient
// operation of the same name. They act on the owned coefficients; those that
// write synchronize the field, and the reductions are collective over the
// ranks the data is partitioned over.

template <typename T>
void CheckSameLayout(const Field<T>& a, const Field<T>& b, const char* what)
{
if (&a.GetLayout() != &b.GetLayout()) {
throw pcms_error(std::string(what) +
": the fields must have the same layout");
}
}

/// @throws pcms_error unless `a` and `b` have the same layout, value basis and
/// kind of field data
template <typename T>
void CheckOperands(const Field<T>& a, const Field<T>& b, const char* what)
{
CheckSameLayout(a, b, what);
if (!SameValueBasis(a.GetData().GetValueBasis(),
b.GetData().GetValueBasis())) {
throw pcms_error(std::string(what) + ": the fields' value bases differ");
}
if (typeid(a.GetData()) != typeid(b.GetData())) {
throw pcms_error(std::string(what) +
": the fields' data are of different kinds");
}
}

/// @brief z = a x + b y
template <typename T>
void LinearSum(T a, const Field<T>& x, T b, const Field<T>& y, Field<T>& z)
{
CheckOperands(x, z, "LinearSum");
CheckOperands(y, z, "LinearSum");
LinearSum(a, x.GetData(), b, y.GetData(), z.GetData());
z.Synchronize();
}

/// @brief z = c x
template <typename T>
void Scale(T c, const Field<T>& x, Field<T>& z)
{
CheckOperands(x, z, "Scale");
Scale(c, x.GetData(), z.GetData());
z.Synchronize();
}

/// @brief z = c
template <typename T>
void Const(T c, Field<T>& z)
{
Const(c, z.GetData());
z.Synchronize();
}

/// @brief z = x / y, entry by entry
template <typename T>
void Div(const Field<T>& x, const Field<T>& y, Field<T>& z)
{
CheckOperands(x, z, "Div");
CheckOperands(y, z, "Div");
Div(x.GetData(), y.GetData(), z.GetData());
z.Synchronize();
}

/// @return the sum over the owned coefficients of every rank of x_i y_i
template <typename T>
[[nodiscard]] T DotProd(const Field<T>& x, const Field<T>& y)
{
CheckOperands(x, y, "DotProd");
T sum = DotProdLocal(x.GetData(), y.GetData());
SumOverRanks(x.GetData(), std::span<T>(&sum, 1));
return sum;
}

/// @return the smallest owned coefficient over every rank
template <typename T>
[[nodiscard]] T Min(const Field<T>& x)
{
T negated = -MinLocal(x.GetData());
MaxOverRanks(x.GetData(), std::span<T>(&negated, 1));
return -negated;
}

/// @return the largest owned coefficient magnitude over every rank
template <typename T>
[[nodiscard]] T MaxNorm(const Field<T>& x)
{
T max = MaxNormLocal(x.GetData());
MaxOverRanks(x.GetData(), std::span<T>(&max, 1));
return max;
}

/// @return the Euclidean norm of the coefficients
template <typename T>
[[nodiscard]] T Norm2(const Field<T>& x)
{
return std::sqrt(DotProd(x, x));
}

/// @brief The pairing of a functional (e.g. the moments E^T W f of a function
/// f) with a function: l(u) = sum_i b_i u_i. Basis-independent: the
/// functional is dual to the coefficients.
template <typename T>
[[nodiscard]] T Apply(const Field<T>& functional, const Function<T>& u)
{
return DotProd(functional, static_cast<const Field<T>&>(u));
}

} // namespace pcms::detail

#endif // PCMS_FIELD_DETAIL_FIELD_OPS_HPP
Loading
Loading