From f2e462842e810898ff40a7bf799dac7f76f815f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 30 Aug 2024 09:24:04 +0300 Subject: [PATCH 1/4] exC: make script executable --- exercises/exerciseC/run_with_all_configurations.sh | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) mode change 100644 => 100755 exercises/exerciseC/run_with_all_configurations.sh diff --git a/exercises/exerciseC/run_with_all_configurations.sh b/exercises/exerciseC/run_with_all_configurations.sh old mode 100644 new mode 100755 index 30a1414..ffe1012 --- a/exercises/exerciseC/run_with_all_configurations.sh +++ b/exercises/exerciseC/run_with_all_configurations.sh @@ -1,9 +1,13 @@ -# This is bash -# It runs the python script multiple times with different arguments +#!/bin/bash +set -ex + +# This is a bash script. +# It runs the python program multiple times with different arguments. + for i in {1..5} # Number of processes do for j in {1..5} # Number of threads do python process_images.py $i $j images/* done -done \ No newline at end of file +done From 46da504eba4abcef200d602abf242731ce9ffeeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 30 Aug 2024 11:42:40 +0300 Subject: [PATCH 2/4] add map codealong dump --- slides/map_example_codealong.py | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 slides/map_example_codealong.py diff --git a/slides/map_example_codealong.py b/slides/map_example_codealong.py new file mode 100644 index 0000000..b07d6a4 --- /dev/null +++ b/slides/map_example_codealong.py @@ -0,0 +1,40 @@ +# Computer scientists love the Fibonacci sequence +# [https://en.wikipedia.org/wiki/Fibonacci_sequence], possibly +# described by Acharya Pingala in छन्दःशास्त्र (Chhandaḥśāstra). +# +# The basic definition is that the next item in the sequence +# if the sum of the two preceding items, and the two initial +# items and 0 and 1. +# +# We end up with [0, 1, 1, 2, 3, 5, 8, 13, ...] + +def fibbo(n): + match n: + case 0 | 1: + return n + case _: + return fibbo(n-1) + fibbo(n-2) + +# For example: the first few items in the sequence: +for n in range(36): + print(n, fibbo(n)) + +# A (nowadays) less commonly used way to construct the mapping +# from n to fibbo(n) is to use the map function. +# map returns a generator, and we call list to collect the numbers: +sequence = list(map(fibbo, range(36))) +print(sequence) + +# To split the work between multiple Python processes, we can +# use the multiprocessing module: +import multiprocessing + +pool = multiprocessing.Pool() +sequence = pool.map(fibbo, range(36)) +print(sequence) + +# We may use 'with' to clean up the pool after we're done with +# the workers: +with multiprocessing.Pool(10) as pool: + sequence = pool.map(fibbo, range(36)) +print(sequence) From 8b58bbdef918ac6d254c8a41769c91148b46872e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 30 Aug 2024 12:50:14 +0300 Subject: [PATCH 3/4] adjust list formatting --- exercises/exerciseB/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/exercises/exerciseB/README.md b/exercises/exerciseB/README.md index 40ab9e6..6728cc7 100644 --- a/exercises/exerciseB/README.md +++ b/exercises/exerciseB/README.md @@ -14,6 +14,7 @@ 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`. @@ -23,5 +24,6 @@ for different values of the number of steps. 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. From ca3cf86e779e64f4aa225aa60709d7846881a9a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 30 Aug 2024 13:52:16 +0300 Subject: [PATCH 4/4] exB: move slides into the exercise folder A symlink is provided for compatibility. --- exercises/exerciseB/map_example_codealong.py | 40 +++++++++++++++++++ slides/map_example_codealong.py | 41 +------------------- 2 files changed, 41 insertions(+), 40 deletions(-) create mode 100644 exercises/exerciseB/map_example_codealong.py mode change 100644 => 120000 slides/map_example_codealong.py diff --git a/exercises/exerciseB/map_example_codealong.py b/exercises/exerciseB/map_example_codealong.py new file mode 100644 index 0000000..b07d6a4 --- /dev/null +++ b/exercises/exerciseB/map_example_codealong.py @@ -0,0 +1,40 @@ +# Computer scientists love the Fibonacci sequence +# [https://en.wikipedia.org/wiki/Fibonacci_sequence], possibly +# described by Acharya Pingala in छन्दःशास्त्र (Chhandaḥśāstra). +# +# The basic definition is that the next item in the sequence +# if the sum of the two preceding items, and the two initial +# items and 0 and 1. +# +# We end up with [0, 1, 1, 2, 3, 5, 8, 13, ...] + +def fibbo(n): + match n: + case 0 | 1: + return n + case _: + return fibbo(n-1) + fibbo(n-2) + +# For example: the first few items in the sequence: +for n in range(36): + print(n, fibbo(n)) + +# A (nowadays) less commonly used way to construct the mapping +# from n to fibbo(n) is to use the map function. +# map returns a generator, and we call list to collect the numbers: +sequence = list(map(fibbo, range(36))) +print(sequence) + +# To split the work between multiple Python processes, we can +# use the multiprocessing module: +import multiprocessing + +pool = multiprocessing.Pool() +sequence = pool.map(fibbo, range(36)) +print(sequence) + +# We may use 'with' to clean up the pool after we're done with +# the workers: +with multiprocessing.Pool(10) as pool: + sequence = pool.map(fibbo, range(36)) +print(sequence) diff --git a/slides/map_example_codealong.py b/slides/map_example_codealong.py deleted file mode 100644 index b07d6a4..0000000 --- a/slides/map_example_codealong.py +++ /dev/null @@ -1,40 +0,0 @@ -# Computer scientists love the Fibonacci sequence -# [https://en.wikipedia.org/wiki/Fibonacci_sequence], possibly -# described by Acharya Pingala in छन्दःशास्त्र (Chhandaḥśāstra). -# -# The basic definition is that the next item in the sequence -# if the sum of the two preceding items, and the two initial -# items and 0 and 1. -# -# We end up with [0, 1, 1, 2, 3, 5, 8, 13, ...] - -def fibbo(n): - match n: - case 0 | 1: - return n - case _: - return fibbo(n-1) + fibbo(n-2) - -# For example: the first few items in the sequence: -for n in range(36): - print(n, fibbo(n)) - -# A (nowadays) less commonly used way to construct the mapping -# from n to fibbo(n) is to use the map function. -# map returns a generator, and we call list to collect the numbers: -sequence = list(map(fibbo, range(36))) -print(sequence) - -# To split the work between multiple Python processes, we can -# use the multiprocessing module: -import multiprocessing - -pool = multiprocessing.Pool() -sequence = pool.map(fibbo, range(36)) -print(sequence) - -# We may use 'with' to clean up the pool after we're done with -# the workers: -with multiprocessing.Pool(10) as pool: - sequence = pool.map(fibbo, range(36)) -print(sequence) diff --git a/slides/map_example_codealong.py b/slides/map_example_codealong.py new file mode 120000 index 0000000..7e35e80 --- /dev/null +++ b/slides/map_example_codealong.py @@ -0,0 +1 @@ +../exercises/exerciseB/map_example_codealong.py \ No newline at end of file