From ac3ca898156bfd9f118826b644018e1bc84af25a Mon Sep 17 00:00:00 2001 From: Gromiusz Date: Sat, 19 Oct 2024 23:39:42 +0200 Subject: [PATCH] feat: richardson with threads - add_elements and substract_elements methods --- code/richardson_abstract.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/code/richardson_abstract.py b/code/richardson_abstract.py index 2abff903..0cbaa03c 100644 --- a/code/richardson_abstract.py +++ b/code/richardson_abstract.py @@ -1,4 +1,5 @@ from abc import ABC, abstractmethod +import threading class modified_richardson_base(ABC): """ @@ -95,6 +96,33 @@ class modified_richardson(modified_richardson_base): return [scalar * vi for vi in vec] +class modified_richardson_with_threads(modified_richardson_base): + def compute_with_threads(self, v1, v2, function: function): + """ Executes the provided function on many threads """ + result = [0] * len(v1) + threads = [] + for i in range(len(v1)): + t = threading.Thread(target=function, args=(v1, v2, result, i)) + threads.append(t) + t.start() + + for t in threads: + t.join() + + return result + def subtract_elements(self, v1, v2, result, index): + """ This function is executed by single thread. It calculates one row in matrix """ + result[index] = v1[index] - v2[index] + + def add_elements(self, v1, v2, result, index): + """ As above """ + result[index] = v1[index] - v2[index] + + def vec_sub(self, v1, v2): + return self.compute_with_threads(v1, v2, self.subtract_elements) + + def vec_add(self, v1, v2): + return self.compute_with_threads(v1, v2, self.add_elements) \ No newline at end of file