From b9988fdda9f47916611f1059605f60746fffd904 Mon Sep 17 00:00:00 2001 From: Jenni Rinker Date: Thu, 29 Aug 2024 18:20:21 +0300 Subject: [PATCH] updating exercise 2b --- Exercise2b/README.md | 27 +++++++++++++++++++++++++++ Exercise2b/numerical_integration.py | 22 ++-------------------- 2 files changed, 29 insertions(+), 20 deletions(-) create mode 100644 Exercise2b/README.md diff --git a/Exercise2b/README.md b/Exercise2b/README.md new file mode 100644 index 0000000..cd1269f --- /dev/null +++ b/Exercise2b/README.md @@ -0,0 +1,27 @@ +# Exercise 2b: multiprocessing and map + +Objective: introduce `map` and `Pool.map`. + +In the `numerical_integration.py` file, we give Python code that calculates +the integral of a function in two different ways: numerically and analytically. + +The given functions are `integrate` (numerical integration), `f` (the function +to integrate), and `F` (the analytical integral). + +We want to check the precision of the numerical integration as a function of +the number of steps in the domain. To do this, we calculate and print the +relative differences between the analytic result and the numerical result +for different values of the number of steps. + +**TASKS**: + 0. Read `numerical_integration.py` and familiarize yourselves with the code. + 1. Update the `main` function so that it calculates the numerical error without + any parallelization. You can use a for loop or `map`. + 2. Note the execution time for this serial implementation. + 3. Implement the parallel version using `multiprocessing.Pool`. + 4. Compare the timing for the parallel version with the serial time. + What speed-up did you get? + +**BONUS TASKS (very optional)**: + 5. Implement a parallel version with threads (using `multiprocessing.pool.ThreadPool`). + 6. Time this version, and hypothetize about the result. diff --git a/Exercise2b/numerical_integration.py b/Exercise2b/numerical_integration.py index 55826eb..1efa519 100644 --- a/Exercise2b/numerical_integration.py +++ b/Exercise2b/numerical_integration.py @@ -1,19 +1,5 @@ -# Exercise 2b - -# Here we have a Python function which calculates the integral in two -# different ways: numerically and analytically. -# -# We want to check the precision of the numerical integration as a function of -# the number of steps (subintervals). To do this, we calculate and print the -# relative differences between the analytic result and the numerical result -# for different values of the number of steps. -# -# Steps: -# 0. Familizare yourselves with code below. -# 1. Implement the serial version using a for loop or map -# 2. Implement the parallel version using multiprocessing.Pool -# 3. Time both versions -# 4. What (if any) do you get? +"""Exercise 2b: multiprocessing +""" def integrate(f, a, b, n): "Perform numerical integration of f in range [a, b], with n steps" @@ -50,7 +36,3 @@ def main(): if __name__ == '__main__': main() - -# Bonus steps, very optional: -# 6. Implement a parallel version with threads (using multiprocessing.pool.ThreadPool). -# 7. Time this version, and hypothetize about the result.