Skip to content

Commit

Permalink
Just flip the flag at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
eggrobin committed Mar 29, 2024
1 parent ddceebd commit 11cc8b8
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 27 deletions.
2 changes: 1 addition & 1 deletion base/macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ inline void noreturn() { std::exit(0); }
#define PRINCIPIA_CONFIGURABLE_TEST_SUFFIX
#endif
#define PRINCIPIA_CONFIGURABLE_TEST_NAME(Fixture) \
Fixture##PRINCIPIA_CONFIGURABLE_TEST_SUFFIX
Fixture##(PRINCIPIA_CONFIGURABLE_TEST_SUFFIX)

// Set this to 1 to test analytical series based on piecewise Poisson series.
#define PRINCIPIA_CONTINUOUS_TRAJECTORY_SUPPORTS_PIECEWISE_POISSON_SERIES 0
Expand Down
35 changes: 15 additions & 20 deletions ksp_plugin_test/ksp_plugin_test.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@
<Import Project="..\shared\journal.vcxitems" Label="Shared" />
<Import Project="..\shared\astronomy.vcxitems" Label="Shared" />
</ImportGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PreprocessorDefinitions>PRINCIPIA_CAN_OVERRIDE_FMA_USAGE_AT_RUNTIME=1</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<PreprocessorDefinitions>PRINCIPIA_CAN_OVERRIDE_FMA_USAGE_AT_RUNTIME=1</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release_LLVM|x64'">
<ClCompile>
<PreprocessorDefinitions>PRINCIPIA_CAN_OVERRIDE_FMA_USAGE_AT_RUNTIME=1</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\ksp_plugin\celestial.cpp" />
<ClCompile Include="..\ksp_plugin\equator_relevance_threshold.cpp" />
Expand Down Expand Up @@ -40,29 +55,9 @@
<ClCompile Include="..\ksp_plugin\plugin.cpp" />
<ClCompile Include="..\ksp_plugin\renderer.cpp" />
<ClCompile Include="..\ksp_plugin\vessel.cpp" />
<ClCompile Include="benchmark.cpp" />
<ClCompile Include="celestial_test.cpp" />
<ClCompile Include="equator_relevance_threshold_test.cpp" />
<ClCompile Include="flight_plan_optimizer_test.cpp" />
<ClCompile Include="flight_plan_test.cpp" />
<ClCompile Include="interface_external_test.cpp" />
<ClCompile Include="interface_flight_plan_test.cpp" />
<ClCompile Include="interface_planetarium_test.cpp" />
<ClCompile Include="interface_renderer_test.cpp" />
<ClCompile Include="interface_test.cpp" />
<ClCompile Include="manœuvre_test.cpp" />
<ClCompile Include="orbit_analyser_test.cpp" />
<ClCompile Include="part_test.cpp" />
<ClCompile Include="pile_up_test.cpp" />
<ClCompile Include="planetarium_test.cpp" />
<ClCompile Include="plugin_compatibility_test.cpp" />
<ClCompile Include="plugin_compatibility_test_no_fma.cpp" />
<ClCompile Include="plugin_integration_test.cpp" />
<ClCompile Include="plugin_io.cpp" />
<ClCompile Include="plugin_test.cpp" />
<ClCompile Include="renderer_test.cpp" />
<ClCompile Include="fake_plugin.cpp" />
<ClCompile Include="vessel_test.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="mock_celestial.hpp" />
Expand Down
126 changes: 123 additions & 3 deletions ksp_plugin_test/plugin_compatibility_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

