Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Dockerfile to install parallel package #131

Merged
merged 12 commits into from
Dec 10, 2024
Merged
28 changes: 20 additions & 8 deletions .github/workflows/ucc-benchmarks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,30 @@ on:

jobs:
run-benchmarks:
runs-on: ubuntu-latest
runs-on: ucc-benchmarks

steps:
- name: Checkout Code
uses: actions/checkout@v3
# Checkout the repository
- name: Checkout code
uses: actions/checkout@v4

# Build the Docker image
- name: Build Docker image
run: docker build -t ucc-benchmark .

- name: Build and Run Docker Container
# Run the benchmarks in the Docker container
- name: Run benchmarks
run: |
docker build -t ucc-benchmark .
docker run --rm -v $PWD/benchmarks/results:/ucc/benchmarks/results ucc-benchmark
docker run --rm \
-v ${{ github.workspace }}/benchmarks/results:/ucc/benchmarks/results \
ucc-benchmark bash -c "source /venv/bin/activate && ./benchmarks/scripts/run_benchmarks.sh"

- name: Commit and Push Results
# Commit and push benchmark results using a dedicated action
- name: Commit and push results
uses: EndBug/add-and-commit@v9
with:
author_name: GitHub Actions
author_email: actions@github.com
message: "Update benchmark results"
add: "benchmarks/results"
add: "benchmarks/results/*"
push: true
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Start from the official Python image (or another base image if preferred)
FROM python:3.12-slim

# Install GNU Parallel and other dependencies
RUN apt-get update && apt-get install -y parallel

# Copy the entire project into the container
COPY . /ucc

Expand All @@ -14,7 +16,6 @@ RUN python3 -m venv /venv
RUN /venv/bin/pip install --no-cache-dir -r /ucc/requirements.txt
RUN /venv/bin/pip install -e . && /venv/bin/pip show ucc


# Make sure the shell script is executable
RUN chmod +x /ucc/benchmarks/scripts/run_benchmarks.sh

Expand Down
11 changes: 6 additions & 5 deletions benchmarks/scripts/benchmark_script.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# benchmark_script.py
import sys
from common import log_performance, save_results, get_compile_function, get_native_rep

# Ensure both QASM file and compiler are provided as arguments
if len(sys.argv) < 3:
print("Usage: python3 my_benchmark_script.py <qasm_file> <compiler>")
if len(sys.argv) < 4:
print("Usage: python3 benchmark_script.py <qasm_file> <compiler> <results_folder>")
sys.exit(1)

# Get the QASM file and compiler passed as command-line arguments
# Get the QASM file, compiler, and results folder passed as command-line arguments
qasm_file = sys.argv[1]
compiler_alias = sys.argv[2]
results_folder = sys.argv[3] # New argument for results folder

# Read the QASM file
with open(qasm_file, "r") as file:
Expand All @@ -27,5 +29,4 @@
results_log = [log_entry]

# Save the results to a CSV file
save_results(results_log, benchmark_name="gates", folder="../results", append=True)

save_results(results_log, benchmark_name="gates", folder=results_folder, append=True)
4 changes: 3 additions & 1 deletion benchmarks/scripts/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,12 @@ def save_results(results_log, benchmark_name="gates", folder="../results", appen
df = pd.DataFrame(results_log)
current_date = datetime.now().strftime("%Y-%m-%d")

# Ensure the folder exists
os.makedirs(folder, exist_ok=True)

# Create the filename based on the current date
file_name = f"{benchmark_name}_{current_date}.csv"
file_path = os.path.join(folder, file_name)

# Check if the file exists and append if needed
if append:
# If the file exists and the date matches, append data
Expand Down
31 changes: 14 additions & 17 deletions benchmarks/scripts/run_benchmarks.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
#!/bin/bash

# Get the absolute path of the current directory
SCRIPT_DIR=$(dirname "$(realpath "$0")")

# Define the results folder path
RESULTS_FOLDER="$SCRIPT_DIR/../results"

# Ensure the results folder exists
mkdir -p "$RESULTS_FOLDER"

# Define the common folder path
QASM_FOLDER="../circuits/qasm2/"
QASM_FOLDER="$SCRIPT_DIR/../circuits/qasm2/"

# Define your list of QASM file names (without the common path)
QASM_FILES=(
Expand All @@ -18,33 +27,21 @@ COMPILERS=("ucc" "qiskit" "pytket" "cirq")

# Default parallelism 4 (can be overridden by a command line argument)
PARALLELISM="${1:-4}"
echo "Running with parallelism: $PARALLELISM"

# Function to handle the kill signal
trap 'echo "All jobs killed"; exit' SIGINT SIGTERM

# Run the jobs in parallel using GNU Parallel
if command -v parallel &> /dev/null; then
echo "Running benchmarks with GNU Parallel."
else
echo "Installing GNU Parallel..."
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
sudo apt update && sudo apt install parallel -y
elif [[ "$OSTYPE" == "darwin"* ]]; then
brew install parallel
else
echo "Please install GNU Parallel manually."
fi
fi

# Prepare the list of commands to run in parallel
commands=()
for qasm_file in "${QASM_FILES[@]}"; do
for compiler in "${COMPILERS[@]}"; do
# Combine the common folder path with the QASM file
full_qasm_file="${QASM_FOLDER}${qasm_file}"

# Build the command
command="python3 benchmark_script.py \"$full_qasm_file\" \"$compiler\""
# Build the command, passing the results folder as an argument
command="python3 $(dirname "$0")/benchmark_script.py \"$full_qasm_file\" \"$compiler\" \"$RESULTS_FOLDER\""

commands+=("$command")
done
done
Expand Down
Loading