Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:13:05

0001 // -*- C++ -*-
0002 //
0003 // Package:     Services
0004 // Class  :     ResourceInformationService
0005 //
0006 // Implementation:
0007 
0008 /** \class edm::service::ResourceInformationService
0009 
0010 \author W. David Dagenhart, created 29 April, 2022
0011 
0012 */
0013 
0014 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0015 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0017 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0018 #include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
0019 #include "FWCore/Utilities/interface/EDMException.h"
0020 #include "FWCore/Utilities/interface/ResourceInformation.h"
0021 
0022 #include <string>
0023 #include <vector>
0024 
0025 namespace edm {
0026   namespace service {
0027 
0028     class ResourceInformationService : public ResourceInformation {
0029     public:
0030       ResourceInformationService(ParameterSet const&, ActivityRegistry&);
0031 
0032       static void fillDescriptions(ConfigurationDescriptions&);
0033 
0034       std::vector<AcceleratorType> const& acceleratorTypes() const final;
0035       std::vector<std::string> const& cpuModels() const final;
0036       std::vector<std::string> const& gpuModels() const final;
0037 
0038       std::string const& nvidiaDriverVersion() const final;
0039       int cudaDriverVersion() const final;
0040       int cudaRuntimeVersion() const final;
0041 
0042       // Same as cpuModels except in a single string with models separated by ", "
0043       std::string const& cpuModelsFormatted() const final;
0044       double cpuAverageSpeed() const final;
0045 
0046       void initializeAcceleratorTypes(std::vector<std::string> const& selectedAccelerators) final;
0047       void setCPUModels(std::vector<std::string> const&) final;
0048       void setGPUModels(std::vector<std::string> const&) final;
0049 
0050       void setNvidiaDriverVersion(std::string const&) final;
0051       void setCudaDriverVersion(int) final;
0052       void setCudaRuntimeVersion(int) final;
0053 
0054       void setCpuModelsFormatted(std::string const&) final;
0055       void setCpuAverageSpeed(double) final;
0056 
0057       void postBeginJob();
0058 
0059     private:
0060       void throwIfLocked() const;
0061 
0062       std::vector<AcceleratorType> acceleratorTypes_;
0063       std::vector<std::string> cpuModels_;
0064       std::vector<std::string> gpuModels_;
0065 
0066       std::string nvidiaDriverVersion_;
0067       int cudaDriverVersion_ = 0;
0068       int cudaRuntimeVersion_ = 0;
0069 
0070       std::string cpuModelsFormatted_;
0071       double cpuAverageSpeed_ = 0;
0072 
0073       bool locked_ = false;
0074       bool verbose_;
0075     };
0076 
0077     inline bool isProcessWideService(ResourceInformationService const*) { return true; }
0078 
0079     ResourceInformationService::ResourceInformationService(ParameterSet const& pset, ActivityRegistry& iRegistry)
0080         : verbose_(pset.getUntrackedParameter<bool>("verbose")) {
0081       iRegistry.watchPostBeginJob(this, &ResourceInformationService::postBeginJob);
0082     }
0083 
0084     void ResourceInformationService::fillDescriptions(ConfigurationDescriptions& descriptions) {
0085       ParameterSetDescription desc;
0086       desc.addUntracked<bool>("verbose", false);
0087       descriptions.add("ResourceInformationService", desc);
0088     }
0089 
0090     std::vector<ResourceInformation::AcceleratorType> const& ResourceInformationService::acceleratorTypes() const {
0091       return acceleratorTypes_;
0092     }
0093 
0094     std::vector<std::string> const& ResourceInformationService::cpuModels() const { return cpuModels_; }
0095 
0096     std::vector<std::string> const& ResourceInformationService::gpuModels() const { return gpuModels_; }
0097 
0098     std::string const& ResourceInformationService::nvidiaDriverVersion() const { return nvidiaDriverVersion_; }
0099 
0100     int ResourceInformationService::cudaDriverVersion() const { return cudaDriverVersion_; }
0101 
0102     int ResourceInformationService::cudaRuntimeVersion() const { return cudaRuntimeVersion_; }
0103 
0104     std::string const& ResourceInformationService::cpuModelsFormatted() const { return cpuModelsFormatted_; }
0105 
0106     double ResourceInformationService::cpuAverageSpeed() const { return cpuAverageSpeed_; }
0107 
0108     void ResourceInformationService::initializeAcceleratorTypes(std::vector<std::string> const& selectedAccelerators) {
0109       if (!locked_) {
0110         for (auto const& selected : selectedAccelerators) {
0111           // Test if the string begins with "gpu-"
0112           if (selected.rfind("gpu-", 0) == 0) {
0113             acceleratorTypes_.push_back(AcceleratorType::GPU);
0114             break;
0115           }
0116         }
0117         locked_ = true;
0118       }
0119     }
0120 
0121     void ResourceInformationService::setCPUModels(std::vector<std::string> const& val) {
0122       throwIfLocked();
0123       cpuModels_ = val;
0124     }
0125 
0126     void ResourceInformationService::setGPUModels(std::vector<std::string> const& val) {
0127       throwIfLocked();
0128       gpuModels_ = val;
0129     }
0130 
0131     void ResourceInformationService::setNvidiaDriverVersion(std::string const& val) {
0132       throwIfLocked();
0133       nvidiaDriverVersion_ = val;
0134     }
0135 
0136     void ResourceInformationService::setCudaDriverVersion(int val) {
0137       throwIfLocked();
0138       cudaDriverVersion_ = val;
0139     }
0140 
0141     void ResourceInformationService::setCudaRuntimeVersion(int val) {
0142       throwIfLocked();
0143       cudaRuntimeVersion_ = val;
0144     }
0145 
0146     void ResourceInformationService::setCpuModelsFormatted(std::string const& val) {
0147       throwIfLocked();
0148       cpuModelsFormatted_ = val;
0149     }
0150 
0151     void ResourceInformationService::setCpuAverageSpeed(double val) {
0152       throwIfLocked();
0153       cpuAverageSpeed_ = val;
0154     }
0155 
0156     void ResourceInformationService::throwIfLocked() const {
0157       if (locked_) {
0158         // Only Services should modify ResourceInformationService. Service construction is run serially.
0159         // The lock provides thread safety and prevents modules from modifying ResourceInformationService.
0160         throw edm::Exception(errors::LogicError)
0161             << "Attempt to modify member data after ResourceInformationService was locked ";
0162       }
0163     }
0164 
0165     void ResourceInformationService::postBeginJob() {
0166       if (verbose_) {
0167         LogAbsolute("ResourceInformation") << "ResourceInformationService";
0168         LogAbsolute("ResourceInformation") << "    cpu models:";
0169         if (cpuModels().empty()) {
0170           LogAbsolute("ResourceInformation") << "        None";
0171         } else {
0172           for (auto const& iter : cpuModels()) {
0173             LogAbsolute("ResourceInformation") << "        " << iter;
0174           }
0175         }
0176         LogAbsolute("ResourceInformation") << "    gpu models:";
0177         if (gpuModels().empty()) {
0178           LogAbsolute("ResourceInformation") << "        None";
0179         } else {
0180           for (auto const& iter : gpuModels()) {
0181             LogAbsolute("ResourceInformation") << "        " << iter;
0182           }
0183         }
0184 
0185         LogAbsolute("ResourceInformation") << "    acceleratorTypes:";
0186         if (acceleratorTypes().empty()) {
0187           LogAbsolute("ResourceInformation") << "        None";
0188         } else {
0189           for (auto const& iter : acceleratorTypes()) {
0190             std::string acceleratorTypeString("unknown type");
0191             if (iter == AcceleratorType::GPU) {
0192               acceleratorTypeString = std::string("GPU");
0193             }
0194             LogAbsolute("ResourceInformation") << "        " << acceleratorTypeString;
0195           }
0196         }
0197         LogAbsolute("ResourceInformation") << "    nvidiaDriverVersion: " << nvidiaDriverVersion();
0198         LogAbsolute("ResourceInformation") << "    cudaDriverVersion: " << cudaDriverVersion();
0199         LogAbsolute("ResourceInformation") << "    cudaRuntimeVersion: " << cudaRuntimeVersion();
0200         LogAbsolute("ResourceInformation") << "    cpuModelsFormatted: " << cpuModelsFormatted();
0201         LogAbsolute("ResourceInformation") << "    cpuAverageSpeed: " << cpuAverageSpeed();
0202       }
0203     }
0204 
0205   }  // namespace service
0206 }  // namespace edm
0207 
0208 #include "FWCore/ServiceRegistry/interface/ServiceMaker.h"
0209 
0210 using edm::service::ResourceInformationService;
0211 using ResourceInformationMaker =
0212     edm::serviceregistry::AllArgsMaker<edm::ResourceInformation, ResourceInformationService>;
0213 DEFINE_FWK_SERVICE_MAKER(ResourceInformationService, ResourceInformationMaker);