Skip to content

Commit

Permalink
Add HardwareResourcesDescription
Browse files Browse the repository at this point in the history
This class is inteded to deliver the resource information from
ResourceInformation service to ProcessConfiguration.
  • Loading branch information
makortel committed Jan 29, 2025
1 parent c5aa587 commit 36822c9
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 1 deletion.
27 changes: 27 additions & 0 deletions DataFormats/Provenance/interface/HardwareResourcesDescription.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef DataFormats_Provenance_interface_HardwareResourcesDescription_h
#define DataFormats_Provenance_interface_HardwareResourcesDescription_h

#include <iosfwd>
#include <string>
#include <string_view>
#include <vector>

namespace edm {
struct HardwareResourcesDescription {
HardwareResourcesDescription() = default;
explicit HardwareResourcesDescription(std::string_view serialized);

std::string serialize() const;

bool operator==(HardwareResourcesDescription const& other) const;

std::string microarchitecture;
std::vector<std::string> cpuModels;
std::vector<std::string> selectedAccelerators;
std::vector<std::string> gpuModels;
};

std::ostream& operator<<(std::ostream& os, HardwareResourcesDescription const& rd);
} // namespace edm

#endif
57 changes: 57 additions & 0 deletions DataFormats/Provenance/src/HardwareResourcesDescription.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "DataFormats/Provenance/interface/HardwareResourcesDescription.h"
#include "FWCore/Utilities/interface/EDMException.h"
#include "FWCore/Utilities/interface/compactStringSerializer.h"

#include <iterator>
#include <ostream>

namespace edm {
HardwareResourcesDescription::HardwareResourcesDescription(std::string_view serialized) {
// allowing empty input is mostly for backwards compatibility
if (not serialized.empty()) {
auto ret = edm::compactString::deserialize(serialized,
microarchitecture,
std::back_inserter(cpuModels),
std::back_inserter(selectedAccelerators),
std::back_inserter(gpuModels));
// not comparing against serialized.size() to allow serialized
// to have more content (for kind of forward compatibility)
if (ret == 0) {
throw Exception(errors::EventCorruption) << "Failed to deserialize HardwareResourcesDescription string format";
}
}
}

std::string HardwareResourcesDescription::serialize() const {
return edm::compactString::serialize(microarchitecture, cpuModels, selectedAccelerators, gpuModels);
}

bool HardwareResourcesDescription::operator==(HardwareResourcesDescription const& other) const {
return microarchitecture == other.microarchitecture and std::ranges::equal(cpuModels, other.cpuModels) and
std::ranges::equal(selectedAccelerators, other.selectedAccelerators) and
std::ranges::equal(gpuModels, other.gpuModels);
}

std::ostream& operator<<(std::ostream& os, HardwareResourcesDescription const& rd) {
auto printContainer = [&os](std::string_view header, std::vector<std::string> const& cont) {
os << header << ": " << cont.front();
for (auto it = cont.begin() + 1; it != cont.end(); ++it) {
os << ", " << *it;
}
};

os << "uarch: " << rd.microarchitecture << "\n";
if (not rd.cpuModels.empty()) {
printContainer("CPU models", rd.cpuModels);
os << "\n";
}
if (not rd.selectedAccelerators.empty()) {
printContainer("Selected accelerators", rd.selectedAccelerators);
os << "\n";
}
if (not rd.gpuModels.empty()) {
printContainer("GPU models", rd.gpuModels);
}
return os;
}
} // namespace edm
2 changes: 1 addition & 1 deletion DataFormats/Provenance/test/BuildFile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<use name="DataFormats/Provenance"/>
</bin>

<bin name="testDataFormatsProvenanceCatch2" file="ElementID_t.cpp,Parentage_t.cpp,StoredProcessBlockHelper_t.cpp,CompactHash_t.cpp">
<bin name="testDataFormatsProvenanceCatch2" file="ElementID_t.cpp,Parentage_t.cpp,StoredProcessBlockHelper_t.cpp,CompactHash_t.cpp,HardwareResourcesDescription_t.cpp">
<use name="DataFormats/Provenance"/>
<use name="catch2"/>
</bin>
Expand Down
76 changes: 76 additions & 0 deletions DataFormats/Provenance/test/HardwareResourcesDescription_t.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "catch.hpp"

#include "DataFormats/Provenance/interface/HardwareResourcesDescription.h"
#include "FWCore/Utilities/interface/EDMException.h"

TEST_CASE("HardwareResourcesDescription", "[HardwareResourcesDescription]") {
SECTION("Construction from empty string") {
CHECK(edm::HardwareResourcesDescription("") == edm::HardwareResourcesDescription());
}

SECTION("Default construction") {
edm::HardwareResourcesDescription resources;
CHECK(edm::HardwareResourcesDescription(resources.serialize()) == resources);
}

SECTION("Microarchitecture") {
edm::HardwareResourcesDescription resources;
resources.microarchitecture = "x86-64-v3";
CHECK(edm::HardwareResourcesDescription(resources.serialize()) == resources);
}

SECTION("CPU models") {
edm::HardwareResourcesDescription resources;
resources.cpuModels = {"Intel something", "AMD something else"};
CHECK(edm::HardwareResourcesDescription(resources.serialize()) == resources);
}

SECTION("accelerators") {
edm::HardwareResourcesDescription resources;
resources.selectedAccelerators = {"cpu", "gpu"};
CHECK(edm::HardwareResourcesDescription(resources.serialize()) == resources);
}

SECTION("GPU models") {
edm::HardwareResourcesDescription resources;
resources.gpuModels = {"NVIDIA something", "NVIDIA something else"};
CHECK(edm::HardwareResourcesDescription(resources.serialize()) == resources);
}

SECTION("All fields") {
edm::HardwareResourcesDescription resources;
resources.microarchitecture = "x86-64-v3";
resources.cpuModels = {"Intel something", "AMD something else"};
resources.selectedAccelerators = {"cpu", "gpu"};
resources.gpuModels = {"NVIDIA something", "NVIDIA something else"};
CHECK(edm::HardwareResourcesDescription(resources.serialize()) == resources);
}

SECTION("Serialization has additional things (forward compatibility)") {
edm::HardwareResourcesDescription resources, resources2;
resources.microarchitecture = "x86-64-v3";
resources.cpuModels = {"Intel something", "AMD something else"};
resources.selectedAccelerators = {"cpu", "gpu"};
resources.gpuModels = {"NVIDIA something", "NVIDIA something else"};
resources2.microarchitecture = "this";
resources2.cpuModels = {"is"};
resources2.selectedAccelerators = {"something"};
resources2.gpuModels = {"else"};
auto const serial = resources.serialize() + resources2.serialize();
CHECK(edm::HardwareResourcesDescription(serial) == resources);
}

SECTION("Error cases") {
SECTION("Invalid serialized string") {
CHECK_THROWS_AS(edm::HardwareResourcesDescription("foo"), edm::Exception);

edm::HardwareResourcesDescription resources;
resources.microarchitecture = "x86-64-v3";
auto serialized = resources.serialize();
serialized.back() = ',';
CHECK_THROWS_AS(edm::HardwareResourcesDescription(serialized), edm::Exception);
serialized.pop_back();
CHECK_THROWS_AS(edm::HardwareResourcesDescription(serialized), edm::Exception);
}
}
}

0 comments on commit 36822c9

Please sign in to comment.