Skip to content
Closed
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
9 changes: 5 additions & 4 deletions src/pcms/interpolator/mls_interpolation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ Write<Real> mls_interpolation(const Reals source_values,
const Reals source_coordinates,
const Reals target_coordinates,
const SupportResults& support, const LO& dim,
const LO& degree, RadialBasisFunction bf)
const LO& degree, RadialBasisFunction bf,
double lambda_factor)
{

const auto nvertices_target = target_coordinates.size() / dim;
Expand All @@ -116,19 +117,19 @@ Write<Real> mls_interpolation(const Reals source_values,
case RadialBasisFunction::RBF_GAUSSIAN:
interpolated_values = detail::mls_interpolation(
source_values, source_coordinates, target_coordinates, support, dim,
degree, RBF_GAUSSIAN{});
degree, RBF_GAUSSIAN{}, lambda_factor);
break;

case RadialBasisFunction::RBF_C4:
interpolated_values = detail::mls_interpolation(
source_values, source_coordinates, target_coordinates, support, dim,
degree, RBF_C4{});
degree, RBF_C4{}, lambda_factor);
break;

case RadialBasisFunction::RBF_CONST:
interpolated_values = detail::mls_interpolation(
source_values, source_coordinates, target_coordinates, support, dim,
degree, RBF_CONST{});
degree, RBF_CONST{}, lambda_factor);
break;
}

Expand Down
3 changes: 2 additions & 1 deletion src/pcms/interpolator/mls_interpolation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Write<Real> mls_interpolation(const Reals source_values,
const Reals source_coordinates,
const Reals target_coordinates,
const SupportResults& support, const LO& dim,
const LO& degree, RadialBasisFunction bf);
const LO& degree, RadialBasisFunction bf,
double lambda_factor = 0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a docstring to this function explaining all of the inputs? And, what the lambda factor is?

} // namespace pcms
#endif
27 changes: 21 additions & 6 deletions src/pcms/interpolator/mls_interpolation_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ void scale_column_trans_matrix(const ScratchMatView& matrix,
}
}

KOKKOS_INLINE_FUNCTION
void add_regularization(const member_type& team, ScratchMatView& square_matrix,
Real lambda_factor)
{
Kokkos::parallel_for(Kokkos::TeamThreadRange(team, square_matrix.extent(0)),
[=](int i) { square_matrix(i, i) += lambda_factor; });
}

