Skip to content

Commit

Permalink
Let the test fail gracefully if no devies are found
Browse files Browse the repository at this point in the history
If more than one device is available, run the tests on all available
devices.

Replace elements_with_stide with uniform_elements.
  • Loading branch information
fwyzard committed Feb 21, 2024
1 parent 71e399a commit de8af41
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 65 deletions.
58 changes: 31 additions & 27 deletions DataFormats/SiPixelDigiSoA/test/alpaka/Digis_test.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <cstdlib>
#include <unistd.h>

#include <alpaka/alpaka.hpp>
Expand All @@ -6,43 +7,46 @@
#include "DataFormats/SiPixelDigiSoA/interface/SiPixelDigisHost.h"
#include "DataFormats/SiPixelDigiSoA/interface/SiPixelDigisSoA.h"
#include "DataFormats/SiPixelDigiSoA/interface/alpaka/SiPixelDigisSoACollection.h"
#include "FWCore/Utilities/interface/stringize.h"
#include "HeterogeneousCore/AlpakaInterface/interface/config.h"
#include "HeterogeneousCore/AlpakaInterface/interface/devices.h"
#include "HeterogeneousCore/AlpakaInterface/interface/host.h"
#include "HeterogeneousCore/AlpakaInterface/interface/memory.h"
#include "HeterogeneousCore/AlpakaInterface/interface/traits.h"
#include "HeterogeneousCore/AlpakaInterface/interface/workdivision.h"

using namespace ALPAKA_ACCELERATOR_NAMESPACE;

