Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
Config.cxx
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * PB, Patrick Bos, Netherlands eScience Center, p.bos@esciencecenter.nl
5 *
6 * Copyright (c) 2021, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
15#include "PriorityQueue.h"
17
18#include <thread> // std::thread::hardware_concurrency()
19#include <cstdio>
20
21namespace RooFit {
22namespace MultiProcess {
23
24/** \class Config
25 *
26 * \brief Configuration for MultiProcess infrastructure
27 *
28 * This class offers user-accessible configuration of the MultiProcess infrastructure.
29 * Since the rest of the MultiProcess classes are only accessible at compile time, a
30 * separate class is needed to set configuration. Currently, the configurable parts
31 * are:
32 * 1. the number of workers to be deployed,
33 * 2. the number of event-tasks in LikelihoodJobs,
34 * 3. and the number of component-tasks in LikelihoodJobs.
35 *
36 * The default number of workers is set using 'std::thread::hardware_concurrency()'.
37 * To change it, use 'Config::setDefaultNWorkers()' to set it to a different value
38 * before creation of a new JobManager instance. Note that it cannot be set to zero
39 * and also cannot be changed after JobManager has been instantiated.
40 *
41 * Use Config::getDefaultNWorkers() to access the current value.
42 *
43 * Under Config::LikelihoodJob, we find two members for the number of tasks to use to
44 * calculate the range of events and components in parallel, respectively:
45 * defaultNEventTasks and defaultNComponentTasks. Newly created LikelihoodJobs will
46 * then use these values at construction time. Note that (like with the number of
47 * workers) the number cannot be changed for an individual LikelihoodJob after it has
48 * been created.
49 *
50 * Both event- and component-based tasks by default are set to automatic mode using
51 * the automaticNEventTasks and automaticNComponentTasks constants (both under
52 * Config::LikelihoodJob as well). These are currently set to zero, but this could
53 * change. Automatic mode for events means that the number of tasks is set to the
54 * number of workers in the JobManager, with events divided equally over workers. For
55 * components, the automatic mode uses just 1 task for all components. These automatic
56 * modes may change in the future (for instance, we may switch them around).
57 *
58 * Under Config::Queue, we can set the desired queue type: FIFO or Priority. This
59 * setting is used when a JobManager is spun up, i.e. usually when the first Job
60 * starts. At this point, the Queue is also created according to the setting. The
61 * default is FIFO. When using a Priority Queue, the priority of tasks in a Job
62 * can be set using either setTaskPriorities or suggestTaskOrder. If no priorities
63 * are set, the Priority queue simply assumes equal priority for all tasks. The
64 * resulting order then depends on the implementation of std::priority_queue.
65 */
66
67void Config::setDefaultNWorkers(unsigned int N_workers)
68{
70 printf("Warning: Config::setDefaultNWorkers cannot set number of workers after JobManager has been instantiated!\n");
71 } else if (N_workers == 0) {
72 printf("Warning: Config::setDefaultNWorkers cannot set number of workers to zero.\n");
73 } else {
74 defaultNWorkers_ = N_workers;
75 }
76}
77
78void Config::setTimingAnalysis(bool timingAnalysis)
79{
80 if (JobManager::is_instantiated() && JobManager::instance()->process_manager().is_initialized()) {
81 printf("Warning: Config::setTimingAnalysis cannot set logging of timings, forking has already taken place!\n");
82 } else {
83 timingAnalysis_ = timingAnalysis;
84 }
85}
86
88{
89 return timingAnalysis_;
90}
91
93{
94 return defaultNWorkers_;
95}
96
98{
100 printf("Warning: cannot set RooFit::MultiProcess queue type after JobManager has been instantiated!\n");
101 return false;
102 }
103 queueType_ = queueType;
104 return true;
105}
106
108{
109 return queueType_;
110}
111
112/// Set the priority for Job tasks in Priority queue mode.
113///
114/// Only useful in Priority queue mode, in FIFO mode this doesn't do anything.
115/// A higher value means a higher priority.
116///
117/// \param[in] job_id Job ID to set task order for.
118/// \param[in] task_priorities Task priority values, where vector index equals task ID.
119void Config::Queue::setTaskPriorities(std::size_t job_id, const std::vector<std::size_t>& task_priorities)
120{
121 if (queueType_ == QueueType::Priority) {
122 dynamic_cast<PriorityQueue *>(JobManager::instance()->queue())->setTaskPriorities(job_id, task_priorities);
123 }
124}
125
126/// Set the desired order for executing tasks of a Job in Priority queue mode.
127///
128/// Only useful in Priority queue mode, in FIFO mode this doesn't do anything.
129///
130/// Translates the desired order to priorities. Because workers will start
131/// stealing work immediately after it has been queued, the desired order
132/// cannot be guaranteed -- hence "suggest" -- because the first queued task
133/// will possibly be taken before higher priority tasks have been sent to
134/// the queue.
135///
136/// \param[in] job_id Job ID to set task order for.
137/// \param[in] task_order Task IDs in the desired order.
138void Config::Queue::suggestTaskOrder(std::size_t job_id, const std::vector<Task>& task_order)
139{
140 if (queueType_ == QueueType::Priority) {
141 dynamic_cast<PriorityQueue *>(JobManager::instance()->queue())->suggestTaskOrder(job_id, task_order);
142 }
143}
144
145// initialize static members
146unsigned int Config::defaultNWorkers_ = std::thread::hardware_concurrency();
150bool Config::timingAnalysis_ = false;
151
152} // namespace MultiProcess
153} // namespace RooFit
static void setDefaultNWorkers(unsigned int N_workers)
Definition Config.cxx:67
static bool getTimingAnalysis()
Definition Config.cxx:87
static unsigned int defaultNWorkers_
Definition Config.h:51
static unsigned int getDefaultNWorkers()
Definition Config.cxx:92
static bool timingAnalysis_
Definition Config.h:52
static void setTimingAnalysis(bool timingAnalysis)
Definition Config.cxx:78
static JobManager * instance()
Queue that orders tasks according to specified task priorities.
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition JSONIO.h:26
static std::size_t defaultNComponentTasks
Definition Config.h:38
static constexpr std::size_t automaticNEventTasks
Definition Config.h:34
static constexpr std::size_t automaticNComponentTasks
Definition Config.h:35
static void suggestTaskOrder(std::size_t job_id, const std::vector< Task > &task_order)
Set the desired order for executing tasks of a Job in Priority queue mode.
Definition Config.cxx:138
static QueueType getQueueType()
Definition Config.cxx:107
static bool setQueueType(QueueType queueType)
Definition Config.cxx:97
static void setTaskPriorities(std::size_t job_id, const std::vector< std::size_t > &task_priorities)
Set the priority for Job tasks in Priority queue mode.
Definition Config.cxx:119