Skip to content

Commit 8cb6597

Browse files
committed
Add HardwareResourcesDescription
This class is inteded to deliver the resource information from ResourceInformation service to ProcessConfiguration.
1 parent f6b178d commit 8cb6597

File tree

4 files changed

+165
-1
lines changed

4 files changed

+165
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef DataFormats_Provenance_interface_HardwareResourcesDescription_h
2+
#define DataFormats_Provenance_interface_HardwareResourcesDescription_h
3+
4+
#include <iosfwd>
5+
#include <string>
6+
#include <string_view>
7+
#include <vector>
8+
9+
namespace edm {
10+
struct HardwareResourcesDescription {
11+
HardwareResourcesDescription() = default;
12+
explicit HardwareResourcesDescription(std::string_view serialized);
13+
14+
std::string serialize() const;
15+
16+
bool operator==(HardwareResourcesDescription const& other) const;
17+
18+
std::string microarchitecture;
19+
std::vector<std::string> cpuModels;
20+
std::vector<std::string> selectedAccelerators;
21+
std::vector<std::string> gpuModels;
22+
};
23+
24+
std::ostream& operator<<(std::ostream& os, HardwareResourcesDescription const& rd);
25+
} // namespace edm
26+
27+
#endif
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include "DataFormats/Provenance/interface/HardwareResourcesDescription.h"
2+
#include "FWCore/Utilities/interface/EDMException.h"
3+
#include "FWCore/Utilities/interface/compactStringSerializer.h"
4+
5+
#include <iterator>
6+
#include <ostream>
7+
8+
namespace edm {
9+
HardwareResourcesDescription::HardwareResourcesDescription(std::string_view serialized) {
10+
// allowing empty input is mostly for backwards compatibility
11+
if (not serialized.empty()) {
12+
auto ret = edm::compactString::deserialize(serialized,
13+
microarchitecture,
14+
std::back_inserter(cpuModels),
15+
std::back_inserter(selectedAccelerators),
16+
std::back_inserter(gpuModels));
17+
// not comparing against serialized.size() to allow serialized
18+
// to have more content (for kind of forward compatibility)
19+
if (ret == 0) {
20+
throw Exception(errors::EventCorruption) << "Failed to deserialize HardwareResourcesDescription string format";
21+
}
22+
}
23+
}
24+
25+
std::string HardwareResourcesDescription::serialize() const {
26+
return edm::compactString::serialize(microarchitecture, cpuModels, selectedAccelerators, gpuModels);
27+
}
28+
29+
bool HardwareResourcesDescription::operator==(HardwareResourcesDescription const& other) const {
30+
return microarchitecture == other.microarchitecture and std::ranges::equal(cpuModels, other.cpuModels) and
31+
std::ranges::equal(selectedAccelerators, other.selectedAccelerators) and
32+
std::ranges::equal(gpuModels, other.gpuModels);
33+
}
34+
35+
std::ostream& operator<<(std::ostream& os, HardwareResourcesDescription const& rd) {
36+
auto printContainer = [&os](std::string_view header, std::vector<std::string> const& cont) {
37+
os << header << ": " << cont.front();
38+
for (auto it = cont.begin() + 1; it != cont.end(); ++it) {
39+
os << ", " << *it;
40+
}
41+
};
42+
43+
os << "uarch: " << rd.microarchitecture << "\n";
44+
if (not rd.cpuModels.empty()) {
45+
printContainer("CPU models", rd.cpuModels);
46+
os << "\n";
47+
}
48+
if (not rd.selectedAccelerators.empty()) {
49+
printContainer("Selected accelerators", rd.selectedAccelerators);
50+
os << "\n";
51+
}
52+
if (not rd.gpuModels.empty()) {
53+
printContainer("GPU models", rd.gpuModels);
54+
}
55+
return os;
56+
}
57+
} // namespace edm

DataFormats/Provenance/test/BuildFile.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<use name="DataFormats/Provenance"/>
44
</bin>
55

