|
| 1 | +.. Copyright 2013-2021 Lawrence Livermore National Security, LLC and other |
| 2 | + Spack Project Developers. See the top-level COPYRIGHT file for details. |
| 3 | +
|
| 4 | + SPDX-License-Identifier: (Apache-2.0 OR MIT) |
| 5 | +
|
| 6 | +.. _rocmpackage: |
| 7 | + |
| 8 | +----------- |
| 9 | +ROCmPackage |
| 10 | +----------- |
| 11 | + |
| 12 | +The ``ROCmPackage`` is not a build system but a helper package. Like ``CudaPackage``, |
| 13 | +it provides standard variants, dependencies, and conflicts to facilitate building |
| 14 | +packages using GPUs though for AMD in this case. |
| 15 | + |
| 16 | +You can find the source for this package (and suggestions for setting up your |
| 17 | +``compilers.yaml`` and ``packages.yaml`` files) at |
| 18 | +`<https://github.com/spack/spack/blob/develop/lib/spack/spack/build_systems/rocm.py>`__. |
| 19 | + |
| 20 | +^^^^^^^^ |
| 21 | +Variants |
| 22 | +^^^^^^^^ |
| 23 | + |
| 24 | +This package provides the following variants: |
| 25 | + |
| 26 | +* **rocm** |
| 27 | + |
| 28 | + This variant is used to enable/disable building with ``rocm``. |
| 29 | + The default is disabled (or ``False``). |
| 30 | + |
| 31 | +* **amdgpu_target** |
| 32 | + |
| 33 | + This variant supports the optional specification of the AMD GPU architecture. |
| 34 | + Valid values are the names of the GPUs (e.g., ``gfx701``), which are maintained |
| 35 | + in the ``amdgpu_targets`` property. |
| 36 | + |
| 37 | +^^^^^^^^^^^^ |
| 38 | +Dependencies |
| 39 | +^^^^^^^^^^^^ |
| 40 | + |
| 41 | +This package defines basic ``rocm`` dependencies, including ``llvm`` and ``hip``. |
| 42 | + |
| 43 | +^^^^^^^^^ |
| 44 | +Conflicts |
| 45 | +^^^^^^^^^ |
| 46 | + |
| 47 | +Conflicts are used to prevent builds with known bugs or issues. This package |
| 48 | +already requires that the ``amdgpu_target`` always be specified for ``rocm`` |
| 49 | +builds. It also defines a conflict that prevents builds with an ``amdgpu_target`` |
| 50 | +when ``rocm`` is disabled. |
| 51 | + |
| 52 | +Refer to `Conflicts <https://spack.readthedocs.io/en/latest/packaging_guide.html?highlight=conflicts#conflicts>`__ |
| 53 | +for more information on package conflicts. |
| 54 | + |
| 55 | +^^^^^^^ |
| 56 | +Methods |
| 57 | +^^^^^^^ |
| 58 | + |
| 59 | +This package provides one custom helper method, which is used to build |
| 60 | +standard AMD hip compiler flags. |
| 61 | + |
| 62 | +**hip_flags** |
| 63 | + |
| 64 | + This built-in static method returns the appropriately formatted |
| 65 | + ``--amdgpu-target`` build option for ``hipcc``. |
| 66 | + |
| 67 | + This method must be explicitly called when you are creating the |
| 68 | + arguments for your build in order to use the values. |
| 69 | + |
| 70 | +^^^^^ |
| 71 | +Usage |
| 72 | +^^^^^ |
| 73 | + |
| 74 | +This helper package can be added to your package by adding it as a base |
| 75 | +class of your package. For example, you can add it to your |
| 76 | +:ref:`CMakePackage <cmakepackage>`-based package as follows: |
| 77 | + |
| 78 | +.. code-block:: python |
| 79 | + :emphasize-lines: 1,3-7,14-25 |
| 80 | +
|
| 81 | + class MyRocmPackage(CMakePackage, ROCmPackage): |
| 82 | + ... |
| 83 | + # Ensure +rocm and amdgpu_targets are passed to dependencies |
| 84 | + depends_on('mydeppackage', when='+rocm') |
| 85 | + for val in ROCmPackage.amdgpu_targets: |
| 86 | + depends_on('mydeppackage amdgpu_target={0}'.format(val), |
| 87 | + when='amdgpu_target={0}'.format(val)) |
| 88 | + ... |
| 89 | +
|
| 90 | + def cmake_args(self): |
| 91 | + spec = self.spec |
| 92 | + args = [] |
| 93 | + ... |
| 94 | + if '+rocm' in spec: |
| 95 | + # Set up the hip macros needed by the build |
| 96 | + args.extend([ |
| 97 | + '-DENABLE_HIP=ON', |
| 98 | + '-DHIP_ROOT_DIR={0}'.format(spec['hip'].prefix]) |
| 99 | + rocm_archs = spec.variants['amdgpu_target'].value |
| 100 | + if 'none' not in rocm_archs: |
| 101 | + args.append('-DHIP_HIPCC_FLAGS=--amdgpu-target={0}' |
| 102 | + .format(",".join(rocm_archs))) |
| 103 | + else: |
| 104 | + # Ensure build with hip is disabled |
| 105 | + args.append('-DENABLE_HIP=OFF') |
| 106 | + ... |
| 107 | + return args |
| 108 | + ... |
| 109 | +
|
| 110 | +assuming only on the ``ENABLE_HIP``, ``HIP_ROOT_DIR``, and ``HIP_HIPCC_FLAGS`` |
| 111 | +macros are required to be set and the only dependency needing rocm options |
| 112 | +is ``mydeppackage``. You will need to customize the flags as needed for your |
| 113 | +build. |
| 114 | +
|
| 115 | +This example also illustrates how to check for the ``rocm`` variant using |
| 116 | +``self.spec`` and how to retrieve the ``amdgpu_target`` variant's value |
| 117 | +using ``self.spec.variants['amdgpu_target'].value``. |
| 118 | +
|
| 119 | +All five packages using ``ROCmPackage`` as of January 2021 also use the |
| 120 | +:ref:`CudaPackage <cudapackage>`. So it is worth looking at those packages |
| 121 | +to get ideas for creating a package that can support both ``cuda`` and |
| 122 | +``rocm``. |
0 commit comments