From de8af41b706e56ae885e18c5c6419441cad57a1e Mon Sep 17 00:00:00 2001 From: Andrea Bocci Date: Wed, 21 Feb 2024 00:24:39 +0100 Subject: [PATCH] Let the test fail gracefully if no devies are found If more than one device is available, run the tests on all available devices. Replace elements_with_stide with uniform_elements. --- .../SiPixelDigiSoA/test/alpaka/Digis_test.cc | 58 +++++++------- .../test/alpaka/Digis_test.dev.cc | 75 +++++++++---------- .../SiPixelDigiSoA/test/alpaka/Digis_test.h | 13 ++++ 3 files changed, 81 insertions(+), 65 deletions(-) create mode 100644 DataFormats/SiPixelDigiSoA/test/alpaka/Digis_test.h diff --git a/DataFormats/SiPixelDigiSoA/test/alpaka/Digis_test.cc b/DataFormats/SiPixelDigiSoA/test/alpaka/Digis_test.cc index f1d9ce9cd2b37..5425ff70eb1f4 100644 --- a/DataFormats/SiPixelDigiSoA/test/alpaka/Digis_test.cc +++ b/DataFormats/SiPixelDigiSoA/test/alpaka/Digis_test.cc @@ -1,3 +1,4 @@ +#include #include #include @@ -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(); + 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()[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; } diff --git a/DataFormats/SiPixelDigiSoA/test/alpaka/Digis_test.dev.cc b/DataFormats/SiPixelDigiSoA/test/alpaka/Digis_test.dev.cc index 9bb35bfc4d7f8..721b17912c65b 100644 --- a/DataFormats/SiPixelDigiSoA/test/alpaka/Digis_test.dev.cc +++ b/DataFormats/SiPixelDigiSoA/test/alpaka/Digis_test.dev.cc @@ -1,49 +1,48 @@ +#include + #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 >> - 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 >> + 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 >> - 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 >> + 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(groups, items); - alpaka::exec(queue, workDiv, TestFillKernel{}, digi_view); - alpaka::exec(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(groups, items); + alpaka::exec(queue, workDiv, TestFillKernel{}, digi_view); + alpaka::exec(queue, workDiv, TestVerifyKernel{}, digi_view); + } - } // namespace testDigisSoA -} // namespace ALPAKA_ACCELERATOR_NAMESPACE +} // namespace ALPAKA_ACCELERATOR_NAMESPACE::testDigisSoA diff --git a/DataFormats/SiPixelDigiSoA/test/alpaka/Digis_test.h b/DataFormats/SiPixelDigiSoA/test/alpaka/Digis_test.h new file mode 100644 index 0000000000000..fbe2ffecc0f79 --- /dev/null +++ b/DataFormats/SiPixelDigiSoA/test/alpaka/Digis_test.h @@ -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