29#include <boost/program_options.hpp>
30#include <boost/algorithm/string/predicate.hpp>
84namespace po = boost::program_options;
85namespace fs = boost::filesystem;
125 ::setenv(
"LC_ALL",
"C", 1);
136 bool omp_env_present =
getenv(
"OMP_NUM_THREADS") ||
getenv(
"OMP_DYNAMIC");
137 bool mkl_env_present =
getenv(
"MKL_NUM_THREADS") ||
getenv(
"MKL_DYNAMIC");
138 if (!omp_env_present && !mkl_env_present) {
140 void (*set_num_threads)(int) =
reinterpret_cast<void (*)(
int)
>(dlsym(RTLD_DEFAULT,
"MKL_Set_Num_Threads"));
141 void (*set_dynamic)(int) =
reinterpret_cast<void (*)(
int)
>(dlsym(RTLD_DEFAULT,
"MKL_Set_Dynamic"));
142 if (set_num_threads) {
147 logger.
debug() <<
"Disabling dynamic multithreading";
162 std::make_shared<SourceWithOnDemandPropertiesFactory>(
task_provider);
164 std::make_shared<SourceGroupWithOnDemandPropertiesFactory>(
task_provider);
194 CheckImages::getInstance().reportConfigDependencies(config_manager);
217 "List the possible output properties for the given input parameters and exit");
219 "Show the columns created for each property");
221 "Show the columns created for each property, for the given configuration");
223 "Dump parameters with default values into a configuration file");
227 po::positional_options_description p;
228 p.add(
"python-arg", -1);
234 template <
typename T>
236 out << opt.long_name() <<
'=' << boost::any_cast<T>(default_value) <<
std::endl;
240 template <
typename T>
242 auto values = boost::any_cast<std::vector<T>>(default_value);
243 if (values.empty()) {
244 out <<
"# " << opt.long_name() <<
'=' <<
std::endl;
247 for (
const auto& v : values)
248 out << opt.long_name() <<
'=' << v <<
std::endl;
256 {
typeid(bool), &writeDefault<bool>},
257 {
typeid(int), &writeDefault<int>},
258 {
typeid(double), &writeDefault<double>},
262 decltype(printers)::const_iterator printer;
266 boost::any default_value;
269 if (!p->semantic()->apply_default(default_value)) {
272 else if ((printer = printers.find(default_value.type())) == printers.end()) {
273 std::cout <<
'#' << p->long_name() <<
"=<Unknown type " << default_value.type().name() <<
'>' <<
std::endl;
276 printer->second(
std::cout, *p, default_value);
315 if (args.
find(
"config-file") != args.
end()) {
316 auto cfg_file = args.
at(
"config-file").as<fs::path>();
317 if (cfg_file !=
"" && !fs::exists(cfg_file)) {
318 throw Elements::Exception() <<
"The configuration file '" << cfg_file <<
"' does not exist";
328 config_manager.initialize(args);
331 auto memory_config = config_manager.getConfiguration<
MemoryConfig>();
332 TileManager::getInstance()->setOptions(memory_config.getTileSize(),
333 memory_config.getTileSize(), memory_config.getTileMaxMemory());
335 CheckImages::getInstance().configure(config_manager);
369 auto prefetch = source_grouping->requiredProperties();
370 auto deblending_prefetch = deblending->requiredProperties();
371 prefetch.insert(deblending_prefetch.begin(), deblending_prefetch.end());
372 if (!prefetch.empty()) {
373 prefetcher = std::make_shared<Prefetcher>(thread_pool, multithreading_config.
getMaxQueueSize());
374 prefetcher->requestProperties(prefetch);
383 prefetcher->setNextStage(source_grouping);
386 partition->setNextStage(source_grouping);
389 source_grouping->setNextStage(deblending);
390 deblending->setNextStage(measurement);
393 logger.
info() <<
"Writing output following measure order";
394 measurement->setNextStage(output);
396 logger.
info() <<
"Writing output following segmentation order";
397 auto sorter = std::make_shared<Sorter>();
398 measurement->setNextStage(sorter);
399 sorter->setNextStage(output);
402 segmentation->Observable<SegmentationProgress>::addObserver(progress_mediator->getSegmentationObserver());
403 segmentation->Observable<SourceInterface>::addObserver(progress_mediator->getDetectionObserver());
404 deblending->Observable<SourceGroupInterface>::addObserver(progress_mediator->getDeblendingObserver());
405 measurement->Observable<SourceGroupInterface>::addObserver(progress_mediator->getMeasurementObserver());
408 if (CheckImages::getInstance().getSegmentationImage(0) !=
nullptr) {
409 segmentation->Observable<SourceInterface>::addObserver(std::make_shared<DetectionIdCheckImage>());
411 if (CheckImages::getInstance().getPartitionImage(0) !=
nullptr) {
412 measurement->Observable<SourceGroupInterface>::addObserver(std::make_shared<SourceIdCheckImage>());
414 if (CheckImages::getInstance().getGroupImage(0) !=
nullptr) {
415 measurement->Observable<SourceGroupInterface>::addObserver(std::make_shared<GroupIdCheckImage>());
417 if (CheckImages::getInstance().getMoffatImage(0) !=
nullptr) {
418 measurement->Observable<SourceGroupInterface>::addObserver(std::make_shared<MoffatCheckImage>());
420 const auto& detection_frames = config_manager.getConfiguration<
DetectionFrameConfig>().getDetectionFrames();
423 measurement->startThreads();
425 size_t prev_writen_rows = 0;
426 size_t frame_number = 0;
427 for (
auto& detection_frame : detection_frames) {
432 << frame_number <<
" / " << detection_frames.size() <<
" : " << detection_frame->getLabel();
433 segmentation->processFrame(detection_frame);
436 logger.
error() <<
"Failed to process the frame! " <<
e.what();
437 measurement->stopThreads();
442 prefetcher->synchronize();
444 measurement->synchronizeThreads();
446 size_t nb_writen_rows = output->flush();
449 logger.
info() << (nb_writen_rows - prev_writen_rows) <<
" sources detected in frame, " << nb_writen_rows <<
" total";
451 prev_writen_rows = nb_writen_rows;
457 measurement->stopThreads();
459 CheckImages::getInstance().saveImages();
460 TileManager::getInstance()->flush();
461 progress_mediator->done();
463 if (prev_writen_rows > 0) {
464 logger.
info() <<
"total " << prev_writen_rows <<
" sources detected";
484 auto& config_manager = ConfigManager::getInstance(
conf_man_id);
486 auto options = config_manager.closeRegistration();
488 options.add_options()(
"*", po::value<std::vector<std::string>>());
493 auto& config_manager = ConfigManager::getInstance(
conf_man_id);
494 config_manager.initialize(args);
511 for (
int i = 0; i < argc; ++i) {
513 if (option ==
"--config-file") {
517 if (boost::starts_with(option,
"--config-file=")) {
520 if (option ==
"--plugin-directory") {
521 plugin_options_input.
emplace_back(
"--plugin-directory");
524 if (boost::starts_with(option,
"--plugin-directory=")) {
527 if (option ==
"--plugin") {
531 if (boost::starts_with(option,
"--plugin=")) {
544 if (local_env[
"ELEMENTS_CONF_PATH"].empty()) {
545 local_env[
"ELEMENTS_CONF_PATH"] =
".:/etc";
547 local_env[
"ELEMENTS_CONF_PATH"] =
".:" + local_env[
"ELEMENTS_CONF_PATH"] +
":/etc";
564 plugin_options_input.emplace_back(
"--log-level");
565 plugin_options_input.emplace_back(
"ERROR");
568 int argc_tmp = plugin_options_input.size();
570 for (
unsigned int i = 0; i < plugin_options_input.size(); ++i) {
571 auto& option_str = plugin_options_input[i];
572 argv_tmp[i] = option_str.
data();
576 plugin_options_program.run(argc_tmp,
const_cast<char **
>(argv_tmp.
data()));
void error(const std::string &logMessage)
void debug(const std::string &logMessage)
static Logging getLogger(const std::string &name="")
void fatal(const std::string &logMessage)
void info(const std::string &logMessage)
static void onTerminate() noexcept
virtual void handleMessage(const std::shared_ptr< SourceGroupInterface > &group) override
std::list< std::shared_ptr< SourceGroupInterface > > m_list
boost::program_options::options_description defineSpecificProgramOptions() override
Elements::ExitCode mainMethod(std::map< std::string, boost::program_options::variable_value > &args) override
std::string & m_plugin_path
virtual ~PluginOptionsMain()=default
PluginOptionsMain(std::string &plugin_path, std::vector< std::string > &plugin_list)
std::vector< std::string > & m_plugin_list
DeblendingFactory deblending_factory
std::shared_ptr< OutputRegistry > output_registry
std::pair< po::options_description, po::positional_options_description > defineProgramArguments() override
Return the arguments that the program accepts.
po::options_description getConfigParameters()
PartitionFactory partition_factory
SegmentationFactory segmentation_factory
void printDefaults()
Print a configuration file populated with defaults.
std::shared_ptr< SourceGroupFactory > group_factory
ProgressReporterFactory progress_printer_factory
GroupingFactory grouping_factory
std::shared_ptr< SourceFactory > source_factory
std::shared_ptr< TaskFactoryRegistry > task_factory_registry
SEMain(const std::string &plugin_path, const std::vector< std::string > &plugin_list)
OutputFactory output_factory
Elements::ExitCode mainMethod(std::map< std::string, po::variable_value > &args) override
static void writeDefaultMultiple(std::ostream &out, const po::option_description &opt, const boost::any &default_value)
Print a multiple-value option.
static void writeDefault(std::ostream &out, const po::option_description &opt, const boost::any &default_value)
Print a simple option.
MeasurementFactory measurement_factory
PluginManager plugin_manager
std::shared_ptr< TaskProvider > task_provider
po::options_description config_parameters
std::list< std::shared_ptr< SourceWithOnDemandProperties > > m_list
virtual void handleMessage(const std::shared_ptr< SourceWithOnDemandProperties > &source) override
T emplace_back(T... args)
#define CREATE_MANAGER_WITH_ARGS(MANAGER, ELEMENTS_PROGRAM,...)
std::underlying_type< ExitCode >::type ExitCodeType
long getUniqueManagerId() noexcept
T set_terminate(T... args)