From 46edc6eb7587d8f36a69385ed78b006625b18ef1 Mon Sep 17 00:00:00 2001 From: jestes15 Date: Thu, 12 Aug 2021 23:06:45 -0500 Subject: [PATCH 1/4] Added a solution for Numba and optimized some part of the code --- PrimePython/solution_4/Dockerfile | 12 +++++ PrimePython/solution_4/PrimePY.py | 78 +++++++++++++++++++++++++++++++ PrimePython/solution_4/README.md | 28 +++++++++++ 3 files changed, 118 insertions(+) create mode 100644 PrimePython/solution_4/Dockerfile create mode 100644 PrimePython/solution_4/PrimePY.py create mode 100644 PrimePython/solution_4/README.md diff --git a/PrimePython/solution_4/Dockerfile b/PrimePython/solution_4/Dockerfile new file mode 100644 index 000000000..6bf2b6b45 --- /dev/null +++ b/PrimePython/solution_4/Dockerfile @@ -0,0 +1,12 @@ +# For more information, please refer to https://aka.ms/vscode-docker-python +FROM python:3.9-slim-buster + +# Install pip requirements +RUN python -m pip install numba +RUN python -m pip install numpy + +WORKDIR /opt/app +COPY PrimePY.py . + +# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug +ENTRYPOINT ["python3", "PrimePY.py"] \ No newline at end of file diff --git a/PrimePython/solution_4/PrimePY.py b/PrimePython/solution_4/PrimePY.py new file mode 100644 index 000000000..6acc44284 --- /dev/null +++ b/PrimePython/solution_4/PrimePY.py @@ -0,0 +1,78 @@ +""" +Python Prime Sieve using Numpy and Numba +Based on MyFirstPython Program (tm) Dave Plummer 8/9/2018 + +Adapted by Joshua Estes 08/12/2021 + +This version is nearly a copy from PrimePython/solution3 but adapted to the Numba Python Compiler. +""" + +from numba import njit +import numpy as np +from math import sqrt +import time + +prime_numbers = { + 10: 4, + 100: 25, + 1000: 168, + 10000: 1229, + 100000: 9592, + 1000000: 78498, + 10000000: 664579, + 100000000: 5761455 +} + + +@njit(fastmath=True) +def run_sieve(size): + bits = np.ones(((size + 1) // 2), dtype=np.bool_) + factor = 1 + q = sqrt(size) / 2 + bits_view = bits[factor:] + while factor <= q: + for v in bits_view.flat: + if v: + break + factor += 1 + bits_view = bits[factor + 1:] + factor2 = 2 * factor + start = factor2 * (factor + 1) - factor - 1 + step = factor2 + 1 + bits_view[start::step] = False + factor += 1 + + +def count_primes(size): + if size < 2: + return 0 + return np.sum(np.ones(((size + 1) // 2), dtype=np.bool_)) + + +def get_primes(size): + if size < 2: + return tuple() + primes = np.where(np.ones(((size + 1) // 2), dtype=np.bool_))[0] * 2 + 1 + primes[0] = 2 + return primes + + +def print_results(duration, passes, size): + print(f"Passes: {passes}, Time: {duration}, Avg: {duration / passes}, Limit: {size}, " + f"Count: {len(get_primes(size))}", end="\n") + print("jestes15_numpy/numba; %s; %s; 1; algorithm=base, faithful=no, bits=8" % (passes, duration)) + + +def run(): + run_sieve(100) # Give numba the function call to compile run_sieve + passes, size = 0, int(input("Input the highest number: ")) + iStart: float = time.time() + while (time.time() - iStart) < 5: + run_sieve(size) + passes += 1 + iEnd: float = time.time() - iStart + print_results(iEnd, passes, size) # Passes: 6917951, Time: 5.00100302696228, Avg: 7.229023488258706e-07 + + +if __name__ == "__main__": + run() diff --git a/PrimePython/solution_4/README.md b/PrimePython/solution_4/README.md new file mode 100644 index 000000000..02a92b61a --- /dev/null +++ b/PrimePython/solution_4/README.md @@ -0,0 +1,28 @@ +# Python solution by jestes15 + +![Algorithm](https://img.shields.io/badge/Algorithm-base-green) +![Faithfulness](https://img.shields.io/badge/Faithful-yes-green) +![Parallelism](https://img.shields.io/badge/Parallel-no-green) +![Bit count](https://img.shields.io/badge/Bits-unknown-yellowgreen) + +This is an unfaithful solution due to the inclusion of numpy and numba. Copied from solution 3 with some minor tune ups in the code. + +# Results on my machine + + - Intel i7-10759H + - Python: 3.8.8 64 bit + + +Report with select other solutions: +``` + Single-threaded +┌───────┬────────────────┬──────────┬──────────────────────┬─────────┬──────────┬─────────┬───────────┬──────────┬──────┬───────────────┐ +│ Index │ Implementation │ Solution │ Label │ Passes │ Duration │ Threads │ Algorithm │ Faithful │ Bits │ Passes/Second │ +├───────┼────────────────┼──────────┼──────────────────────┼─────────┼──────────┼─────────┼───────────┼──────────┼──────┼───────────────┤ +│ 1 │ c │ 1 │ mckoss-c830 │ 12469 │ 5.00000 │ 1 │ wheel │ yes │ 1 │ 2493.80000 │ +│ 2 │ python │ 3 │ emillynge_numpy │ 7830 │ 5.00030 │ 1 │ base │ no │ 8 │ 1565.90684 │ +│ 3 │ python │ 4 │ jestes15_numpy_numba │ 6917951 │ 5.00100 │ 1 │ base │ no │ 8 │ 1,383,5905 │ +│ 4 │ python │ 2 │ ssovest │ 2179 │ 5.00037 │ 1 │ base │ yes │ 8 │ 435.76803 │ +│ 5 │ python │ 1 │ davepl │ 40 │ 10.05578 │ 1 │ base │ yes │ │ 3.97781 │ +└───────┴────────────────┴──────────┴──────────────────────┴─────────┴──────────┴─────────┴───────────┴──────────┴──────┴───────────────┘ +``` \ No newline at end of file From 321a7071822655046bbee81926fcd4d8fada811f Mon Sep 17 00:00:00 2001 From: Joshua Estees Date: Thu, 12 Aug 2021 23:10:30 -0500 Subject: [PATCH 2/4] Update README.md Added OS to the machine description --- PrimePython/solution_4/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PrimePython/solution_4/README.md b/PrimePython/solution_4/README.md index 02a92b61a..7e90c49fe 100644 --- a/PrimePython/solution_4/README.md +++ b/PrimePython/solution_4/README.md @@ -9,7 +9,7 @@ This is an unfaithful solution due to the inclusion of numpy and numba. Copied f # Results on my machine - - Intel i7-10759H + - Intel i7-10759H, Windows 11 - Python: 3.8.8 64 bit @@ -25,4 +25,4 @@ Report with select other solutions: │ 4 │ python │ 2 │ ssovest │ 2179 │ 5.00037 │ 1 │ base │ yes │ 8 │ 435.76803 │ │ 5 │ python │ 1 │ davepl │ 40 │ 10.05578 │ 1 │ base │ yes │ │ 3.97781 │ └───────┴────────────────┴──────────┴──────────────────────┴─────────┴──────────┴─────────┴───────────┴──────────┴──────┴───────────────┘ -``` \ No newline at end of file +``` From d75ac3b84409a6de3f32d608caa498b0a050e4de Mon Sep 17 00:00:00 2001 From: jestes15 Date: Thu, 12 Aug 2021 23:15:44 -0500 Subject: [PATCH 3/4] Ran the program with size as 1000000 for results that comply with the table in the README file --- PrimePython/solution_4/PrimePY.py | 2 +- PrimePython/solution_4/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/PrimePython/solution_4/PrimePY.py b/PrimePython/solution_4/PrimePY.py index 6acc44284..86a243dfa 100644 --- a/PrimePython/solution_4/PrimePY.py +++ b/PrimePython/solution_4/PrimePY.py @@ -71,7 +71,7 @@ def run(): run_sieve(size) passes += 1 iEnd: float = time.time() - iStart - print_results(iEnd, passes, size) # Passes: 6917951, Time: 5.00100302696228, Avg: 7.229023488258706e-07 + print_results(iEnd, passes, size) if __name__ == "__main__": diff --git a/PrimePython/solution_4/README.md b/PrimePython/solution_4/README.md index 02a92b61a..5c129c8e5 100644 --- a/PrimePython/solution_4/README.md +++ b/PrimePython/solution_4/README.md @@ -21,7 +21,7 @@ Report with select other solutions: ├───────┼────────────────┼──────────┼──────────────────────┼─────────┼──────────┼─────────┼───────────┼──────────┼──────┼───────────────┤ │ 1 │ c │ 1 │ mckoss-c830 │ 12469 │ 5.00000 │ 1 │ wheel │ yes │ 1 │ 2493.80000 │ │ 2 │ python │ 3 │ emillynge_numpy │ 7830 │ 5.00030 │ 1 │ base │ no │ 8 │ 1565.90684 │ -│ 3 │ python │ 4 │ jestes15_numpy_numba │ 6917951 │ 5.00100 │ 1 │ base │ no │ 8 │ 1,383,5905 │ +│ 3 │ python │ 4 │ jestes15_numpy_numba │ 8503 │ 5.00100 │ 1 │ base │ no │ 8 │ 1700.60000 │ │ 4 │ python │ 2 │ ssovest │ 2179 │ 5.00037 │ 1 │ base │ yes │ 8 │ 435.76803 │ │ 5 │ python │ 1 │ davepl │ 40 │ 10.05578 │ 1 │ base │ yes │ │ 3.97781 │ └───────┴────────────────┴──────────┴──────────────────────┴─────────┴──────────┴─────────┴───────────┴──────────┴──────┴───────────────┘ From 65a3b512b21fa726b8a259448e6bc8f796e77469 Mon Sep 17 00:00:00 2001 From: Joshua Estes Date: Mon, 18 Apr 2022 14:36:40 -0500 Subject: [PATCH 4/4] Fixed report in README where solution was in the wrong order. --- PrimePython/solution_4/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PrimePython/solution_4/README.md b/PrimePython/solution_4/README.md index bae56fc40..9783ee05e 100644 --- a/PrimePython/solution_4/README.md +++ b/PrimePython/solution_4/README.md @@ -20,8 +20,8 @@ Report with select other solutions: │ Index │ Implementation │ Solution │ Label │ Passes │ Duration │ Threads │ Algorithm │ Faithful │ Bits │ Passes/Second │ ├───────┼────────────────┼──────────┼──────────────────────┼─────────┼──────────┼─────────┼───────────┼──────────┼──────┼───────────────┤ │ 1 │ c │ 1 │ mckoss-c830 │ 12469 │ 5.00000 │ 1 │ wheel │ yes │ 1 │ 2493.80000 │ -│ 2 │ python │ 3 │ emillynge_numpy │ 7830 │ 5.00030 │ 1 │ base │ no │ 8 │ 1565.90684 │ -│ 3 │ python │ 4 │ jestes15_numpy_numba │ 8503 │ 5.00100 │ 1 │ base │ no │ 8 │ 1700.60000 │ +│ 2 │ python │ 4 │ jestes15_numpy_numba │ 8503 │ 5.00100 │ 1 │ base │ no │ 8 │ 1700.60000 │ +│ 3 │ python │ 3 │ emillynge_numpy │ 7830 │ 5.00030 │ 1 │ base │ no │ 8 │ 1565.90684 │ │ 4 │ python │ 2 │ ssovest │ 2179 │ 5.00037 │ 1 │ base │ yes │ 8 │ 435.76803 │ │ 5 │ python │ 1 │ davepl │ 40 │ 10.05578 │ 1 │ base │ yes │ │ 3.97781 │ └───────┴────────────────┴──────────┴──────────────────────┴─────────┴──────────┴─────────┴───────────┴──────────┴──────┴───────────────┘