#!/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 "" # 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 -n $P ./build/linux/x86_64/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 "--------------------------------------------------------"