|
1 | 1 | import FWCore.ParameterSet.Config as cms |
2 | 2 |
|
| 3 | +import os |
| 4 | + |
| 5 | +from HeterogeneousCore.Common.PlatformStatus import PlatformStatus |
| 6 | + |
3 | 7 | class ModuleTypeResolverAlpaka: |
4 | 8 | def __init__(self, accelerators, backend): |
5 | | - # first element is used as the default is nothing is set |
| 9 | + # first element is used as the default if nothing is set |
6 | 10 | self._valid_backends = [] |
7 | 11 | if "gpu-nvidia" in accelerators: |
8 | 12 | self._valid_backends.append("cuda_async") |
| 13 | + if "gpu-amd" in accelerators: |
| 14 | + self._valid_backends.append("rocm_async") |
9 | 15 | if "cpu" in accelerators: |
10 | 16 | self._valid_backends.append("serial_sync") |
11 | 17 | if len(self._valid_backends) == 0: |
@@ -45,26 +51,64 @@ class ProcessAcceleratorAlpaka(cms.ProcessAccelerator): |
45 | 51 | ProcessAcceleratorCUDA) define. |
46 | 52 | """ |
47 | 53 | def __init__(self): |
48 | | - super(ProcessAcceleratorAlpaka,self).__init__() |
| 54 | + super(ProcessAcceleratorAlpaka, self).__init__() |
49 | 55 | self._backend = None |
| 56 | + |
50 | 57 | # User-facing interface |
51 | 58 | def setBackend(self, backend): |
52 | 59 | self._backend = backend |
| 60 | + |
53 | 61 | # Framework-facing interface |
54 | 62 | def moduleTypeResolver(self, accelerators): |
55 | 63 | return ModuleTypeResolverAlpaka(accelerators, self._backend) |
| 64 | + |
56 | 65 | def apply(self, process, accelerators): |
57 | | - if not hasattr(process, "AlpakaServiceSerialSync"): |
| 66 | + # Propagate the AlpakaService messages through the MessageLogger |
| 67 | + if not hasattr(process.MessageLogger, "AlpakaService"): |
| 68 | + process.MessageLogger.AlpakaService = cms.untracked.PSet() |
| 69 | + |
| 70 | + # Check if the CPU backend is available |
| 71 | + try: |
| 72 | + if not "cpu" in accelerators: |
| 73 | + raise False |
58 | 74 | from HeterogeneousCore.AlpakaServices.AlpakaServiceSerialSync_cfi import AlpakaServiceSerialSync |
59 | | - process.add_(AlpakaServiceSerialSync) |
60 | | - if not hasattr(process, "AlpakaServiceCudaAsync"): |
| 75 | + except: |
| 76 | + # the CPU backend is not available, do not load the AlpakaServiceSerialSync |
| 77 | + if hasattr(process, "AlpakaServiceSerialSync"): |
| 78 | + del process.AlpakaServiceSerialSync |
| 79 | + else: |
| 80 | + # the CPU backend is available, ensure the AlpakaServiceSerialSync is loaded |
| 81 | + if not hasattr(process, "AlpakaServiceSerialSync"): |
| 82 | + process.add_(AlpakaServiceSerialSync) |
| 83 | + |
| 84 | + # Check if CUDA is available, and if the system has at least one usable NVIDIA GPU |
| 85 | + try: |
| 86 | + if not "gpu-nvidia" in accelerators: |
| 87 | + raise False |
61 | 88 | from HeterogeneousCore.AlpakaServices.AlpakaServiceCudaAsync_cfi import AlpakaServiceCudaAsync |
62 | | - process.add_(AlpakaServiceCudaAsync) |
| 89 | + except: |
| 90 | + # CUDA is not available, do not load the AlpakaServiceCudaAsync |
| 91 | + if hasattr(process, "AlpakaServiceCudaAsync"): |
| 92 | + del process.AlpakaServiceCudaAsync |
| 93 | + else: |
| 94 | + # CUDA is available, ensure the AlpakaServiceCudaAsync is loaded |
| 95 | + if not hasattr(process, "AlpakaServiceCudaAsync"): |
| 96 | + process.add_(AlpakaServiceCudaAsync) |
63 | 97 |
|
64 | | - if not hasattr(process.MessageLogger, "AlpakaService"): |
65 | | - process.MessageLogger.AlpakaService = cms.untracked.PSet() |
| 98 | + # Check if ROCm is available, and if the system has at least one usable AMD GPU |
| 99 | + try: |
| 100 | + if not "gpu-amd" in accelerators: |
| 101 | + raise False |
| 102 | + from HeterogeneousCore.AlpakaServices.AlpakaServiceROCmAsync_cfi import AlpakaServiceROCmAsync |
| 103 | + except: |
| 104 | + # ROCm is not available, do not load the AlpakaServiceROCmAsync |
| 105 | + if hasattr(process, "AlpakaServiceROCmAsync"): |
| 106 | + del process.AlpakaServiceROCmAsync |
| 107 | + else: |
| 108 | + # ROCm is available, ensure the AlpakaServiceROCmAsync is loaded |
| 109 | + if not hasattr(process, "AlpakaServiceROCmAsync"): |
| 110 | + process.add_(AlpakaServiceROCmAsync) |
66 | 111 |
|
67 | | - process.AlpakaServiceSerialSync.enabled = "cpu" in accelerators |
68 | | - process.AlpakaServiceCudaAsync.enabled = "gpu-nvidia" in accelerators |
69 | 112 |
|
| 113 | +# Ensure this module is kept in the configuration when dumping it |
70 | 114 | cms.specialImportRegistry.registerSpecialImportForType(ProcessAcceleratorAlpaka, "from HeterogeneousCore.AlpakaCore.ProcessAcceleratorAlpaka import ProcessAcceleratorAlpaka") |
0 commit comments