namespace ALPAKA_ACCELERATOR_NAMESPACE {
namespace testDigisSoA {
#include "Digis_test.h"

void runKernels(SiPixelDigisSoAView digis_view, Queue& queue);
using namespace ALPAKA_ACCELERATOR_NAMESPACE;

int main() {
// Get the list of devices on the current platform
auto const& devices = cms::alpakatools::devices<Platform>();
if (devices.empty()) {
std::cerr << "No devices available for the " EDM_STRINGIZE(ALPAKA_ACCELERATOR_NAMESPACE) " backend, "
"the test will be skipped.\n";
exit(EXIT_FAILURE);
}
} // namespace ALPAKA_ACCELERATOR_NAMESPACE

int main() {
const auto host = cms::alpakatools::host();
const auto device = cms::alpakatools::devices<Platform>()[0];
Queue queue(device);

// Inner scope to deallocate memory before destroying the stream
{
// Instantiate tracks on device. PortableDeviceCollection allocates
// SoA on device automatically.
SiPixelDigisSoACollection digis_d(1000, queue);
testDigisSoA::runKernels(digis_d.view(), queue);

// Instantate tracks on host. This is where the data will be
// copied to from device.
SiPixelDigisHost digis_h(digis_d.view().metadata().size(), queue);

std::cout << digis_h.view().metadata().size() << std::endl;
alpaka::memcpy(queue, digis_h.buffer(), digis_d.const_buffer());
alpaka::wait(queue);
// Run the test on each device
for (const auto& device : devices) {
Queue queue(device);

// Inner scope to deallocate memory before destroying the stream
{
// Instantiate tracks on device. PortableDeviceCollection allocates
// SoA on device automatically.
SiPixelDigisSoACollection digis_d(1000, queue);
testDigisSoA::runKernels(digis_d.view(), queue);

// Instantate tracks on host. This is where the data will be
// copied to from device.
SiPixelDigisHost digis_h(digis_d.view().metadata().size(), queue);

std::cout << digis_h.view().metadata().size() << std::endl;
alpaka::memcpy(queue, digis_h.buffer(), digis_d.const_buffer());
alpaka::wait(queue);
}
}

return 0;
return EXIT_SUCCESS;
}
75 changes: 37 additions & 38 deletions DataFormats/SiPixelDigiSoA/test/alpaka/Digis_test.dev.cc
Original file line number Diff line number Diff line change
@@ -1,49 +1,48 @@
#include <alpaka/alpaka.hpp>

#include "DataFormats/SiPixelDigiSoA/interface/SiPixelDigisDevice.h"
#include "DataFormats/SiPixelDigiSoA/interface/alpaka/SiPixelDigisSoACollection.h"
#include "DataFormats/SiPixelDigiSoA/interface/SiPixelDigisHost.h"
#include "HeterogeneousCore/AlpakaInterface/interface/workdivision.h"
#include "DataFormats/SiPixelDigiSoA/interface/alpaka/SiPixelDigisSoACollection.h"
#include "HeterogeneousCore/AlpakaInterface/interface/config.h"
#include "HeterogeneousCore/AlpakaInterface/interface/traits.h"
#include "HeterogeneousCore/AlpakaInterface/interface/workdivision.h"

using namespace alpaka;

namespace ALPAKA_ACCELERATOR_NAMESPACE {
#include "Digis_test.h"

using namespace cms::alpakatools;
namespace testDigisSoA {
namespace ALPAKA_ACCELERATOR_NAMESPACE::testDigisSoA {

class TestFillKernel {
public:
template <typename TAcc, typename = std::enable_if_t<isAccelerator<TAcc>>>
ALPAKA_FN_ACC void operator()(TAcc const& acc, SiPixelDigisSoAView digi_view) const {
for (int32_t j : elements_with_stride(acc, digi_view.metadata().size())) {
digi_view[j].clus() = j;
digi_view[j].rawIdArr() = j * 2;
digi_view[j].xx() = j * 3;
digi_view[j].moduleId() = j * 4;
}
class TestFillKernel {
public:
template <typename TAcc, typename = std::enable_if_t<alpaka::isAccelerator<TAcc>>>
ALPAKA_FN_ACC void operator()(TAcc const& acc, SiPixelDigisSoAView digi_view) const {
for (int32_t j : cms::alpakatools::uniform_elements(acc, digi_view.metadata().size())) {
digi_view[j].clus() = j;
digi_view[j].rawIdArr() = j * 2;
digi_view[j].xx() = j * 3;
digi_view[j].moduleId() = j * 4;
}
};
}
};

class TestVerifyKernel {
public:
template <typename TAcc, typename = std::enable_if_t<isAccelerator<TAcc>>>
ALPAKA_FN_ACC void operator()(TAcc const& acc, SiPixelDigisSoAConstView digi_view) const {
for (uint32_t j : elements_with_stride(acc, digi_view.metadata().size())) {
assert(digi_view[j].clus() == int(j));
assert(digi_view[j].rawIdArr() == j * 2);
assert(digi_view[j].xx() == j * 3);
assert(digi_view[j].moduleId() == j * 4);
}
class TestVerifyKernel {
public:
template <typename TAcc, typename = std::enable_if_t<alpaka::isAccelerator<TAcc>>>
ALPAKA_FN_ACC void operator()(TAcc const& acc, SiPixelDigisSoAConstView digi_view) const {
for (uint32_t j : cms::alpakatools::uniform_elements(acc, digi_view.metadata().size())) {
assert(digi_view[j].clus() == int(j));
assert(digi_view[j].rawIdArr() == j * 2);
assert(digi_view[j].xx() == j * 3);
assert(digi_view[j].moduleId() == j * 4);
}
};

void runKernels(SiPixelDigisSoAView digi_view, Queue& queue) {
uint32_t items = 64;
uint32_t groups = divide_up_by(digi_view.metadata().size(), items);
auto workDiv = make_workdiv<Acc1D>(groups, items);
alpaka::exec<Acc1D>(queue, workDiv, TestFillKernel{}, digi_view);
alpaka::exec<Acc1D>(queue, workDiv, TestVerifyKernel{}, digi_view);
}
};

void runKernels(SiPixelDigisSoAView digi_view, Queue& queue) {
uint32_t items = 64;
uint32_t groups = cms::alpakatools::divide_up_by(digi_view.metadata().size(), items);
auto workDiv = cms::alpakatools::make_workdiv<Acc1D>(groups, items);
alpaka::exec<Acc1D>(queue, workDiv, TestFillKernel{}, digi_view);
alpaka::exec<Acc1D>(queue, workDiv, TestVerifyKernel{}, digi_view);
}

} // namespace testDigisSoA
} // namespace ALPAKA_ACCELERATOR_NAMESPACE
} // namespace ALPAKA_ACCELERATOR_NAMESPACE::testDigisSoA
13 changes: 13 additions & 0 deletions DataFormats/SiPixelDigiSoA/test/alpaka/Digis_test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef DataFormats_SiPixelDigiSoA_test_alpaka_Digis_test_h
#define DataFormats_SiPixelDigiSoA_test_alpaka_Digis_test_h

#include "DataFormats/SiPixelDigiSoA/interface/SiPixelDigisSoA.h"
#include "HeterogeneousCore/AlpakaInterface/interface/config.h"

namespace ALPAKA_ACCELERATOR_NAMESPACE::testDigisSoA {

void runKernels(SiPixelDigisSoAView digis_view, Queue& queue);

} // namespace ALPAKA_ACCELERATOR_NAMESPACE::testDigisSoA

#endif // DataFormats_SiPixelDigiSoA_test_alpaka_Digis_test_h

0 comments on commit de8af41

Please sign in to comment.