-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests and rudimentary protections for default-constructed Portabl…
…eCollections Also add tests for zero-sized PortableCollections
- Loading branch information
Showing
8 changed files
with
299 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#define CATCH_CONFIG_MAIN | ||
#include <catch.hpp> |
121 changes: 121 additions & 0 deletions
121
DataFormats/Portable/test/alpaka/test_catch2_portableCollection.dev.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#include <catch.hpp> | ||
|
||
#include "DataFormats/Portable/interface/PortableCollection.h" | ||
#include "DataFormats/SoATemplate/interface/SoACommon.h" | ||
#include "DataFormats/SoATemplate/interface/SoALayout.h" | ||
#include "DataFormats/SoATemplate/interface/SoAView.h" | ||
#include "FWCore/Utilities/interface/stringize.h" | ||
#include "HeterogeneousCore/AlpakaInterface/interface/config.h" | ||
#include "HeterogeneousCore/AlpakaInterface/interface/workdivision.h" | ||
|
||
// each test binary is built for a single Alpaka backend | ||
using namespace ALPAKA_ACCELERATOR_NAMESPACE; | ||
|
||
namespace { | ||
GENERATE_SOA_LAYOUT(TestLayout, SOA_COLUMN(double, x), SOA_COLUMN(int32_t, id), SOA_SCALAR(uint32_t, num)) | ||
|
||
using TestSoA = TestLayout<>; | ||
|
||
constexpr auto s_tag = "[PortableCollection]"; | ||
} // namespace | ||
|
||
TEST_CASE("PortableCollection<T, TDev>", s_tag) { | ||
// get the list of devices on the current platform | ||
auto const& devices = cms::alpakatools::devices<Platform>(); | ||
if (devices.empty()) { | ||
FAIL("No devices available for the " EDM_STRINGIZE(ALPAKA_ACCELERATOR_NAMESPACE) " backend, " | ||
"the test will be skipped."); | ||
} | ||
|
||
SECTION("Default constructor") { | ||
PortableHostCollection<TestSoA> coll_h; | ||
REQUIRE(coll_h.size() == 0); | ||
REQUIRE(not coll_h.isValid()); | ||
|
||
// Following lines would be undefined behavior, and could lead to crashes | ||
//coll->num() = 42; | ||
//REQUIRE(coll->num() == 42); | ||
|
||
// CopyToDevice<PortableHostCollection<T>> is not defined | ||
#ifndef ALPAKA_ACC_CPU_B_SEQ_T_SEQ_ENABLED | ||
for (auto const& device : devices) { | ||
auto queue = Queue(device); | ||
auto coll_d = cms::alpakatools::CopyToDevice<PortableHostCollection<TestSoA>>::copyAsync(queue, coll_h); | ||
REQUIRE(coll_d.size() == 0); | ||
REQUIRE(not coll_d.isValid()); | ||
alpaka::wait(queue); | ||
} | ||
#endif | ||
} | ||
|
||
SECTION("Zero size") { | ||
int constexpr size = 0; | ||
PortableHostCollection<TestSoA> coll_h(size, cms::alpakatools::host()); | ||
REQUIRE(coll_h.isValid()); | ||
REQUIRE(coll_h->metadata().size() == size); | ||
coll_h->num() = 42; | ||
|
||
// CopyToDevice<PortableHostCollection<T>> is not defined | ||
#ifdef ALPAKA_ACC_CPU_B_SEQ_T_SEQ_ENABLED | ||
REQUIRE(coll_h->num() == 42); | ||
#else | ||
for (auto const& device : devices) { | ||
auto queue = Queue(device); | ||
auto coll_d = cms::alpakatools::CopyToDevice<PortableHostCollection<TestSoA>>::copyAsync(queue, coll_h); | ||
REQUIRE(coll_d.isValid()); | ||
REQUIRE(coll_d.size() == size); | ||
|
||
auto div = cms::alpakatools::make_workdiv<Acc1D>(1, 1); | ||
alpaka::exec<Acc1D>( | ||
queue, | ||
div, | ||
[] ALPAKA_FN_ACC(Acc1D const& acc, TestSoA::ConstView view) { | ||
assert(view.metadata().size() == size); | ||
assert(view.num() == 42); | ||
}, | ||
coll_d.const_view()); | ||
alpaka::wait(queue); | ||
} | ||
#endif | ||
} | ||
|
||
SECTION("Non-zero size") { | ||
int constexpr size = 10; | ||
PortableHostCollection<TestSoA> coll_h(size, cms::alpakatools::host()); | ||
REQUIRE(coll_h.isValid()); | ||
coll_h->num() = 20; | ||
|
||
for (int i = 0; i < size; ++i) { | ||
coll_h->id(i) = i * 2 + 1; | ||
} | ||
|
||
// CopyToDevice<PortableHostCollection<T>> is not defined | ||
#ifdef ALPAKA_ACC_CPU_B_SEQ_T_SEQ_ENABLED | ||
for (int i = 0; i < size; ++i) { | ||
assert(coll_h->id(i) == i * 2 + 1); | ||
} | ||
#else | ||
for (auto const& device : devices) { | ||
auto queue = Queue(device); | ||
auto coll_d = cms::alpakatools::CopyToDevice<PortableHostCollection<TestSoA>>::copyAsync(queue, coll_h); | ||
REQUIRE(coll_d.isValid()); | ||
REQUIRE(coll_d.size() == size); | ||
|
||
auto div = cms::alpakatools::make_workdiv<Acc1D>(1, size); | ||
alpaka::exec<Acc1D>( | ||
queue, | ||
div, | ||
[] ALPAKA_FN_ACC(Acc1D const& acc, TestSoA::ConstView view) { | ||
assert(view.metadata().size() == size); | ||
assert(view.num() == 20); | ||
for (int i : cms::alpakatools::uniform_elements(acc)) { | ||
assert(view.id(i) == i * 2 + 1); | ||
} | ||
}, | ||
coll_d.const_view()); | ||
|
||
alpaka::wait(queue); | ||
} | ||
#endif | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
DataFormats/Portable/test/test_catch2_portableHostCollection.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include <catch.hpp> | ||
|
||
#include "DataFormats/Portable/interface/PortableHostCollection.h" | ||
#include "DataFormats/SoATemplate/interface/SoACommon.h" | ||
#include "DataFormats/SoATemplate/interface/SoALayout.h" | ||
#include "DataFormats/SoATemplate/interface/SoAView.h" | ||
|
||
namespace { | ||
GENERATE_SOA_LAYOUT(TestLayout, SOA_COLUMN(double, x), SOA_COLUMN(int32_t, id), SOA_SCALAR(uint32_t, num)) | ||
|
||
using TestSoA = TestLayout<>; | ||
|
||
constexpr auto s_tag = "[PortableHostCollection]"; | ||
} // namespace | ||
|
||
TEST_CASE("PortableHostCollection<T>", s_tag) { | ||
SECTION("Default constructor") { | ||
PortableHostCollection<TestSoA> coll; | ||
REQUIRE(coll.size() == 0); | ||
REQUIRE(not coll.isValid()); | ||
|
||
// Following lines would be undefined behavior, and could lead to crashes | ||
//coll->num() = 42; | ||
//REQUIRE(coll->num() == 42); | ||
} | ||
|
||
SECTION("Zero size") { | ||
int constexpr size = 0; | ||
PortableHostCollection<TestSoA> coll(size, cms::alpakatools::host()); | ||
REQUIRE(coll.size() == size); | ||
REQUIRE(coll.isValid()); | ||
|
||
coll->num() = 42; | ||
REQUIRE(coll->num() == 42); | ||
} | ||
|
||
SECTION("Non-zero size") { | ||
int constexpr size = 10; | ||
PortableHostCollection<TestSoA> coll(size, cms::alpakatools::host()); | ||
REQUIRE(coll.size() == size); | ||
REQUIRE(coll.isValid()); | ||
|
||
coll->num() = 42; | ||
for (int i = 0; i < size; ++i) { | ||
coll->id()[i] = i * 2; | ||
} | ||
|
||
REQUIRE(coll->num() == 42); | ||
for (int i = 0; i < size; ++i) { | ||
REQUIRE(coll->id()[i] == i * 2); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters