Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ This is a monorepo containing a collection of GitHub Actions maintained by Lizar
| [release_create](actions/release_create#readme) | Create a new release | composite | bash, javascript |
| [release_homebrew](actions/release_homebrew#readme) | Validate and update Homebrew formula | composite | bash, python |
| [release_setup](actions/release_setup#readme) | Prepare a release | docker | python |
| [setup_cuda](actions/setup_cuda#readme) | Set up NVIDIA CUDA Toolkit on Linux runners | composite | bash |
| [setup_python](actions/setup_python#readme) | Set up Python environment | composite | bash |

## Contributions
Expand Down
131 changes: 131 additions & 0 deletions actions/setup_cuda/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# setup_cuda

A reusable action to install NVIDIA CUDA Toolkit on Linux runners using the runfile installer.

This action provides a consistent way to install CUDA Toolkit across different Linux runners, including both standard
Ubuntu and ARM-based Ubuntu runners. The installation uses NVIDIA's official runfile installers and sets up all
necessary environment variables for C/C++ compilation.

## 🛠️ Prep Work

This action is designed for Linux runners only and requires:
- Ubuntu-based runner (standard x86_64 or ARM64/aarch64)
- Sufficient disk space (CUDA Toolkit requires ~3-4 GB)
- `sudo` access (required for installation)

> [!NOTE]
> This action installs the CUDA Toolkit only (compiler, libraries, headers) and does not install GPU drivers,
> as they are not needed for compilation and are not available in standard GitHub Actions runners.

> [!TIP]
> To find the correct CUDA version and driver version combination, visit the
> [NVIDIA CUDA Toolkit Downloads](https://developer.nvidia.com/cuda-downloads) page and select your desired version.
> The driver version is part of the runfile name.

## 🚀 Basic Usage

See [action.yml](action.yml)

### CUDA 12.4.1
```yaml
steps:
- name: Setup CUDA 12.4.1
uses: LizardByte/actions/actions/setup_cuda@master
with:
cuda-version: '12.4.1'
driver-version: '550.54.15'
- name: Verify CUDA Version
run: nvcc --version
```

### Custom Installation Path
```yaml
steps:
- name: Setup CUDA
uses: LizardByte/actions/actions/setup_cuda@master
with:
cuda-version: '12.6.2'
driver-version: '560.35.03'
install-path: '/opt/cuda'
```

## 📥 Inputs

| Name | Description | Default | Required |
|----------------|-------------------------------------------------------------------------|-------------------|----------|
| cuda-version | The version of CUDA Toolkit to install (e.g., '12.6.2', '11.8.0') | | `true` |
| driver-version | The driver version in the runfile name (e.g., '560.35.03', '520.61.05') | | `true` |
| install-path | Installation path for CUDA Toolkit | `/usr/local/cuda` | `false` |

> [!NOTE]
> The `driver-version` is the version number included in NVIDIA's runfile name. For example, for the file
> `cuda_12.4.1_550.54.15_linux.run`, the cuda-version is `12.4.1` and the driver-version is `550.54.15`.
> You can find these on the [NVIDIA CUDA Downloads](https://developer.nvidia.com/cuda-downloads) page.

## 📤 Outputs

| Name | Description |
|--------------|----------------------------------------|
| cuda-version | The version of CUDA that was installed |
| cuda-path | The installation path of CUDA Toolkit |
| nvcc-path | The path to the nvcc compiler |

## 📝 Notes

### Supported CUDA Versions

This action can install **any** CUDA Toolkit version available from NVIDIA, as long as you provide the correct
`cuda-version` and `driver-version` combination. There is no hardcoded list of supported versions.

> [!TIP]
> To find the driver version for any CUDA version:
> 1. Visit [NVIDIA CUDA Toolkit Archive](https://developer.nvidia.com/cuda-toolkit-archive)
> 2. Select your desired CUDA version
> 3. Choose "Linux" → "x86_64" (or "sbsa" for ARM) → "Ubuntu" → "runfile (local)"
> 4. The download link will show the full runfile name, which includes the driver version
>
> For example: `cuda_12.4.1_550.54.15_linux.run` means driver version is `550.54.15`

> [!NOTE]
> The action automatically detects your architecture and downloads the appropriate installer:
> - **x86_64**: Downloads `cuda_X.Y.Z_DDD.DD.DD_linux.run`
> - **aarch64**: Downloads `cuda_X.Y.Z_DDD.DD.DD_linux_sbsa.run` (Server Base System Architecture)

### Environment Variables

This action automatically sets up the following environment variables for subsequent steps:

- `CUDA_PATH` - Path to CUDA installation (e.g., `/usr/local/cuda`)
- `CUDA_HOME` - Same as CUDA_PATH (for compatibility)
- `CUDA_ROOT` - Same as CUDA_PATH (for compatibility)
- `CMAKE_CUDA_COMPILER` - Path to nvcc compiler
- `PATH` - Updated to include `${CUDA_PATH}/bin`
- `LD_LIBRARY_PATH` - Updated to include `${CUDA_PATH}/lib64`
- `LIBRARY_PATH` - Updated to include `${CUDA_PATH}/lib64`
- `CPATH` - Updated to include `${CUDA_PATH}/include`

These variables make it easy to compile CUDA code with various build systems (Make, CMake, etc.).

### Installation Details

- **Installation Method**: Official NVIDIA runfile installer
- **Components Installed**: CUDA Toolkit only (compiler, libraries, headers)
- **Components NOT Installed**: GPU drivers, OpenGL libraries (not needed for compilation)
- **Installation Size**: ~3-4 GB depending on version
- **Installation Time**: ~2-5 minutes depending on runner speed

### CMake Integration

The action sets `CMAKE_CUDA_COMPILER` automatically, so CMake will find the correct nvcc compiler.

## 🔗 See Also

- [more_space](../more_space) - Free up disk space if needed before CUDA installation
- [monitor_space](../monitor_space) - Monitor disk space usage during CUDA installation

## ⚠️ Limitations

- **Linux Only**: This action only supports Linux runners (Ubuntu-based)
- **No GPU Execution**: GitHub Actions runners don't have GPUs, so you can compile CUDA code but not run it
- **No Driver Installation**: GPU drivers are not installed (not needed for compilation)
- **Architecture Support**: Only x86_64 and ARM64/aarch64 architectures are supported
53 changes: 53 additions & 0 deletions actions/setup_cuda/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
name: "Setup CUDA Toolkit"
description: "A reusable action to install NVIDIA CUDA Toolkit on Linux runners using runfile installer."
author: "LizardByte"

branding:
icon: cpu
color: green

inputs:
cuda-version:
description: The version of CUDA Toolkit to install (e.g., '12.6.2', '11.8.0').
required: true
driver-version:
description: >
The driver version included in the CUDA installer (e.g., '560.35.03', '520.61.05').
This can be found on NVIDIA's CUDA Toolkit download page.
required: true
install-path:
description: "Installation path for CUDA Toolkit"
required: false
default: "/usr/local/cuda"

outputs:
cuda-version:
description: "The version of CUDA that was installed"
value: ${{ steps.setup.outputs.cuda-version }}
cuda-path:
description: "The installation path of CUDA Toolkit"
value: ${{ steps.setup.outputs.cuda-path }}
nvcc-path:
description: "The path to the nvcc compiler"
value: ${{ steps.setup.outputs.nvcc-path }}

runs:
using: "composite"
steps:
- name: Make script executable
shell: bash
run: chmod +x "${GITHUB_ACTION_PATH}/setup_cuda.sh"

- name: Setup CUDA Toolkit
env:
INPUT_CUDA_VERSION: ${{ inputs.cuda-version }}
INPUT_DRIVER_VERSION: ${{ inputs.driver-version }}
INPUT_INSTALL_PATH: ${{ inputs.install-path }}
id: setup
shell: bash
run: |
"${GITHUB_ACTION_PATH}/setup_cuda.sh" \
--cuda-version="${INPUT_CUDA_VERSION}" \
--driver-version="${INPUT_DRIVER_VERSION}" \
--install-path="${INPUT_INSTALL_PATH}"
16 changes: 16 additions & 0 deletions actions/setup_cuda/ci-matrix.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"runs-on": "ubuntu-24.04-arm",
"with": {
"cuda-version": "13.1.0",
"driver-version": "590.44.01"
}
},
{
"runs-on": "ubuntu-latest",
"with": {
"cuda-version": "13.1.0",
"driver-version": "590.44.01"
}
}
]
127 changes: 127 additions & 0 deletions actions/setup_cuda/post-ci.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#!/bin/bash

set -euo pipefail

echo "Verifying CUDA installation..."

# Check if nvcc is available
if command -v nvcc &>/dev/null; then
echo "✓ nvcc found in PATH"
nvcc --version
else
echo "✗ nvcc not found in PATH"
exit 1
fi

# Check environment variables
echo ""
echo "Environment variables:"
echo " CUDA_PATH=${CUDA_PATH:-not set}"
echo " CUDA_HOME=${CUDA_HOME:-not set}"
echo " CMAKE_CUDA_COMPILER=${CMAKE_CUDA_COMPILER:-not set}"

# Verify CUDA_PATH is set
if [[ -z "${CUDA_PATH:-}" ]]; then
echo "✗ CUDA_PATH not set"
exit 1
fi

# Check if CUDA directories exist
echo ""
echo "CUDA installation structure:"
if [[ -d "${CUDA_PATH}/bin" ]]; then
echo "✓ ${CUDA_PATH}/bin exists"
else
echo "✗ ${CUDA_PATH}/bin not found"
exit 1
fi

if [[ -d "${CUDA_PATH}/include" ]]; then
echo "✓ ${CUDA_PATH}/include exists"
else
echo "✗ ${CUDA_PATH}/include not found"
exit 1
fi

if [[ -d "${CUDA_PATH}/lib64" ]]; then
echo "✓ ${CUDA_PATH}/lib64 exists"
else
echo "✗ ${CUDA_PATH}/lib64 not found"
exit 1
fi

# Try to compile a simple CUDA program
echo ""
echo "Testing CUDA compilation..."

cat > test_cuda.cu << 'EOF'
#include <stdio.h>

__global__ void hello_cuda() {
printf("Hello from CUDA!\n");
}

int main() {
printf("CUDA compilation test\n");
printf("This program was compiled successfully with nvcc\n");
return 0;
}
EOF

if nvcc -o test_cuda test_cuda.cu; then
echo "✓ CUDA compilation successful"
if ./test_cuda; then
echo "✓ CUDA program execution successful"
else
echo "✗ CUDA program execution failed"
rm -f test_cuda test_cuda.cu
exit 1
fi
else
echo "✗ CUDA compilation failed"
rm -f test_cuda.cu
exit 1
fi

# Clean up test files
rm -f test_cuda test_cuda.cu

# Test CMake integration
echo ""
echo "Testing CMake CUDA support..."

if command -v cmake &>/dev/null; then
cat > test_cuda_cmake.cu << 'EOF'
#include <stdio.h>

int main() {
printf("CMake CUDA test\n");
return 0;
}
EOF

cat > CMakeLists.txt << 'EOF'
cmake_minimum_required(VERSION 3.18)
project(CUDATest CUDA)

add_executable(test_cmake test_cuda_cmake.cu)
EOF

mkdir -p build_test
if cmake -B build_test -S . &>/dev/null && cmake --build build_test &>/dev/null; then
echo "✓ CMake CUDA configuration successful"
if ./build_test/test_cmake; then
echo "✓ CMake CUDA build successful"
fi
else
echo "✗ CMake CUDA configuration failed (this may be expected if CMake is not available)"
fi

# Clean up CMake test files
rm -rf build_test test_cuda_cmake.cu CMakeLists.txt
else
echo "⚠ CMake not available, skipping CMake test"
fi

echo ""
echo "Post-CI verification complete!"
60 changes: 60 additions & 0 deletions actions/setup_cuda/pre-ci.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/bash

set -euo pipefail

echo "Pre-CI: Freeing up disk space for CUDA installation..."
echo ""

# Determine which branch/ref to use for downloading the cleanup script
# If we're running in the LizardByte/actions repository itself, use the current commit SHA
# Otherwise, use master branch
if [[ "${GITHUB_REPOSITORY:-}" == "LizardByte/actions" ]]; then
# Running in the actions repo - use the current commit SHA
# GITHUB_SHA is reliable for both PRs and branches
CLEANUP_REF="${GITHUB_SHA:-master}"
echo "Detected LizardByte/actions repository, using commit: ${CLEANUP_REF}"
else
# Running in a different repository - use master
CLEANUP_REF="master"
fi

# Download the cleanup script from more_space action
# Using the raw GitHub URL to get the script from the same repository
CLEANUP_SCRIPT_URL="https://raw.githubusercontent.com/LizardByte/actions/${CLEANUP_REF}/actions/more_space/cleanup.sh"
CLEANUP_SCRIPT="/tmp/cleanup_more_space.sh"

echo "Downloading cleanup script from more_space action..."
if curl -fsSL -o "${CLEANUP_SCRIPT}" "${CLEANUP_SCRIPT_URL}"; then
echo "✓ Downloaded cleanup script"
else
echo "✗ Failed to download cleanup script, trying wget..."
if wget -q -O "${CLEANUP_SCRIPT}" "${CLEANUP_SCRIPT_URL}"; then
echo "✓ Downloaded cleanup script with wget"
else
echo "✗ Failed to download cleanup script"
exit 1
fi
fi

# Make the script executable
chmod +x "${CLEANUP_SCRIPT}"

# Run the cleanup script with options suitable for CUDA installation
# Clean most things but keep JVM which might be needed for tests
bash "${CLEANUP_SCRIPT}" \
--remove-android=true \
--remove-codeql=true \
--remove-docker-images=true \
--remove-docs-linux=true \
--remove-dotnet=true \
--remove-haskell=true \
--remove-jvm=false \
--remove-swift=true \
--remove-tool-cache=true

# Clean up the downloaded script
rm -f "${CLEANUP_SCRIPT}"

echo ""
echo "Pre-CI cleanup complete!"
echo ""
Loading
Loading