Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions DataFormats/Headers/include/Headers/Stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,19 @@ namespace header
/// - returns a Stack ready to be shipped.
struct Stack {

private:
static void freefn(void* data, void* hint)
{
boost::container::pmr::memory_resource* resource = static_cast<boost::container::pmr::memory_resource*>(hint);
resource->deallocate(data, 0, 0);
}
using memory_resource = o2::pmr::memory_resource;

private:
struct freeobj {
freeobj() {}
freeobj(boost::container::pmr::memory_resource* mr) : resource(mr) {}

boost::container::pmr::memory_resource* resource{ nullptr };
void operator()(o2::byte* ptr) { Stack::freefn(ptr, resource); }
freeobj(memory_resource* mr) : resource(mr) {}
memory_resource* resource{ nullptr };
void operator()(o2::byte* ptr) { resource->deallocate(ptr, 0, 0); }
};

public:
using allocator_type = boost::container::pmr::polymorphic_allocator<o2::byte>;
using value_type = o2::byte;
using BufferType = std::unique_ptr<value_type[], freeobj>;
using BufferType = std::unique_ptr<value_type[], freeobj>; //this gives us proper default move semantics for free

Stack() = default;
Stack(Stack&&) = default;
Expand All @@ -64,8 +58,6 @@ struct Stack {
allocator_type get_allocator() const { return allocator; }

//
boost::container::pmr::memory_resource* getFreefnHint() const noexcept { return allocator.resource(); }
static auto getFreefn() noexcept { return &freefn; }

/// The magic constructors: take arbitrary number of headers and serialize them
/// into the buffer buffer allocated by the specified polymorphic allocator. By default
Expand All @@ -86,15 +78,15 @@ struct Stack {
: allocator{ allocatorArg },
bufferSize{ calculateSize(std::forward<Headers>(headers)...) },
buffer{ static_cast<o2::byte*>(allocator.resource()->allocate(bufferSize, alignof(std::max_align_t))),
freeobj(getFreefnHint()) }
freeobj(allocator.resource()) }
{
inject(buffer.get(), std::forward<Headers>(headers)...);
}

private:
allocator_type allocator{ boost::container::pmr::new_delete_resource() };
size_t bufferSize{ 0 };
BufferType buffer{ nullptr, freeobj{ getFreefnHint() } };
BufferType buffer{ nullptr, freeobj{ allocator.resource() } };

template <typename T, typename... Args>
static size_t calculateSize(T&& h, Args&&... args) noexcept
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@
#include <FairMQTransportFactory.h>
#include <fairmq/MemoryResources.h>
#include <fairmq/MemoryResourceTools.h>
#include "Types.h"

namespace o2
{

using byte = unsigned char;

namespace memory_resource
namespace pmr
{

using FairMQMemoryResource = fair::mq::FairMQMemoryResource;
using ChannelResource = fair::mq::ChannelResource;
using namespace fair::mq::pmr;

template <typename ContainerT>
FairMQMessagePtr getMessage(ContainerT&& container, FairMQMemoryResource* targetResource = nullptr)
Expand Down Expand Up @@ -209,6 +209,8 @@ class OwningMessageSpectatorAllocator

using ByteSpectatorAllocator = SpectatorAllocator<o2::byte>;
using BytePmrAllocator = boost::container::pmr::polymorphic_allocator<o2::byte>;
template <class T>
using vector = std::vector<T, o2::pmr::polymorphic_allocator<T>>;

//__________________________________________________________________________________________________
/// Return a std::vector spanned over the contents of the message, takes ownership of the message
Expand All @@ -227,7 +229,11 @@ inline static ChannelResource* getTransportAllocator(FairMQTransportFactory* fac
return factory->GetMemoryResource();
}

}; //namespace memory_resource
}; //namespace pmr

template <class T>
using vector = std::vector<T, o2::pmr::polymorphic_allocator<T>>;

}; //namespace o2

#endif
8 changes: 4 additions & 4 deletions DataFormats/MemoryResources/test/testMemoryResources.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

namespace o2
{
namespace memory_resource
namespace pmr
{
auto factoryZMQ = FairMQTransportFactory::CreateTransportFactory("zeromq");
auto factorySHM = FairMQTransportFactory::CreateTransportFactory("shmem");
Expand Down Expand Up @@ -103,7 +103,7 @@ BOOST_AUTO_TEST_CASE(getMessage_test)
v.emplace_back(2);
v.emplace_back(3);
void* vectorBeginPtr = &v[0];
message = o2::memory_resource::getMessage(std::move(v));
message = o2::pmr::getMessage(std::move(v));
BOOST_CHECK(message != nullptr);
BOOST_CHECK(message->GetData() == vectorBeginPtr);
}
Expand All @@ -118,7 +118,7 @@ BOOST_AUTO_TEST_CASE(getMessage_test)
v.emplace_back(5);
v.emplace_back(6);
void* vectorBeginPtr = &v[0];
message = o2::memory_resource::getMessage(std::move(v), allocSHM);
message = o2::pmr::getMessage(std::move(v), allocSHM);
BOOST_CHECK(message != nullptr);
BOOST_CHECK(message->GetData() != vectorBeginPtr);
}
Expand Down Expand Up @@ -146,7 +146,7 @@ BOOST_AUTO_TEST_CASE(adoptVector_test)
BOOST_CHECK(adoptedOwner[1].i == 2);
BOOST_CHECK(adoptedOwner[2].i == 1);

auto reclaimedMessage = o2::memory_resource::getMessage(std::move(adoptedOwner));
auto reclaimedMessage = o2::pmr::getMessage(std::move(adoptedOwner));
BOOST_CHECK(reclaimedMessage.get() == messageAddr);
BOOST_CHECK(adoptedOwner.size() == 0);

Expand Down
4 changes: 2 additions & 2 deletions Framework/Core/src/DataAllocator.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ FairMQMessagePtr DataAllocator::headerMessageFromOutput(Output const& spec,
DataProcessingHeader dph{mTimingInfo->timeslice, 1};
auto context = mContextRegistry->get<MessageContext>();

auto channelAlloc = o2::memory_resource::getTransportAllocator(context->proxy().getTransport(channel, 0));
return o2::memory_resource::getMessage(o2::header::Stack{ channelAlloc, dh, dph, spec.metaHeader });
auto channelAlloc = o2::pmr::getTransportAllocator(context->proxy().getTransport(channel, 0));
return o2::pmr::getMessage(o2::header::Stack{ channelAlloc, dh, dph, spec.metaHeader });
}

void DataAllocator::addPartToContext(FairMQMessagePtr&& payloadMessage, const Output& spec,
Expand Down
4 changes: 2 additions & 2 deletions Framework/Core/src/Dispatcher.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ void Dispatcher::sendFairMQ(FairMQDevice* device, const DataRef& inputData, cons
DataProcessingHeader dphout{ dph->startTime, dph->duration };
o2::header::Stack headerStack{ dhout, dphout };

auto channelAlloc = o2::memory_resource::getTransportAllocator(device->Transport());
FairMQMessagePtr msgHeaderStack = o2::memory_resource::getMessage(std::move(headerStack), channelAlloc);
auto channelAlloc = o2::pmr::getTransportAllocator(device->Transport());
FairMQMessagePtr msgHeaderStack = o2::pmr::getMessage(std::move(headerStack), channelAlloc);

char* payloadCopy = new char[dh->payloadSize];
memcpy(payloadCopy, inputData.payload, dh->payloadSize);
Expand Down
4 changes: 2 additions & 2 deletions Framework/Core/src/ExternalFairMQDeviceProxy.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ void broadcastMessage(FairMQDevice &device, o2::header::Stack &&headerStack, Fai

// FIXME: this assumes there is only one output from here... This should
// really do the matchmaking between inputs and output channels.
auto channelAlloc = o2::memory_resource::getTransportAllocator(channelInfo.second[index].Transport());
FairMQMessagePtr headerMessage = o2::memory_resource::getMessage(std::move(headerStack), channelAlloc);
auto channelAlloc = o2::pmr::getTransportAllocator(channelInfo.second[index].Transport());
FairMQMessagePtr headerMessage = o2::pmr::getMessage(std::move(headerStack), channelAlloc);

FairMQParts out;
out.AddPart(std::move(headerMessage));
Expand Down
4 changes: 2 additions & 2 deletions Framework/Core/src/LifetimeHelpers.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ ExpirationHandler::Handler LifetimeHelpers::enumerate(ConcreteDataMatcher const&
DataProcessingHeader dph{ timestamp, 1 };

auto&& transport = rawDeviceService.device()->GetChannel(sourceChannel, 0).Transport();
auto channelAlloc = o2::memory_resource::getTransportAllocator(transport);
auto header = o2::memory_resource::getMessage(o2::header::Stack{ channelAlloc, dh, dph });
auto channelAlloc = o2::pmr::getTransportAllocator(transport);
auto header = o2::pmr::getMessage(o2::header::Stack{ channelAlloc, dh, dph });
ref.header = std::move(header);

auto payload = rawDeviceService.device()->NewMessage(*counter);
Expand Down
3 changes: 3 additions & 0 deletions Framework/Utils/include/Utils/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ namespace o2
{
namespace workflows
{
//TODO: this is to make DPLmerger compile, but this code is (and always was) horribly broken - please remove.
inline void freefn(void* data, void* /*hint*/) { delete static_cast<char*>(data); };

//
o2f::Output getOutput(const o2f::OutputSpec outputSpec);
std::shared_ptr<std::vector<o2f::Output>> getOutputList(const o2f::Outputs outputSpecs);
Expand Down
2 changes: 1 addition & 1 deletion Framework/Utils/src/DPLMerger.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ o2f::DataProcessorSpec defineMerger(std::string devName, o2f::Inputs usrInputs,
(*mergerFuncPtr)(outputBuffer, itInputs);
}
// Adopting the buffer as new chunk
ctx.outputs().adoptChunk((*outputPtr), &outputBuffer[0], outputBuffer.size(), header::Stack::getFreefn(),
ctx.outputs().adoptChunk((*outputPtr), &outputBuffer[0], outputBuffer.size(), &freefn,
nullptr);
};
} } };
Expand Down
6 changes: 3 additions & 3 deletions Utilities/O2Device/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ addDataBlock(O2Message& message, o2::header::Stack&& headerStack, T data);
- data:
- ```FairMQMessagePtr``` (```std::unique_ptr<FairMQMessage>```)
- STL-like container that uses a ```pmr::polymorphic_allocator``` as allocator. Note: this is only efficient if
the memory resource used to construct the allocator is ```o2::memory_resource::ChannelResource```, otherwise an implicit copy occurs.
the memory resource used to construct the allocator is ```o2::pmr::ChannelResource```, otherwise an implicit copy occurs.

### forEach()
Executes a function on each data block within the message.
Expand All @@ -39,11 +39,11 @@ the function is a callable object (lambda, functor, std::function) and needs to
#### basic example, inside a FairMQDevice:
```C++
// make sure we have the allocator associated with the transport appropriate for the selected channel:
auto outputChannelAllocator = o2::memory_resource::getTransportAllocator(GetChannel("dataOut").Transport());
auto outputChannelAllocator = o2::pmr::getTransportAllocator(GetChannel("dataOut").Transport());

//the data
using namespace boost::container::pmr;
std::vector<int, polymorphic_allocator<int>> dataVector(polymorphic_allocator<int>{outputChannelAllocator});
o2::pmr::vector<int> dataVector(outputChannelAllocator);
dataVector.reserve(10);
dataVector.push_back(1);
dataVector.push_back(2);
Expand Down
4 changes: 2 additions & 2 deletions Utilities/O2Device/include/O2Device/Utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ using O2Message = FairMQParts;
//__________________________________________________________________________________________________
// addDataBlock for generic (compatible) containers, that is contiguous containers using the pmr allocator
template <typename ContainerT, typename std::enable_if<!std::is_same<ContainerT, FairMQMessagePtr>::value, int>::type = 0>
bool addDataBlock(O2Message& parts, o2::header::Stack&& inputStack, ContainerT&& inputData, o2::memory_resource::FairMQMemoryResource* targetResource = nullptr)
bool addDataBlock(O2Message& parts, o2::header::Stack&& inputStack, ContainerT&& inputData, o2::pmr::FairMQMemoryResource* targetResource = nullptr)
{
using std::move;
using std::forward;
Expand All @@ -62,7 +62,7 @@ bool addDataBlock(O2Message& parts, o2::header::Stack&& inputStack, ContainerT&&
// addDataBlock for data already wrapped in FairMQMessagePtr
// note: since we cannot partially specialize function templates, use SFINAE here instead
template <typename ContainerT, typename std::enable_if<std::is_same<ContainerT, FairMQMessagePtr>::value, int>::type = 0>
bool addDataBlock(O2Message& parts, o2::header::Stack&& inputStack, ContainerT&& dataMessage, o2::memory_resource::FairMQMemoryResource* targetResource = nullptr)
bool addDataBlock(O2Message& parts, o2::header::Stack&& inputStack, ContainerT&& dataMessage, o2::pmr::FairMQMemoryResource* targetResource = nullptr)
{
using std::move;

Expand Down
6 changes: 3 additions & 3 deletions Utilities/O2Device/test/test_O2Device.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

using namespace o2::Base;
using namespace o2::header;
using namespace o2::memory_resource;
using namespace o2::pmr;

auto factoryZMQ = FairMQTransportFactory::CreateTransportFactory("zeromq");
auto factorySHM = FairMQTransportFactory::CreateTransportFactory("shmem");
Expand All @@ -34,7 +34,7 @@ BOOST_AUTO_TEST_CASE(getMessage_Stack)
Stack s1{ DataHeader{ gDataDescriptionInvalid, gDataOriginInvalid, DataHeader::SubSpecificationType{ 0 } },
NameHeader<9>{ "somename" } };

auto message = o2::memory_resource::getMessage(std::move(s1), allocZMQ);
auto message = o2::pmr::getMessage(std::move(s1), allocZMQ);

BOOST_REQUIRE(s1.data() == nullptr);
BOOST_REQUIRE(message != nullptr);
Expand All @@ -50,7 +50,7 @@ BOOST_AUTO_TEST_CASE(getMessage_Stack)
NameHeader<9>{ "somename" } };
BOOST_TEST(allocZMQ->getNumberOfMessages() == 1);

auto message = o2::memory_resource::getMessage(std::move(s1), allocSHM);
auto message = o2::pmr::getMessage(std::move(s1), allocSHM);

BOOST_TEST(allocZMQ->getNumberOfMessages() == 0);
BOOST_TEST(allocSHM->getNumberOfMessages() == 0);
Expand Down
2 changes: 1 addition & 1 deletion Utilities/O2MessageMonitor/src/O2MessageMonitor.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void O2MessageMonitor::Run()
type = subChannels[0].GetType();
}

auto dataResource = o2::memory_resource::getTransportAllocator(subChannels[0].Transport());
auto dataResource = o2::pmr::getTransportAllocator(subChannels[0].Transport());

while (CheckCurrentState(RUNNING) && (--mIterations) != 0) {
this_thread::sleep_for(chrono::milliseconds(mDelay));
Expand Down
18 changes: 9 additions & 9 deletions Utilities/aliceHLTwrapper/src/MessageFormat.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ int MessageFormat::addMessage(uint8_t* buffer, unsigned size)
return mBlockDescriptors.size() - count;
}

int MessageFormat::addMessages(const vector<BufferDesc_t>& list)
int MessageFormat::addMessages(const std::vector<BufferDesc_t>& list)
{
// add list of messages
int totalCount = 0;
Expand Down Expand Up @@ -163,13 +163,13 @@ int MessageFormat::addMessages(const vector<BufferDesc_t>& list)
}

int MessageFormat::readBlockSequence(uint8_t* buffer, unsigned size,
vector<BlockDescriptor>& descriptorList) const
std::vector<BlockDescriptor>& descriptorList) const
{
// read a sequence of blocks consisting of AliHLTComponentBlockData followed by payload
// from a buffer
if (buffer == nullptr) return 0;
unsigned position = 0;
vector<BlockDescriptor> input;
std::vector<BlockDescriptor> input;
while (position + sizeof(AliHLTComponentBlockData) < size) {
AliHLTComponentBlockData* p = reinterpret_cast<AliHLTComponentBlockData*>(buffer + position);
if (p->fStructSize == 0 || // no valid header
Expand Down Expand Up @@ -199,7 +199,7 @@ int MessageFormat::readBlockSequence(uint8_t* buffer, unsigned size,
}

int MessageFormat::readHOMERFormat(uint8_t* buffer, unsigned size,
vector<BlockDescriptor>& descriptorList) const
std::vector<BlockDescriptor>& descriptorList) const
{
// read message payload in HOMER format
if (mpFactory == nullptr) const_cast<MessageFormat*>(this)->mpFactory = new o2::alice_hlt::HOMERFactory;
Expand All @@ -224,7 +224,7 @@ int MessageFormat::readHOMERFormat(uint8_t* buffer, unsigned size,
return nofBlocks;
}

int MessageFormat::readO2Format(const vector<BufferDesc_t>& list, std::vector<BlockDescriptor>& descriptorList, HeartbeatHeader& hbh, HeartbeatTrailer& hbt) const
int MessageFormat::readO2Format(const std::vector<BufferDesc_t>& list, std::vector<BlockDescriptor>& descriptorList, HeartbeatHeader& hbh, HeartbeatTrailer& hbt) const
{
int partNumber = 0;
const o2::header::DataHeader* dh = nullptr;
Expand Down Expand Up @@ -278,10 +278,10 @@ int MessageFormat::readO2Format(const vector<BufferDesc_t>& list, std::vector<Bl
return list.size()/2;
}

vector<MessageFormat::BufferDesc_t> MessageFormat::createMessages(const AliHLTComponentBlockData* blocks,
unsigned count, unsigned totalPayloadSize,
const AliHLTComponentEventData* evtData,
boost::signals2::signal<unsigned char* (unsigned int)> *cbAllocate)
std::vector<MessageFormat::BufferDesc_t> MessageFormat::createMessages(const AliHLTComponentBlockData* blocks,
unsigned count, unsigned totalPayloadSize,
const AliHLTComponentEventData* evtData,
boost::signals2::signal<unsigned char*(unsigned int)>* cbAllocate)
{
// O2 output mode does not support event info struct
// for the moment simply ignore it, not sure if this is the best
Expand Down