Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-02-12 04:02:26

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