-
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.
This class is inteded to deliver the resource information from ResourceInformation service to ProcessConfiguration.
- Loading branch information
Showing
4 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
DataFormats/Provenance/interface/HardwareResourcesDescription.h
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,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
57
DataFormats/Provenance/src/HardwareResourcesDescription.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,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 |
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
76 changes: 76 additions & 0 deletions
76
DataFormats/Provenance/test/HardwareResourcesDescription_t.cpp
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,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); | ||
} | ||
} | ||
} |