CMS 3D CMS Logo

List of all members | Public Member Functions | Static Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | Friends
edm::SharedResourcesRegistry Class Reference

#include <SharedResourcesRegistry.h>

Public Member Functions

SharedResourcesAcquirer createAcquirer (std::vector< std::string > const &) const
 
std::pair< SharedResourcesAcquirer, std::shared_ptr< std::recursive_mutex > > createAcquirerForSourceDelayedReader ()
 
const SharedResourcesRegistryoperator= (const SharedResourcesRegistry &)=delete
 
void registerSharedResource (const std::string &)
 A resource name must be registered before it can be used in the createAcquirer call. More...
 
 SharedResourcesRegistry (const SharedResourcesRegistry &)=delete
 

Static Public Member Functions

static SharedResourcesRegistryinstance ()
 

Static Public Attributes

static const std::string kLegacyModuleResourceName {"__legacy__"}
 All legacy modules share this resource. More...
 

Private Member Functions

 SharedResourcesRegistry ()
 
 ~SharedResourcesRegistry ()=default
 

Private Attributes

unsigned int nLegacy_
 
edm::propagate_const< std::shared_ptr< SerialTaskQueue > > queueForDelayedReader_
 
edm::propagate_const< std::shared_ptr< std::recursive_mutex > > resourceForDelayedReader_
 
std::map< std::string, std::pair< std::shared_ptr< SerialTaskQueue >, unsigned int > > resourceMap_
 

Friends

class ::testSharedResourcesRegistry
 

Detailed Description

Definition at line 39 of file SharedResourcesRegistry.h.

Constructor & Destructor Documentation

◆ SharedResourcesRegistry() [1/2]

edm::SharedResourcesRegistry::SharedResourcesRegistry ( const SharedResourcesRegistry )
delete

◆ SharedResourcesRegistry() [2/2]

SharedResourcesRegistry::SharedResourcesRegistry ( )
private

Definition at line 30 of file SharedResourcesRegistry.cc.

◆ ~SharedResourcesRegistry()

edm::SharedResourcesRegistry::~SharedResourcesRegistry ( )
privatedefault

Member Function Documentation

◆ createAcquirer()

SharedResourcesAcquirer SharedResourcesRegistry::createAcquirer ( std::vector< std::string > const &  resourceNames) const

Definition at line 75 of file SharedResourcesRegistry.cc.

References cms::cuda::assert(), spr::find(), kLegacyModuleResourceName, eostools::move(), Skims_PA_cff::name, and resourceMap_.

Referenced by edm::one::impl::SharedResourcesUser< T >::createAcquirer(), edm::EDAnalyzer::doBeginJob(), edm::EDProducer::doBeginJob(), and edm::EDFilter::doBeginJob().

75  {
76  // The acquirer will acquire the shared resources declared by a module
77  // so that only it can use those resources while it runs. The other
78  // modules using the same resource will not be run until the module
79  // that acquired the resources completes its task.
80 
81  // The legacy shared resource is special.
82  // Legacy modules cannot run concurrently with each other or
83  // any other module that has declared any shared resource. Treat
84  // one modules that call usesResource with no argument in the
85  // same way.
86 
87  // Sort by how often used and then by name
88  // Consistent sorting avoids deadlocks and this particular order optimizes performance
89  std::map<std::pair<unsigned int, std::string>, std::shared_ptr<SerialTaskQueue>> sortedResources;
90 
91  // Is this acquirer for a module that depends on the legacy shared resource?
92  if (std::find(resourceNames.begin(), resourceNames.end(), kLegacyModuleResourceName) != resourceNames.end()) {
93  for (auto const& resource : resourceMap_) {
94  // It's redundant to declare legacy if the legacy modules
95  // all declare all the other resources, so just skip it.
96  // But if the only shared resource is the legacy resource don't skip it.
97  if (resource.first == kLegacyModuleResourceName && resourceMap_.size() > 1)
98  continue;
99  //legacy modules are not allowed to depend on ES shared resources
100  if (resource.first.substr(0, 3) == "es_")
101  continue;
102  //If only one module wants it, it really isn't shared
103  if (resource.second.second > 1) {
104  sortedResources.insert(
105  std::make_pair(std::make_pair(resource.second.second, resource.first), resource.second.first));
106  }
107  }
108  // Handle cases where the module does not declare the legacy resource
109  } else {
110  for (auto const& name : resourceNames) {
111  auto resource = resourceMap_.find(name);
112  assert(resource != resourceMap_.end());
113  //If only one module wants it, it really isn't shared
114  if (resource->second.second > 1) {
115  sortedResources.insert(
116  std::make_pair(std::make_pair(resource->second.second, resource->first), resource->second.first));
117  }
118  }
119  }
120 
121  std::vector<std::shared_ptr<SerialTaskQueue>> queues;
122  queues.reserve(sortedResources.size());
123  for (auto const& resource : sortedResources) {
124  queues.push_back(resource.second);
125  }
126  if (queues.empty()) {
127  //Calling code is depending on there being at least one shared queue
128  queues.reserve(1);
129  queues.push_back(std::make_shared<SerialTaskQueue>());
130  }
131 
132  return SharedResourcesAcquirer(std::move(queues));
133  }
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
assert(be >=bs)
std::map< std::string, std::pair< std::shared_ptr< SerialTaskQueue >, unsigned int > > resourceMap_
static const std::string kLegacyModuleResourceName
All legacy modules share this resource.
def move(src, dest)
Definition: eostools.py:511

◆ createAcquirerForSourceDelayedReader()

std::pair< SharedResourcesAcquirer, std::shared_ptr< std::recursive_mutex > > SharedResourcesRegistry::createAcquirerForSourceDelayedReader ( )

Definition at line 64 of file SharedResourcesRegistry.cc.

References edm::get_underlying(), eostools::move(), queueForDelayedReader_, and resourceForDelayedReader_.

Referenced by edm::PoolSource::PoolSource(), and edm::RootDelayedReader::RootDelayedReader().

64  {
65  if (not resourceForDelayedReader_) {
67  std::make_shared<std::recursive_mutex>(); // propagate_const<T> has no reset() function
68  queueForDelayedReader_ = std::make_shared<SerialTaskQueue>();
69  }
70 
71  std::vector<std::shared_ptr<SerialTaskQueue>> queues = {get_underlying(queueForDelayedReader_)};
73  }
edm::propagate_const< std::shared_ptr< SerialTaskQueue > > queueForDelayedReader_
edm::propagate_const< std::shared_ptr< std::recursive_mutex > > resourceForDelayedReader_
constexpr T & get_underlying(propagate_const< T > &)
def move(src, dest)
Definition: eostools.py:511

◆ instance()

SharedResourcesRegistry * SharedResourcesRegistry::instance ( )
static

◆ operator=()

const SharedResourcesRegistry& edm::SharedResourcesRegistry::operator= ( const SharedResourcesRegistry )
delete

◆ registerSharedResource()

void SharedResourcesRegistry::registerSharedResource ( const std::string &  resourceName)

A resource name must be registered before it can be used in the createAcquirer call.

Definition at line 32 of file SharedResourcesRegistry.cc.

References kLegacyModuleResourceName, nLegacy_, resourceMap_, and mitigatedMETSequence_cff::U.

Referenced by edm::EDAnalyzer::EDAnalyzer(), edm::EDFilter::EDFilter(), edm::EDProducer::EDProducer(), and edm::one::impl::SharedResourcesUser< T >::usesResource().

32  {
33  auto& queueAndCounter = resourceMap_[resourceName];
34 
35  if (resourceName == kLegacyModuleResourceName) {
36  ++nLegacy_;
37  for (auto& resource : resourceMap_) {
38  if (!resource.second.first) {
39  resource.second.first = std::make_shared<SerialTaskQueue>();
40  }
41  ++resource.second.second;
42  }
43  } else {
44  // count the number of times the resource was registered
45  ++queueAndCounter.second;
46 
47  // When first registering a nonlegacy resource, we have to
48  // account for any legacy resource registrations already made.
49  if (queueAndCounter.second == 1) {
50  if (nLegacy_ > 0U) {
51  queueAndCounter.first = std::make_shared<SerialTaskQueue>();
52  queueAndCounter.second += nLegacy_;
53  }
54  // If registering a nonlegacy resource the second time and
55  // the legacy resource has not been registered yet,
56  // we know we will need the queue so go ahead and create it.
57  } else if (queueAndCounter.second == 2) {
58  queueAndCounter.first = std::make_shared<SerialTaskQueue>();
59  }
60  }
61  }
std::map< std::string, std::pair< std::shared_ptr< SerialTaskQueue >, unsigned int > > resourceMap_
static const std::string kLegacyModuleResourceName
All legacy modules share this resource.

Friends And Related Function Documentation

◆ ::testSharedResourcesRegistry

friend class ::testSharedResourcesRegistry
friend

Definition at line 42 of file SharedResourcesRegistry.h.

Member Data Documentation

◆ kLegacyModuleResourceName

const std::string SharedResourcesRegistry::kLegacyModuleResourceName {"__legacy__"}
static

◆ nLegacy_

unsigned int edm::SharedResourcesRegistry::nLegacy_
private

Definition at line 80 of file SharedResourcesRegistry.h.

Referenced by registerSharedResource().

◆ queueForDelayedReader_

edm::propagate_const<std::shared_ptr<SerialTaskQueue> > edm::SharedResourcesRegistry::queueForDelayedReader_
private

Definition at line 78 of file SharedResourcesRegistry.h.

Referenced by createAcquirerForSourceDelayedReader().

◆ resourceForDelayedReader_

edm::propagate_const<std::shared_ptr<std::recursive_mutex> > edm::SharedResourcesRegistry::resourceForDelayedReader_
private

Definition at line 76 of file SharedResourcesRegistry.h.

Referenced by createAcquirerForSourceDelayedReader().

◆ resourceMap_

std::map<std::string, std::pair<std::shared_ptr<SerialTaskQueue>, unsigned int> > edm::SharedResourcesRegistry::resourceMap_
private

Definition at line 74 of file SharedResourcesRegistry.h.

Referenced by createAcquirer(), and registerSharedResource().