hpc-lab-code/lab3/prime/lab3_prime.sh
2026-01-21 18:30:58 +08:00

141 lines
4.3 KiB
Bash
Executable File

#!/bin/bash
# Lab 3: Prime Number Calculation Performance Test
# This script tests the parallel prime calculation program with different N and process counts
echo "=========================================="
echo "Lab 3: Prime Number Calculation Performance Test"
echo "=========================================="
echo ""
# get arch using uname -m
# if aarch64 then use arm64-v8a else use x86_64
ARCH=$(uname -m)
if [ "$ARCH" == "aarch64" ]; then
BUILD_ARCH="arm64-v8a"
else
BUILD_ARCH="x86_64"
fi
# Array of N values
N_VALUES=(100000 200000 400000 800000)
# Array of process counts
PROCESS_COUNTS=(1 2 4 6 8)
# Output file for results
OUTPUT_FILE="prime_results.txt"
# Clear previous results
> $OUTPUT_FILE
# Print header
echo "N值 进程数 素数个数 执行时间(秒)" | tee -a $OUTPUT_FILE
echo "--------------------------------------------------------" | tee -a $OUTPUT_FILE
# Loop through each N value
for N in "${N_VALUES[@]}"; do
echo ""
echo "Testing N = $N"
echo "------------------------"
# Loop through each process count
for P in "${PROCESS_COUNTS[@]}"; do
echo -n "Running with $P process(es)... "
# Run the program and capture output
OUTPUT=$(mpirun --oversubscribe --hostfile ~/mpi_hosts -np $P ./build/linux/$BUILD_ARCH/release/prime_par_naive $N 2>&1)
# Extract prime count and time from output
PRIME_COUNT=$(echo "$OUTPUT" | grep "Between" | grep -oP '\d+(?= primes)')
TIME=$(echo "$OUTPUT" | grep "Time =" | grep -oP '[0-9.]+(?= seconds)')
# Print result
if [ ! -z "$PRIME_COUNT" ] && [ ! -z "$TIME" ]; then
echo "$N $P $PRIME_COUNT $TIME" | tee -a $OUTPUT_FILE
echo "Done! (Primes: $PRIME_COUNT, Time: ${TIME}s)"
else
echo "Error running program!"
echo "$N $P ERROR ERROR" | tee -a $OUTPUT_FILE
fi
done
done
echo ""
echo "=========================================="
echo "Test completed!"
echo "=========================================="
echo ""
echo "Results saved to: $OUTPUT_FILE"
echo ""
echo "Summary Table:"
echo "--------------------------------------------------------"
cat $OUTPUT_FILE
echo "--------------------------------------------------------"
echo ""
echo "=========================================="
echo "Begin Optimized Test!"
echo "=========================================="
echo ""
ARCH=$(uname -m)
if [ "$ARCH" == "aarch64" ]; then
BUILD_ARCH="arm64-v8a"
else
BUILD_ARCH="x86_64"
fi
# Array of N values
N_VALUES=(100000 200000 400000 800000)
# Array of process counts
PROCESS_COUNTS=(1 2 4 6 8)
# Output file for results
OUTPUT_FILE="prime_results_opt.txt"
# Clear previous results
> $OUTPUT_FILE
# Print header
echo "N值 进程数 素数个数 执行时间(秒)" | tee -a $OUTPUT_FILE
echo "--------------------------------------------------------" | tee -a $OUTPUT_FILE
# Loop through each N value
for N in "${N_VALUES[@]}"; do
echo ""
echo "Testing N = $N"
echo "------------------------"
# Loop through each process count
for P in "${PROCESS_COUNTS[@]}"; do
echo -n "Running with $P process(es)... "
# Run the program and capture output
OUTPUT=$(mpirun --oversubscribe --hostfile ~/mpi_hosts -np $P ./build/linux/$BUILD_ARCH/release/prime_par_naive $N $(echo "$N/$P" | bc) 2>&1)
# Extract prime count and time from output
PRIME_COUNT=$(echo "$OUTPUT" | grep "Between" | grep -oP '\d+(?= primes)')
TIME=$(echo "$OUTPUT" | grep "Time =" | grep -oP '[0-9.]+(?= seconds)')
# Print result
if [ ! -z "$PRIME_COUNT" ] && [ ! -z "$TIME" ]; then
echo "$N $P $PRIME_COUNT $TIME" | tee -a $OUTPUT_FILE
echo "Done! (Primes: $PRIME_COUNT, Time: ${TIME}s)"
else
echo "Error running program!"
echo "$N $P ERROR ERROR" | tee -a $OUTPUT_FILE
fi
done
done
$(echo "$N/$P" | bc)
echo ""
echo "=========================================="
echo "Test completed!"
echo "=========================================="
echo ""
echo "Results saved to: $OUTPUT_FILE"
echo ""
echo "Summary Table:"
echo "--------------------------------------------------------"
cat $OUTPUT_FILE
echo "--------------------------------------------------------"