Skip to content

Commit

Permalink
Do not default construct PortableCollections
Browse files Browse the repository at this point in the history
Do not use default-constructed PortableCollections and similar objects: these
have a not well-defined semantics, especially if put into the Event.
  • Loading branch information
fwyzard committed Dec 5, 2024
1 parent 48600da commit 2cd534d
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 34 deletions.
5 changes: 2 additions & 3 deletions DataFormats/VertexSoA/test/alpaka/ZVertexSoA_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ int main() {

// If the device is actually the host, use the collection as-is.
// Otherwise, copy the data from the device to the host.
ZVertexHost zvertex_h;
#ifdef ALPAKA_ACC_CPU_B_SEQ_T_SEQ_ENABLED
zvertex_h = std::move(zvertex_d);
ZVertexHost zvertex_h = std::move(zvertex_d);
#else
zvertex_h = cms::alpakatools::CopyToHost<ZVertexSoACollection>::copyAsync(queue, zvertex_d);
ZVertexHost zvertex_h = cms::alpakatools::CopyToHost<ZVertexSoACollection>::copyAsync(queue, zvertex_d);
#endif
alpaka::wait(queue);
std::cout << zvertex_h.view().metadata().size() << std::endl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
event.emplace(digisF5HBToken_, std::move(df5_));

if (qie11Digis.empty()) {
event.emplace(digisF01HEToken_);
event.emplace(digisF3HBToken_);
event.emplace(digisF01HEToken_, 0, event.queue());
event.emplace(digisF3HBToken_, 0, event.queue());

} else {
auto size_f1 = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
hostProductMulti2_ = portabletest::TestHostMultiCollection2{deviceProductMulti2.sizes(), iEvent.queue()};
hostProductMulti3_ = portabletest::TestHostMultiCollection3{deviceProductMulti3.sizes(), iEvent.queue()};

alpaka::memcpy(iEvent.queue(), hostProduct_.buffer(), deviceProduct.const_buffer());
alpaka::memcpy(iEvent.queue(), hostProductMulti2_.buffer(), deviceProductMulti2.const_buffer());
alpaka::memcpy(iEvent.queue(), hostProductMulti3_.buffer(), deviceProductMulti3.const_buffer());
alpaka::memcpy(iEvent.queue(), hostProduct_->buffer(), deviceProduct.const_buffer());
alpaka::memcpy(iEvent.queue(), hostProductMulti2_->buffer(), deviceProductMulti2.const_buffer());
alpaka::memcpy(iEvent.queue(), hostProductMulti3_->buffer(), deviceProductMulti3.const_buffer());
}
} // namespace ALPAKA_ACCELERATOR_NAMESPACE
28 changes: 22 additions & 6 deletions HeterogeneousCore/AlpakaTest/plugins/alpaka/TestHelperClass.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef HeterogeneousCore_AlpakaTest_plugins_alpaka_TestHelperClass_h
#define HeterogeneousCore_AlpakaTest_plugins_alpaka_TestHelperClass_h

#include <optional>

#include "DataFormats/PortableTestObjects/interface/TestHostCollection.h"
#include "DataFormats/PortableTestObjects/interface/alpaka/TestDeviceCollection.h"
#include "FWCore/Framework/interface/ConsumesCollector.h"
Expand All @@ -23,9 +25,23 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {

void makeAsync(device::Event const& iEvent, device::EventSetup const& iSetup);

portabletest::TestHostCollection moveFrom() { return std::move(hostProduct_); }
portabletest::TestHostMultiCollection2 moveFromMulti2() { return std::move(hostProductMulti2_); }
portabletest::TestHostMultiCollection3 moveFromMulti3() { return std::move(hostProductMulti3_); }
portabletest::TestHostCollection moveFrom() {
auto&& product = std::move(*hostProduct_);
hostProduct_.reset();
return product;
}

portabletest::TestHostMultiCollection2 moveFromMulti2() {
auto&& product = std::move(*hostProductMulti2_);
hostProductMulti2_.reset();
return product;
}

portabletest::TestHostMultiCollection3 moveFromMulti3() {
auto&& product = std::move(*hostProductMulti3_);
hostProductMulti3_.reset();
return product;
}

private:
const device::EDGetToken<portabletest::TestDeviceCollection> getToken_;
Expand All @@ -35,9 +51,9 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
const device::ESGetToken<AlpakaESTestDataCDevice, AlpakaESTestRecordC> esTokenDevice_;

// hold the output product between acquire() and produce()
portabletest::TestHostCollection hostProduct_;
portabletest::TestHostMultiCollection2 hostProductMulti2_;
portabletest::TestHostMultiCollection3 hostProductMulti3_;
std::optional<portabletest::TestHostCollection> hostProduct_;
std::optional<portabletest::TestHostMultiCollection2> hostProductMulti2_;
std::optional<portabletest::TestHostMultiCollection3> hostProductMulti3_;
};
} // namespace ALPAKA_ACCELERATOR_NAMESPACE

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// C++ includes
#include <memory>
#include <optional>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>

