Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
LikelihoodGradientJob.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
14
21#include "RooMsgService.h"
22#include "RooMinimizer.h"
23
25#include "Minuit2/MnStrategy.h"
26
27namespace RooFit {
28namespace TestStatistics {
29
30LikelihoodGradientJob::LikelihoodGradientJob(std::shared_ptr<RooAbsL> likelihood,
31 std::shared_ptr<WrapperCalculationCleanFlags> calculation_is_clean,
32 std::size_t N_dim, RooMinimizer *minimizer, SharedOffset offset)
33 : LikelihoodGradientWrapper(std::move(likelihood), std::move(calculation_is_clean), N_dim, minimizer,
34 std::move(offset)),
35 grad_(N_dim),
36 N_tasks_(N_dim)
37{
40}
41
47
53
55{
56 assert(istrat >= 0);
57 ROOT::Minuit2::MnStrategy strategy(static_cast<unsigned int>(istrat));
58
59 setStepTolerance(strategy.GradientStepTolerance());
60 setGradTolerance(strategy.GradientTolerance());
61 setNCycles(strategy.GradientNCycles());
62}
63
68
73
78
83
84///////////////////////////////////////////////////////////////////////////////
85/// Job overrides:
86
91
92// SYNCHRONIZATION FROM WORKERS TO MASTER
93
95{
97 zmq::message_t message(sizeof(task_result_t));
98 memcpy(message.data(), &task_result, sizeof(task_result_t));
99 get_manager()->messenger().send_from_worker_to_master(std::move(message));
100}
101
103{
104 auto result = message.data<task_result_t>();
105 grad_[result->task_id] = result->grad;
108 return job_completed;
109}
110
111// END SYNCHRONIZATION FROM WORKERS TO MASTER
112
113// SYNCHRONIZATION FROM MASTER TO WORKERS (STATE)
114
116{
117 // TODO optimization: only send changed parameters (now sending all)
118 zmq::message_t gradient_message(grad_.begin(), grad_.end());
120 double maxFCN = minimizer_->maxFCN();
121 double fcnOffset = minimizer_->fcnOffset();
122 ++state_id_;
123
125 zmq::message_t offsets_message(shared_offset_.offsets().begin(), shared_offset_.offsets().end());
127 id_, state_id_, isCalculating_, maxFCN, fcnOffset, std::move(gradient_message),
128 std::move(minuit_internal_x_message), std::move(offsets_message));
130 } else {
132 std::move(gradient_message),
133 std::move(minuit_internal_x_message));
134 }
135}
136
142
144{
145 bool more;
146
148 assert(more);
150
151 if (more) {
152 auto maxFCN = get_manager()->messenger().receive_from_master_on_worker<double>(&more);
153 minimizer_->maxFCN() = maxFCN;
154 assert(more);
155
156 auto fcnOffset = get_manager()->messenger().receive_from_master_on_worker<double>(&more);
157 minimizer_->fcnOffset() = fcnOffset;
158 assert(more);
159
161 assert(more);
166
172
173 if (more) {
174 // offsets also incoming
176 assert(!more);
178 std::size_t N_offsets = offsets_message.size() / sizeof(ROOT::Math::KahanSum<double>);
182 }
183
184 // Since the gradient parallelization only support Minuit 2, we can do this cast
186
187 // note: the next call must stay after the (possible) update of the offset, because it
188 // calls the likelihood function, so the offset must be correct at this point
190 minimizer_->fitter()->Config().ParamsSettings());
191 }
192}
193
194// END SYNCHRONIZATION FROM MASTER TO WORKERS (STATE)
195
196///////////////////////////////////////////////////////////////////////////////
197/// Calculation stuff (mostly duplicates of RooGradMinimizerFcn code):
198
200{
201 // Since the gradient parallelization only support Minuit 2, we can do this cast
203
204 // Calculate the derivative etc for these parameters
205 grad_[i_component] = gradf_.FastPartialDerivative(minim.GetFCN(), minimizer_->fitter()->Config().ParamsSettings(),
207}
208
210{
211 if (get_manager()->process_manager().is_master()) {
212 isCalculating_ = true;
214
215 // master fills queue with tasks
216 for (std::size_t ix = 0; ix < N_tasks_; ++ix) {
219 }
221 // wait for task results back from workers to master (put into _grad)
223
224 calculation_is_clean_->gradient = true;
225 isCalculating_ = false;
227 }
228}
229
231{
232 if (get_manager()->process_manager().is_master()) {
233 if (!calculation_is_clean_->gradient) {
235 }
236
237 // put the results from _grad into *grad
238 for (Int_t ix = 0; ix < minimizer_->getNPar(); ++ix) {
239 grad[ix] = grad_[ix].derivative;
240 }
241 }
242}
243
245 double *previous_gstep)
246{
247 if (get_manager()->process_manager().is_master()) {
248 for (std::size_t i_component = 0; i_component < N_tasks_; ++i_component) {
250 }
251
252 if (!calculation_is_clean_->gradient) {
255 }
259 }
260 }
261
262 // put the results from _grad into *grad
263 for (Int_t ix = 0; ix < minimizer_->getNPar(); ++ix) {
264 grad[ix] = grad_[ix].derivative;
265 previous_g2[ix] = grad_[ix].second_derivative;
266 previous_gstep[ix] = grad_[ix].step_size;
267 }
268 }
269}
270
275
277{
278 return true;
279}
280
281} // namespace TestStatistics
282} // namespace RooFit
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
The Kahan summation is a compensated summation algorithm, which significantly reduces numerical error...
Definition Util.h:137
double ErrorDef() const
error definition
Minuit2Minimizer class implementing the ROOT::Math::Minimizer interface for Minuit2 minimization algo...
API class for defining four levels of strategies: low (0), medium (1), high (2), very high (>=3); act...
Definition MnStrategy.h:27
void SetInitialGradient(std::span< const ROOT::Fit::ParameterSettings > parameters, std::vector< DerivatorElement > &gradient)
This function was not implemented as in Minuit2.
void SetNCycles(unsigned int value)
void SetupDifferentiate(unsigned int nDim, const FCNBase *function, const double *cx, std::span< const ROOT::Fit::ParameterSettings > parameters)
This function sets internal state based on input parameters.
DerivatorElement FastPartialDerivative(const FCNBase *function, std::span< const ROOT::Fit::ParameterSettings > parameters, unsigned int i_component, const DerivatorElement &previous)
static bool getTimingAnalysis()
Definition Config.cxx:87
std::size_t id_
Definition Job.h:45
std::size_t state_id_
Definition Job.h:46
JobManager * get_manager()
Get JobManager instance; create and activate if necessary.
Definition Job.cxx:112
void gather_worker_results()
Wait for all tasks to be retrieved for the current Job.
Definition Job.cxx:126
value_t receive_from_master_on_worker(bool *more=nullptr)
Definition Messenger.h:176
void send_from_worker_to_master(T &&item)
specialization that sends the final message
Definition Messenger.h:192
void publish_from_master_to_workers(T &&item)
specialization that sends the final message
Definition Messenger.h:150
static void start_timer(std::string section_name)
static void end_timer(std::string section_name)
virtual void add(JobTask job_task)=0
Enqueue a task.
bool usesMinuitInternalValues() override
Implement usesMinuitInternalValues to return true when you want Minuit to send this class Minuit-inte...
void update_state() override
Virtual function to update any necessary state on workers.
std::vector< ROOT::Minuit2::DerivatorElement > grad_
void fillGradientWithPrevResult(double *grad, double *previous_grad, double *previous_g2, double *previous_gstep) override
void updateMinuitInternalParameterValues(const std::vector< double > &minuit_internal_x) override
Minuit passes in parameter values that may not conform to RooFit internal standards (like applying ra...
void run_derivator(unsigned int i_component) const
Calculation stuff (mostly duplicates of RooGradMinimizerFcn code):
void send_back_task_result_from_worker(std::size_t task) override
void synchronizeWithMinimizer(const ROOT::Math::MinimizerOptions &options) override
Synchronize minimizer settings with calculators in child classes.
LikelihoodGradientJob(std::shared_ptr< RooAbsL > likelihood, std::shared_ptr< WrapperCalculationCleanFlags > calculation_is_clean, std::size_t N_dim, RooMinimizer *minimizer, SharedOffset offset)
void setStepTolerance(double step_tolerance) const
void setGradTolerance(double grad_tolerance) const
void evaluate_task(std::size_t task) override
Job overrides:
bool receive_task_result_on_master(const zmq::message_t &message) override
void synchronizeParameterSettingsImpl(const std::vector< ROOT::Fit::ParameterSettings > &parameter_settings) override
Virtual base class for implementation of likelihood gradient calculation strategies.
std::shared_ptr< WrapperCalculationCleanFlags > calculation_is_clean_
Wrapper class around ROOT::Math::Minimizer that provides a seamless interface between the minimizer f...
auto fitter()
Return underlying ROOT fitter object.
std::unique_ptr< ROOT::Math::Minimizer > _minimizer
! pointer to used minimizer
double & fcnOffset() const
double & maxFCN()
int getNPar() const
OffsetVec & offsets()
std::size_t State
Definition types.h:23
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:67
combined job_object, state and task identifier type
Definition types.h:25