-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfb
More file actions
54 lines (43 loc) · 1.21 KB
/
fb
File metadata and controls
54 lines (43 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//Fibonacci
#include <stdio.h>
#include <omp.h>
#define N 40 // Compute Fibonacci(N)
// Sequential Fibonacci (Recursive)
long long fibonacci_sequential(int n) {
if (n <= 1)
return n;
return fibonacci_sequential(n - 1) + fibonacci_sequential(n - 2);
}
// Parallel Fibonacci using OpenMP Tasks
long long fibonacci_parallel(int n) {
if (n <= 1)
return n;
long long x, y;
#pragma omp task shared(x)
x = fibonacci_parallel(n - 1);
#pragma omp task shared(y)
y = fibonacci_parallel(n - 2);
#pragma omp taskwait
return x + y;
}
int main() {
double start, end;
long long result_seq, result_par;
// Sequential Fibonacci
start = omp_get_wtime();
result_seq = fibonacci_sequential(N);
end = omp_get_wtime();
printf("Sequential Fibonacci(%d) = %lld\n", N, result_seq);
printf("Sequential Time: %f seconds\n", end - start);
// Parallel Fibonacci
start = omp_get_wtime();
#pragma omp parallel
{
#pragma omp single
result_par = fibonacci_parallel(N);
}
end = omp_get_wtime();
printf("Parallel Fibonacci(%d) = %lld\n", N, result_par);
printf("Parallel Time: %f seconds\n", end - start);
return 0;
}