30#include <boost/program_options.hpp>
31#include <boost/algorithm/string/predicate.hpp>
85namespace po = boost::program_options;
86namespace fs = boost::filesystem;
126 ::setenv(
"LC_ALL",
"C", 1);
137 bool omp_env_present =
getenv(
"OMP_NUM_THREADS") ||
getenv(
"OMP_DYNAMIC");
138 bool mkl_env_present =
getenv(
"MKL_NUM_THREADS") ||
getenv(
"MKL_DYNAMIC");
139 if (!omp_env_present && !mkl_env_present) {
141 void (*set_num_threads)(int) =
reinterpret_cast<void (*)(
int)
>(dlsym(RTLD_DEFAULT,
"MKL_Set_Num_Threads"));
142 void (*set_dynamic)(int) =
reinterpret_cast<void (*)(
int)
>(dlsym(RTLD_DEFAULT,
"MKL_Set_Dynamic"));
143 if (set_num_threads) {
148 logger.
debug() <<
"Disabling dynamic multithreading";
163 std::make_shared<SourceWithOnDemandPropertiesFactory>(
task_provider);
165 std::make_shared<SourceGroupWithOnDemandPropertiesFactory>(
task_provider);
218 "List the possible output properties for the given input parameters and exit");
220 "Show the columns created for each property");
222 "Show the columns created for each property, for the given configuration");
224 "Dump parameters with default values into a configuration file");
228 po::positional_options_description p;
229 p.add(
"python-arg", -1);
235 template <
typename T>
237 out << opt.long_name() <<
'=' << boost::any_cast<T>(default_value) <<
std::endl;
241 template <
typename T>
243 auto values = boost::any_cast<std::vector<T>>(default_value);
244 if (values.empty()) {
245 out <<
"# " << opt.long_name() <<
'=' <<
std::endl;
248 for (
const auto& v : values)
249 out << opt.long_name() <<
'=' << v <<
std::endl;
257 {
typeid(bool), &writeDefault<bool>},
258 {
typeid(int), &writeDefault<int>},
259 {
typeid(double), &writeDefault<double>},
263 decltype(printers)::const_iterator printer;
267 boost::any default_value;
270 if (!p->semantic()->apply_default(default_value)) {
273 else if ((printer = printers.find(default_value.type())) == printers.end()) {
274 std::cout <<
'#' << p->long_name() <<
"=<Unknown type " << default_value.type().name() <<
'>' <<
std::endl;
277 printer->second(
std::cout, *p, default_value);
298 return Elements::ExitCode::OK;
303 return Elements::ExitCode::OK;
308 return Elements::ExitCode::OK;
316 if (args.
find(
"config-file") != args.
end()) {
317 auto cfg_file = args.
at(
"config-file").as<fs::path>();
318 if (cfg_file !=
"" && !fs::exists(cfg_file)) {
319 throw Elements::Exception() <<
"The configuration file '" << cfg_file <<
"' does not exist";
329 config_manager.initialize(args);
332 auto memory_config = config_manager.getConfiguration<
MemoryConfig>();
334 memory_config.getTileSize(), memory_config.getTileMaxMemory());
350 return Elements::ExitCode::OK;
370 auto prefetch = source_grouping->requiredProperties();
371 auto deblending_prefetch = deblending->requiredProperties();
372 prefetch.insert(deblending_prefetch.begin(), deblending_prefetch.end());
373 if (!prefetch.empty()) {
374 prefetcher = std::make_shared<Prefetcher>(thread_pool, multithreading_config.
getMaxQueueSize());
375 prefetcher->requestProperties(prefetch);
384 prefetcher->setNextStage(source_grouping);
387 partition->setNextStage(source_grouping);
390 source_grouping->setNextStage(deblending);
391 deblending->setNextStage(measurement);
394 logger.
info() <<
"Writing output following measure order";
395 measurement->setNextStage(output);
397 logger.
info() <<
"Writing output following segmentation order";
398 auto sorter = std::make_shared<Sorter>();
399 measurement->setNextStage(sorter);
400 sorter->setNextStage(output);
403 segmentation->Observable<SegmentationProgress>::addObserver(progress_mediator->getSegmentationObserver());
404 segmentation->Observable<SourceInterface>::addObserver(progress_mediator->getDetectionObserver());
405 deblending->Observable<SourceGroupInterface>::addObserver(progress_mediator->getDeblendingObserver());
406 measurement->Observable<SourceGroupInterface>::addObserver(progress_mediator->getMeasurementObserver());
410 segmentation->Observable<SourceInterface>::addObserver(std::make_shared<DetectionIdCheckImage>());
413 measurement->Observable<SourceGroupInterface>::addObserver(std::make_shared<SourceIdCheckImage>());
416 measurement->Observable<SourceGroupInterface>::addObserver(std::make_shared<GroupIdCheckImage>());
419 measurement->Observable<SourceGroupInterface>::addObserver(std::make_shared<MoffatCheckImage>());
421 const auto& detection_frames = config_manager.getConfiguration<
DetectionFrameConfig>().getDetectionFrames();
424 measurement->startThreads();
426 size_t prev_writen_rows = 0;
427 size_t frame_number = 0;
428 for (
auto& detection_frame : detection_frames) {
433 << frame_number <<
" / " << detection_frames.size() <<
" : " << detection_frame->getLabel();
434 segmentation->processFrame(detection_frame);
437 logger.
error() <<
"Failed to process the frame! " <<
e.what();
438 measurement->stopThreads();
439 return Elements::ExitCode::NOT_OK;
443 prefetcher->synchronize();
445 measurement->synchronizeThreads();
447 size_t nb_writen_rows = output->flush();
450 logger.
info() << (nb_writen_rows - prev_writen_rows) <<
" sources detected in frame, " << nb_writen_rows <<
" total";
452 prev_writen_rows = nb_writen_rows;
458 measurement->stopThreads();
462 progress_mediator->done();
464 if (prev_writen_rows > 0) {
465 logger.
info() <<
"total " << prev_writen_rows <<
" sources detected";
470 return Elements::ExitCode::OK;
487 auto options = config_manager.closeRegistration();
489 options.add_options()(
"*", po::value<std::vector<std::string>>());
495 config_manager.initialize(args);
499 return Elements::ExitCode::OK;
512 for (
int i = 0; i < argc; ++i) {
514 if (option ==
"--config-file") {
518 if (boost::starts_with(option,
"--config-file=")) {
521 if (option ==
"--plugin-directory") {
522 plugin_options_input.
emplace_back(
"--plugin-directory");
525 if (boost::starts_with(option,
"--plugin-directory=")) {
528 if (option ==
"--plugin") {
532 if (boost::starts_with(option,
"--plugin=")) {
545 if (local_env[
"ELEMENTS_CONF_PATH"].empty()) {
546 local_env[
"ELEMENTS_CONF_PATH"] =
".:/etc";
548 local_env[
"ELEMENTS_CONF_PATH"] =
".:" + local_env[
"ELEMENTS_CONF_PATH"] +
":/etc";
565 plugin_options_input.emplace_back(
"--log-level");
566 plugin_options_input.emplace_back(
"ERROR");
569 int argc_tmp = plugin_options_input.size();
571 for (
unsigned int i = 0; i < plugin_options_input.size(); ++i) {
572 auto& option_str = plugin_options_input[i];
573 argv_tmp[i] = option_str.
data();
577 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
static ConfigManager & getInstance(long id)
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)