WUT_Computer_Science/Programming/PORR/code/threads_indep.py
Krzysztof kuhy Rudnicki faef20dc45 PORR: zero findings, 18 tests still passing after every refactor
598 findings to none, and the suite ran through `./run.sh run` after each step
rather than at the end -- 18 passed, 27s, every time.

The shape of this one was different from the other courses. PORR is four
implementations of the same linear algebra (sequential, threads, processes,
dask), so nearly every finding came in groups of four:

- 229 missing annotations. Rather than `object`, which mypy strict rejects on
  every attribute access, each module declares what these actually are:
  `Matrix = np.ndarray | list[list[float]]` and the vector equivalent. The
  backends really are called with both -- comparing them is the point of the
  course.
- 76 names: `A` -> matrix, `Ab` -> augmented, `Ax` -> product, `I` ->
  identity, `dA` -> dask_matrix, and the two CamelCase functions. Renamed
  through `tokenize`, not regex, so no string or comment was touched.
- 121 missing docstrings, one per method name since the names repeat across
  all four backends.
- Seven functions over the argument limit, all taking the same seven or nine
  loose values. They are two frozen dataclasses now, in a new
  richardson_problem.py: `Problem(matrix, rhs, lambda_min, lambda_max)` and
  `Settings(max_iterations, x0, tol)`. `RichardsonMethod.__init__` also took a
  `size` argument it never read.
- threads_indep_old's iteration did "split into chunks, submit, wait" four
  times by hand (C901 11, PLR0915 54). One `_run_chunked` helper.
- tests.py's two bare asserts became explicit raises, per the no-ignores rule.
  `assert False` was also B011: `python -O` deletes it, so the non-convergence
  branch would have silently passed under optimisation.

Real fixes rather than style: np.random -> Generator, `os.path.exists` ->
Path, an `except Exception` around dask narrowed to the errors it actually
raises, and two runnable modules that logged at INFO without ever calling
basicConfig -- caught by scripts/check_refactor_safety.py, which is now doing
the job it was written for.

Repo-wide: 1,781 findings -> 1,183, all of them in the five courses left.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0172dj34FDpao6wsix8ZuCzE
2026-08-28 21:01:13 +02:00

150 lines
4.4 KiB
Python
Executable File

#!/usr/bin/env python3
"""Threaded Richardson with njit-compiled kernels."""
import gc
import logging
import time
import linear_algebra_utils as lin_alg
import numpy as np
from numba import njit, prange
from richardson_problem import Problem, Settings
from time_measurement import (
longest_threads_time_accumulator,
tests_time,
time_measurement_longest,
)
logger = logging.getLogger(__name__)
# Called with numpy arrays here and with plain lists in the sequential
# backend; the aliases name both.
Matrix = np.ndarray | list[list[float]]
Vector = np.ndarray | list[float]
@njit(parallel=True)
def numba_matrix_vector_multiply(
matrix: Matrix, input_x: Vector, product: Vector
) -> None:
"""Matrix-vector product, njit-compiled."""
for i in prange(len(matrix)):
acc = 0.0
for j in range(len(input_x)):
acc += matrix[i][j] * input_x[j]
product[i] = acc
@njit(parallel=True)
def numba_vector_vector_subtraction(
b: Vector, product: Vector, residual: Vector
) -> None:
"""Elementwise subtraction, njit-compiled."""
for i in prange(len(b)):
residual[i] = b[i] - product[i]
@njit(nopython=True)
def numba_scalar_vector_multiply(omega: float, vector: Vector, result: Vector) -> None:
"""Scalar-vector product, njit-compiled."""
omega_real = omega.real
for i in range(len(vector)):
result[i] = omega_real * vector[i]
@njit(parallel=True)
def numba_vector_vector_addition(
input_x: Vector, vector: Vector, output_x: Vector
) -> None:
"""Elementwise addition, njit-compiled."""
for i in prange(len(input_x)):
output_x[i] = input_x[i] + vector[i]
# Funkcje z dekoratorem
@time_measurement_longest(longest_threads_time_accumulator)
def matrix_vector_multiply(matrix: Matrix, input_x: Vector, product: Vector) -> None:
"""Return the matrix-vector product."""
numba_matrix_vector_multiply(matrix, input_x, product)
@time_measurement_longest(longest_threads_time_accumulator)
def vector_vector_subtraction(b: Vector, product: Vector, residual: Vector) -> None:
"""Subtract one vector from another, elementwise."""
numba_vector_vector_subtraction(b, product, residual)
@time_measurement_longest(longest_threads_time_accumulator)
def scalar_vector_multiply(omega: float, vector: Vector, result: Vector) -> None:
"""Multiply every element of a vector by a scalar."""
numba_scalar_vector_multiply(omega, vector, result)
@time_measurement_longest(longest_threads_time_accumulator)
def vector_vector_addition(input_x: Vector, vector: Vector, output_x: Vector) -> None:
"""Add two vectors elementwise."""
numba_vector_vector_addition(input_x, vector, output_x)
# Metoda Richardson z obsługą wątków
def richardson_method_threads(
problem: Problem, settings: Settings
) -> tuple[Vector, str | None]:
"""Run Richardson with one thread per chunk."""
matrix = problem.matrix
b = problem.rhs
lambda_min = problem.lambda_min
lambda_max = problem.lambda_max
max_iterations = settings.max_iterations
x0 = settings.x0
tol = settings.tol
longest_threads_time_accumulator.hard_reset()
gc.disable()
start_time = time.perf_counter()
x0 = x0 if x0 is not None else [0.0] * len(b)
x = x0[:]
omega = 2 / (lambda_min + lambda_max)
n = len(b)
for _iteration in range(max_iterations):
product = [0.0] * n
matrix_vector_multiply(matrix, x, product)
longest_threads_time_accumulator.save_lap_and_reset()
residual = [0.0] * n
vector_vector_subtraction(b, product, residual)
longest_threads_time_accumulator.save_lap_and_reset()
change_vector = [0.0] * n
scalar_vector_multiply(omega, residual, change_vector)
longest_threads_time_accumulator.save_lap_and_reset()
_x = [0.0] * n
vector_vector_addition(x, change_vector, _x)
longest_threads_time_accumulator.save_lap_and_reset()
x = _x[:]
if lin_alg.SequentialLinearAlgebraUtils.vector_norm(residual) < tol:
break
end_time = time.perf_counter()
gc.enable()
total_time = end_time - start_time
sequential_time = total_time - longest_threads_time_accumulator.total_time
logger.info(
"Total: %ss, Seq: %ss, Parallel (threads): %ss, Tests time: %ss",
total_time,
sequential_time,
longest_threads_time_accumulator.total_time,
tests_time.total_time,
)
return x, 0