/**
* @struct ResultConvertNormal
* @brief Stores the results of matrix and vector transformations.
Expand Down Expand Up @@ -283,7 +291,8 @@ KOKKOS_INLINE_FUNCTION
ResultConvertNormal convert_normal_equation(const ScratchMatView& matrix,
const ScratchVecView& weight_vector,
const ScratchVecView& rhs,
member_type team)
member_type team,
double lambda_factor)
{

int m = matrix.extent(0);
Expand Down Expand Up @@ -319,6 +328,10 @@ ResultConvertNormal convert_normal_equation(const ScratchMatView& matrix,

team.team_barrier();

add_regularization(team, square_matrix, lambda_factor);

team.team_barrier();

KokkosBlas::Experimental::
Gemv<KokkosBlas::Mode::Team, KokkosBlas::Algo::Gemv::Unblocked>::invoke(
team, 'N', 1.0, scaled_matrix, rhs, 0.0, transformed_rhs);
Expand Down Expand Up @@ -419,7 +432,8 @@ void mls_interpolation(RealConstDefaultScalarArrayView source_values,
RealConstDefaultScalarArrayView target_coordinates,
const SupportResults& support, const LO& dim,
const LO& degree, Func rbf_func,
RealDefaultScalarArrayView approx_target_values)
RealDefaultScalarArrayView approx_target_values,
double lambda_factor)
{
PCMS_FUNCTION_TIMER;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any limits on the range of the lambda? I.e., does it need to be positive? If so, we should check those preconditions with an assert here.

static_assert(std::is_invocable_r_v<double, Func, double, double>,
Expand Down Expand Up @@ -550,8 +564,8 @@ void mls_interpolation(RealConstDefaultScalarArrayView source_values,

team.team_barrier();

auto result = convert_normal_equation(vandermonde_matrix, phi_vector,
support_values, team);
auto result = convert_normal_equation(
vandermonde_matrix, phi_vector, support_values, team, lambda_factor);

team.team_barrier();

Expand Down Expand Up @@ -593,7 +607,8 @@ Write<Real> mls_interpolation(const Reals source_values,
const Reals source_coordinates,
const Reals target_coordinates,
const SupportResults& support, const LO& dim,
const LO& degree, Func rbf_func)
const LO& degree, Func rbf_func,
double lambda_factor)
{
const auto nvertices_source = source_coordinates.size() / dim;

Expand All @@ -619,7 +634,7 @@ Write<Real> mls_interpolation(const Reals source_values,

mls_interpolation(source_values_array_view, source_coordinates_array_view,
target_coordinates_array_view, support, dim, degree,
rbf_func, interpolated_values_array_view);
rbf_func, interpolated_values_array_view, lambda_factor);

return interpolated_values;
}
Expand Down
27 changes: 14 additions & 13 deletions test/test_linear_solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ TEST_CASE("solver test")

team.team_barrier();

double lambda = 0.5;
auto result = convert_normal_equation(vandermonde_matrix, phi,
support_values, team);
support_values, team, lambda);

team.team_barrier();

Expand Down Expand Up @@ -159,10 +160,10 @@ TEST_CASE("solver test")
Kokkos::View<double**, Kokkos::HostSpace> expected_solution(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are the expected results changing so much here? Shouldn't we get the same values when lambda=0?

"expected solution", nvertices_target, size);

expected_lhs_matrix(0, 0, 0) = 35.0;
expected_lhs_matrix(0, 0, 0) = 35.5;
expected_lhs_matrix(0, 0, 1) = 44.0;
expected_lhs_matrix(0, 1, 0) = 44.0;
expected_lhs_matrix(0, 1, 1) = 56.0;
expected_lhs_matrix(0, 1, 1) = 56.5;

expected_rhs_vector(0, 0) = 76.0;
expected_rhs_vector(0, 1) = 100.0;
Expand All @@ -174,13 +175,13 @@ TEST_CASE("solver test")
expected_scaled_matrix(0, 1, 1) = 4.0;
expected_scaled_matrix(0, 1, 2) = 6.0;

expected_solution(0, 0) = -6.0;
expected_solution(0, 1) = 6.5;
expected_solution(0, 0) = -1.519713;
expected_solution(0, 1) = 2.953405;

expected_lhs_matrix(1, 0, 0) = 30.0;
expected_lhs_matrix(1, 0, 0) = 30.5;
expected_lhs_matrix(1, 0, 1) = 20.0;
expected_lhs_matrix(1, 1, 0) = 20.0;
expected_lhs_matrix(1, 1, 1) = 30.0;
expected_lhs_matrix(1, 1, 1) = 30.5;

expected_rhs_vector(1, 0) = 70.0;
expected_rhs_vector(1, 1) = 40.0;
Expand All @@ -192,8 +193,8 @@ TEST_CASE("solver test")
expected_scaled_matrix(1, 1, 1) = -2.0;
expected_scaled_matrix(1, 1, 2) = 1.0;

expected_solution(1, 0) = 2.6;
expected_solution(1, 1) = -0.4;
expected_solution(1, 0) = 2.517680;
expected_solution(1, 1) = -0.339463;

for (int i = 0; i < nvertices_target; ++i) {
for (int j = 0; j < size; ++j) {
Expand All @@ -203,7 +204,7 @@ TEST_CASE("solver test")
i, j, l);
REQUIRE_THAT(
expected_scaled_matrix(i, j, l),
Catch::Matchers::WithinAbs(host_result_scaled(i, j, l), 1E-10));
Catch::Matchers::WithinAbs(host_result_scaled(i, j, l), 1E-6));
}
}
for (int j = 0; j < size; ++j) {
Expand All @@ -213,22 +214,22 @@ TEST_CASE("solver test")
k);
REQUIRE_THAT(
expected_lhs_matrix(i, j, k),
Catch::Matchers::WithinAbs(host_result_lhs(i, j, k), 1E-10));
Catch::Matchers::WithinAbs(host_result_lhs(i, j, k), 1E-6));
}
}
for (int j = 0; j < size; ++j) {
printf("A^T Q b: (%f,%f) at (%d,%d)\n", expected_rhs_vector(i, j),
host_result_rhs(i, j), i, j);
REQUIRE_THAT(expected_rhs_vector(i, j),
Catch::Matchers::WithinAbs(host_result_rhs(i, j), 1E-10));
Catch::Matchers::WithinAbs(host_result_rhs(i, j), 1E-6));
}

for (int j = 0; j < size; ++j) {
printf("A^T Q b: (%f,%f) at (%d,%d)\n", expected_solution(i, j),
host_result_solution(i, j), i, j);
REQUIRE_THAT(
expected_solution(i, j),
Catch::Matchers::WithinAbs(host_result_solution(i, j), 1E-10));
Catch::Matchers::WithinAbs(host_result_solution(i, j), 1E-6));
}
}
}
Expand Down