Skip to content

Commit a5a049a

Browse files
Make tofu runs support multiple years on single machine (#143)
1 parent 8c63ab1 commit a5a049a

File tree

4 files changed

+47
-37
lines changed

4 files changed

+47
-37
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ repos:
2525
types_or: [javascript, jsx, ts, tsx]
2626
additional_dependencies:
2727
- prettier@3.1.0
28+
language_version: 18.18.0

infrastructure/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Place your benchmark YAML files in the `benchmarks` directory. Example:
2424

2525
```yaml
2626
machine-type: c4-standard-2
27-
year: 2024
27+
years: [2023, 2024]
2828
benchmarks:
2929
Short description: Sector-coupled PyPSA-Eur infrastructure run for Italy considering 2050 as single planning horizon (LP, lot of variables, strongly intermeshed constraints)
3030
Model name: PyPSA-Eur

infrastructure/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ resource "google_compute_instance" "benchmark_instances" {
117117
metadata = {
118118
ssh-keys = var.ssh_user != "" && var.ssh_key_path != "" ? "${var.ssh_user}:${file(var.ssh_key_path)}" : null
119119
benchmark_file = each.value.filename
120-
benchmark_year = lookup(each.value.content, "year", "2024")
120+
benchmark_years = jsonencode(lookup(each.value.content, "years", ["2024"]))
121121
benchmark_content = file("${path.module}/benchmarks/${each.value.filename}")
122122
enable_gcs_upload = tostring(var.enable_gcs_upload)
123123
gcs_bucket_name = var.gcs_bucket_name

infrastructure/startup-script.sh

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,13 @@ echo "Setting up conda environment..."
2424
echo "source ~/miniconda3/bin/activate" >> ~/.bashrc
2525
~/miniconda3/bin/conda init bash
2626

27-
# Get benchmark year from instance metadata
28-
BENCHMARK_YEAR=$(curl -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/attributes/benchmark_year")
29-
echo "Using benchmark year: ${BENCHMARK_YEAR}"
27+
# Get benchmark years from instance metadata
28+
BENCHMARK_YEARS_JSON=$(curl -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/attributes/benchmark_years")
29+
echo "Retrieved benchmark years: ${BENCHMARK_YEARS_JSON}"
3030

31-
# Create conda environment with the appropriate year
32-
cd /solver-benchmark/
33-
~/miniconda3/bin/conda env create -f runner/envs/benchmark-${BENCHMARK_YEAR}-fixed.yaml
34-
35-
# Activate the conda environment
36-
echo "Activating conda environment benchmark-${BENCHMARK_YEAR}..."
37-
source ~/miniconda3/bin/activate
38-
conda activate benchmark-${BENCHMARK_YEAR}
39-
40-
# Add auto-activation to .bashrc
41-
echo -e "\n# Automatically activate benchmark environment" >> ~/.bashrc
42-
echo "conda activate benchmark-${BENCHMARK_YEAR}" >> ~/.bashrc
43-
44-
# Verify environment is active
45-
echo "Current conda environment:"
46-
conda info --envs | grep "*"
47-
48-
echo "Setup completed at $(date)"
49-
echo "Conda environment benchmark-${BENCHMARK_YEAR} is now active and will be activated on login"
31+
# Parse the JSON array into a space-separated string for benchmark_all.sh
32+
BENCHMARK_YEARS_STR=$(echo "${BENCHMARK_YEARS_JSON}" | jq -r 'join(" ")')
33+
echo "Parsed benchmark years: ${BENCHMARK_YEARS_STR}"
5034

5135
# Get benchmark filename from instance metadata
5236
BENCHMARK_FILE=$(curl -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/attributes/benchmark_file")
@@ -58,28 +42,53 @@ BENCHMARK_CONTENT=$(curl -H "Metadata-Flavor: Google" "http://metadata.google.in
5842
# Write the benchmark file - preserve the exact content
5943
echo "${BENCHMARK_CONTENT}" > /solver-benchmark/benchmarks/${BENCHMARK_FILE}
6044

61-
# Run the benchmarks
62-
echo "Starting benchmarks..."
63-
python /solver-benchmark/runner/run_benchmarks.py /solver-benchmark/benchmarks/${BENCHMARK_FILE} ${BENCHMARK_YEAR}
45+
# Make benchmark_all.sh executable
46+
cd /solver-benchmark/
47+
chmod +x ./runner/benchmark_all.sh
48+
49+
# Run the benchmark_all.sh script with our years
50+
echo "Starting benchmarks for years: ${BENCHMARK_YEARS_STR}"
51+
source ~/miniconda3/bin/activate
52+
./runner/benchmark_all.sh -y "${BENCHMARK_YEARS_STR}" ./benchmarks/${BENCHMARK_FILE}
53+
BENCHMARK_EXIT_CODE=$?
54+
55+
if [ $BENCHMARK_EXIT_CODE -ne 0 ]; then
56+
echo "ERROR: Benchmark failed with exit code $BENCHMARK_EXIT_CODE at $(date)"
57+
exit $BENCHMARK_EXIT_CODE
58+
fi
59+
60+
echo "All benchmarks completed at $(date)"
61+
62+
# Create timestamp for the results
63+
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
64+
echo "Using timestamp: ${TIMESTAMP}"
65+
66+
# Create a copy of results with timestamp
67+
CLEAN_FILENAME=$(basename "${BENCHMARK_FILE}" .yaml)
68+
RESULTS_COPY="/tmp/${CLEAN_FILENAME}_${TIMESTAMP}.csv"
69+
echo "Creating copy of results as: ${RESULTS_COPY}"
70+
71+
cp /solver-benchmark/results/benchmark_results.csv "${RESULTS_COPY}"
72+
COPY_EXIT_CODE=$?
73+
74+
if [ $COPY_EXIT_CODE -ne 0 ]; then
75+
echo "ERROR: Failed to copy benchmark results at $(date). Exit code: $COPY_EXIT_CODE"
76+
echo "Check if file exists: /solver-benchmark/results/benchmark_results.csv"
77+
ls -la /solver-benchmark/results/
78+
exit $COPY_EXIT_CODE
79+
fi
80+
81+
echo "Benchmark results successfully copied at $(date)"
6482

6583
# ----- GCS UPLOAD CONFIGURATION -----
84+
# Only proceed if the benchmark and copy operations were successful
6685
# Check if GCS upload is enabled
6786
ENABLE_GCS_UPLOAD=$(curl -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/attributes/enable_gcs_upload")
6887
if [ "${ENABLE_GCS_UPLOAD}" == "true" ]; then
6988
# Get the GCS bucket name
7089
GCS_BUCKET_NAME=$(curl -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/attributes/gcs_bucket_name")
7190
echo "Using GCS bucket: ${GCS_BUCKET_NAME}"
7291

73-
# Create timestamp for the file
74-
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
75-
echo "Using timestamp: ${TIMESTAMP}"
76-
77-
# Create the properly named copy of results
78-
CLEAN_FILENAME=$(basename "${BENCHMARK_FILE}" .yaml)
79-
RESULTS_COPY="/tmp/${CLEAN_FILENAME}_${TIMESTAMP}.csv"
80-
echo "Creating copy of results as: ${RESULTS_COPY}"
81-
cp /solver-benchmark/results/benchmark_results.csv "${RESULTS_COPY}"
82-
8392
# Ensure gsutil is available (should be on GCP instances by default)
8493
if ! command -v gsutil &> /dev/null; then
8594
echo "Installing Google Cloud SDK..."

0 commit comments

Comments
 (0)