Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-08-15 01:07:43

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/CUDAService.h"
0020 #endif  // ALPAKA_ACC_GPU_CUDA_ENABLED
0021 
0022 namespace ALPAKA_ACCELERATOR_NAMESPACE {
0023 
0024   AlpakaService::AlpakaService(edm::ParameterSet const& config, edm::ActivityRegistry&)
0025       : enabled_(config.getUntrackedParameter<bool>("enabled")),
0026         verbose_(config.getUntrackedParameter<bool>("verbose")) {
0027 #ifdef ALPAKA_ACC_GPU_CUDA_ENABLED
0028     // rely on the CUDAService to initialise the CUDA devices
0029     edm::Service<CUDAService> cudaService;
0030 #endif  // ALPAKA_ACC_GPU_CUDA_ENABLED
0031 
0032     // TODO from Andrea Bocci:
0033     //   - handle alpaka caching allocators ?
0034     //   - extract and print more information about the platform and devices
0035 
0036     if (not enabled_) {
0037       edm::LogInfo("AlpakaService") << ALPAKA_TYPE_ALIAS_NAME(AlpakaService) << " disabled by configuration";
0038       return;
0039     }
0040 
0041 #ifdef ALPAKA_ACC_GPU_CUDA_ENABLED
0042     if (not cudaService->enabled()) {
0043       enabled_ = false;
0044       edm::LogInfo("AlpakaService") << ALPAKA_TYPE_ALIAS_NAME(AlpakaService) << " disabled by CUDAService";
0045       return;
0046     }
0047 #endif  // ALPAKA_ACC_GPU_CUDA_ENABLED
0048 
0049     // enumerate all devices on this platform
0050     auto const& devices = cms::alpakatools::devices<Platform>();
0051     if (devices.empty()) {
0052       const std::string platform = boost::core::demangle(typeid(Platform).name());
0053       edm::LogWarning("AlpakaService") << "Could not find any devices on platform " << platform << ".\n"
0054                                        << "Disabling " << ALPAKA_TYPE_ALIAS_NAME(AlpakaService) << ".";
0055       enabled_ = false;
0056       return;
0057     }
0058 
0059     {
0060       const char* suffix[] = {"s.", ":", "s:"};
0061       const auto n = devices.size();
0062       edm::LogInfo out("AlpakaService");
0063       out << ALPAKA_TYPE_ALIAS_NAME(AlpakaService) << " succesfully initialised.\n";
0064       out << "Found " << n << " device" << suffix[n < 2 ? n : 2];
0065       for (auto const& device : devices) {
0066         out << "\n  - " << alpaka::getName(device);
0067       }
0068     }
0069 
0070     // initialise the queue and event caches
0071     cms::alpakatools::getQueueCache<Queue>().clear();
0072     cms::alpakatools::getEventCache<Event>().clear();
0073 
0074     // initialise the caching memory allocators
0075     cms::alpakatools::getHostCachingAllocator<Queue>();
0076     for (auto const& device : devices)
0077       cms::alpakatools::getDeviceCachingAllocator<Device, Queue>(device);
0078   }
0079 
0080   AlpakaService::~AlpakaService() {
0081     // clean up the caching memory allocators
0082     cms::alpakatools::getHostCachingAllocator<Queue>().freeAllCached();
0083     for (auto const& device : cms::alpakatools::devices<Platform>())
0084       cms::alpakatools::getDeviceCachingAllocator<Device, Queue>(device).freeAllCached();
0085 
0086     // clean up the queue and event caches
0087     cms::alpakatools::getQueueCache<Queue>().clear();
0088     cms::alpakatools::getEventCache<Event>().clear();
0089   }
0090 
0091   void AlpakaService::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0092     edm::ParameterSetDescription desc;
0093     desc.addUntracked<bool>("enabled", true);
0094     desc.addUntracked<bool>("verbose", false);
0095 
0096     descriptions.add(ALPAKA_TYPE_ALIAS_NAME(AlpakaService), desc);
0097   }
0098 
0099 }  // namespace ALPAKA_ACCELERATOR_NAMESPACE