Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement ResourceInformationService
- Loading branch information
Showing
9 changed files
with
262 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// -*- C++ -*- | ||
// | ||
// Package: Services | ||
// Class : ResourceInformationService | ||
// | ||
// Implementation: | ||
|
||
/** \class edm::service::ResourceInformationService | ||
\author W. David Dagenhart, created 29 April, 2022 | ||
*/ | ||
|
||
#include "FWCore/MessageLogger/interface/MessageLogger.h" | ||
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h" | ||
#include "FWCore/ParameterSet/interface/ParameterSet.h" | ||
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h" | ||
#include "FWCore/ServiceRegistry/interface/ActivityRegistry.h" | ||
#include "FWCore/ServiceRegistry/interface/ServiceMaker.h" | ||
#include "FWCore/Utilities/interface/EDMException.h" | ||
#include "FWCore/Utilities/interface/ResourceInformation.h" | ||
|
||
#include <map> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace edm { | ||
namespace service { | ||
|
||
class ResourceInformationService : public ResourceInformation { | ||
public: | ||
ResourceInformationService(ParameterSet const&, ActivityRegistry&); | ||
|
||
static void fillDescriptions(ConfigurationDescriptions&); | ||
|
||
std::vector<AcceleratorType> const& acceleratorTypes() const final; | ||
std::map<std::string, unsigned int> const& models() const final; | ||
|
||
void insertIntoModels(std::map<std::string, unsigned int> const& iModels) final; | ||
void initializeAcceleratorTypes(std::vector<std::string> const& selectedAccelerators) final; | ||
void lock() final; | ||
|
||
void postBeginJob(); | ||
|
||
private: | ||
std::vector<AcceleratorType> acceleratorTypes_; | ||
std::map<std::string, unsigned int> models_; | ||
bool locked_ = false; | ||
bool verbose_; | ||
}; | ||
|
||
ResourceInformationService::ResourceInformationService(ParameterSet const& pset, ActivityRegistry& iRegistry) | ||
: verbose_(pset.getUntrackedParameter<bool>("verbose")) { | ||
iRegistry.watchPostBeginJob(this, &ResourceInformationService::postBeginJob); | ||
} | ||
|
||
void ResourceInformationService::fillDescriptions(ConfigurationDescriptions& descriptions) { | ||
ParameterSetDescription desc; | ||
desc.addUntracked<bool>("verbose", false); | ||
descriptions.add("ResourceInformationService", desc); | ||
} | ||
|
||
std::vector<ResourceInformation::AcceleratorType> const& ResourceInformationService::acceleratorTypes() const { | ||
return acceleratorTypes_; | ||
} | ||
std::map<std::string, unsigned int> const& ResourceInformationService::models() const { return models_; } | ||
|
||
void ResourceInformationService::insertIntoModels(std::map<std::string, unsigned int> const& iModels) { | ||
if (locked_) { | ||
// Only Services should call this function. Service construction is run serially. | ||
// The lock provides thread safety and prevents modules from modifying models_. | ||
throw edm::Exception(errors::LogicError) | ||
<< "Attempt to add models after ResourceInformationService was locked "; | ||
} | ||
for (const auto& [key, value] : iModels) { | ||
auto& count = models_[key]; | ||
count += value; | ||
} | ||
} | ||
|
||
void ResourceInformationService::initializeAcceleratorTypes(std::vector<std::string> const& selectedAccelerators) { | ||
if (locked_) { | ||
// The lock provides thread safety and prevents modules from modifying acceleratorTypes_. | ||
throw edm::Exception(errors::LogicError) | ||
<< "Attempt to add models after ResourceInformationService was locked "; | ||
} | ||
for (auto const& selected : selectedAccelerators) { | ||
// Test if the string begins with "gpu-" | ||
if (selected.rfind("gpu-", 0) == 0) { | ||
acceleratorTypes_.push_back(AcceleratorType::GPU); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
void ResourceInformationService::lock() { locked_ = true; } | ||
|
||
void ResourceInformationService::postBeginJob() { | ||
if (verbose_) { | ||
LogAbsolute("ResourceInformation") << "ResourceInformationService"; | ||
LogAbsolute("ResourceInformation") << " models:"; | ||
if (models().empty()) { | ||
LogAbsolute("ResourceInformation") << " None"; | ||
} else { | ||
for (auto const& iter : models()) { | ||
LogAbsolute("ResourceInformation") << " " << iter.first << " " << iter.second; | ||
} | ||
} | ||
|
||
LogAbsolute("ResourceInformation") << " acceleratorTypes:"; | ||
if (acceleratorTypes().empty()) { | ||
LogAbsolute("ResourceInformation") << " None"; | ||
} else { | ||
for (auto const& iter : acceleratorTypes()) { | ||
std::string acceleratorTypeString("unknown type"); | ||
if (iter == AcceleratorType::GPU) { | ||
acceleratorTypeString = std::string("GPU"); | ||
} | ||
LogAbsolute("ResourceInformation") << " " << acceleratorTypeString; | ||
} | ||
} | ||
} | ||
} | ||
|
||
} // namespace service | ||
} // namespace edm | ||
|
||
using edm::service::ResourceInformationService; | ||
using ResourceInformationMaker = | ||
edm::serviceregistry::AllArgsMaker<edm::ResourceInformation, ResourceInformationService>; | ||
DEFINE_FWK_SERVICE_MAKER(ResourceInformationService, ResourceInformationMaker); |
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,16 @@ | ||
#!/bin/bash | ||
|
||
LOCAL_TEST_DIR=${CMSSW_BASE}/src/FWCore/Services/test | ||
LOCAL_TMP_DIR=${CMSSW_BASE}/tmp/${SCRAM_ARCH} | ||
|
||
function die { echo Failure $1: status $2 ; exit $2 ; } | ||
|
||
pushd ${LOCAL_TMP_DIR} | ||
|
||
cmsRun -p ${LOCAL_TEST_DIR}/test_resourceInformationService_cfg.py &> test_resourceInformationService.log || die "cmsRun test_resourceInformationService_cfg.py" $? | ||
|
||
grep -A 1 "acceleratorTypes:" test_resourceInformationService.log | grep "GPU" || die "Check for existence of GPU acceleratorType" $? | ||
grep -A 1 "models:" test_resourceInformationService.log | grep "None" && die "Check there is at least one model (not None)" 1 | ||
popd | ||
|
||
exit 0 |
30 changes: 30 additions & 0 deletions
30
FWCore/Services/test/test_resourceInformationService_cfg.py
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,30 @@ | ||
# Test ResourceInformationService | ||
# The shell script will examine its printout | ||
import FWCore.ParameterSet.Config as cms | ||
|
||
# This class is a hack just for the test to get | ||
# 'gpu-foo' into @selected_accelerators | ||
class ProcessAcceleratorTest(cms.ProcessAccelerator): | ||
def __init__(self): | ||
super(ProcessAcceleratorTest,self).__init__() | ||
self._labels = ["test1", "gpu-foo", "test2"] | ||
self._enabled = ["test1", "gpu-foo", "test2"] | ||
def labels(self): | ||
return self._labels | ||
def enabledLabels(self): | ||
return self._enabled | ||
|
||
process = cms.Process("PROD") | ||
process.add_(ProcessAcceleratorTest()) | ||
|
||
process.ResourceInformationService = cms.Service("ResourceInformationService", | ||
verbose = cms.untracked.bool(True) | ||
) | ||
process.CPU = cms.Service("CPU") | ||
process.options.accelerators = ["*"] | ||
|
||
process.maxEvents = cms.untracked.PSet( | ||
input = cms.untracked.int32(1) | ||
) | ||
process.source = cms.Source("EmptySource") | ||
|
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,36 @@ | ||
#ifndef FWCore_Utilities_ResourceInformation_h | ||
#define FWCore_Utilities_ResourceInformation_h | ||
|
||
/** \class edm::ResourceInformation | ||
Description: | ||
Usage: | ||
\author W. David Dagenhart, created May 3, 2022 | ||
*/ | ||
|
||
#include <map> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace edm { | ||
|
||
class ResourceInformation { | ||
public: | ||
ResourceInformation(); | ||
ResourceInformation(ResourceInformation const&) = delete; | ||
ResourceInformation const& operator=(ResourceInformation const&) = delete; | ||
virtual ~ResourceInformation(); | ||
|
||
enum class AcceleratorType { GPU }; | ||
|
||
virtual std::vector<AcceleratorType> const& acceleratorTypes() const = 0; | ||
virtual std::map<std::string, unsigned int> const& models() const = 0; | ||
|
||
virtual void insertIntoModels(std::map<std::string, unsigned int> const& iModels) = 0; | ||
virtual void initializeAcceleratorTypes(std::vector<std::string> const& selectedAccelerators) = 0; | ||
virtual void lock() = 0; | ||
}; | ||
} // namespace edm | ||
#endif |
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,4 @@ | ||
#include "FWCore/Utilities/interface/ResourceInformation.h" | ||
|
||
edm::ResourceInformation::ResourceInformation() {} | ||
edm::ResourceInformation::~ResourceInformation() {} |
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