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

#13: Debug incorrect reported values of W when beta is non zero #14

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 56 additions & 16 deletions src/ccm_milp/generator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# DARMA Toolkit v. 1.0.0

Check notice on line 1 in src/ccm_milp/generator.py

View workflow job for this annotation

GitHub Actions / code-quality (ubuntu-latest, 3.8)

Similar lines in 2 files

Check notice on line 1 in src/ccm_milp/generator.py

View workflow job for this annotation

GitHub Actions / code-quality (ubuntu-latest, 3.8)

Similar lines in 2 files

Check notice on line 1 in src/ccm_milp/generator.py

View workflow job for this annotation

GitHub Actions / code-quality (ubuntu-latest, 3.8)

Similar lines in 2 files
#
# Copyright 2024 National Technology & Engineering Solutions of Sandia, LLC
# (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
Expand Down Expand Up @@ -39,7 +39,7 @@

from ccm_milp.configuration import Config

class CcmMilpGenerator:

Check notice on line 42 in src/ccm_milp/generator.py

View workflow job for this annotation

GitHub Actions / code-quality (ubuntu-latest, 3.8)

Too many instance attributes (21/10) (too-many-instance-attributes)
"""Manage CCM-MILP problem - Setup, Generate and Solve"""

def __init__(self, configuration : Config, input_problem):
Expand Down Expand Up @@ -86,22 +86,49 @@

# Check solver
if solver_name not in pulp.listSolvers(onlyAvailable=True):
print("*** Available LP solvers: ", pulp.listSolvers(onlyAvailable=True))
print("# Available LP solvers: ", pulp.listSolvers(onlyAvailable=True))
raise ValueError(f"Solver not found: {solver_name}")

print("# Solver: ", solver_name)
self.solve_problem(self.problem, solver_name)

def verify_and_tally_edge(self, r_snd: int, r_rcv: int, c_ind: int):
"""Check psi-chi consistency and return weight of existing edges"""
if not pulp.value(self.psi[r_snd, r_rcv, c_ind]):
# Edges absent from psi do not exist
return 0.0

# Retrieve communication entry
comm = self.task_communications[c_ind]

# Perform sanity checks
if not pulp.value(self.chi[r_snd, comm[0]]):
raise ValueError(
f"Inconsistent results: communication edge {c_ind}"
f" initiating from rank {r_snd}"
f" but its starting point {comm[0]}"
f" does not belong to rank {r_snd}")
if not pulp.value(self.chi[r_rcv, comm[1]]):
raise ValueError(
f"Inconsistent results: communication edge {c_ind}"
f" terminating at rank {r_rcv}"
f" but its end point {comm[1]}"
f" does not belong to rank {r_rcv}")

# Return communication weight
return comm[2]

def output_solution(self):

Check notice on line 121 in src/ccm_milp/generator.py

View workflow job for this annotation

GitHub Actions / code-quality (ubuntu-latest, 3.8)

Too many local variables (20/15) (too-many-locals)

Check notice on line 121 in src/ccm_milp/generator.py

View workflow job for this annotation

GitHub Actions / code-quality (ubuntu-latest, 3.8)

Too many branches (19/12) (too-many-branches)
"""Generate output report"""
if self.problem.status == pulp.LpStatusOptimal:

Check notice on line 123 in src/ccm_milp/generator.py

View workflow job for this annotation

GitHub Actions / code-quality (ubuntu-latest, 3.8)

Too many nested blocks (6/5) (too-many-nested-blocks)
solution = {"w_max": pulp.value(self.w_max)}
machine_memory_blocks_assigned = [[] for i in range(self.i)]
rank_totals = {}
rank_totals = []
total_unhomed_blocks = 0

# Iterate over ranks
for i in range(self.i):
# Compute work for rank i
unhomed_blocks = 0
delta_cost = 0
for n in range(self.n):
Expand All @@ -121,21 +148,32 @@
total_load += self.task_loads[k]
total_work += self.config.alpha * self.task_loads[k]

# Add communication costs when relevant
if self.psi:
comm_cost = 0.0
c_l, c_o, c_i = 0.0, 0.0, 0.0
# Iterate over ranks
for j in range(self.i):
# Check for communications between ranks i and j
for m in range(self.m):
if( pulp.value(self.psi[i, j, m]) == 1.0 and
pulp.value(self.chi[i, self.task_communications[m][0]]) == 1.0 and
pulp.value(self.chi[j, self.task_communications[m][1]]) == 1.0
):
comm_cost += self.task_communications[m][2] * (
self.config.gamma if i == j else self.config.beta
)
total_work += comm_cost
# Distinguish local from global communications
if i == j:
# Tally local communications
c_l += self.verify_and_tally_edge(i, i, m)

# Skip subsequent non-local communications operations
continue

# Tally outgoing communications
c_o += self.verify_and_tally_edge(i, j, m)

# Tally incoming communications
c_i += self.verify_and_tally_edge(j, i, m)

# Update rank total work with communication costs
total_work += self.config.beta * max(c_o, c_i) + self.config.gamma * c_l

# Keep track of totals on rank
rank_totals[i] = (total_load, total_work, unhomed_blocks)
rank_totals.append((total_load, total_work, unhomed_blocks))

# Compute assignment array of tasks to ranks
assignments = [-1] * self.k
Expand All @@ -149,20 +187,22 @@
for key, value in solution.items():
if key == "w_max":
continue
elif value:
if value:
print(key)

print("\n# Solution summary:")
for i in range(self.i):
t = rank_totals[i]
print(f"Rank {i}: L = {t[0]}, W = {t[1]}, unhomed: {t[2]}")
print("w_max =", solution["w_max"])
print(f"Rank {i}: "
f"L = {rank_totals[i][0]}, "
f"W = {rank_totals[i][1]}, "
f"unhomed: {rank_totals[i][2]}")
print("W_max =", solution["w_max"])
print(f"$assignments={assignments};")

else: # if self.problem.status == pulp.LpStatusOptimal
print("# No solution found")

def setup_milp(self):

Check notice on line 205 in src/ccm_milp/generator.py

View workflow job for this annotation

GitHub Actions / code-quality (ubuntu-latest, 3.8)

Too many branches (22/12) (too-many-branches)

Check notice on line 205 in src/ccm_milp/generator.py

View workflow job for this annotation

GitHub Actions / code-quality (ubuntu-latest, 3.8)

Too many statements (54/50) (too-many-statements)
""" Solve a minimization of a mixed-integer linear program. """
# instantiante LP minimization problem
self.problem = pulp.LpProblem("CCM_MILP", pulp.LpMinimize)
Expand Down
Loading