-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathO2PrimaryServerDevice.h
More file actions
289 lines (246 loc) · 9.07 KB
/
O2PrimaryServerDevice.h
File metadata and controls
289 lines (246 loc) · 9.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// @author Sandro Wenzel
#ifndef O2_DEVICES_PRIMSERVDEVICE_H_
#define O2_DEVICES_PRIMSERVDEVICE_H_
#include <FairMQDevice.h>
#include <FairPrimaryGenerator.h>
#include <Generators/GeneratorFactory.h>
#include <FairMQMessage.h>
#include <SimulationDataFormat/Stack.h>
#include <SimulationDataFormat/MCEventHeader.h>
#include <TMessage.h>
#include <TClass.h>
#include <SimulationDataFormat/PrimaryChunk.h>
#include <Generators/GeneratorFromFile.h>
#include <Generators/PrimaryGenerator.h>
#include <SimConfig/SimConfig.h>
#include <CommonUtils/ConfigurableParam.h>
#include <CommonUtils/RngHelper.h>
#include <typeinfo>
#include <thread>
#include <TROOT.h>
#include <TStopwatch.h>
namespace o2
{
namespace devices
{
class O2PrimaryServerDevice final : public FairMQDevice
{
public:
/// Default constructor
O2PrimaryServerDevice()
{
mStack.setExternalMode(true);
}
/// Default destructor
~O2PrimaryServerDevice() final
{
if (mGeneratorThread.joinable()) {
mGeneratorThread.join();
}
}
protected:
void initGenerator()
{
TStopwatch timer;
timer.Start();
auto& conf = o2::conf::SimConfig::Instance();
o2::eventgen::GeneratorFactory::setPrimaryGenerator(conf, &mPrimGen);
mPrimGen.SetEvent(&mEventHeader);
auto embedinto_filename = conf.getEmbedIntoFileName();
if (!embedinto_filename.empty()) {
mPrimGen.embedInto(embedinto_filename);
}
mPrimGen.Init();
LOG(INFO) << "Generator initialization took " << timer.CpuTime() << "s";
generateEvent(); // generate a first event
}
// function generating one event
void generateEvent()
{
TStopwatch timer;
timer.Start();
mStack.Reset();
mPrimGen.GenerateEvent(&mStack);
timer.Stop();
LOG(INFO) << "Event generation took " << timer.CpuTime() << "s";
}
void InitTask() final
{
LOG(INFO) << "Init Server device ";
// init sim config
auto& conf = o2::conf::SimConfig::Instance();
auto& vm = GetConfig()->GetVarMap();
conf.resetFromParsedMap(vm);
// output varmap
for (auto& keyvalue : vm) {
LOG(INFO) << "///// " << keyvalue.first << " " << keyvalue.second.value().type().name();
}
// update the parameters from an INI/JSON file, if given (overrides code-based version)
o2::conf::ConfigurableParam::updateFromFile(conf.getConfigFile());
// update the parameters from stuff given at command line (overrides file-based version)
o2::conf::ConfigurableParam::updateFromString(conf.getKeyValueString());
// MC ENGINE
LOG(INFO) << "ENGINE SET TO " << vm["mcEngine"].as<std::string>();
// CHUNK SIZE
mChunkGranularity = vm["chunkSize"].as<unsigned int>();
LOG(INFO) << "CHUNK SIZE SET TO " << mChunkGranularity;
// initial initial seed --> we should store this somewhere
mInitialSeed = vm["seed"].as<int>();
mInitialSeed = o2::utils::RngHelper::setGRandomSeed(mInitialSeed);
LOG(INFO) << "RNG INITIAL SEED " << mInitialSeed;
mMaxEvents = conf.getNEvents();
// need to make ROOT thread-safe since we use ROOT services in all places
ROOT::EnableThreadSafety();
// lunch initialization of particle generator asynchronously
// so that we reach the RUNNING state of the server quickly
// and do not block here
mGeneratorThread = std::thread(&O2PrimaryServerDevice::initGenerator, this);
// init pipe
auto pipeenv = getenv("ALICE_O2SIMSERVERTODRIVER_PIPE");
if (pipeenv) {
mPipeToDriver = atoi(pipeenv);
LOG(INFO) << "ASSIGNED PIPE HANDLE " << mPipeToDriver;
} else {
LOG(INFO) << "DID NOT FIND ENVIRONMENT VARIABLE TO INIT PIPE";
}
}
// method reacting to requests to get the simulation configuration
bool HandleConfigRequest(FairMQMessagePtr& request)
{
LOG(INFO) << "received config request";
// just sending the simulation configuration to anyone that wants it
auto& conf = o2::conf::SimConfig::Instance();
const auto& confdata = conf.getConfigData();
TMessage* tmsg = new TMessage(kMESS_OBJECT);
tmsg->WriteObjectAny((void*)&confdata, TClass::GetClass(typeid(confdata)));
auto free_tmessage = [](void* data, void* hint) { delete static_cast<TMessage*>(hint); };
std::unique_ptr<FairMQMessage> message(
fTransportFactory->CreateMessage(tmsg->Buffer(), tmsg->BufferSize(), free_tmessage, tmsg));
// send answer
if (Send(message, "primary-get", 0) > 0) {
LOG(INFO) << "config reply send ";
return true;
}
return true;
}
bool ConditionalRun() override
{
auto& channel = fChannels.at("primary-get").at(0);
std::unique_ptr<FairMQMessage> request(channel.NewMessage());
auto bytes = channel.Receive(request);
if (bytes < 0) {
LOG(ERROR) << "Some error occurred on socket during receive";
return true; // keep going
}
return HandleRequest(request, 0);
}
/// Overloads the ConditionalRun() method of FairMQDevice
bool HandleRequest(FairMQMessagePtr& request, int /*index*/)
{
LOG(INFO) << "GOT A REQUEST WITH SIZE " << request->GetSize();
std::string requeststring(static_cast<char*>(request->GetData()), request->GetSize());
if (requeststring.compare("configrequest") == 0) {
return HandleConfigRequest(request);
}
else if (requeststring.compare("primrequest") != 0) {
LOG(INFO) << "unknown request\n";
return true;
}
static int counter = 0;
if (counter >= mMaxEvents && mNeedNewEvent) {
return false;
}
LOG(INFO) << "Received request for work ";
if (mNeedNewEvent) {
// we need a newly generated event now
if (mGeneratorThread.joinable()) {
mGeneratorThread.join();
}
mNeedNewEvent = false;
mPartCounter = 0;
counter++;
}
auto& prims = mStack.getPrimaries();
auto numberofparts = (int)std::ceil(prims.size() / (1. * mChunkGranularity));
// number of parts should be at least 1 (even if empty)
numberofparts = std::max(1, numberofparts);
o2::data::PrimaryChunk m;
o2::data::SubEventInfo i;
i.eventID = counter;
i.maxEvents = mMaxEvents;
i.part = mPartCounter + 1;
i.nparts = numberofparts;
i.seed = counter + mInitialSeed;
i.index = m.mParticles.size();
i.mMCEventHeader = mEventHeader;
m.mSubEventInfo = i;
int endindex = prims.size() - mPartCounter * mChunkGranularity;
int startindex = prims.size() - (mPartCounter + 1) * mChunkGranularity;
if (startindex < 0) {
startindex = 0;
}
if (endindex < 0) {
endindex = 0;
}
for (int index = startindex; index < endindex; ++index) {
m.mParticles.emplace_back(prims[index]);
}
LOG(INFO) << "Sending " << m.mParticles.size() << " particles";
LOG(INFO) << "treating ev " << counter << " part " << i.part << " out of " << i.nparts;
// feedback to driver if new event started
if (mPipeToDriver != -1 && i.part == 1) {
if (write(mPipeToDriver, &counter, sizeof(counter))) {
}
}
mPartCounter++;
if (mPartCounter == numberofparts) {
mNeedNewEvent = true;
// start generation of a new event
mGeneratorThread = std::thread(&O2PrimaryServerDevice::generateEvent, this);
}
TMessage* tmsg = new TMessage(kMESS_OBJECT);
tmsg->WriteObjectAny((void*)&m, TClass::GetClass("o2::data::PrimaryChunk"));
auto free_tmessage = [](void* data, void* hint) { delete static_cast<TMessage*>(hint); };
std::unique_ptr<FairMQMessage> message(
fTransportFactory->CreateMessage(tmsg->Buffer(), tmsg->BufferSize(), free_tmessage, tmsg));
// send answer
TStopwatch timer;
timer.Start();
auto code = Send(message, "primary-get", 0, 5000); // we introduce timeout in order not to block other requests
timer.Stop();
auto time = timer.CpuTime();
if (code > 0) {
LOG(INFO) << "Reply send in " << time << "s";
return true;
} else {
LOG(WARN) << "Sending process had problems. Return code : " << code << " time " << time << "s";
}
return true;
}
private:
std::string mOutChannelName = "";
o2::eventgen::PrimaryGenerator mPrimGen;
o2::dataformats::MCEventHeader mEventHeader;
o2::data::Stack mStack; // the stack which is filled
int mChunkGranularity = 500; // how many primaries to send to a worker
int mLastPosition = 0; // last position in stack vector
int mPartCounter = 0;
bool mNeedNewEvent = true;
int mMaxEvents = 2;
int mInitialSeed = -1;
int mPipeToDriver = -1; // handle for direct piper to driver (to communicate meta info)
std::thread mGeneratorThread; //! a thread used to concurrently init the particle generator
// or to generate events
};
} // namespace devices
} // namespace o2
#endif