namespace principia {
namespace interface {
namespace {

using ::testing::AllOf;
using ::testing::ElementsAre;
Expand All @@ -54,6 +53,7 @@ using namespace principia::ksp_plugin::_flight_plan;
using namespace principia::ksp_plugin::_frames;
using namespace principia::ksp_plugin::_plugin;
using namespace principia::ksp_plugin_test::_plugin_io;
using namespace principia::numerics::_fma;
using namespace principia::physics::_apsides;
using namespace principia::physics::_discrete_trajectory;
using namespace principia::quantities::_named_quantities;
Expand All @@ -68,7 +68,7 @@ using namespace std::chrono_literals;
const char preferred_compressor[] = "gipfeli";
const char preferred_encoder[] = "base64";

class PRINCIPIA_CONFIGURABLE_TEST_NAME(PluginCompatibilityTest)
class PluginCompatibilityTest
: public testing::Test {
protected:
PluginCompatibilityTest()
Expand Down Expand Up @@ -253,6 +253,127 @@ TEST_F(PluginCompatibilityTest, Reach) {
// Make sure that we can upgrade, save, and reload.
WriteAndReadBack(std::move(plugin));
}

TEST_F(PluginCompatibilityTest, ReachNoFMA) {
FMAPreventer run_this_test_without_fma;
StringLogSink log_warning(google::WARNING);
not_null<std::unique_ptr<Plugin const>> plugin = ReadPluginFromFile(
SOLUTION_DIR / "ksp_plugin_test" / "saves" / "3072.proto.b64",
/*compressor=*/"gipfeli",
/*decoder=*/"base64");
EXPECT_THAT(log_warning.string(),
AllOf(HasSubstr("pre-Galileo"), Not(HasSubstr("pre-Frobenius"))));
auto const test = plugin->GetVessel("f2d77873-4776-4809-9dfb-de9e7a0620a6");
EXPECT_THAT(test->name(), Eq("TEST"));
EXPECT_THAT(TTSecond(test->trajectory().front().time),
Eq("1970-08-14T08:03:18"_DateTime));
EXPECT_THAT(TTSecond(test->psychohistory()->back().time),
Eq("1970-08-14T08:47:05"_DateTime));
EXPECT_FALSE(test->has_flight_plan());

auto const ifnity = plugin->GetVessel("29142a79-7acd-47a9-a34d-f9f2a8e1b4ed");
EXPECT_THAT(ifnity->name(), Eq("IFNITY-5.2"));
EXPECT_THAT(TTSecond(ifnity->trajectory().front().time),
Eq("1970-08-14T08:03:46"_DateTime));
EXPECT_THAT(TTSecond(ifnity->psychohistory()->back().time),
Eq("1970-08-14T08:47:05"_DateTime));
ASSERT_TRUE(ifnity->has_flight_plan());
ifnity->ReadFlightPlanFromMessage();
FlightPlan const& flight_plan = ifnity->flight_plan();
EXPECT_THAT(
flight_plan.adaptive_step_parameters().length_integration_tolerance(),
Eq(1 * Metre));
EXPECT_THAT(flight_plan.adaptive_step_parameters().max_steps(), Eq(16'000));
EXPECT_THAT(flight_plan.number_of_manœuvres(), Eq(16));
std::vector<std::pair<DateTime, Speed>> manœuvre_ignition_tt_seconds_and_Δvs;
for (int i = 0; i < flight_plan.number_of_manœuvres(); ++i) {
manœuvre_ignition_tt_seconds_and_Δvs.emplace_back(
TTSecond(flight_plan.GetManœuvre(i).initial_time()),
flight_plan.GetManœuvre(i).Δv().Norm());
}
// The flight plan only covers the inner solar system (this is probably
// because of #3035).
// It also differs from https://youtu.be/7BDxZV7UD9I?t=439.
EXPECT_THAT(manœuvre_ignition_tt_seconds_and_Δvs,
ElementsAre(Pair("1970-08-14T09:34:49"_DateTime,
3.80488671073918022e+03 * (Metre / Second)),
Pair("1970-08-15T13:59:24"_DateTime,
3.04867185471741759e-04 * (Metre / Second)),
Pair("1970-12-22T07:48:21"_DateTime,
1.58521291818444873e-03 * (Metre / Second)),
Pair("1971-01-08T17:36:55"_DateTime,
1.40000000034068623e-03 * (Metre / Second)),
Pair("1971-07-02T17:16:00"_DateTime,
1.00000000431022681e-04 * (Metre / Second)),
Pair("1971-09-06T03:27:33"_DateTime,
1.78421858738381537e-03 * (Metre / Second)),
Pair("1972-02-13T22:47:26"_DateTime,
7.72606625794511597e-04 * (Metre / Second)),
Pair("1972-03-25T16:30:19"_DateTime,
5.32846131747503372e-03 * (Metre / Second)),
Pair("1972-12-24T04:09:32"_DateTime,
3.45000000046532824e-03 * (Metre / Second)),
Pair("1973-06-04T01:59:07"_DateTime,
9.10695453328359134e-03 * (Metre / Second)),
Pair("1973-07-09T06:07:17"_DateTime,
4.49510921430966881e-01 * (Metre / Second)),
Pair("1973-09-10T03:59:44"_DateTime,
1.00000000431022681e-04 * (Metre / Second)),
Pair("1974-11-20T17:34:27"_DateTime,
5.10549409572428781e-01 * (Metre / Second)),
Pair("1975-10-07T01:29:45"_DateTime,
2.86686518692948443e-02 * (Metre / Second)),
Pair("1975-12-29T21:27:13"_DateTime,
1.00404183285598275e-03 * (Metre / Second)),
Pair("1977-07-28T22:47:53"_DateTime,
1.39666705839172456e-01 * (Metre / Second))));

// Compute the flybys.
std::map<Instant, std::string> flybys;
for (Index index = 0; plugin->HasCelestial(index); ++index) {
Celestial const& celestial = plugin->GetCelestial(index);
auto const& celestial_trajectory = celestial.trajectory();
auto const& flight_plan_trajectory = flight_plan.GetAllSegments();
DiscreteTrajectory<Barycentric> apoapsides;
DiscreteTrajectory<Barycentric> periapsides;

// The begin time avoid spurious periapsides right after the launch.
ComputeApsides(celestial_trajectory,
flight_plan_trajectory,
flight_plan_trajectory.upper_bound("1970-08-15T00:00:00"_TT),
flight_plan_trajectory.end(),
/*max_points=*/100,
apoapsides,
periapsides);
auto const radius = celestial.body()->mean_radius();
for (auto const& [time, _] : periapsides) {
if ((celestial_trajectory.EvaluatePosition(time) -
flight_plan_trajectory.EvaluatePosition(time))
.Norm() < 50 * radius) {
flybys[time] = celestial.body()->name();
}
}
}

// The flybys are reasonably similar to https://youtu.be/7BDxZV7UD9I?t=439,
// but not identical.
EXPECT_THAT(
flybys,
ElementsAre(
Pair(ResultOf(&TTSecond, "1970-12-23T07:16:42"_DateTime), "Venus"),
Pair(ResultOf(&TTSecond, "1971-08-29T23:33:54"_DateTime), "Mars"),
Pair(ResultOf(&TTSecond, "1972-03-26T11:23:30"_DateTime), "Earth"),
Pair(ResultOf(&TTSecond, "1972-03-27T01:02:40"_DateTime), "Moon"),
Pair(ResultOf(&TTSecond, "1972-11-02T21:15:50"_DateTime), "Venus"),
Pair(ResultOf(&TTSecond, "1973-06-15T13:17:25"_DateTime), "Venus"),
Pair(ResultOf(&TTSecond, "1974-07-25T22:45:52"_DateTime), "Mercury"),
Pair(ResultOf(&TTSecond, "1974-09-05T08:27:45"_DateTime), "Venus"),
Pair(ResultOf(&TTSecond, "1975-04-18T00:42:26"_DateTime), "Venus"),
Pair(ResultOf(&TTSecond, "1976-04-26T17:38:08"_DateTime), "Venus")));

// Make sure that we can upgrade, save, and reload.
WriteAndReadBack(std::move(plugin));
}
#endif

TEST_F(PluginCompatibilityTest, DISABLED_Butcher) {
Expand Down Expand Up @@ -456,6 +577,5 @@ TEST_F(PluginCompatibilityTest, DISABLED_SECULAR_Debug) {
/*decoder=*/"base64");
}

} // namespace
} // namespace interface
} // namespace principia
3 changes: 0 additions & 3 deletions ksp_plugin_test/plugin_compatibility_test_no_fma.cpp

This file was deleted.

18 changes: 18 additions & 0 deletions numerics/fma.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,25 @@ constexpr bool CanEmitFMAInstructions = false;
// The functions in this file unconditionally wrap the appropriate intrinsics.
// The caller may only use them if |UseHardwareFMA| is true.
#if PRINCIPIA_USE_FMA_IF_AVAILABLE()
#if PRINCIPIA_CAN_OVERRIDE_FMA_USAGE_AT_RUNTIME
inline bool DefaultUseHardwareFMA =
CanEmitFMAInstructions && HasCPUFeatures(CPUFeatureFlags::FMA);
inline bool InternalUseHardwareFMA = DefaultUseHardwareFMA;
class FMAPreventer {
public:
FMAPreventer() {
InternalUseHardwareFMA = false;
}
~FMAPreventer() {
InternalUseHardwareFMA = DefaultUseHardwareFMA;
}
};
inline bool const& UseHardwareFMA = InternalUseHardwareFMA;
#else
inline bool const UseHardwareFMA =
CanEmitFMAInstructions && HasCPUFeatures(CPUFeatureFlags::FMA);
class FMAPreventer; // Undefined.
#endif
#else
inline bool const UseHardwareFMA = false;
#endif
Expand All @@ -43,6 +60,7 @@ inline double FusedNegatedMultiplySubtract(double a, double b, double c);
} // namespace internal

using internal::CanEmitFMAInstructions;
using internal::FMAPreventer;
using internal::FusedMultiplyAdd;
using internal::FusedMultiplySubtract;
using internal::FusedNegatedMultiplyAdd;
Expand Down

0 comments on commit 11cc8b8

Please sign in to comment.