Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
Messenger.h
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 * IP, Inti Pelupessy, Netherlands eScience Center, i.pelupessy@esciencecenter.nl
6 *
7 * Copyright (c) 2021, CERN
8 *
9 * Redistribution and use in source and binary forms,
10 * with or without modification, are permitted according to the terms
11 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
12 */
13#ifndef ROOT_ROOFIT_MultiProcess_Messenger
14#define ROOT_ROOFIT_MultiProcess_Messenger
15
17
18#include <sstream>
19#include <unistd.h> // getpid
20
21#ifdef NDEBUG
22#undef NDEBUG
23#define turn_NDEBUG_back_on
24#endif
25
26namespace RooFit {
27namespace MultiProcess {
28
29// -- WORKER - QUEUE COMMUNICATION --
30
31template <typename T, typename... Ts>
33{
34#ifndef NDEBUG
35 std::stringstream ss;
36 ss << "PID " << getpid() << " sends W2Q " << item;
37 debug_print(ss.str());
38#endif
39
40 send_item(this_worker_qw_, item, /*more=*/false);
42}
43
44template <typename value_t>
46{
48
49#ifndef NDEBUG
50 std::stringstream ss;
51 ss << "PID " << getpid() << " receives W(" << this_worker_id << ")2Q " << value;
52 debug_print(ss.str());
53#endif
54
55 return value;
56}
57
58template <typename T, typename... Ts>
60{
61#ifndef NDEBUG
62 std::stringstream ss;
63 ss << "PID " << getpid() << " sends Q2W(" << this_worker_id << ") " << item;
64 debug_print(ss.str());
65#endif
66
67 send_item(qw_[this_worker_id], item, /*more=*/false);
69}
70
71template <typename value_t>
73{
75
76#ifndef NDEBUG
77 std::stringstream ss;
78 ss << "PID " << getpid() << " receives Q2W " << value;
79 debug_print(ss.str());
80#endif
81
82 return value;
83}
84
85// -- QUEUE - MASTER COMMUNICATION --
86
87template <typename T, typename... Ts>
89{
90#ifndef NDEBUG
91 std::stringstream ss;
92 ss << "PID " << getpid() << " sends Q2M " << item;
93 debug_print(ss.str());
94#endif
95
96 send_item(mq_, item, /*more=*/false);
98}
99
100template <typename value_t>
102{
104
105#ifndef NDEBUG
106 std::stringstream ss;
107 ss << "PID " << getpid() << " receives Q2M " << value;
108 debug_print(ss.str());
109#endif
110
111 return value;
112}
113
114template <typename T, typename... Ts>
116{
117#ifndef NDEBUG
118 std::stringstream ss;
119 ss << "PID " << getpid() << " sends M2Q " << item;
120 debug_print(ss.str());
121#endif
122
123 send_item(mq_, item, /*more=*/false);
125}
126
127template <typename value_t>
129{
131
132#ifndef NDEBUG
133 std::stringstream ss;
134 ss << "PID " << getpid() << " receives M2Q " << value;
135 debug_print(ss.str());
136#endif
137
138 return value;
139}
140
141// -- MASTER - WORKER COMMUNICATION --
142
143/// specialization that sends the final part of a message
144template <typename T>
146{
147#ifndef NDEBUG
148 std::stringstream ss;
149 ss << "PID " << getpid() << " sends M2W " << item;
150 debug_print(ss.str());
151#endif
152
153 for (auto &channel : mw_) {
154 send_item(channel, item, /*more=*/false);
155 }
156}
157
158/// specialization that sends the first parts of multipart messages
159template <typename T, typename T2, typename... Ts>
161{
162#ifndef NDEBUG
163 std::stringstream ss;
164 ss << "PID " << getpid() << " sends M2W " << item;
165 debug_print(ss.str());
166#endif
167
168 for (auto &channel : mw_) {
169 send_item(channel, item, /*more=*/true);
170 }
171 publish_from_master_to_workers(std::forward<T2>(item2), std::forward<Ts>(items)...);
172}
173
174template <typename value_t>
176{
178
179#ifndef NDEBUG
180 std::stringstream ss;
181 ss << "PID " << getpid() << " receives M2W " << value;
182 debug_print(ss.str());
183#endif
184
185 return value;
186}
187
188/// specialization that sends the final part of a message
189template <typename T>
191{
192#ifndef NDEBUG
193 std::stringstream ss;
194 ss << "PID " << getpid() << " sends W2M " << item;
195 debug_print(ss.str());
196#endif
197
198 send_item(this_worker_mw_, item, /*more=*/false);
199}
200
201/// specialization that sends the first parts of multipart messages
202template <typename T, typename T2, typename... Ts>
204{
205#ifndef NDEBUG
206 std::stringstream ss;
207 ss << "PID " << getpid() << " sends W2M " << item;
208 debug_print(ss.str());
209#endif
210
211 send_item(this_worker_mw_, item, /*more=*/true);
212 send_from_worker_to_master(std::forward<T2>(item2), std::forward<Ts>(items)...);
213}
214
215template <typename value_t>
217{
219 bool more_parts = false;
220 auto value = receive_item<value_t>(channel, &more_parts);
222 if (more) {
223 *more = more_parts;
224 }
225
226#ifndef NDEBUG
227 std::stringstream ss;
228 ss << "PID " << getpid() << " receives W2M " << value;
229 debug_print(ss.str());
230#endif
231
232 return value;
233}
234
235} // namespace MultiProcess
236} // namespace RooFit
237
238#ifdef turn_NDEBUG_back_on
239#define NDEBUG
240#undef turn_NDEBUG_back_on
241#endif
242
243#endif // ROOT_ROOFIT_MultiProcess_Messenger
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 value
One endpoint of a full-duplex interprocess message pipe.
Definition Channel.h:55
Channel & select_worker_channel_on_master()
On master: pick the worker channel to receive the next message from.
value_t receive_from_master_on_worker(bool *more=nullptr)
Definition Messenger.h:175
void send_from_worker_to_master(T &&item)
specialization that sends the final part of a message
Definition Messenger.h:190
value_t receive_from_worker_on_master(bool *more=nullptr)
Definition Messenger.h:216
void update_worker_channel_on_master(Channel &channel, bool more)
void debug_print(std::string s)
Function called from send and receive template functions in debug builds used to monitor the messages...
value_t receive_from_worker_on_queue(std::size_t this_worker_id)
Definition Messenger.h:45
void publish_from_master_to_workers(T &&item)
specialization that sends the final part of a message
Definition Messenger.h:145
void send_from_queue_to_worker(std::size_t this_worker_id)
void send_item(Channel &channel, const T &item, bool more)
Definition Channel.h:120
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:73