Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F92952554
fibonacci_cutoff.c
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Mon, Nov 25, 02:39
Size
950 B
Mime Type
text/x-c
Expires
Wed, Nov 27, 02:39 (1 d, 22 h)
Engine
blob
Format
Raw Data
Handle
22543986
Attached To
R11821 phys-743-lecture
fibonacci_cutoff.c
View Options
#include "timing.h"
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
long
int
fibonacci_seq
(
int
n
)
{
long
int
x
,
y
;
if
(
n
<
2
)
{
return
n
;
}
else
{
x
=
fibonacci_seq
(
n
-
1
);
y
=
fibonacci_seq
(
n
-
2
);
return
(
x
+
y
);
}
}
long
int
fibonacci
(
int
n
,
int
level
,
int
cutoff
)
{
long
int
x
,
y
;
if
(
n
<
2
)
{
return
n
;
}
else
if
(
level
<
cutoff
)
{
#pragma omp task shared(x)
x
=
fibonacci
(
n
-
1
,
level
+
1
,
cutoff
);
#pragma omp task shared(y)
y
=
fibonacci
(
n
-
2
,
level
+
1
,
cutoff
);
#pragma omp taskwait
return
(
x
+
y
);
}
else
{
x
=
fibonacci_seq
(
n
-
1
);
y
=
fibonacci_seq
(
n
-
2
);
return
(
x
+
y
);
}
}
int
main
()
{
int
n
=
42
;
int
cutoff
=
10
;
double
t1
,
t2
;
long
int
fib
=
0
;
t1
=
second
();
#pragma omp parallel
{
#pragma omp single nowait
{
fib
=
fibonacci
(
n
,
0
,
cutoff
);
}
}
t2
=
second
();
printf
(
"Fib(%d) = %ld (in %g [s])
\n
"
,
n
,
fib
,
(
t2
-
t1
));
return
0
;
}
Event Timeline
Log In to Comment