Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:50:24

0001 #include <boost/core/demangle.hpp>
0002 
0003 #include <alpaka/alpaka.hpp>
0004 
0005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0006 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0007 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0008 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0009 #include "HeterogeneousCore/AlpakaCore/interface/EventCache.h"
0010 #include "HeterogeneousCore/AlpakaCore/interface/QueueCache.h"
0011 #include "HeterogeneousCore/AlpakaInterface/interface/config.h"
0012 #include "HeterogeneousCore/AlpakaInterface/interface/devices.h"
0013 #include "HeterogeneousCore/AlpakaInterface/interface/getDeviceCachingAllocator.h"
0014 #include "HeterogeneousCore/AlpakaInterface/interface/getHostCachingAllocator.h"
0015 #include "HeterogeneousCore/AlpakaServices/interface/alpaka/AlpakaService.h"
0016 
0017 #ifdef ALPAKA_ACC_GPU_CUDA_ENABLED
0018 #include "FWCore/ServiceRegistry/interface/Service.h"
0019 #include "HeterogeneousCore/CUDAServices/interface/CUDAInterface.h"
0020 #endif  // ALPAKA_ACC_GPU_CUDA_ENABLED
0021 
0022 #ifdef ALPAKA_ACC_GPU_HIP_ENABLED
0023 #include "FWCore/ServiceRegistry/interface/Service.h"
0024 #include "HeterogeneousCore/ROCmServices/interface/ROCmInterface.h"
0025 #endif  // ALPAKA_ACC_GPU_HIP_ENABLED
0026 
0027 namespace ALPAKA_ACCELERATOR_NAMESPACE {
0028 
0029   AlpakaService::AlpakaService(edm::ParameterSet const& config, edm::ActivityRegistry&)
0030       : enabled_(config.getUntrackedParameter<bool>("enabled")),
0031         verbose_(config.getUntrackedParameter<bool>("verbose")) {
0032 #ifdef ALPAKA_ACC_GPU_CUDA_ENABLED
0033     // rely on the CUDAService to initialise the CUDA devices
0034     edm::Service<CUDAInterface> cuda;
0035 #endif  // ALPAKA_ACC_GPU_CUDA_ENABLED
0036 #ifdef ALPAKA_ACC_GPU_HIP_ENABLED
0037     // rely on the ROCmService to initialise the ROCm devices
0038     edm::Service<ROCmInterface> rocm;
0039 #endif  // ALPAKA_ACC_GPU_HIP_ENABLED
0040 
0041     // TODO from Andrea Bocci:
0042     //   - handle alpaka caching allocators ?
0043     //   - extract and print more information about the platform and devices
0044 
0045     if (not enabled_) {
0046       edm::LogInfo("AlpakaService") << ALPAKA_TYPE_ALIAS_NAME(AlpakaService) << " disabled by configuration";
0047       return;
0048     }
0049 
0050 #ifdef ALPAKA_ACC_GPU_CUDA_ENABLED
0051     if (not cuda or not cuda->enabled()) {
0052       enabled_ = false;
0053       edm::LogInfo("AlpakaService") << ALPAKA_TYPE_ALIAS_NAME(AlpakaService) << " disabled by CUDAService";
0054       return;
0055     }
0056 #endif  // ALPAKA_ACC_GPU_CUDA_ENABLED
0057 #ifdef ALPAKA_ACC_GPU_HIP_ENABLED
0058     if (not rocm or not rocm->enabled()) {
0059       enabled_ = false;
0060       edm::LogInfo("AlpakaService") << ALPAKA_TYPE_ALIAS_NAME(AlpakaService) << " disabled by ROCmService";
0061       return;
0062     }
0063 #endif  // ALPAKA_ACC_GPU_HIP_ENABLED
0064 
0065     // enumerate all devices on this platform
0066     auto const& devices = cms::alpakatools::devices<Platform>();
0067     if (devices.empty()) {
0068       const std::string platform = boost::core::demangle(typeid(Platform).name());
0069       edm::LogWarning("AlpakaService") << "Could not find any devices on platform " << platform << ".\n"
0070                                        << "Disabling " << ALPAKA_TYPE_ALIAS_NAME(AlpakaService) << ".";
0071       enabled_ = false;
0072       return;
0073     }
0074 
0075     {
0076       const char* suffix[] = {"s.", ":", "s:"};
0077       const auto n = devices.size();
0078       edm::LogInfo out("AlpakaService");
0079       out << ALPAKA_TYPE_ALIAS_NAME(AlpakaService) << " succesfully initialised.\n";
0080       out << "Found " << n << " device" << suffix[n < 2 ? n : 2];
0081       for (auto const& device : devices) {
0082         out << "\n  - " << alpaka::getName(device);
0083       }
0084     }
0085 
0086     // initialise the queue and event caches
0087     cms::alpakatools::getQueueCache<Queue>().clear();
0088     cms::alpakatools::getEventCache<Event>().clear();
0089 
0090     // initialise the caching memory allocators
0091     cms::alpakatools::getHostCachingAllocator<Queue>();
0092     for (auto const& device : devices)
0093       cms::alpakatools::getDeviceCachingAllocator<Device, Queue>(device);
0094   }
0095 
0096   AlpakaService::~AlpakaService() {
0097     // clean up the caching memory allocators
0098     cms::alpakatools::getHostCachingAllocator<Queue>().freeAllCached();
0099     for (auto const& device : cms::alpakatools::devices<Platform>())
0100       cms::alpakatools::getDeviceCachingAllocator<Device, Queue>(device).freeAllCached();
0101 
0102     // clean up the queue and event caches
0103     cms::alpakatools::getQueueCache<Queue>().clear();
0104     cms::alpakatools::getEventCache<Event>().clear();
0105   }
0106 
0107   void AlpakaService::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0108     edm::ParameterSetDescription desc;
0109     desc.addUntracked<bool>("enabled", true);
0110     desc.addUntracked<bool>("verbose", false);
0111 
0112     descriptions.add(ALPAKA_TYPE_ALIAS_NAME(AlpakaService), desc);
0113   }
0114 
0115 }  // namespace ALPAKA_ACCELERATOR_NAMESPACE