36
benchmark.py
36
benchmark.py
@@ -37,7 +37,8 @@ import numpy as np
|
||||
# Memory and performance monitoring
|
||||
import psutil
|
||||
import gc
|
||||
from memory_profiler import profile
|
||||
import tracemalloc
|
||||
from memory_profiler import profile, memory_usage
|
||||
|
||||
# Add HDH to path
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'HDH')))
|
||||
@@ -118,17 +119,34 @@ class HDHBenchmarkSuite:
|
||||
|
||||
# Initial memory measurement
|
||||
gc.collect() # Force garbage collection
|
||||
|
||||
# Start memory tracking with tracemalloc
|
||||
tracemalloc.start()
|
||||
initial_snapshot = tracemalloc.take_snapshot()
|
||||
initial_memory = self.get_memory_usage()
|
||||
|
||||
try:
|
||||
# Time the conversion
|
||||
# Time the conversion and track memory
|
||||
start_time = time.perf_counter()
|
||||
hdh = from_qiskit(circuit)
|
||||
conversion_time = time.perf_counter() - start_time
|
||||
|
||||
# Memory peak measurement
|
||||
# Memory peak measurement using tracemalloc
|
||||
current_snapshot = tracemalloc.take_snapshot()
|
||||
top_stats = current_snapshot.compare_to(initial_snapshot, 'lineno')
|
||||
|
||||
# Calculate memory difference in MB
|
||||
tracemalloc_memory = sum(stat.size_diff for stat in top_stats) / 1024 / 1024
|
||||
|
||||
# Also get psutil measurement as backup
|
||||
peak_memory = self.get_memory_usage()
|
||||
memory_used = peak_memory - initial_memory
|
||||
psutil_memory = peak_memory - initial_memory
|
||||
|
||||
# Use the larger of the two measurements (tracemalloc is usually more accurate)
|
||||
memory_used = max(abs(tracemalloc_memory), abs(psutil_memory))
|
||||
|
||||
# Stop tracemalloc
|
||||
tracemalloc.stop()
|
||||
|
||||
# HDH statistics
|
||||
hdh_nodes = len(hdh.S)
|
||||
@@ -142,8 +160,12 @@ class HDHBenchmarkSuite:
|
||||
if hdh_nodes > 1:
|
||||
try:
|
||||
num_parts = min(3, max(2, hdh_nodes // 2))
|
||||
# Calculate capacity: distribute qubits evenly across partitions with some buffer
|
||||
num_qubits = circuit.num_qubits
|
||||
capacity = max(1, (num_qubits + num_parts - 1) // num_parts + 1) # Ceiling division + buffer
|
||||
|
||||
start_partition = time.perf_counter()
|
||||
partitions = compute_cut(hdh, num_parts)
|
||||
partitions, _ = compute_cut(hdh, num_parts, capacity)
|
||||
partitioning_time = time.perf_counter() - start_partition
|
||||
partition_cost = cost(hdh, partitions)
|
||||
except Exception as e:
|
||||
@@ -177,6 +199,10 @@ class HDHBenchmarkSuite:
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
# Make sure to stop tracemalloc even on error
|
||||
if tracemalloc.is_tracing():
|
||||
tracemalloc.stop()
|
||||
|
||||
self.logger.error(f"Benchmark failed for {circuit_name}: {str(e)}")
|
||||
self.logger.debug(traceback.format_exc())
|
||||
|
||||
|
||||
Referencia en una nueva incidencia
Block a user