Skip to content

Commit

Permalink
[wip] Make everything compile again
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Dec 2, 2022
1 parent bb9ae54 commit e0b784d
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 28 deletions.
82 changes: 76 additions & 6 deletions include/podio/AssociationCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
#include "podio/CollectionBuffers.h"
#include "podio/ICollectionProvider.h"

#include <iomanip>
#include <memory>
#include <mutex>
#include <ostream>
#include <string>
#include <type_traits>

namespace podio {

Expand All @@ -26,12 +31,19 @@ class AssociationCollection : public podio::CollectionBase {
using AssocT = Association<FromT, ToT>;
using MutableAssocT = MutableAssociation<FromT, ToT>;

using CollectionT = podio::AssociationCollection<FromT, ToT>;
using CollectionDataT = podio::AssociationCollectionData<FromT, ToT>;

public:
using const_iterator = AssociationCollectionIterator<FromT, ToT>;
using iterator = AssociationMutableCollectionIterator<FromT, ToT>;

AssociationCollection() = default;

AssociationCollection(CollectionDataT&& data, bool isSubsetColl) :
m_isSubsetColl(isSubsetColl), m_collectionID(0), m_storage(std::move(data)) {
}

// Move-only type
AssociationCollection(const AssociationCollection&) = delete;
AssociationCollection& operator=(const AssociationCollection&) = delete;
Expand Down Expand Up @@ -107,6 +119,13 @@ class AssociationCollection : public podio::CollectionBase {
m_isPrepared = false;
}

void print(std::ostream& os = std::cout, bool flush = true) const override {
os << *this;
if (flush) {
os.flush();
}
}

// support for the iterator protocol
const_iterator begin() const {
return const_iterator(0, &m_storage.entries);
Expand All @@ -125,10 +144,30 @@ class AssociationCollection : public podio::CollectionBase {
return m_isValid;
}

podio::CollectionBuffers getBuffers() override {
podio::CollectionWriteBuffers getBuffers() override {
return m_storage.getCollectionBuffers(m_isSubsetColl);
}

podio::CollectionReadBuffers createBuffers() override /*const*/ {
// Very cumbersome way at the moment. We get the actual buffers to have the
// references and vector members sized appropriately (we will use this
// information to create new buffers outside)
auto collBuffers = m_storage.getCollectionBuffers(m_isSubsetColl);
auto readBuffers = podio::CollectionReadBuffers{};
readBuffers.references = collBuffers.references;
readBuffers.vectorMembers = collBuffers.vectorMembers;
readBuffers.createCollection = [](podio::CollectionReadBuffers buffers, bool isSubsetColl) {
CollectionDataT data(buffers, isSubsetColl);
return std::make_unique<CollectionT>(std::move(data), isSubsetColl);
};
readBuffers.recast = [](podio::CollectionReadBuffers& buffers) {
if (buffers.data) {
buffers.data = podio::CollectionWriteBuffers::asVector<float>(buffers.data);
}
};
return readBuffers;
}

std::string getTypeName() const override {
return std::string("podio::AssociationCollection<") + FromT::TypeName + "," + ToT::TypeName + ">";
}
Expand Down Expand Up @@ -164,8 +203,20 @@ class AssociationCollection : public podio::CollectionBase {
return m_collectionID;
}

void prepareForWrite() override {
// If the collection has been prepared already there is nothing to do
void prepareForWrite() const override {
// TODO: Replace this double locking pattern with an atomic and only one
// lock. Problem: std::atomic is not default movable
{
std::lock_guard lock{*m_storageMtx};
// If the collection has been prepared already there is nothing to do
if (m_isPrepared) {
return;
}
}

std::lock_guard lock{*m_storageMtx};
// by the time we acquire the lock another thread might have already
// succeeded, so we need to check again now
if (m_isPrepared) {
return;
}
Expand Down Expand Up @@ -196,15 +247,34 @@ class AssociationCollection : public podio::CollectionBase {
// For setReferences, we need to give our own CollectionData access to our
// private entries. Otherwise we would need to expose a public member function
// that gives access to the Obj* which is definitely not what we want
friend class AssociationCollectionData<FromT, ToT>;
friend CollectionDataT;

bool m_isValid{false};
bool m_isPrepared{false};
mutable bool m_isPrepared{false};
bool m_isSubsetColl{false};
int m_collectionID{0};
AssociationCollectionData<FromT, ToT> m_storage{};
mutable std::unique_ptr<std::mutex> m_storageMtx{std::make_unique<std::mutex>()};
mutable AssociationCollectionData<FromT, ToT> m_storage{};
};

template <typename FromT, typename ToT>
std::ostream& operator<<(std::ostream& o, const AssociationCollection<FromT, ToT>& v) {
const auto old_flags = o.flags();
o << " id: weight:" << '\n';
for (const auto&& el : v) {
o << std::scientific << std::showpos << std::setw(12) << el.id() << " " << std::setw(12) << " " << el.getWeight()
<< '\n';

o << " from : ";
o << el.getFrom().id() << std::endl;
o << " to : ";
o << el.getTo().id() << std::endl;
}

o.flags(old_flags);
return o;
}

} // namespace podio

#endif // PODIO_ASSOCIATIONCOLLECTION_H
20 changes: 13 additions & 7 deletions include/podio/detail/AssociationCollectionData.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,24 @@ class AssociationCollectionData {
public:
AssociationObjPointerContainer<FromT, ToT> entries{};

AssociationCollectionData() :
m_rel_from(new std::vector<FromT>()), m_rel_to(new std::vector<ToT>()), m_data(new AssociationDataContainer()) {
m_refCollections.emplace_back(std::make_unique<std::vector<podio::ObjectID>>());
m_refCollections.emplace_back(std::make_unique<std::vector<podio::ObjectID>>());
AssociationCollectionData() = default;

AssociationCollectionData(podio::CollectionReadBuffers buffers, bool isSubsetColl) :
m_rel_from(new std::vector<FromT>()),
m_rel_to(new std::vector<ToT>()),
m_refCollections(std::move(*buffers.references)) {
if (!isSubsetColl) {
m_data.reset(buffers.dataAsVector<float>());
}
}

AssociationCollectionData(const AssociationCollectionData&) = delete;
AssociationCollectionData& operator=(const AssociationCollectionData&) = delete;
AssociationCollectionData(AssociationCollectionData&&) = default;
AssociationCollectionData& operator=(AssociationCollectionData&&) = default;
~AssociationCollectionData() = default;

podio::CollectionBuffers getCollectionBuffers(bool isSubsetColl) {
podio::CollectionWriteBuffers getCollectionBuffers(bool isSubsetColl) {
return {isSubsetColl ? nullptr : (void*)&m_data, &m_refCollections, nullptr};
}

Expand Down Expand Up @@ -177,8 +183,8 @@ class AssociationCollectionData {

private:
// members to handle relations
podio::UVecPtr<FromT> m_rel_from{};
podio::UVecPtr<ToT> m_rel_to{};
podio::UVecPtr<FromT> m_rel_from{nullptr};
podio::UVecPtr<ToT> m_rel_to{nullptr};

// I/O related buffers (as far as necessary)
podio::CollRefCollection m_refCollections{};
Expand Down
3 changes: 3 additions & 0 deletions include/podio/detail/AssociationFwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ using MutableAssociation = AssociationT<detail::GetDefT<FromT>, detail::GetDefT<
template <typename FromT, typename ToT>
class AssociationCollection;

template <typename FromT, typename ToT>
class AssociationCollectionData;

template <typename FromT, typename ToT, bool Mutable>
class AssociationCollectionIteratorT;

Expand Down
44 changes: 29 additions & 15 deletions include/podio/detail/AssociationSIOBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "podio/detail/AssociationFwd.h"

#include "podio/CollectionBuffers.h"

#include "podio/SIOBlock.h"

#include <sio/api.h>
Expand All @@ -26,17 +28,21 @@ class AssociationSIOBlock : public podio::SIOBlock {
}

void read(sio::read_device& device, sio::version_type) override {
auto buffers = _col->getBuffers();
if (!_col->isSubsetCollection()) {
auto* dataVec = buffers.dataAsVector<float>();
m_buffers.references->emplace_back(std::make_unique<std::vector<podio::ObjectID>>());
if (!m_subsetColl) {
m_buffers.references->emplace_back(std::make_unique<std::vector<podio::ObjectID>>());
}

if (!m_subsetColl) {
unsigned size{0};
device.data(size);
dataVec->resize(size);
m_buffers.data = new std::vector<float>(size);
auto* dataVec = m_buffers.dataAsVector<float>();
podio::handlePODDataSIO(device, dataVec->data(), size);
}

// ---- references
auto* refColls = buffers.references;
// ---- read ref collections
auto* refColls = m_buffers.references;
for (auto& refC : *refColls) {
unsigned size{0};
device.data(size);
Expand All @@ -46,27 +52,35 @@ class AssociationSIOBlock : public podio::SIOBlock {
}

void write(sio::write_device& device) override {
_col->prepareForWrite();
auto buffers = _col->getBuffers();
if (!_col->isSubsetCollection()) {
auto* dataVec = buffers.dataAsVector<float>();
if (!m_subsetColl) {
auto* dataVec = podio::CollectionWriteBuffers::asVector<float>(m_buffers.data);
unsigned size = dataVec->size();
device.data(size);
podio::handlePODDataSIO(device, dataVec->data(), size);
}

// ---- references
auto* refColls = buffers.references;
// ---- wirte ref collections ------
auto* refColls = m_buffers.references;
for (auto& refC : *refColls) {
unsigned size = refC->size();
device.data(size);
podio::handlePODDataSIO(device, refC->data(), size);
}
}

void createCollection(const bool subsetCollection = false) override {
setCollection(new AssociationCollection<FromT, ToT>());
_col->setSubsetCollection(subsetCollection);
void createBuffers(const bool subsetCollection = false) override {
m_subsetColl = subsetCollection;

m_buffers.references = new podio::CollRefCollection();
m_buffers.vectorMembers = new podio::VectorMembersInfo();

m_buffers.createCollection = [](podio::CollectionReadBuffers buffers, bool isSubsetColl) {
AssociationCollectionData<FromT, ToT> data(buffers, isSubsetColl);
return std::make_unique<AssociationCollection<FromT, ToT>>(std::move(data), isSubsetColl);
};

// setCollection(new AssociationCollection<FromT, ToT>());
// _col->setSubsetCollection(subsetCollection);
}

SIOBlock* create(const std::string& name) const override {
Expand Down

0 comments on commit e0b784d

Please sign in to comment.