6-
<bin name="testDataFormatsProvenanceCatch2" file="ElementID_t.cpp,Parentage_t.cpp,StoredProcessBlockHelper_t.cpp,CompactHash_t.cpp">
6+
<bin name="testDataFormatsProvenanceCatch2" file="ElementID_t.cpp,Parentage_t.cpp,StoredProcessBlockHelper_t.cpp,CompactHash_t.cpp,HardwareResourcesDescription_t.cpp">
77
<use name="DataFormats/Provenance"/>
88
<use name="catch2"/>
99
</bin>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include "catch.hpp"
2+
3+
#include "DataFormats/Provenance/interface/HardwareResourcesDescription.h"
4+
#include "FWCore/Utilities/interface/EDMException.h"
5+
6+
TEST_CASE("HardwareResourcesDescription", "[HardwareResourcesDescription]") {
7+
SECTION("Construction from empty string") {
8+
CHECK(edm::HardwareResourcesDescription("") == edm::HardwareResourcesDescription());
9+
}
10+
11+
SECTION("Default construction") {
12+
edm::HardwareResourcesDescription resources;
13+
CHECK(edm::HardwareResourcesDescription(resources.serialize()) == resources);
14+
}
15+
16+
SECTION("Microarchitecture") {
17+
edm::HardwareResourcesDescription resources;
18+
resources.microarchitecture = "x86-64-v3";
19+
CHECK(edm::HardwareResourcesDescription(resources.serialize()) == resources);
20+
}
21+
22+
SECTION("CPU models") {
23+
edm::HardwareResourcesDescription resources;
24+
resources.cpuModels = {"Intel something", "AMD something else"};
25+
CHECK(edm::HardwareResourcesDescription(resources.serialize()) == resources);
26+
}
27+
28+
SECTION("accelerators") {
29+
edm::HardwareResourcesDescription resources;
30+
resources.selectedAccelerators = {"cpu", "gpu"};
31+
CHECK(edm::HardwareResourcesDescription(resources.serialize()) == resources);
32+
}
33+
34+
SECTION("GPU models") {
35+
edm::HardwareResourcesDescription resources;
36+
resources.gpuModels = {"NVIDIA something", "NVIDIA something else"};
37+
CHECK(edm::HardwareResourcesDescription(resources.serialize()) == resources);
38+
}
39+
40+
SECTION("All fields") {
41+
edm::HardwareResourcesDescription resources;
42+
resources.microarchitecture = "x86-64-v3";
43+
resources.cpuModels = {"Intel something", "AMD something else"};
44+
resources.selectedAccelerators = {"cpu", "gpu"};
45+
resources.gpuModels = {"NVIDIA something", "NVIDIA something else"};
46+
CHECK(edm::HardwareResourcesDescription(resources.serialize()) == resources);
47+
}
48+
49+
SECTION("Serialization has additional things (forward compatibility)") {
50+
edm::HardwareResourcesDescription resources, resources2;
51+
resources.microarchitecture = "x86-64-v3";
52+
resources.cpuModels = {"Intel something", "AMD something else"};
53+
resources.selectedAccelerators = {"cpu", "gpu"};
54+
resources.gpuModels = {"NVIDIA something", "NVIDIA something else"};
55+
resources2.microarchitecture = "this";
56+
resources2.cpuModels = {"is"};
57+
resources2.selectedAccelerators = {"something"};
58+
resources2.gpuModels = {"else"};
59+
auto const serial = resources.serialize() + resources2.serialize();
60+
CHECK(edm::HardwareResourcesDescription(serial) == resources);
61+
}
62+
63+
SECTION("Error cases") {
64+
SECTION("Invalid serialized string") {
65+
CHECK_THROWS_AS(edm::HardwareResourcesDescription("foo"), edm::Exception);
66+
67+
edm::HardwareResourcesDescription resources;
68+
resources.microarchitecture = "x86-64-v3";
69+
auto serialized = resources.serialize();
70+
SECTION("Last container does not have the delimiter") {
71+
serialized.back() = ',';
72+
CHECK_THROWS_AS(edm::HardwareResourcesDescription(serialized), edm::Exception);
73+
}
74+
SECTION("Too few containers") {
75+
serialized.pop_back();
76+
CHECK_THROWS_AS(edm::HardwareResourcesDescription(serialized), edm::Exception);
77+
}
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)