diff --git a/src/language/modules/CoreModule.cpp b/src/language/modules/CoreModule.cpp index d031a75bc7d3437383eb71c74b3401fa9e3d678b..b73f7f0a2f94d2259daae18ab8e1476a28ad1f41 100644 --- a/src/language/modules/CoreModule.cpp +++ b/src/language/modules/CoreModule.cpp @@ -31,6 +31,7 @@ #include <language/utils/UnaryOperatorRegisterForRn.hpp> #include <language/utils/UnaryOperatorRegisterForRnxn.hpp> #include <language/utils/UnaryOperatorRegisterForZ.hpp> +#include <utils/ExecutionStatManager.hpp> #include <utils/Messenger.hpp> #include <utils/PugsUtils.hpp> #include <utils/RandomEngine.hpp> @@ -144,6 +145,33 @@ CoreModule::CoreModule() : BuiltinModule(true) )); + this->_addBuiltinFunction("stop", + std::function( + + []() -> bool { + bool has_stop_file = false; + + if (parallel::rank() == 0) { + std::filesystem::path stop_file("STOP"); + if (std::filesystem::exists(stop_file)) { + const double elapse_time = ExecutionStatManager::getInstance().getElapseTime(); + + const double stop_file_age = std::chrono::duration_cast<std::chrono::seconds>( + std::chrono::system_clock::now() - + std::chrono::clock_cast<std::chrono::system_clock>( + std::filesystem::last_write_time(stop_file))) + .count(); + + has_stop_file = elapse_time > stop_file_age; + } + } + parallel::broadcast(has_stop_file, 0); + + return has_stop_file; + } + + )); + this->_addNameValue("cout", ast_node_data_type_from<std::shared_ptr<const OStream>>, EmbeddedData{std::make_shared<DataHandler<const OStream>>(std::make_shared<OStream>(std::cout))}); diff --git a/src/utils/ExecutionStatManager.cpp b/src/utils/ExecutionStatManager.cpp index d24d821b49439485e50c204181b7a6ec733643e4..06f7942cf37955c7d715fded89b464da74586eb5 100644 --- a/src/utils/ExecutionStatManager.cpp +++ b/src/utils/ExecutionStatManager.cpp @@ -161,6 +161,12 @@ ExecutionStatManager::printInfo() } } +double +ExecutionStatManager::getElapseTime() const +{ + return m_elapse_time.seconds(); +} + double ExecutionStatManager::getCumulativeElapseTime() const { diff --git a/src/utils/ExecutionStatManager.hpp b/src/utils/ExecutionStatManager.hpp index 75adce89bd5469df2cc60d6c8db6c220dc3fc85a..f07099eecf4be81a7cea41b1456f826be5c06cbf 100644 --- a/src/utils/ExecutionStatManager.hpp +++ b/src/utils/ExecutionStatManager.hpp @@ -29,6 +29,8 @@ class ExecutionStatManager ~ExecutionStatManager() = default; public: + double getElapseTime() const; + double getCumulativeElapseTime() const; double getCumulativeTotalCPUTime() const;