From 9e243d39f04d6fb9380dd287842f60b42228f3d1 Mon Sep 17 00:00:00 2001 From: Michael Blaschek <michael.blaschek@univie.ac.at> Date: Thu, 29 Oct 2020 11:19:17 +0100 Subject: [PATCH] Update README.md, fibonacci.py, fibonacci_openmp.c files --- README.md | 23 ++++++++++++++++------- fibonacci.py | 42 +++++++++++++++++++++++++++++++++--------- fibonacci_openmp.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 16 deletions(-) create mode 100644 fibonacci_openmp.c diff --git a/README.md b/README.md index 29ce11c..f56287b 100644 --- a/README.md +++ b/README.md @@ -58,19 +58,28 @@ Of course that is a silly job. However, there are things to learn for this examp # Writing a job file -There are things to consider when writing a job +There are things to consider when writing a job: +1. How many `tasks` or threads (each CPU has 2 threads) the job should be allowed to use. +2. How much `time` your Jobs will need. This can be quite tricky. If you make your job too short, the program will be killed. If it is too long the job will take longer to start. Although that is currently no problem. + ```bash #!/bin/bash # SLURM specific commands -#SBATCH --job-name=test -#SBATCH --output=test.log +#SBATCH --job-name=fibonacci +#SBATCH --output=fibonacci.log #SBATCH --ntasks=1 -#SBATCH --time=01:30 +#SBATCH --time=05:00 # Your Code below here -srun - +module load miniconda3 +# Execute the Fibonacci Script with the miniconda Python +# use /usr/bin/time -v [program] +# gives statistics on the resources the program uses +# nice for testing +/usr/bin/time -v python3 fibonacci.py ``` +Let's have a look at the output. + # Interactive Sessions @@ -110,7 +119,7 @@ How to submit an MPI batch job #SBATCH --ntasks=7 # Load the OpenMPI module. -module load openmpi/4.0.5-gcc-8.3.1-773ztsv +module load openmpi/4.0.5 # Run the program with mpirun. Additional mpirun options # are not required. mpirun will get the info (e.g., hosts) diff --git a/fibonacci.py b/fibonacci.py index c726d75..ec3d51e 100644 --- a/fibonacci.py +++ b/fibonacci.py @@ -1,14 +1,38 @@ #!/usr/bin/env python3 -def fib(term): - if term <= 1: - return (term) - else: - return (fib(term-1) + fib(term-2)) +def fib(n): + if n == 0: + return 0 + elif n == 1: + return 1 + else: + return fib(n-1) + fib(n-2) + +def fibi(n): + a, b = 0, 1 + for i in range(n): + a, b = b, a + b + return a + +memo = {0:0, 1:1} +def fibm(n): + if not n in memo: + memo[n] = fibm(n-1) + fibm(n-2) + return memo[n] if __name__ == "__main__": - # Change this value to adjust the number of terms in the sequence. - number_of_terms = 100 - for i in range(number_of_terms): - print(fib(i)) + import time + a = time.time() + print("Recursive version") + print(fib(41), end=', ') + print('Elapsed:', time.strftime("%Hh%Mm%Ss", time.gmtime(time.time()-a))) + + a = time.time() + print("Loop version") + print(fibi(41), end=', ') + print('Elapsed:', time.strftime("%Hh%Mm%Ss", time.gmtime(time.time()-a))) + a = time.time() + print("Memory version") + print(fibm(41), end=', ') + print('Elapsed:', time.strftime("%Hh%Mm%Ss", time.gmtime(time.time()-a))) diff --git a/fibonacci_openmp.c b/fibonacci_openmp.c new file mode 100644 index 0000000..2d5158b --- /dev/null +++ b/fibonacci_openmp.c @@ -0,0 +1,30 @@ +#include <omp.h> +#include <stdio.h> + + +long long fib(long long n) +{ + if (n < 2) { + return 1; + } + return fib(n - 2) + fib(n - 1); +} +/* Creates same amount of threads as number of CPUs, privatizes n, + and lets each thread compute threads "round robin"-style, thus + preventing one only thread executing the last and heaviest blocks. */ +int main(int argc, char ** argv) +{ + long long n = 0; + + omp_set_num_threads(omp_get_num_procs()); + + #pragma omp parallel private(n) + { + #pragma omp for schedule(dynamic, 1) + for (n = 0; n <= 45; n++) { + printf("Fib(%lld): %lld\n", n, fib(n)); + } + + } + return 0; +} -- GitLab