CMS 3D CMS Logo

SharedResourcesRegistry.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: FWCore/Framework
4 // Class : SharedResourcesRegistry
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author: Chris Jones
10 // Created: Sun, 06 Oct 2013 15:48:50 GMT
11 //
12 
13 // system include files
14 #include <algorithm>
15 #include <cassert>
16 
17 // user include files
20 
21 namespace edm {
22 
24 
26  static SharedResourcesRegistry s_instance;
27  return &s_instance;
28  }
29 
31 
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  }
62 
63  std::pair<SharedResourcesAcquirer, std::shared_ptr<std::recursive_mutex>>
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  }
74 
75  SharedResourcesAcquirer SharedResourcesRegistry::createAcquirer(std::vector<std::string> const& resourceNames) const {
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  //If only one module wants it, it really isn't shared
100  if (resource.second.second > 1) {
101  sortedResources.insert(
102  std::make_pair(std::make_pair(resource.second.second, resource.first), resource.second.first));
103  }
104  }
105  // Handle cases where the module does not declare the legacy resource
106  } else {
107  for (auto const& name : resourceNames) {
108  auto resource = resourceMap_.find(name);
109  assert(resource != resourceMap_.end());
110  //If only one module wants it, it really isn't shared
111  if (resource->second.second > 1) {
112  sortedResources.insert(
113  std::make_pair(std::make_pair(resource->second.second, resource->first), resource->second.first));
114  }
115  }
116  }
117 
118  std::vector<std::shared_ptr<SerialTaskQueue>> queues;
119  queues.reserve(sortedResources.size());
120  for (auto const& resource : sortedResources) {
121  queues.push_back(resource.second);
122  }
123  if (queues.empty()) {
124  //Calling code is depending on there being at least one shared queue
125  queues.reserve(1);
126  queues.push_back(std::make_shared<SerialTaskQueue>());
127  }
128 
129  return SharedResourcesAcquirer(std::move(queues));
130  }
131 } // namespace edm
edm::SharedResourcesAcquirer
Definition: SharedResourcesAcquirer.h:34
edm::SharedResourcesRegistry::resourceForDelayedReader_
edm::propagate_const< std::shared_ptr< std::recursive_mutex > > resourceForDelayedReader_
Definition: SharedResourcesRegistry.h:77
edm::SharedResourcesRegistry::registerSharedResource
void registerSharedResource(const std::string &)
A resource name must be registered before it can be used in the createAcquirer call.
Definition: SharedResourcesRegistry.cc:32
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::SharedResourcesRegistry::kLegacyModuleResourceName
static const std::string kLegacyModuleResourceName
All legacy modules share this resource.
Definition: SharedResourcesRegistry.h:53
cms::cuda::assert
assert(be >=bs)
edm::SharedResourcesRegistry::createAcquirerForSourceDelayedReader
std::pair< SharedResourcesAcquirer, std::shared_ptr< std::recursive_mutex > > createAcquirerForSourceDelayedReader()
Definition: SharedResourcesRegistry.cc:64
spr::find
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
edm::get_underlying
T & get_underlying(propagate_const< T > &)
Definition: propagate_const.h:103
edm::SharedResourcesRegistry::queueForDelayedReader_
edm::propagate_const< std::shared_ptr< SerialTaskQueue > > queueForDelayedReader_
Definition: SharedResourcesRegistry.h:79
SharedResourcesAcquirer.h
edm::SharedResourcesRegistry::SharedResourcesRegistry
SharedResourcesRegistry()
Definition: SharedResourcesRegistry.cc:30
mitigatedMETSequence_cff.U
U
Definition: mitigatedMETSequence_cff.py:36
edm::SharedResourcesRegistry::instance
static SharedResourcesRegistry * instance()
Definition: SharedResourcesRegistry.cc:25
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::SharedResourcesRegistry::createAcquirer
SharedResourcesAcquirer createAcquirer(std::vector< std::string > const &) const
Definition: SharedResourcesRegistry.cc:75
edm::SharedResourcesRegistry
Definition: SharedResourcesRegistry.h:39
eostools.move
def move(src, dest)
Definition: eostools.py:511
SharedResourcesRegistry.h
edm::SharedResourcesRegistry::nLegacy_
unsigned int nLegacy_
Definition: SharedResourcesRegistry.h:81
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
edm::SharedResourcesRegistry::resourceMap_
std::map< std::string, std::pair< std::shared_ptr< SerialTaskQueue >, unsigned int > > resourceMap_
Definition: SharedResourcesRegistry.h:75