#include <alpaka/alpaka.hpp>

#include "DataFormats/Common/interface/AllocateForOverwrite.h"
#include "DataFormats/Common/interface/DetSetVector.h"
#include "DataFormats/SiPixelClusterSoA/interface/alpaka/SiPixelClustersSoACollection.h"
#include "DataFormats/SiPixelDigi/interface/PixelDigi.h"
Expand Down Expand Up @@ -52,7 +54,7 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
const SiPixelClusterThresholds clusterThresholds_;
uint32_t nDigis_ = 0;

SiPixelDigisSoACollection digis_d;
std::optional<SiPixelDigisSoACollection> digis_d_;
};

SiPixelPhase2DigiToCluster::SiPixelPhase2DigiToCluster(const edm::ParameterSet& iConfig)
Expand Down Expand Up @@ -88,7 +90,6 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
const TrackerGeometry* geom_ = &iSetup.getData(geomToken_);

uint32_t nDigis = 0;

for (const auto& det : input) {
nDigis += det.size();
}
Expand All @@ -107,39 +108,33 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
auto const gind = genericDet->index();
for (auto const& px : det) {
digis_h.view()[nDigis].moduleId() = uint16_t(gind);

digis_h.view()[nDigis].xx() = uint16_t(px.row());
digis_h.view()[nDigis].yy() = uint16_t(px.column());
digis_h.view()[nDigis].adc() = uint16_t(px.adc());

digis_h.view()[nDigis].clus() = 0;

digis_h.view()[nDigis].pdigi() = uint32_t(px.packedData());

digis_h.view()[nDigis].rawIdArr() = uint32_t(detid);

nDigis++;
++nDigis;
}
}

digis_d = SiPixelDigisSoACollection(nDigis, iEvent.queue());
alpaka::memcpy(iEvent.queue(), digis_d.buffer(), digis_h.buffer());

Algo_.makePhase2ClustersAsync(iEvent.queue(), clusterThresholds_, digis_d.view(), nDigis_);
digis_d_ = SiPixelDigisSoACollection(nDigis, iEvent.queue());
alpaka::memcpy(iEvent.queue(), digis_d_->buffer(), digis_h.buffer());
Algo_.makePhase2ClustersAsync(iEvent.queue(), clusterThresholds_, digis_d_->view(), nDigis_);
}

void SiPixelPhase2DigiToCluster::produce(device::Event& iEvent, device::EventSetup const& iSetup) {
if (nDigis_ == 0) {
SiPixelClustersSoACollection clusters_d{pixelTopology::Phase2::numberOfModules, iEvent.queue()};
SiPixelDigisSoACollection digis_d_zero{nDigis_, iEvent.queue()};
SiPixelDigisSoACollection digis_d_zero{0, iEvent.queue()};
iEvent.emplace(digiPutToken_, std::move(digis_d_zero));
iEvent.emplace(clusterPutToken_, std::move(clusters_d));
return;
} else {
digis_d_->setNModules(Algo_.nModules());
iEvent.emplace(digiPutToken_, std::move(*digis_d_));
iEvent.emplace(clusterPutToken_, Algo_.getClusters());
}

digis_d.setNModules(Algo_.nModules());
iEvent.emplace(digiPutToken_, std::move(digis_d));
iEvent.emplace(clusterPutToken_, Algo_.getClusters());
digis_d_.reset();
}

} // namespace ALPAKA_ACCELERATOR_NAMESPACE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <memory>
#include <optional>
#include <stdexcept>
#include <string>
#include <utility>
Expand Down Expand Up @@ -262,10 +263,10 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
// are no valid pointers to clusters' Collection columns, instantiation
// of TrackingRecHits fail. Example: workflow 11604.0

iEvent.emplace(digiPutToken_, nDigis_, iEvent.queue());
iEvent.emplace(digiPutToken_, 0, iEvent.queue());
iEvent.emplace(clusterPutToken_, pixelTopology::Phase1::numberOfModules, iEvent.queue());
if (includeErrors_) {
iEvent.emplace(digiErrorPutToken_);
iEvent.emplace(digiErrorPutToken_, 0, iEvent.queue());
iEvent.emplace(fmtErrorToken_);
}
return;
Expand Down

0 comments on commit 2cd534d

Please sign in to comment.