Skip to content
Snippets Groups Projects
Commit 9e243d39 authored by Michael Blaschek's avatar Michael Blaschek :bicyclist:
Browse files

Update README.md, fibonacci.py, fibonacci_openmp.c files

parent 269bb9e6
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
#!/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)))
#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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment