mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-15 09:31:45 +00:00
Add new Send/Receive methods with smart pointers and no flag checks.
This commit is contained in:
committed by
Mohammad Al-Turany
parent
105e734808
commit
a7ab33a10e
@@ -38,14 +38,15 @@ void FairMQBenchmarkSampler::Run()
|
||||
boost::thread resetEventCounter(boost::bind(&FairMQBenchmarkSampler::ResetEventCounter, this));
|
||||
|
||||
void* buffer = operator new[](fEventSize);
|
||||
FairMQMessage* baseMsg = fTransportFactory->CreateMessage(buffer, fEventSize);
|
||||
|
||||
unique_ptr<FairMQMessage> baseMsg(fTransportFactory->CreateMessage(buffer, fEventSize));
|
||||
|
||||
// store the channel reference to avoid traversing the map on every loop iteration
|
||||
const FairMQChannel& dataChannel = fChannels.at("data-out").at(0);
|
||||
|
||||
while (CheckCurrentState(RUNNING))
|
||||
{
|
||||
FairMQMessage* msg = fTransportFactory->CreateMessage();
|
||||
unique_ptr<FairMQMessage> msg(fTransportFactory->CreateMessage());
|
||||
msg->Copy(baseMsg);
|
||||
|
||||
dataChannel.Send(msg);
|
||||
@@ -56,12 +57,8 @@ void FairMQBenchmarkSampler::Run()
|
||||
{
|
||||
boost::this_thread::sleep(boost::posix_time::milliseconds(1));
|
||||
}
|
||||
|
||||
delete msg;
|
||||
}
|
||||
|
||||
delete baseMsg;
|
||||
|
||||
try {
|
||||
resetEventCounter.interrupt();
|
||||
resetEventCounter.join();
|
||||
|
@@ -28,8 +28,7 @@ class FairMQBenchmarkSampler : public FairMQDevice
|
||||
public:
|
||||
enum
|
||||
{
|
||||
InputFile = FairMQDevice::Last,
|
||||
EventSize,
|
||||
EventSize = FairMQDevice::Last,
|
||||
EventRate,
|
||||
Last
|
||||
};
|
||||
|
@@ -32,14 +32,12 @@ void FairMQBuffer::Run()
|
||||
|
||||
while (CheckCurrentState(RUNNING))
|
||||
{
|
||||
FairMQMessage* msg = fTransportFactory->CreateMessage();
|
||||
std::unique_ptr<FairMQMessage> msg(fTransportFactory->CreateMessage());
|
||||
|
||||
if (dataInChannel.Receive(msg) > 0)
|
||||
{
|
||||
dataOutChannel.Send(msg);
|
||||
}
|
||||
|
||||
delete msg;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -29,28 +29,32 @@ FairMQMerger::~FairMQMerger()
|
||||
|
||||
void FairMQMerger::Run()
|
||||
{
|
||||
FairMQPoller* poller = fTransportFactory->CreatePoller(fChannels.at("data-in"));
|
||||
std::unique_ptr<FairMQPoller> poller(fTransportFactory->CreatePoller(fChannels.at("data-in")));
|
||||
|
||||
// store the channel references to avoid traversing the map on every loop iteration
|
||||
const FairMQChannel& dataOutChannel = fChannels.at("data-out").at(0);
|
||||
FairMQChannel* dataInChannels[fChannels.at("data-in").size()];
|
||||
std::vector<FairMQChannel*> dataInChannels(fChannels.at("data-in").size());
|
||||
for (int i = 0; i < fChannels.at("data-in").size(); ++i)
|
||||
{
|
||||
dataInChannels[i] = &(fChannels.at("data-in").at(i));
|
||||
dataInChannels.at(i) = &(fChannels.at("data-in").at(i));
|
||||
}
|
||||
|
||||
while (CheckCurrentState(RUNNING))
|
||||
{
|
||||
FairMQMessage* msg = fTransportFactory->CreateMessage();
|
||||
std::unique_ptr<FairMQMessage> msg(fTransportFactory->CreateMessage());
|
||||
|
||||
poller->Poll(100);
|
||||
|
||||
// Loop over the data input channels.
|
||||
for (int i = 0; i < fChannels.at("data-in").size(); ++i)
|
||||
{
|
||||
// Check if the channel has data ready to be received.
|
||||
if (poller->CheckInput(i))
|
||||
{
|
||||
// Try receiving the data.
|
||||
if (dataInChannels[i]->Receive(msg) > 0)
|
||||
{
|
||||
// If data was received, send it to output.
|
||||
if (dataOutChannel.Send(msg) < 0)
|
||||
{
|
||||
LOG(DEBUG) << "Blocking send interrupted by a command";
|
||||
@@ -64,9 +68,5 @@ void FairMQMerger::Run()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
delete msg;
|
||||
}
|
||||
|
||||
delete poller;
|
||||
}
|
||||
|
@@ -28,7 +28,7 @@ FairMQProxy::~FairMQProxy()
|
||||
|
||||
void FairMQProxy::Run()
|
||||
{
|
||||
FairMQMessage* msg = fTransportFactory->CreateMessage();
|
||||
std::unique_ptr<FairMQMessage> msg(fTransportFactory->CreateMessage());
|
||||
|
||||
// store the channel references to avoid traversing the map on every loop iteration
|
||||
const FairMQChannel& dataInChannel = fChannels.at("data-in").at(0);
|
||||
@@ -41,6 +41,4 @@ void FairMQProxy::Run()
|
||||
dataOutChannel.Send(msg);
|
||||
}
|
||||
}
|
||||
|
||||
delete msg;
|
||||
}
|
||||
|
@@ -29,11 +29,9 @@ void FairMQSink::Run()
|
||||
|
||||
while (CheckCurrentState(RUNNING))
|
||||
{
|
||||
FairMQMessage* msg = fTransportFactory->CreateMessage();
|
||||
std::unique_ptr<FairMQMessage> msg(fTransportFactory->CreateMessage());
|
||||
|
||||
dataChannel.Receive(msg);
|
||||
|
||||
delete msg;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -41,7 +41,7 @@ void FairMQSplitter::Run()
|
||||
|
||||
while (CheckCurrentState(RUNNING))
|
||||
{
|
||||
FairMQMessage* msg = fTransportFactory->CreateMessage();
|
||||
std::unique_ptr<FairMQMessage> msg(fTransportFactory->CreateMessage());
|
||||
|
||||
if (dataInChannel.Receive(msg) > 0)
|
||||
{
|
||||
@@ -52,7 +52,5 @@ void FairMQSplitter::Run()
|
||||
direction = 0;
|
||||
}
|
||||
}
|
||||
|
||||
delete msg;
|
||||
}
|
||||
}
|
||||
|
@@ -71,17 +71,18 @@ class GenericFileSink : public FairMQDevice, public InputPolicy, public OutputPo
|
||||
{
|
||||
int receivedMsg = 0;
|
||||
|
||||
while (GetCurrentState() == RUNNING)
|
||||
{
|
||||
FairMQMessage* msg = fTransportFactory->CreateMessage();
|
||||
// store the channel reference to avoid traversing the map on every loop iteration
|
||||
const FairMQChannel& inputChannel = fChannels["data-in"].at(0);
|
||||
|
||||
if (fChannels["data-in"].at(0).Receive(msg) > 0)
|
||||
while (CheckCurrentState(RUNNING))
|
||||
{
|
||||
std::unique_ptr<FairMQMessage> msg(fTransportFactory->CreateMessage());
|
||||
|
||||
if (inputChannel.Receive(msg) > 0)
|
||||
{
|
||||
OutputPolicy::AddToFile(InputPolicy::DeSerializeMsg(msg));
|
||||
OutputPolicy::AddToFile(InputPolicy::DeSerializeMsg(msg.get()));
|
||||
receivedMsg++;
|
||||
}
|
||||
|
||||
delete msg;
|
||||
}
|
||||
|
||||
MQLOG(INFO) << "Received " << receivedMsg << " messages!";
|
||||
|
@@ -44,15 +44,18 @@ void GenericFileSink<InputPolicy, OutputPolicy>::Run()
|
||||
{
|
||||
int receivedMsg = 0;
|
||||
|
||||
// store the channel reference to avoid traversing the map on every loop iteration
|
||||
const FairMQChannel& inputChannel = fChannels["data-in"].at(0);
|
||||
|
||||
while (CheckCurrentState(RUNNING))
|
||||
{
|
||||
FairMQMessage* msg = fTransportFactory->CreateMessage();
|
||||
if (fChannels.at("data-in").at(0).Receive(msg) > 0)
|
||||
std::unique_ptr<FairMQMessage> msg(fTransportFactory->CreateMessage());
|
||||
|
||||
if (inputChannel.Receive(msg) > 0)
|
||||
{
|
||||
OutputPolicy::AddToFile(InputPolicy::DeSerializeMsg(msg));
|
||||
OutputPolicy::AddToFile(InputPolicy::DeSerializeMsg(msg.get()));
|
||||
receivedMsg++;
|
||||
}
|
||||
delete msg;
|
||||
}
|
||||
|
||||
MQLOG(INFO) << "Received " << receivedMsg << " messages!";
|
||||
|
@@ -38,22 +38,25 @@ class GenericMerger : public FairMQDevice, public MergerPolicy, public InputPoli
|
||||
|
||||
virtual void Run()
|
||||
{
|
||||
FairMQPoller* poller = fTransportFactory->CreatePoller(fChannels["data-in"]);
|
||||
std::unique_ptr<FairMQPoller> poller(fTransportFactory->CreatePoller(fChannels["data-in"]));
|
||||
|
||||
int received = 0;
|
||||
|
||||
while (GetCurrentState() == RUNNING)
|
||||
{
|
||||
FairMQMessage* msg = fTransportFactory->CreateMessage();
|
||||
std::unique_ptr<FairMQMessage> msg(fTransportFactory->CreateMessage());
|
||||
// MergerPolicy::
|
||||
poller->Poll(fBlockingTime);
|
||||
|
||||
for (int i = 0; i < fChannels["datain"].size(); i++)
|
||||
for (int i = 0; i < fChannels.at("data-in").size(); i++)
|
||||
{
|
||||
if (poller->CheckInput(i))
|
||||
{
|
||||
received = fChannels["data-in"].at(i).Receive(msg)
|
||||
MergerPolicy::Merge(InputPolicy::DeSerializeMsg(msg));
|
||||
received = fChannels.at("data-in").at(i).Receive(msg)
|
||||
if (received > 0)
|
||||
{
|
||||
MergerPolicy::Merge(InputPolicy::DeSerializeMsg(msg));
|
||||
}
|
||||
}
|
||||
|
||||
OutputPolicy::SetMessage(msg);
|
||||
@@ -64,11 +67,7 @@ class GenericMerger : public FairMQDevice, public MergerPolicy, public InputPoli
|
||||
received = 0;
|
||||
}
|
||||
}
|
||||
|
||||
delete msg;
|
||||
}
|
||||
|
||||
delete poller;
|
||||
}
|
||||
};
|
||||
|
||||
|
@@ -120,31 +120,29 @@ class GenericProcessor : public FairMQDevice, public InputPolicy, public OutputP
|
||||
int receivedMsgs = 0;
|
||||
int sentMsgs = 0;
|
||||
|
||||
const FairMQChannel& inputChannel = fChannels["data-in"].at(0);
|
||||
const FairMQChannel& outputChannel = fChannels["data-out"].at(0);
|
||||
|
||||
while (CheckCurrentState(RUNNING))
|
||||
{
|
||||
FairMQMessage* msg = fTransportFactory->CreateMessage();
|
||||
std::unique_ptr<FairMQMessage> msg(fTransportFactory->CreateMessage());
|
||||
|
||||
++receivedMsgs;
|
||||
|
||||
if (fChannels["data-in"].at(0).Receive(msg) > 0)
|
||||
if (inputChannel.Receive(msg) > 0)
|
||||
{
|
||||
// InputPolicy::DeSerializeMsg(msg) --> deserialize data of msg and fill output container
|
||||
// TaskPolicy::ExecuteTask( ... ) --> process output container
|
||||
TaskPolicy::ExecuteTask(InputPolicy::DeSerializeMsg(msg));
|
||||
TaskPolicy::ExecuteTask(InputPolicy::DeSerializeMsg(msg.get()));
|
||||
|
||||
// OutputPolicy::fMessage point to msg
|
||||
OutputPolicy::SetMessage(msg);
|
||||
OutputPolicy::SetMessage(msg.get());
|
||||
|
||||
// TaskPolicy::GetOutputData() --> Get processed output container
|
||||
// OutputPolicy::message(...) --> Serialize output container and fill fMessage
|
||||
fChannels["data-out"].at(0).Send(OutputPolicy::SerializeMsg(TaskPolicy::GetOutputData()));
|
||||
outputChannel.Send(OutputPolicy::SerializeMsg(TaskPolicy::GetOutputData()));
|
||||
sentMsgs++;
|
||||
}
|
||||
|
||||
if (msg)
|
||||
{
|
||||
msg->CloseMessage();
|
||||
}
|
||||
}
|
||||
|
||||
MQLOG(INFO) << "Received " << receivedMsgs << " and sent " << sentMsgs << " messages!";
|
||||
|
@@ -13,7 +13,7 @@
|
||||
*/
|
||||
|
||||
#ifndef GENERICSAMPLER_H
|
||||
#define GENERICSAMPLER_H
|
||||
#define GENERICSAMPLER_H
|
||||
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
@@ -62,7 +62,7 @@ class base_GenericSampler : public FairMQDevice, public T, public U
|
||||
typedef K key_type;
|
||||
typedef L task_type;
|
||||
typedef base_GenericSampler<T,U,K,L> self_type;
|
||||
|
||||
|
||||
public:
|
||||
enum
|
||||
{
|
||||
@@ -82,7 +82,6 @@ class base_GenericSampler : public FairMQDevice, public T, public U
|
||||
typedef source_type source_type;
|
||||
typedef serialization_type serialization_type;
|
||||
};
|
||||
|
||||
*/
|
||||
|
||||
virtual void SetTransport(FairMQTransportFactory* factory);
|
||||
@@ -98,14 +97,12 @@ class base_GenericSampler : public FairMQDevice, public T, public U
|
||||
virtual int GetProperty(const int key, const int default_ = 0);
|
||||
virtual void SetProperty(const int key, const std::string& value);
|
||||
virtual std::string GetProperty(const int key, const std::string& default_ = "");
|
||||
|
||||
|
||||
void SendPart(int socketIdx);
|
||||
int GetSocketNumber() const;
|
||||
int GetCurrentIndex() const;
|
||||
void SetContinuous(bool flag);
|
||||
|
||||
|
||||
/// ///////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/*
|
||||
register the tasks you want to process and, which will be
|
||||
called by ExecuteTasks() member function. The registration is done by filling
|
||||
@@ -130,29 +127,27 @@ class base_GenericSampler : public FairMQDevice, public T, public U
|
||||
|
||||
To communicate with the Host derived class via callback, three methods from the host class are callable (only
|
||||
after binding these methods in the GenericSampler<I,O>::InitTask() )
|
||||
|
||||
*/
|
||||
template<typename RegistrationManager>
|
||||
void RegisterTask(RegistrationManager manage)
|
||||
{
|
||||
manage(this,fTaskList);
|
||||
LOG(DEBUG)<<"Current Number of registered tasks = "<<fTaskList.size();
|
||||
manage(this, fTaskList);
|
||||
LOG(DEBUG) << "Current Number of registered tasks = " << fTaskList.size();
|
||||
}
|
||||
/// ///////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void ExecuteTasks()
|
||||
{
|
||||
for(const auto& p : fTaskList)
|
||||
{
|
||||
LOG(DEBUG)<<"Execute Task "<< p.first;
|
||||
LOG(DEBUG) << "Execute Task " << p.first;
|
||||
p.second();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
virtual void InitTask();
|
||||
virtual void Run();
|
||||
|
||||
|
||||
private:
|
||||
std::string fOutChanName;
|
||||
int64_t fNumEvents;
|
||||
@@ -160,30 +155,29 @@ class base_GenericSampler : public FairMQDevice, public T, public U
|
||||
int fEventRate;
|
||||
int fEventCounter;
|
||||
bool fContinuous;
|
||||
std::map<key_type, task_type > fTaskList; // to handle Task list
|
||||
|
||||
|
||||
std::map<key_type, task_type> fTaskList; // to handle Task list
|
||||
|
||||
// automatically enable or disable the call of policy function members for binding of host functions.
|
||||
// this template functions use SFINAE to detect the existence of the policy function signature.
|
||||
template<typename S = source_type ,FairMQ::tools::enable_if_hasNot_BindSendPart<S> = 0 >
|
||||
void BindingSendPart(){}
|
||||
template<typename S = source_type ,FairMQ::tools::enable_if_has_BindSendPart<S> = 0 >
|
||||
template<typename S = source_type,FairMQ::tools::enable_if_hasNot_BindSendPart<S> = 0>
|
||||
void BindingSendPart() {}
|
||||
template<typename S = source_type,FairMQ::tools::enable_if_has_BindSendPart<S> = 0>
|
||||
void BindingSendPart()
|
||||
{
|
||||
source_type::BindSendPart(std::bind(&base_GenericSampler::SendPart,this,std::placeholders::_1) );
|
||||
}
|
||||
|
||||
template<typename S = source_type ,FairMQ::tools::enable_if_hasNot_BindGetSocketNumber<S> = 0 >
|
||||
void BindingGetSocketNumber(){}
|
||||
template<typename S = source_type ,FairMQ::tools::enable_if_has_BindGetSocketNumber<S> = 0 >
|
||||
|
||||
template<typename S = source_type,FairMQ::tools::enable_if_hasNot_BindGetSocketNumber<S> = 0>
|
||||
void BindingGetSocketNumber() {}
|
||||
template<typename S = source_type,FairMQ::tools::enable_if_has_BindGetSocketNumber<S> = 0>
|
||||
void BindingGetSocketNumber()
|
||||
{
|
||||
source_type::BindGetSocketNumber(std::bind(&base_GenericSampler::GetSocketNumber,this) );
|
||||
}
|
||||
|
||||
template<typename S = source_type ,FairMQ::tools::enable_if_hasNot_BindGetCurrentIndex<S> = 0 >
|
||||
void BindingGetCurrentIndex(){}
|
||||
template<typename S = source_type ,FairMQ::tools::enable_if_has_BindGetCurrentIndex<S> = 0 >
|
||||
|
||||
template<typename S = source_type,FairMQ::tools::enable_if_hasNot_BindGetCurrentIndex<S> = 0>
|
||||
void BindingGetCurrentIndex() {}
|
||||
template<typename S = source_type,FairMQ::tools::enable_if_has_BindGetCurrentIndex<S> = 0>
|
||||
void BindingGetCurrentIndex()
|
||||
{
|
||||
source_type::BindGetCurrentIndex(std::bind(&base_GenericSampler::GetCurrentIndex,this) );
|
||||
@@ -193,4 +187,3 @@ class base_GenericSampler : public FairMQDevice, public T, public U
|
||||
#include "GenericSampler.tpl"
|
||||
|
||||
#endif /* GENERICSAMPLER_H */
|
||||
|
||||
|
@@ -33,21 +33,18 @@ void base_GenericSampler<T,U,K,L>::InitTask()
|
||||
BindingSendPart();
|
||||
BindingGetSocketNumber();
|
||||
BindingGetCurrentIndex();
|
||||
|
||||
|
||||
source_type::InitSampler();
|
||||
fNumEvents = source_type::GetNumberOfEvent();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
template <typename T, typename U, typename K, typename L>
|
||||
void base_GenericSampler<T,U,K,L>::Run()
|
||||
{
|
||||
// boost::thread resetEventCounter(boost::bind(&GenericSampler::ResetEventCounter, this));
|
||||
|
||||
int sentMsgs = 0;
|
||||
|
||||
|
||||
boost::timer::auto_cpu_timer timer;
|
||||
|
||||
LOG(INFO) << "Number of events to process: " << fNumEvents;
|
||||
@@ -56,32 +53,36 @@ void base_GenericSampler<T,U,K,L>::Run()
|
||||
{
|
||||
for (fCurrentIdx = 0; fCurrentIdx < fNumEvents; fCurrentIdx++)
|
||||
{
|
||||
for(auto& p : fChannels[fOutChanName])
|
||||
for (auto& p : fChannels[fOutChanName])
|
||||
{
|
||||
FairMQMessage* msg = fTransportFactory->CreateMessage();
|
||||
serialization_type::SetMessage(msg);
|
||||
std::unique_ptr<FairMQMessage> msg(fTransportFactory->CreateMessage());
|
||||
serialization_type::SetMessage(msg.get());
|
||||
source_type::SetIndex(fCurrentIdx);
|
||||
ExecuteTasks();
|
||||
p.Send(serialization_type::SerializeMsg(source_type::GetOutData()));
|
||||
if (msg)
|
||||
msg->CloseMessage();
|
||||
sentMsgs++;
|
||||
|
||||
if(fChannels[fOutChanName].size()>1)
|
||||
if (fChannels[fOutChanName].size() > 1)
|
||||
{
|
||||
fCurrentIdx++;
|
||||
|
||||
}
|
||||
|
||||
// Optional event rate limiting
|
||||
// --fEventCounter;
|
||||
// while (fEventCounter == 0) {
|
||||
// boost::this_thread::sleep(boost::posix_time::milliseconds(1));
|
||||
// }
|
||||
|
||||
|
||||
if (!CheckCurrentState(RUNNING))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
// if more than one socket, remove the last incrementation
|
||||
if(fChannels[fOutChanName].size()>1)
|
||||
if (fChannels[fOutChanName].size() > 1)
|
||||
{
|
||||
fCurrentIdx--;
|
||||
}
|
||||
}
|
||||
}
|
||||
while (CheckCurrentState(RUNNING) && fContinuous);
|
||||
@@ -96,14 +97,12 @@ template <typename T, typename U, typename K, typename L>
|
||||
void base_GenericSampler<T,U,K,L>::SendPart(int socketIdx)
|
||||
{
|
||||
fCurrentIdx++;
|
||||
if(fCurrentIdx<fNumEvents)
|
||||
if (fCurrentIdx < fNumEvents)
|
||||
{
|
||||
FairMQMessage* msg = fTransportFactory->CreateMessage();
|
||||
serialization_type::SetMessage(msg);
|
||||
std::unique_ptr<FairMQMessage> msg(fTransportFactory->CreateMessage());
|
||||
serialization_type::SetMessage(msg.get());
|
||||
source_type::SetIndex(fCurrentIdx);
|
||||
fChannels[fOutChanName].at(socketIdx).Send(serialization_type::SerializeMsg(source_type::GetOutData()), "snd-more");
|
||||
if (msg)
|
||||
msg->CloseMessage();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,7 +128,7 @@ void base_GenericSampler<T,U,K,L>::SetContinuous(bool flag)
|
||||
template <typename T, typename U, typename K, typename L>
|
||||
void base_GenericSampler<T,U,K,L>::ResetEventCounter()
|
||||
{
|
||||
while (GetCurrentState() == RUNNING)
|
||||
while (CheckCurrentState(RUNNING))
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -197,8 +196,6 @@ std::string base_GenericSampler<T,U,K,L>::GetProperty(const int key, const std::
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<typename T, typename U>
|
||||
using GenericSampler = base_GenericSampler<T,U,int,std::function<void()> >;
|
||||
typedef std::map<int, std::function<void()> > SamplerTasksMap;
|
||||
|
Reference in New Issue
Block a user