Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef HeterogeneousCore_AlpakaCore_interface_chooseDevice_h
0002 #define HeterogeneousCore_AlpakaCore_interface_chooseDevice_h
0003 
0004 #include <alpaka/alpaka.hpp>
0005 
0006 #include "FWCore/ServiceRegistry/interface/Service.h"
0007 #include "FWCore/Utilities/interface/StreamID.h"
0008 #include "HeterogeneousCore/AlpakaInterface/interface/config.h"
0009 #include "HeterogeneousCore/AlpakaInterface/interface/devices.h"
0010 #include "HeterogeneousCore/AlpakaInterface/interface/traits.h"
0011 #include "HeterogeneousCore/AlpakaServices/interface/alpaka/AlpakaService.h"
0012 
0013 namespace cms::alpakatools {
0014 
0015   template <typename TPlatform, typename = std::enable_if_t<alpaka::isPlatform<TPlatform>>>
0016   alpaka::Dev<TPlatform> const& chooseDevice(edm::StreamID id) {
0017     edm::Service<ALPAKA_TYPE_ALIAS(AlpakaService)> service;
0018     if (not service->enabled()) {
0019       cms::Exception ex("RuntimeError");
0020       ex << "Unable to choose current device because " << ALPAKA_TYPE_ALIAS_NAME(AlpakaService) << " is disabled.\n"
0021          << "If " << ALPAKA_TYPE_ALIAS_NAME(AlpakaService) << " was not explicitly disabled in the configuration,\n"
0022          << "the probable cause is that there is no accelerator or there is some problem\n"
0023          << "with the accelerator runtime or drivers.";
0024       ex.addContext("Calling cms::alpakatools::chooseDevice()");
0025       throw ex;
0026     }
0027 
0028     // For startes we "statically" assign the device based on
0029     // edm::Stream number. This is suboptimal if the number of
0030     // edm::Streams is not a multiple of the number of devices
0031     // (and even then there is no load balancing).
0032 
0033     // TODO: improve the "assignment" logic
0034     auto const& devices = cms::alpakatools::devices<TPlatform>();
0035     return devices[id % devices.size()];
0036   }
0037 
0038 }  // namespace cms::alpakatools
0039 
0040 #endif  // HeterogeneousCore_AlpakaCore_interface_chooseDevice_h