-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinked.cpp
More file actions
117 lines (90 loc) · 2.06 KB
/
linked.cpp
File metadata and controls
117 lines (90 loc) · 2.06 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <omp.h>
#ifndef N
#define N 5
#endif
#ifndef FS
#define FS 25
#endif
struct node{
int data;
int fibdata;
struct node* next;
};
int fib(int n){
int x, y;
if (n < 2){
return (n);
}
else{
#pragma omp task shared(x)
{
x = fib(n - 1);
}
#pragma omp task shared(y)
{
y = fib(n - 2);
}
#pragma omp taskwait
return (x + y);
}
}
void processwork(struct node* p){
int n;
n = p->data;
p->fibdata = fib(n);
}
struct node* init_list(struct node* p){
struct node* head = new node;
struct node* temp = NULL;
p = head;
p->data = FS;
p->fibdata = 0;
for(int i=0; i<N; i++){
temp = new node;
p->next = temp;
p = temp;
p->data = FS+i+1;
p->fibdata = i+1;
}
p->next = NULL;
return head;
}
int main(){
double start_time, run_time;
struct node* p = NULL;
struct node* temp = NULL;
struct node* head = NULL;
std::cout<<"Process linked list"<<std::endl;
std::cout<<"Each linked list node will be processed by function 'processwork()'"<<std::endl;
std::cout<<"Each ll node will compute "<<N<<" fibonacci numbers beginning with "<<FS<<std::endl;
head = init_list(p);
int n_threads=8;
std::cout<<"Enter Number of Threads:";
std::cin>>n_threads;
omp_set_num_threads(8);
start_time = omp_get_wtime();
#pragma omp parallel
{
#pragma omp single nowait
{
// Can also be done using while loop.
for (p=head; p!=NULL; p = p->next){
#pragma omp task firstprivate(p)
{
processwork(p);
}
}
}
}
run_time = omp_get_wtime() - start_time;
p = head;
while (p!=NULL){
std::cout<< p->data <<":"<< p->fibdata <<std::endl;
temp = p->next;
delete p;
p = temp;
}
std::cout<<"Compute Time: "<<run_time<<" seconds"<<std::endl;
return 0;
}